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.

156 lines
4.7 KiB

  1. from datetime import timedelta
  2. import rfc3339
  3. from flask import json
  4. from flask.testing import FlaskClient
  5. from tests.conftest import AuthActions
  6. def test_login_happy_path(auth: AuthActions):
  7. with auth as result:
  8. assert result.status_code == 200
  9. assert result.json[
  10. 'token'] is not None and len(result.json['token']) > 0
  11. def test_bump_happy_path(auth: AuthActions):
  12. with auth:
  13. result = auth.bump()
  14. assert result.status_code == 200
  15. assert (result.json['lastLoginTime'] is not None
  16. and len(result.json['lastLoginTime']) > 0)
  17. def test_logout_happy_path(auth: AuthActions):
  18. auth.login()
  19. result = auth.logout()
  20. assert result.status_code == 200
  21. assert result.json['success']
  22. def test_get_tokens_no_tokens(auth: AuthActions, client: FlaskClient):
  23. auth_header = auth.get_authorization_header_basic()
  24. result = client.get(
  25. '/auth/token',
  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_tokens(auth: AuthActions, client: FlaskClient):
  32. with auth:
  33. auth_header = auth.get_authorization_header_basic()
  34. result = client.get(
  35. '/auth/token',
  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['page'] == 1
  42. assert result.json['lastPage'] == 1
  43. assert result.json['count'] == 1
  44. assert result.json['totalCount'] == 1
  45. assert result.json['items'][0]['token'] == auth.token
  46. def test_get_nonexistant_token(auth: AuthActions, client: FlaskClient):
  47. auth_header = auth.get_authorization_header_basic()
  48. result = client.get(
  49. '/auth/token/not-a-token',
  50. headers={
  51. auth_header[0]: auth_header[1]
  52. })
  53. assert 404 == result.status_code
  54. assert result.json is not None
  55. def test_create_get_delete_token(auth: AuthActions, client: FlaskClient):
  56. auth_header = auth.get_authorization_header_basic()
  57. result = client.post(
  58. '/auth/token',
  59. headers={
  60. auth_header[0]: auth_header[1],
  61. 'Content-Type': 'application/json'
  62. },
  63. data=json.dumps({
  64. 'note': 'test note',
  65. 'enabled': False
  66. }))
  67. assert 200 == result.status_code
  68. assert result.json is not None
  69. assert result.json['token'] is not None
  70. assert result.json['note'] == 'test note'
  71. assert not result.json['enabled']
  72. assert not result.json['isValid']
  73. auth_token = result.json['token']
  74. result = client.get(
  75. '/auth/token/%s' % auth_token,
  76. headers={
  77. auth_header[0]: auth_header[1]
  78. })
  79. assert 200 == result.status_code
  80. assert result.json is not None
  81. assert result.json['token'] == auth_token
  82. result = client.delete(
  83. '/auth/token/%s' % auth_token,
  84. headers={
  85. auth_header[0]: auth_header[1]
  86. })
  87. assert 200 == result.status_code
  88. assert result.json is not None
  89. assert 'message' not in result.json
  90. assert result.json['success']
  91. def test_create_get_delete_expired_token(
  92. auth: AuthActions, client: FlaskClient):
  93. auth_header = auth.get_authorization_header_basic()
  94. result = client.post(
  95. '/auth/token',
  96. headers={
  97. auth_header[0]: auth_header[1],
  98. 'Content-Type': 'application/json'
  99. },
  100. data=json.dumps({
  101. 'note': 'test note',
  102. 'expirationTime': rfc3339.format(
  103. rfc3339.datetime.now() - timedelta(days=1))
  104. }))
  105. assert 200 == result.status_code
  106. assert result.json is not None
  107. assert result.json['token'] is not None
  108. assert result.json['note'] == 'test note'
  109. assert not result.json['isValid']
  110. auth_token = result.json['token']
  111. result = client.get(
  112. '/auth/token/%s' % auth_token,
  113. headers={
  114. auth_header[0]: auth_header[1]
  115. })
  116. assert 200 == result.status_code
  117. assert result.json is not None
  118. assert result.json['token'] == auth_token
  119. result = client.delete(
  120. '/auth/token/%s' % auth_token,
  121. headers={
  122. auth_header[0]: auth_header[1]
  123. })
  124. assert 200 == result.status_code
  125. assert result.json is not None
  126. assert 'message' not in result.json
  127. assert result.json['success']
  128. def test_delete_nonexistant_token(auth: AuthActions, client: FlaskClient):
  129. auth_header = auth.get_authorization_header_basic()
  130. result = client.delete(
  131. '/auth/token/not-a-token',
  132. headers={
  133. auth_header[0]: auth_header[1]
  134. })
  135. assert 404 == result.status_code
  136. assert result.json is not None