group_test.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import pytest
  2. import requests
  3. import json
  4. OK = 200
  5. CREATED = 201
  6. NOT_FOUND = 404
  7. NOT_ACCEPTABLE = 406
  8. CONFLICT = 409
  9. from constants import WEBSITE_PREFIX
  10. def test_group_apis():
  11. from user_test import register_user, get_user, delete_user
  12. resp = register_user('pytest-group-user', 'password')
  13. assert(resp.status_code == CREATED)
  14. resp = get_user('pytest-group-user')
  15. user = json.loads(resp.text)
  16. assert(user['userName'] == 'pytest-group-user')
  17. resp = create_group({
  18. 'name': 'Pytest-Group',
  19. })
  20. assert(resp.status_code == OK)
  21. create_group({
  22. 'name': 'Child-Group'
  23. })
  24. assert(resp.status_code == OK)
  25. root_group = json.loads(get_group_by_name('Pytest-Group').text)
  26. child_group = json.loads(get_group_by_name('Child-Group').text)
  27. resp = edit_group(child_group['id'], {
  28. 'parentGroupId' : root_group['id']
  29. })
  30. assert(resp.status_code == OK)
  31. resp = add_user_to_group(child_group['id'], user)
  32. assert(resp.status_code == OK)
  33. resp = remove_user_from_group(child_group['id'], user)
  34. assert(resp.status_code == OK)
  35. resp = remove_user_from_group(child_group['id'], user)
  36. assert(resp.status_code == NOT_ACCEPTABLE)
  37. delete_user(user['userName'])
  38. delete_group(child_group['id'])
  39. delete_group(root_group['id'])
  40. def get_group(gid):
  41. url = WEBSITE_PREFIX + '/groups/' + gid
  42. return requests.get(url)
  43. def get_group_by_name(group_name):
  44. url = WEBSITE_PREFIX + '/groups' + '?group=' + group_name
  45. return requests.get(url)
  46. def get_all_groups_by_user(user_name):
  47. url = WEBSITE_PREFIX + '/groups' + '?user=' + user_name
  48. return requests.get(url)
  49. def create_group(group):
  50. url = WEBSITE_PREFIX + '/groups'
  51. return requests.post(url, json=group)
  52. def edit_group(gid, updatedGroup):
  53. url = WEBSITE_PREFIX + '/groups/' + gid
  54. return requests.put(url, json=updatedGroup)
  55. def delete_group(gid):
  56. url = WEBSITE_PREFIX + '/admin/groups/' + gid
  57. return requests.delete(url)
  58. def add_user_to_group(gid, user):
  59. url = WEBSITE_PREFIX + '/admin/groups/' + gid + '/users'
  60. return requests.post(url, json=user)
  61. def remove_user_from_group(gid, user):
  62. url = WEBSITE_PREFIX + '/admin/groups/' + gid + '/users'
  63. return requests.delete(url, json=user)