An ebook/comic library service and web client
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.

166 lines
5.0 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. auth.login()
  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. auth.login()
  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_user_happy_path(auth: AuthActions, client: FlaskClient):
  32. auth.login()
  33. auth_header = auth.get_authorization_header_token()
  34. result = client.get(
  35. '/user/{}'.format(client.application.config['test_username']),
  36. headers={
  37. auth_header[0]: auth_header[1]
  38. })
  39. assert 200 == result.status_code
  40. assert result.json is not None
  41. assert result.json['name'] == client.application.config['test_username']
  42. def test_patch_user_happy_path(auth: AuthActions, client: FlaskClient):
  43. auth.login()
  44. auth_header = auth.get_authorization_header_token()
  45. last_login_time = rfc3339.format(datetime.now())
  46. user = client.get(
  47. '/user/{}'.format(client.application.config['test_username']),
  48. headers={
  49. auth_header[0]: auth_header[1]
  50. })
  51. patched_user = client.patch(
  52. '/user/{}'.format(client.application.config['test_username']),
  53. data=json.dumps({
  54. 'version': user.json['version'],
  55. 'lastLoginTime': last_login_time
  56. }),
  57. headers={
  58. auth_header[0]: auth_header[1],
  59. 'Content-Type': 'application/json'
  60. })
  61. assert 200 == patched_user.status_code
  62. assert patched_user.json['version'] == user.json['version'] + 1
  63. assert patched_user.json['lastLoginTime'] == last_login_time
  64. def test_register_user_happy_path(auth: AuthActions, client: FlaskClient):
  65. auth.login()
  66. auth_header = auth.get_authorization_header_token()
  67. result = client.post(
  68. '/user',
  69. data=json.dumps({
  70. 'name': 'test_registered_user'
  71. }),
  72. headers={
  73. auth_header[0]: auth_header[1],
  74. 'Content-Type': 'application/json'
  75. })
  76. assert 200 == result.status_code
  77. assert result.json is not None
  78. assert result.json['name'] == 'test_registered_user'
  79. def test_register_user_invalid_password(
  80. auth: AuthActions, client: FlaskClient):
  81. auth.login()
  82. auth_header = auth.get_authorization_header_token()
  83. result = client.post(
  84. '/user',
  85. data=json.dumps({
  86. 'name': 'test_registered_user',
  87. 'password': ''
  88. }),
  89. headers={
  90. auth_header[0]: auth_header[1],
  91. 'Content-Type': 'application/json'
  92. })
  93. assert 400 == result.status_code
  94. assert result.json is not None
  95. assert 'message' in result.json
  96. def test_register_user_twice_failure(auth: AuthActions, client: FlaskClient):
  97. auth.login()
  98. auth_header = auth.get_authorization_header_token()
  99. result1 = client.post(
  100. '/user',
  101. data=json.dumps({
  102. 'name': 'test_registered_user'
  103. }),
  104. headers={
  105. auth_header[0]: auth_header[1],
  106. 'Content-Type': 'application/json'
  107. })
  108. result2 = client.post(
  109. '/user',
  110. data=json.dumps({
  111. 'name': 'test_registered_user'
  112. }),
  113. headers={
  114. auth_header[0]: auth_header[1],
  115. 'Content-Type': 'application/json'
  116. })
  117. assert 200 == result1.status_code
  118. assert result1.json is not None
  119. assert result1.json['name'] == 'test_registered_user'
  120. assert 400 == result2.status_code
  121. assert result2.json is not None
  122. assert result2.json['message'] == 'User name is already taken.'
  123. def test_delete_user_happy_path(auth: AuthActions, client: FlaskClient):
  124. auth.login()
  125. auth_header = auth.get_authorization_header_token()
  126. result1 = client.post(
  127. '/user',
  128. data=json.dumps({
  129. 'name': 'test_registered_user'
  130. }),
  131. headers={
  132. auth_header[0]: auth_header[1],
  133. 'Content-Type': 'application/json'
  134. })
  135. result2 = client.delete(
  136. '/user/'+result1.json['name'],
  137. headers={
  138. auth_header[0]: auth_header[1]
  139. })
  140. assert 200 == result1.status_code
  141. assert result1.json is not None
  142. assert result1.json['name'] == 'test_registered_user'
  143. assert 200 == result2.status_code
  144. assert result2.json is not None
  145. assert 'message' in result2.json