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.

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