123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- import pytest
- import requests
- import json
- OK = 200
- CREATED = 201
- CONFLICT = 409
- 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 = 'http://localhost:8080/user/' + userName
- return requests.get(url)
- def register_user(userName, password):
- url = 'http://localhost:8080/user/register'
- data = {
- 'userName': userName,
- 'password': password
- }
- return requests.post(url, json=data)
- def get_all_users():
- url = 'http://localhost:8080/admin/users'
- resp = requests.get(url)
- return resp
- def delete_user(userName):
- url = 'http://localhost:8080/admin/users'
- data = {
- 'userName': userName
- }
- return requests.delete(url, json=data)
- def update_user(userName, updatedData):
- url = 'http://localhost:8080/user/' + userName
- return requests.put(url, json=updatedData)
-
|