test_user_api.py 13KB

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