test_user_api.py 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. import transaction
  4. from tracim_backend import models
  5. from tracim_backend.exceptions import AuthenticationFailed
  6. from tracim_backend.exceptions import TooShortAutocompleteString
  7. from tracim_backend.exceptions import UserDoesNotExist
  8. from tracim_backend.exceptions import UserNotActive
  9. from tracim_backend.lib.core.group import GroupApi
  10. from tracim_backend.lib.core.user import UserApi
  11. from tracim_backend.lib.core.userworkspace import RoleApi
  12. from tracim_backend.lib.core.workspace import WorkspaceApi
  13. from tracim_backend.models import User
  14. from tracim_backend.models.context_models import UserInContext
  15. from tracim_backend.models.data import UserRoleInWorkspace
  16. from tracim_backend.tests import DefaultTest
  17. from tracim_backend.tests import eq_
  18. class TestUserApi(DefaultTest):
  19. def test_unit__create_minimal_user__ok__nominal_case(self):
  20. api = UserApi(
  21. current_user=None,
  22. session=self.session,
  23. config=self.config,
  24. )
  25. u = api.create_minimal_user('bob@bob')
  26. assert u.email == 'bob@bob'
  27. assert u.display_name == 'bob'
  28. def test_unit__create_minimal_user_and_update__ok__nominal_case(self):
  29. api = UserApi(
  30. current_user=None,
  31. session=self.session,
  32. config=self.config,
  33. )
  34. u = api.create_minimal_user('bob@bob')
  35. api.update(u, 'bob', 'bob@bob', 'pass', do_save=True)
  36. nu = api.get_one_by_email('bob@bob')
  37. assert nu is not None
  38. assert nu.email == 'bob@bob'
  39. assert nu.display_name == 'bob'
  40. assert nu.validate_password('pass')
  41. def test__unit__create__user__ok_nominal_case(self):
  42. api = UserApi(
  43. current_user=None,
  44. session=self.session,
  45. config=self.config,
  46. )
  47. u = api.create_user(
  48. email='bob@bob',
  49. password='pass',
  50. name='bob',
  51. timezone='+2',
  52. do_save=True,
  53. do_notify=False,
  54. )
  55. assert u is not None
  56. assert u.email == "bob@bob"
  57. assert u.validate_password('pass')
  58. assert u.display_name == 'bob'
  59. assert u.timezone == '+2'
  60. def test_unit__user_with_email_exists__ok__nominal_case(self):
  61. api = UserApi(
  62. current_user=None,
  63. session=self.session,
  64. config=self.config,
  65. )
  66. u = api.create_minimal_user('bibi@bibi')
  67. api.update(u, 'bibi', 'bibi@bibi', 'pass', do_save=True)
  68. transaction.commit()
  69. eq_(True, api.user_with_email_exists('bibi@bibi'))
  70. eq_(False, api.user_with_email_exists('unknown'))
  71. def test_get_one_by_email(self):
  72. api = UserApi(
  73. current_user=None,
  74. session=self.session,
  75. config=self.config,
  76. )
  77. u = api.create_minimal_user('bibi@bibi')
  78. self.session.flush()
  79. api.update(u, 'bibi', 'bibi@bibi', 'pass', do_save=True)
  80. uid = u.user_id
  81. transaction.commit()
  82. eq_(uid, api.get_one_by_email('bibi@bibi').user_id)
  83. def test_unit__get_one_by_email__err__user_does_not_exist(self):
  84. api = UserApi(
  85. current_user=None,
  86. session=self.session,
  87. config=self.config,
  88. )
  89. with pytest.raises(UserDoesNotExist):
  90. api.get_one_by_email('unknown')
  91. def test_unit__get_all__ok__nominal_case(self):
  92. api = UserApi(
  93. current_user=None,
  94. session=self.session,
  95. config=self.config,
  96. )
  97. u1 = api.create_minimal_user('bibi@bibi')
  98. users = api.get_all()
  99. # u1 + Admin user from BaseFixture
  100. assert 2 == len(users)
  101. def test_unit__get_known__user__admin__too_short_acp_str(self):
  102. api = UserApi(
  103. current_user=None,
  104. session=self.session,
  105. config=self.config,
  106. )
  107. u1 = api.create_user(
  108. email='email@email',
  109. name='name',
  110. do_notify=False,
  111. do_save=True,
  112. )
  113. with pytest.raises(TooShortAutocompleteString):
  114. api.get_known_user('e')
  115. def test_unit__get_known__user__admin__by_email(self):
  116. api = UserApi(
  117. current_user=None,
  118. session=self.session,
  119. config=self.config,
  120. )
  121. u1 = api.create_user(
  122. email='email@email',
  123. name='name',
  124. do_notify=False,
  125. do_save=True,
  126. )
  127. users = api.get_known_user('email')
  128. assert len(users) == 1
  129. assert users[0] == u1
  130. def test_unit__get_known__user__user__no_workspace_empty_known_user(self):
  131. admin = self.session.query(models.User) \
  132. .filter(models.User.email == 'admin@admin.admin') \
  133. .one()
  134. api = UserApi(
  135. current_user=admin,
  136. session=self.session,
  137. config=self.config,
  138. )
  139. u1 = api.create_user(
  140. email='email@email',
  141. name='name',
  142. do_notify=False,
  143. do_save=True,
  144. )
  145. api2 = UserApi(
  146. current_user=u1,
  147. session=self.session,
  148. config=self.config,
  149. )
  150. users = api2.get_known_user('email')
  151. assert len(users) == 0
  152. def test_unit__get_known__user__same_workspaces_users_by_name(self):
  153. admin = self.session.query(models.User) \
  154. .filter(models.User.email == 'admin@admin.admin') \
  155. .one()
  156. api = UserApi(
  157. current_user=None,
  158. session=self.session,
  159. config=self.config,
  160. )
  161. u1 = api.create_user(
  162. email='email@email',
  163. name='name',
  164. do_notify=False,
  165. do_save=True,
  166. )
  167. u2 = api.create_user(
  168. email='email2@email2',
  169. name='name2',
  170. do_notify=False,
  171. do_save=True,
  172. )
  173. u3 = api.create_user(
  174. email='notfound@notfound',
  175. name='notfound',
  176. do_notify=False,
  177. do_save=True,
  178. )
  179. wapi = WorkspaceApi(
  180. current_user=admin,
  181. session=self.session,
  182. config=self.app_config,
  183. )
  184. workspace = wapi.create_workspace(
  185. 'test workspace n°1',
  186. save_now=True)
  187. role_api = RoleApi(
  188. current_user=admin,
  189. session=self.session,
  190. config=self.app_config,
  191. )
  192. role_api.create_one(u1, workspace, UserRoleInWorkspace.READER, False)
  193. role_api.create_one(u2, workspace, UserRoleInWorkspace.READER, False)
  194. role_api.create_one(u3, workspace, UserRoleInWorkspace.READER, False)
  195. api2 = UserApi(
  196. current_user=u1,
  197. session=self.session,
  198. config=self.config,
  199. )
  200. users = api2.get_known_user('name')
  201. assert len(users) == 2
  202. assert users[0] == u1
  203. assert users[1] == u2
  204. def test_unit__get_known__user__same_workspaces_users_by_email(self):
  205. admin = self.session.query(models.User) \
  206. .filter(models.User.email == 'admin@admin.admin') \
  207. .one()
  208. api = UserApi(
  209. current_user=None,
  210. session=self.session,
  211. config=self.config,
  212. )
  213. u1 = api.create_user(
  214. email='email@email',
  215. name='name',
  216. do_notify=False,
  217. do_save=True,
  218. )
  219. u2 = api.create_user(
  220. email='email2@email2',
  221. name='name2',
  222. do_notify=False,
  223. do_save=True,
  224. )
  225. u3 = api.create_user(
  226. email='notfound@notfound',
  227. name='notfound',
  228. do_notify=False,
  229. do_save=True,
  230. )
  231. wapi = WorkspaceApi(
  232. current_user=admin,
  233. session=self.session,
  234. config=self.app_config,
  235. )
  236. workspace = wapi.create_workspace(
  237. 'test workspace n°1',
  238. save_now=True)
  239. role_api = RoleApi(
  240. current_user=admin,
  241. session=self.session,
  242. config=self.app_config,
  243. )
  244. role_api.create_one(u1, workspace, UserRoleInWorkspace.READER, False)
  245. role_api.create_one(u2, workspace, UserRoleInWorkspace.READER, False)
  246. role_api.create_one(u3, workspace, UserRoleInWorkspace.READER, False)
  247. api2 = UserApi(
  248. current_user=u1,
  249. session=self.session,
  250. config=self.config,
  251. )
  252. users = api2.get_known_user('email')
  253. assert len(users) == 2
  254. assert users[0] == u1
  255. assert users[1] == u2
  256. def test_unit__get_known__user__admin__by_name(self):
  257. api = UserApi(
  258. current_user=None,
  259. session=self.session,
  260. config=self.config,
  261. )
  262. u1 = api.create_user(
  263. email='email@email',
  264. name='name',
  265. do_notify=False,
  266. do_save=True,
  267. )
  268. users = api.get_known_user('nam')
  269. assert len(users) == 1
  270. assert users[0] == u1
  271. def test_unit__get_one__ok__nominal_case(self):
  272. api = UserApi(
  273. current_user=None,
  274. session=self.session,
  275. config=self.config,
  276. )
  277. u = api.create_minimal_user('titi@titi')
  278. api.update(u, 'titi', 'titi@titi', 'pass', do_save=True)
  279. one = api.get_one(u.user_id)
  280. eq_(u.user_id, one.user_id)
  281. def test_unit__get_user_with_context__nominal_case(self):
  282. user = User(
  283. email='admin@tracim.tracim',
  284. display_name='Admin',
  285. is_active=True,
  286. )
  287. api = UserApi(
  288. current_user=None,
  289. session=self.session,
  290. config=self.config,
  291. )
  292. new_user = api.get_user_with_context(user)
  293. assert isinstance(new_user, UserInContext)
  294. assert new_user.user == user
  295. assert new_user.profile == 'nobody'
  296. assert new_user.user_id == user.user_id
  297. assert new_user.email == 'admin@tracim.tracim'
  298. assert new_user.display_name == 'Admin'
  299. assert new_user.is_active is True
  300. # TODO - G.M - 03-05-2018 - [avatar][calendar] Should test this
  301. # with true value when those param will be available.
  302. assert new_user.avatar_url is None
  303. assert new_user.calendar_url is None
  304. def test_unit__get_current_user_ok__nominal_case(self):
  305. user = User(email='admin@tracim.tracim')
  306. api = UserApi(
  307. current_user=user,
  308. session=self.session,
  309. config=self.config,
  310. )
  311. new_user = api.get_current_user()
  312. assert isinstance(new_user, User)
  313. assert user == new_user
  314. def test_unit__get_current_user__err__user_not_exist(self):
  315. api = UserApi(
  316. current_user=None,
  317. session=self.session,
  318. config=self.config,
  319. )
  320. with pytest.raises(UserDoesNotExist):
  321. api.get_current_user()
  322. def test_unit__authenticate_user___ok__nominal_case(self):
  323. api = UserApi(
  324. current_user=None,
  325. session=self.session,
  326. config=self.config,
  327. )
  328. user = api.authenticate_user('admin@admin.admin', 'admin@admin.admin')
  329. assert isinstance(user, User)
  330. assert user.email == 'admin@admin.admin'
  331. def test_unit__authenticate_user___err__user_not_active(self):
  332. api = UserApi(
  333. current_user=None,
  334. session=self.session,
  335. config=self.config,
  336. )
  337. gapi = GroupApi(
  338. current_user=None,
  339. session=self.session,
  340. config=self.config,
  341. )
  342. groups = [gapi.get_one_with_name('users')]
  343. user = api.create_user(
  344. email='test@test.test',
  345. password='pass',
  346. name='bob',
  347. groups=groups,
  348. timezone='Europe/Paris',
  349. do_save=True,
  350. do_notify=False,
  351. )
  352. api.disable(user)
  353. with pytest.raises(UserNotActive):
  354. api.authenticate_user('test@test.test', 'test@test.test')
  355. def test_unit__authenticate_user___err__wrong_password(self):
  356. api = UserApi(
  357. current_user=None,
  358. session=self.session,
  359. config=self.config,
  360. )
  361. with pytest.raises(AuthenticationFailed):
  362. api.authenticate_user('admin@admin.admin', 'wrong_password')
  363. def test_unit__authenticate_user___err__wrong_user(self):
  364. api = UserApi(
  365. current_user=None,
  366. session=self.session,
  367. config=self.config,
  368. )
  369. with pytest.raises(AuthenticationFailed):
  370. api.authenticate_user('admin@admin.admin', 'wrong_password')