user_test.py 2.1 KB

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