test_session.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # coding=utf-8
  2. import pytest
  3. from sqlalchemy.exc import OperationalError
  4. from tracim.tests import FunctionalTest
  5. from tracim.tests import FunctionalTestNoDB
  6. class TestLogoutEndpoint(FunctionalTest):
  7. def test_api__access_logout_get_enpoint__ok__nominal_case(self):
  8. res = self.testapp.post_json('/api/v2/sessions/logout', status=204)
  9. def test_api__access_logout_post_enpoint__ok__nominal_case(self):
  10. res = self.testapp.get('/api/v2/sessions/logout', status=204)
  11. class TestLoginEndpointUnititedDB(FunctionalTestNoDB):
  12. @pytest.mark.xfail(raises=OperationalError,
  13. reason='Not supported yet by hapic')
  14. def test_api__try_login_enpoint__err_500__no_inited_db(self):
  15. params = {
  16. 'email': 'admin@admin.admin',
  17. 'password': 'admin@admin.admin',
  18. }
  19. res = self.testapp.post_json(
  20. '/api/v2/sessions/login',
  21. params=params,
  22. status=500,
  23. )
  24. assert isinstance(res.json, dict)
  25. assert 'code' in res.json.keys()
  26. assert 'message' in res.json.keys()
  27. assert 'details' in res.json.keys()
  28. class TestLoginEndpoint(FunctionalTest):
  29. def test_api__try_login_enpoint__ok_204__nominal_case(self):
  30. params = {
  31. 'email': 'admin@admin.admin',
  32. 'password': 'admin@admin.admin',
  33. }
  34. res = self.testapp.post_json(
  35. '/api/v2/sessions/login',
  36. params=params,
  37. status=204,
  38. )
  39. def test_api__try_login_enpoint__err_400__bad_password(self):
  40. params = {
  41. 'email': 'admin@admin.admin',
  42. 'password': 'bad_password',
  43. }
  44. res = self.testapp.post_json(
  45. '/api/v2/sessions/login',
  46. status=400,
  47. params=params,
  48. )
  49. assert isinstance(res.json, dict)
  50. assert 'code' in res.json.keys()
  51. assert 'message' in res.json.keys()
  52. assert 'details' in res.json.keys()
  53. def test_api__try_login_enpoint__err_400__unregistered_user(self):
  54. params = {
  55. 'email': 'unknown_user@unknown.unknown',
  56. 'password': 'bad_password',
  57. }
  58. res = self.testapp.post_json(
  59. '/api/v2/sessions/login',
  60. status=400,
  61. params=params,
  62. )
  63. assert isinstance(res.json, dict)
  64. assert 'code' in res.json.keys()
  65. assert 'message' in res.json.keys()
  66. assert 'details' in res.json.keys()
  67. def test_api__try_login_enpoint__err_400__no_json_body(self):
  68. res = self.testapp.post_json('/api/v2/sessions/login', status=400)
  69. assert isinstance(res.json, dict)
  70. assert 'code' in res.json.keys()
  71. assert 'message' in res.json.keys()
  72. assert 'details' in res.json.keys()
  73. class TestWhoamiEndpoint(FunctionalTest):
  74. def test_api__try_whoami_enpoint__ok_200__nominal_case(self):
  75. self.testapp.authorization = (
  76. 'Basic',
  77. (
  78. 'admin@admin.admin',
  79. 'admin@admin.admin'
  80. )
  81. )
  82. res = self.testapp.get('/api/v2/sessions/whoami', status=200)
  83. assert res.json_body['display_name'] == 'Global manager'
  84. assert res.json_body['email'] == 'admin@admin.admin'
  85. assert res.json_body['created']
  86. assert res.json_body['is_active']
  87. assert res.json_body['profile']
  88. assert isinstance(res.json_body['profile']['id'], int)
  89. assert res.json_body['profile']['slug'] == 'administrators'
  90. assert res.json_body['caldav_url'] is None
  91. assert res.json_body['avatar_url'] is None
  92. def test_api__try_whoami_enpoint__err_401__unauthenticated(self):
  93. self.testapp.authorization = (
  94. 'Basic',
  95. (
  96. 'john@doe.doe',
  97. 'lapin'
  98. )
  99. )
  100. res = self.testapp.get('/api/v2/sessions/whoami', status=401)
  101. assert isinstance(res.json, dict)
  102. assert 'code' in res.json.keys()
  103. assert 'message' in res.json.keys()
  104. assert 'details' in res.json.keys()