import pytest import requests import json OK = 200 CREATED = 201 CONFLICT = 409 from constants import WEBSITE_PREFIX def test_multiple_user_apis(): prev_count = len(json.loads(get_all_users().text)) resp = register_user('pytest-user', 'fake-password') assert(resp.status_code == CREATED) # CREATED new_all_users = json.loads(get_all_users().text) assert(len(new_all_users) == prev_count + 1) assert(new_all_users[-1]['userName'] == 'pytest-user') resp = register_user('pytest-user', 'dummy') # Create same username will fail assert(resp.status_code == CONFLICT) # CONFLICT print(new_all_users[-1]) resp = delete_user(new_all_users[-1]['userName']) assert(resp.status_code == OK) assert(len(json.loads(get_all_users().text)) == prev_count) resp = register_user('pytest-user', 'fake-password') assert(resp.status_code == CREATED) resp = register_user('pytest-user2', 'fake-password') assert(resp.status_code == CREATED) updated_test_user2 = { 'firstName': '大强', 'lastName': '苏' } resp = update_user('pytest-user2', updated_test_user2) assert(resp.status_code == OK) resp = json.loads(get_user('pytest-user2').text) assert(resp['firstName'] == '大强') assert(resp['lastName'] == '苏') resp = json.loads(get_user('pytest-user').text) delete_user(resp['userName']) resp = json.loads(get_user('pytest-user2').text) delete_user(resp['userName']) def get_user(userName): url = WEBSITE_PREFIX + '/user/' + userName return requests.get(url) def register_user(userName, password): url = WEBSITE_PREFIX + '/user/register' data = { 'userName': userName, 'password': password } return requests.post(url, json=data) def get_all_users(): url = WEBSITE_PREFIX + '/admin/users' resp = requests.get(url) return resp def delete_user(userName): url = WEBSITE_PREFIX + '/admin/users' data = { 'userName': userName } return requests.delete(url, json=data) def update_user(userName, updatedData): url = WEBSITE_PREFIX + '/user/' + userName return requests.put(url, json=updatedData)