A multipurpose python flask API server and administration SPA
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
6.4 KiB

  1. from datetime import datetime
  2. import rfc3339
  3. from flask import json
  4. from flask.testing import FlaskClient
  5. from tests.conftest import AuthActions
  6. from corvus.service.role_service import ROLE_LIST
  7. def test_get_users_happy_path(auth: AuthActions, client: FlaskClient):
  8. with auth:
  9. auth_header = auth.get_authorization_header_token()
  10. result = client.get(
  11. '/user',
  12. headers={
  13. auth_header[0]: auth_header[1]
  14. })
  15. assert 200 == result.status_code
  16. assert result.json is not None
  17. assert result.json['page'] == 1
  18. assert result.json['lastPage'] == 1
  19. assert result.json['count'] == 1
  20. assert result.json['totalCount'] == 1
  21. assert result.json['items'][0]['name'] == auth.username
  22. def test_get_users_nonexistent_page(auth: AuthActions, client: FlaskClient):
  23. with auth:
  24. auth_header = auth.get_authorization_header_token()
  25. result = client.get(
  26. '/user?page=2',
  27. headers={
  28. auth_header[0]: auth_header[1]
  29. })
  30. assert 404 == result.status_code
  31. assert result.json is not None
  32. def test_get_users_bad_page_parameters(auth: AuthActions, client: FlaskClient):
  33. with auth:
  34. auth_header = auth.get_authorization_header_token()
  35. result = client.get(
  36. '/user?page=a',
  37. headers={
  38. auth_header[0]: auth_header[1]
  39. })
  40. assert 400 == result.status_code
  41. assert result.json is not None
  42. def test_get_user_happy_path(auth: AuthActions, client: FlaskClient):
  43. with auth:
  44. auth_header = auth.get_authorization_header_token()
  45. result = client.get(
  46. '/user/{}'.format(client.application.config['test_username']),
  47. headers={
  48. auth_header[0]: auth_header[1]
  49. })
  50. assert 200 == result.status_code
  51. assert result.json is not None
  52. assert result.json['name'] == client.application.config[
  53. 'test_username']
  54. def test_patch_user_happy_path(auth: AuthActions, client: FlaskClient):
  55. with auth:
  56. auth_header = auth.get_authorization_header_token()
  57. last_login_time = rfc3339.format(datetime.now())
  58. user = client.get(
  59. '/user/{}'.format(client.application.config['test_username']),
  60. headers={
  61. auth_header[0]: auth_header[1]
  62. })
  63. patched_user = client.patch(
  64. '/user/{}'.format(client.application.config['test_username']),
  65. data=json.dumps({
  66. 'version': user.json['version'],
  67. 'lastLoginTime': last_login_time
  68. }),
  69. headers={
  70. auth_header[0]: auth_header[1],
  71. 'Content-Type': 'application/json'
  72. })
  73. assert 200 == patched_user.status_code
  74. assert patched_user.json['version'] == user.json['version'] + 1
  75. assert patched_user.json['lastLoginTime'] == last_login_time
  76. def test_register_user_happy_path(auth: AuthActions, client: FlaskClient):
  77. with auth:
  78. auth_header = auth.get_authorization_header_token()
  79. result = client.post(
  80. '/user',
  81. data=json.dumps({
  82. 'name': 'test_registered_user'
  83. }),
  84. headers={
  85. auth_header[0]: auth_header[1],
  86. 'Content-Type': 'application/json'
  87. })
  88. assert 200 == result.status_code
  89. assert result.json is not None
  90. assert result.json['name'] == 'test_registered_user'
  91. def test_register_user_invalid_password(
  92. auth: AuthActions, client: FlaskClient):
  93. with auth:
  94. auth_header = auth.get_authorization_header_token()
  95. result = client.post(
  96. '/user',
  97. data=json.dumps({
  98. 'name': 'test_registered_user',
  99. 'password': ''
  100. }),
  101. headers={
  102. auth_header[0]: auth_header[1],
  103. 'Content-Type': 'application/json'
  104. })
  105. assert 400 == result.status_code
  106. assert result.json is not None
  107. assert 'message' in result.json
  108. def test_register_user_twice_failure(auth: AuthActions, client: FlaskClient):
  109. with auth:
  110. auth_header = auth.get_authorization_header_token()
  111. result1 = client.post(
  112. '/user',
  113. data=json.dumps({
  114. 'name': 'test_registered_user'
  115. }),
  116. headers={
  117. auth_header[0]: auth_header[1],
  118. 'Content-Type': 'application/json'
  119. })
  120. result2 = client.post(
  121. '/user',
  122. data=json.dumps({
  123. 'name': 'test_registered_user'
  124. }),
  125. headers={
  126. auth_header[0]: auth_header[1],
  127. 'Content-Type': 'application/json'
  128. })
  129. assert 200 == result1.status_code
  130. assert result1.json is not None
  131. assert result1.json['name'] == 'test_registered_user'
  132. assert 400 == result2.status_code
  133. assert result2.json is not None
  134. assert result2.json['message'] == 'User name is already taken.'
  135. def test_delete_user_happy_path(auth: AuthActions, client: FlaskClient):
  136. with auth:
  137. auth_header = auth.get_authorization_header_token()
  138. result1 = client.post(
  139. '/user',
  140. data=json.dumps({
  141. 'name': 'test_registered_user'
  142. }),
  143. headers={
  144. auth_header[0]: auth_header[1],
  145. 'Content-Type': 'application/json'
  146. })
  147. result2 = client.delete(
  148. '/user/'+result1.json['name'],
  149. headers={
  150. auth_header[0]: auth_header[1]
  151. })
  152. assert 200 == result1.status_code
  153. assert result1.json is not None
  154. assert result1.json['name'] == 'test_registered_user'
  155. assert 200 == result2.status_code
  156. assert result2.json is not None
  157. assert 'message' in result2.json
  158. def test_get_roles(auth: AuthActions, client: FlaskClient):
  159. with auth:
  160. auth_header = auth.get_authorization_header_token()
  161. result = client.get(
  162. '/user/roles',
  163. headers={
  164. auth_header[0]: auth_header[1]
  165. })
  166. assert 200 == result.status_code
  167. assert result.json is not None
  168. for role in ROLE_LIST:
  169. assert str(role.data) in result.json