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.

137 lines
4.2 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_user_happy_path(auth: AuthActions, client: FlaskClient):
  7. auth.login()
  8. auth_header = auth.get_authorization_header_token()
  9. result = client.get(
  10. '/user/{}'.format(client.application.config['test_username']),
  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['name'] == client.application.config['test_username']
  17. def test_patch_user_happy_path(auth: AuthActions, client: FlaskClient):
  18. auth.login()
  19. auth_header = auth.get_authorization_header_token()
  20. last_login_time = rfc3339.format(datetime.now())
  21. user = client.get(
  22. '/user/{}'.format(client.application.config['test_username']),
  23. headers={
  24. auth_header[0]: auth_header[1]
  25. })
  26. patched_user = client.patch(
  27. '/user/{}'.format(client.application.config['test_username']),
  28. data=json.dumps({
  29. 'version': user.json['version'],
  30. 'lastLoginTime': last_login_time
  31. }),
  32. headers={
  33. auth_header[0]: auth_header[1],
  34. 'Content-Type': 'application/json'
  35. })
  36. assert 200 == patched_user.status_code
  37. assert patched_user.json['version'] == user.json['version'] + 1
  38. assert patched_user.json['lastLoginTime'] == last_login_time
  39. def test_register_user_happy_path(auth: AuthActions, client: FlaskClient):
  40. auth.login()
  41. auth_header = auth.get_authorization_header_token()
  42. result = client.post(
  43. '/user/',
  44. data=json.dumps({
  45. 'name': 'test_registered_user'
  46. }),
  47. headers={
  48. auth_header[0]: auth_header[1],
  49. 'Content-Type': 'application/json'
  50. })
  51. assert 200 == result.status_code
  52. assert result.json is not None
  53. assert result.json['name'] == 'test_registered_user'
  54. def test_register_user_invalid_password(
  55. auth: AuthActions, client: FlaskClient):
  56. auth.login()
  57. auth_header = auth.get_authorization_header_token()
  58. result = client.post(
  59. '/user/',
  60. data=json.dumps({
  61. 'name': 'test_registered_user',
  62. 'password': ''
  63. }),
  64. headers={
  65. auth_header[0]: auth_header[1],
  66. 'Content-Type': 'application/json'
  67. })
  68. assert 400 == result.status_code
  69. assert result.json is not None
  70. assert 'message' in result.json
  71. def test_register_user_twice_failure(auth: AuthActions, client: FlaskClient):
  72. auth.login()
  73. auth_header = auth.get_authorization_header_token()
  74. result1 = client.post(
  75. '/user/',
  76. data=json.dumps({
  77. 'name': 'test_registered_user'
  78. }),
  79. headers={
  80. auth_header[0]: auth_header[1],
  81. 'Content-Type': 'application/json'
  82. })
  83. result2 = client.post(
  84. '/user/',
  85. data=json.dumps({
  86. 'name': 'test_registered_user'
  87. }),
  88. headers={
  89. auth_header[0]: auth_header[1],
  90. 'Content-Type': 'application/json'
  91. })
  92. assert 200 == result1.status_code
  93. assert result1.json is not None
  94. assert result1.json['name'] == 'test_registered_user'
  95. assert 400 == result2.status_code
  96. assert result2.json is not None
  97. assert result2.json['message'] == 'User name is already taken.'
  98. def test_delete_user_happy_path(auth: AuthActions, client: FlaskClient):
  99. auth.login()
  100. auth_header = auth.get_authorization_header_token()
  101. result1 = client.post(
  102. '/user/',
  103. data=json.dumps({
  104. 'name': 'test_registered_user'
  105. }),
  106. headers={
  107. auth_header[0]: auth_header[1],
  108. 'Content-Type': 'application/json'
  109. })
  110. result2 = client.delete(
  111. '/user/'+result1.json['name'],
  112. headers={
  113. auth_header[0]: auth_header[1]
  114. })
  115. assert 200 == result1.status_code
  116. assert result1.json is not None
  117. assert result1.json['name'] == 'test_registered_user'
  118. assert 200 == result2.status_code
  119. assert result2.json is not None
  120. assert 'message' in result2.json