user_test.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import pytest
  2. import requests
  3. import json
  4. OK = 200
  5. CREATED = 201
  6. CONFLICT = 409
  7. from constants import WEBSITE_PREFIX
  8. def test_multiple_user_apis():
  9. prev_count = len(json.loads(get_all_users().text))
  10. resp = register_user('pytest-user', 'fake-password')
  11. assert(resp.status_code == CREATED) # CREATED
  12. new_all_users = json.loads(get_all_users().text)
  13. assert(len(new_all_users) == prev_count + 1)
  14. assert(new_all_users[-1]['userName'] == 'pytest-user')
  15. resp = register_user('pytest-user', 'dummy') # Create same username will fail
  16. assert(resp.status_code == CONFLICT) # CONFLICT
  17. print(new_all_users[-1])
  18. resp = delete_user(new_all_users[-1]['userName'])
  19. assert(resp.status_code == OK)
  20. assert(len(json.loads(get_all_users().text)) == prev_count)
  21. resp = register_user('pytest-user', 'fake-password')
  22. assert(resp.status_code == CREATED)
  23. resp = register_user('pytest-user2', 'fake-password')
  24. assert(resp.status_code == CREATED)
  25. updated_test_user2 = {
  26. 'firstName': '大强',
  27. 'lastName': '苏'
  28. }
  29. resp = update_user('pytest-user2', updated_test_user2)
  30. assert(resp.status_code == OK)
  31. resp = json.loads(get_user('pytest-user2').text)
  32. assert(resp['firstName'] == '大强')
  33. assert(resp['lastName'] == '苏')
  34. resp = json.loads(get_user('pytest-user').text)
  35. delete_user(resp['userName'])
  36. resp = json.loads(get_user('pytest-user2').text)
  37. delete_user(resp['userName'])
  38. def get_user(userName):
  39. url = WEBSITE_PREFIX + '/user/' + userName
  40. return requests.get(url)
  41. def register_user(userName, password):
  42. url = WEBSITE_PREFIX + '/user/register'
  43. data = {
  44. 'userName': userName,
  45. 'password': password
  46. }
  47. return requests.post(url, json=data)
  48. def get_all_users():
  49. url = WEBSITE_PREFIX + '/admin/users'
  50. resp = requests.get(url)
  51. return resp
  52. def delete_user(userName):
  53. url = WEBSITE_PREFIX + '/admin/users'
  54. data = {
  55. 'userName': userName
  56. }
  57. return requests.delete(url, json=data)
  58. def update_user(userName, updatedData):
  59. url = WEBSITE_PREFIX + '/user/' + userName
  60. return requests.put(url, json=updatedData)