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.

93 lines
2.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_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_twice_failure(auth: AuthActions, client: FlaskClient):
  55. auth.login()
  56. auth_header = auth.get_authorization_header_token()
  57. result1 = client.post(
  58. '/user/',
  59. data=json.dumps({
  60. 'name': 'test_registered_user'
  61. }),
  62. headers={
  63. auth_header[0]: auth_header[1],
  64. 'Content-Type': 'application/json'
  65. })
  66. result2 = client.post(
  67. '/user/',
  68. data=json.dumps({
  69. 'name': 'test_registered_user'
  70. }),
  71. headers={
  72. auth_header[0]: auth_header[1],
  73. 'Content-Type': 'application/json'
  74. })
  75. assert 200 == result1.status_code
  76. assert result1.json is not None
  77. assert result1.json['name'] == 'test_registered_user'
  78. assert 400 == result2.status_code
  79. assert result2.json is not None
  80. assert result2.json['message'] == 'User name is already taken.'