test_user_api.py 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. import transaction
  4. from tracim_backend.exceptions import AuthenticationFailed
  5. from tracim_backend.exceptions import UserDoesNotExist
  6. from tracim_backend.exceptions import UserNotActive
  7. from tracim_backend.lib.core.group import GroupApi
  8. from tracim_backend.lib.core.user import UserApi
  9. from tracim_backend.models import User
  10. from tracim_backend.models.context_models import UserInContext
  11. from tracim_backend.tests import DefaultTest
  12. from tracim_backend.tests import eq_
  13. class TestUserApi(DefaultTest):
  14. def test_unit__create_minimal_user__ok__nominal_case(self):
  15. api = UserApi(
  16. current_user=None,
  17. session=self.session,
  18. config=self.config,
  19. )
  20. u = api.create_minimal_user('bob@bob')
  21. assert u.email == 'bob@bob'
  22. assert u.display_name == 'bob'
  23. def test_unit__create_minimal_user_and_update__ok__nominal_case(self):
  24. api = UserApi(
  25. current_user=None,
  26. session=self.session,
  27. config=self.config,
  28. )
  29. u = api.create_minimal_user('bob@bob')
  30. api.update(u, 'bob', 'bob@bob', 'pass', do_save=True)
  31. nu = api.get_one_by_email('bob@bob')
  32. assert nu is not None
  33. assert nu.email == 'bob@bob'
  34. assert nu.display_name == 'bob'
  35. assert nu.validate_password('pass')
  36. def test__unit__create__user__ok_nominal_case(self):
  37. api = UserApi(
  38. current_user=None,
  39. session=self.session,
  40. config=self.config,
  41. )
  42. u = api.create_user(
  43. email='bob@bob',
  44. password='pass',
  45. name='bob',
  46. timezone='+2',
  47. do_save=True,
  48. do_notify=False,
  49. )
  50. assert u is not None
  51. assert u.email == "bob@bob"
  52. assert u.validate_password('pass')
  53. assert u.display_name == 'bob'
  54. assert u.timezone == '+2'
  55. def test_unit__user_with_email_exists__ok__nominal_case(self):
  56. api = UserApi(
  57. current_user=None,
  58. session=self.session,
  59. config=self.config,
  60. )
  61. u = api.create_minimal_user('bibi@bibi')
  62. api.update(u, 'bibi', 'bibi@bibi', 'pass', do_save=True)
  63. transaction.commit()
  64. eq_(True, api.user_with_email_exists('bibi@bibi'))
  65. eq_(False, api.user_with_email_exists('unknown'))
  66. def test_get_one_by_email(self):
  67. api = UserApi(
  68. current_user=None,
  69. session=self.session,
  70. config=self.config,
  71. )
  72. u = api.create_minimal_user('bibi@bibi')
  73. self.session.flush()
  74. api.update(u, 'bibi', 'bibi@bibi', 'pass', do_save=True)
  75. uid = u.user_id
  76. transaction.commit()
  77. eq_(uid, api.get_one_by_email('bibi@bibi').user_id)
  78. def test_unit__get_one_by_email__err__user_does_not_exist(self):
  79. api = UserApi(
  80. current_user=None,
  81. session=self.session,
  82. config=self.config,
  83. )
  84. with pytest.raises(UserDoesNotExist):
  85. api.get_one_by_email('unknown')
  86. def test_unit__get_all__ok__nominal_case(self):
  87. api = UserApi(
  88. current_user=None,
  89. session=self.session,
  90. config=self.config,
  91. )
  92. u1 = api.create_minimal_user('bibi@bibi')
  93. users = api.get_all()
  94. # u1 + Admin user from BaseFixture
  95. assert 2 == len(users)
  96. def test_unit__get_one__ok__nominal_case(self):
  97. api = UserApi(
  98. current_user=None,
  99. session=self.session,
  100. config=self.config,
  101. )
  102. u = api.create_minimal_user('titi@titi')
  103. api.update(u, 'titi', 'titi@titi', 'pass', do_save=True)
  104. one = api.get_one(u.user_id)
  105. eq_(u.user_id, one.user_id)
  106. def test_unit__get_user_with_context__nominal_case(self):
  107. user = User(
  108. email='admin@tracim.tracim',
  109. display_name='Admin',
  110. is_active=True,
  111. )
  112. api = UserApi(
  113. current_user=None,
  114. session=self.session,
  115. config=self.config,
  116. )
  117. new_user = api.get_user_with_context(user)
  118. assert isinstance(new_user, UserInContext)
  119. assert new_user.user == user
  120. assert new_user.profile == 'nobody'
  121. assert new_user.user_id == user.user_id
  122. assert new_user.email == 'admin@tracim.tracim'
  123. assert new_user.display_name == 'Admin'
  124. assert new_user.is_active is True
  125. # TODO - G.M - 03-05-2018 - [avatar][calendar] Should test this
  126. # with true value when those param will be available.
  127. assert new_user.avatar_url is None
  128. assert new_user.calendar_url is None
  129. def test_unit__get_current_user_ok__nominal_case(self):
  130. user = User(email='admin@tracim.tracim')
  131. api = UserApi(
  132. current_user=user,
  133. session=self.session,
  134. config=self.config,
  135. )
  136. new_user = api.get_current_user()
  137. assert isinstance(new_user, User)
  138. assert user == new_user
  139. def test_unit__get_current_user__err__user_not_exist(self):
  140. api = UserApi(
  141. current_user=None,
  142. session=self.session,
  143. config=self.config,
  144. )
  145. with pytest.raises(UserDoesNotExist):
  146. api.get_current_user()
  147. def test_unit__authenticate_user___ok__nominal_case(self):
  148. api = UserApi(
  149. current_user=None,
  150. session=self.session,
  151. config=self.config,
  152. )
  153. user = api.authenticate_user('admin@admin.admin', 'admin@admin.admin')
  154. assert isinstance(user, User)
  155. assert user.email == 'admin@admin.admin'
  156. def test_unit__authenticate_user___err__user_not_active(self):
  157. api = UserApi(
  158. current_user=None,
  159. session=self.session,
  160. config=self.config,
  161. )
  162. gapi = GroupApi(
  163. current_user=None,
  164. session=self.session,
  165. config=self.config,
  166. )
  167. groups = [gapi.get_one_with_name('users')]
  168. user = api.create_user(
  169. email='test@test.test',
  170. password='pass',
  171. name='bob',
  172. groups=groups,
  173. timezone='Europe/Paris',
  174. do_save=True,
  175. do_notify=False,
  176. )
  177. api.disable(user)
  178. with pytest.raises(UserNotActive):
  179. api.authenticate_user('test@test.test', 'test@test.test')
  180. def test_unit__authenticate_user___err__wrong_password(self):
  181. api = UserApi(
  182. current_user=None,
  183. session=self.session,
  184. config=self.config,
  185. )
  186. with pytest.raises(AuthenticationFailed):
  187. api.authenticate_user('admin@admin.admin', 'wrong_password')
  188. def test_unit__authenticate_user___err__wrong_user(self):
  189. api = UserApi(
  190. current_user=None,
  191. session=self.session,
  192. config=self.config,
  193. )
  194. with pytest.raises(AuthenticationFailed):
  195. api.authenticate_user('admin@admin.admin', 'wrong_password')