test_user.py 36KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. # -*- coding: utf-8 -*-
  2. """
  3. Tests for /api/v2/users subpath endpoints.
  4. """
  5. from time import sleep
  6. import pytest
  7. import transaction
  8. from tracim import models
  9. from tracim.lib.core.content import ContentApi
  10. from tracim.lib.core.user import UserApi
  11. from tracim.lib.core.workspace import WorkspaceApi
  12. from tracim.models import get_tm_session
  13. from tracim.models.contents import ContentTypeLegacy as ContentType
  14. from tracim.models.revision_protection import new_revision
  15. from tracim.tests import FunctionalTest
  16. from tracim.fixtures.content import Content as ContentFixtures
  17. from tracim.fixtures.users_and_groups import Base as BaseFixture
  18. class TestUserRecentlyActiveContentEndpoint(FunctionalTest):
  19. """
  20. Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/contents/recently_active # nopep8
  21. """
  22. fixtures = [BaseFixture]
  23. def test_api__get_recently_active_content__ok__200__nominal_case(self):
  24. # init DB
  25. dbsession = get_tm_session(self.session_factory, transaction.manager)
  26. admin = dbsession.query(models.User) \
  27. .filter(models.User.email == 'admin@admin.admin') \
  28. .one()
  29. workspace_api = WorkspaceApi(
  30. current_user=admin,
  31. session=dbsession,
  32. config=self.app_config
  33. )
  34. workspace = WorkspaceApi(
  35. current_user=admin,
  36. session=dbsession,
  37. config=self.app_config,
  38. ).create_workspace(
  39. 'test workspace',
  40. save_now=True
  41. )
  42. workspace2 = WorkspaceApi(
  43. current_user=admin,
  44. session=dbsession,
  45. config=self.app_config,
  46. ).create_workspace(
  47. 'test workspace2',
  48. save_now=True
  49. )
  50. api = ContentApi(
  51. current_user=admin,
  52. session=dbsession,
  53. config=self.app_config,
  54. )
  55. main_folder_workspace2 = api.create(ContentType.Folder, workspace2, None, 'Hepla', '', True) # nopep8
  56. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  57. # creation order test
  58. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  59. secondly_created = api.create(ContentType.Page, workspace, main_folder, 'another creation_order_test', '', True) # nopep8
  60. # update order test
  61. firstly_created_but_recently_updated = api.create(ContentType.Page, workspace, main_folder, 'update_order_test', '', True) # nopep8
  62. secondly_created_but_not_updated = api.create(ContentType.Page, workspace, main_folder, 'another update_order_test', '', True) # nopep8
  63. with new_revision(
  64. session=dbsession,
  65. tm=transaction.manager,
  66. content=firstly_created_but_recently_updated,
  67. ):
  68. firstly_created_but_recently_updated.description = 'Just an update'
  69. api.save(firstly_created_but_recently_updated)
  70. # comment change order
  71. firstly_created_but_recently_commented = api.create(ContentType.Page, workspace, main_folder, 'this is randomized label content', '', True) # nopep8
  72. secondly_created_but_not_commented = api.create(ContentType.Page, workspace, main_folder, 'this is another randomized label content', '', True) # nopep8
  73. comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True) # nopep8
  74. content_workspace_2 = api.create(ContentType.Page, workspace2,main_folder_workspace2, 'content_workspace_2', '',True) # nopep8
  75. dbsession.flush()
  76. transaction.commit()
  77. self.testapp.authorization = (
  78. 'Basic',
  79. (
  80. 'admin@admin.admin',
  81. 'admin@admin.admin'
  82. )
  83. )
  84. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/recently_active'.format(workspace.workspace_id), status=200) # nopep8
  85. res = res.json_body
  86. assert len(res) == 7
  87. for elem in res:
  88. assert isinstance(elem['content_id'], int)
  89. assert isinstance(elem['content_type'], str)
  90. assert elem['content_type'] != 'comments'
  91. assert isinstance(elem['is_archived'], bool)
  92. assert isinstance(elem['is_deleted'], bool)
  93. assert isinstance(elem['label'], str)
  94. assert isinstance(elem['parent_id'], int) or elem['parent_id'] is None
  95. assert isinstance(elem['show_in_ui'], bool)
  96. assert isinstance(elem['slug'], str)
  97. assert isinstance(elem['status'], str)
  98. assert isinstance(elem['sub_content_types'], list)
  99. for sub_content_type in elem['sub_content_types']:
  100. assert isinstance(sub_content_type, str)
  101. assert isinstance(elem['workspace_id'], int)
  102. # comment is newest than page2
  103. assert res[0]['content_id'] == firstly_created_but_recently_commented.content_id
  104. assert res[1]['content_id'] == secondly_created_but_not_commented.content_id
  105. # last updated content is newer than other one despite creation
  106. # of the other is more recent
  107. assert res[2]['content_id'] == firstly_created_but_recently_updated.content_id
  108. assert res[3]['content_id'] == secondly_created_but_not_updated.content_id
  109. # creation order is inverted here as last created is last active
  110. assert res[4]['content_id'] == secondly_created.content_id
  111. assert res[5]['content_id'] == firstly_created.content_id
  112. # folder subcontent modification does not change folder order
  113. assert res[6]['content_id'] == main_folder.content_id
  114. @pytest.mark.skip('Test should be fixed')
  115. def test_api__get_recently_active_content__ok__200__limit_2_multiple(self):
  116. # TODO - G.M - 2018-07-20 - Better fix for this test, do not use sleep()
  117. # anymore to fix datetime lack of precision.
  118. # init DB
  119. dbsession = get_tm_session(self.session_factory, transaction.manager)
  120. admin = dbsession.query(models.User) \
  121. .filter(models.User.email == 'admin@admin.admin') \
  122. .one()
  123. workspace_api = WorkspaceApi(
  124. current_user=admin,
  125. session=dbsession,
  126. config=self.app_config
  127. )
  128. workspace = WorkspaceApi(
  129. current_user=admin,
  130. session=dbsession,
  131. config=self.app_config,
  132. ).create_workspace(
  133. 'test workspace',
  134. save_now=True
  135. )
  136. workspace2 = WorkspaceApi(
  137. current_user=admin,
  138. session=dbsession,
  139. config=self.app_config,
  140. ).create_workspace(
  141. 'test workspace2',
  142. save_now=True
  143. )
  144. api = ContentApi(
  145. current_user=admin,
  146. session=dbsession,
  147. config=self.app_config,
  148. )
  149. main_folder_workspace2 = api.create(ContentType.Folder, workspace2, None, 'Hepla', '', True) # nopep8
  150. sleep(1)
  151. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  152. # creation order test
  153. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  154. sleep(1)
  155. secondly_created = api.create(ContentType.Page, workspace, main_folder, 'another creation_order_test', '', True) # nopep8
  156. # update order test
  157. firstly_created_but_recently_updated = api.create(ContentType.Page, workspace, main_folder, 'update_order_test', '', True) # nopep8
  158. sleep(1)
  159. secondly_created_but_not_updated = api.create(ContentType.Page, workspace, main_folder, 'another update_order_test', '', True) # nopep8
  160. sleep(1)
  161. with new_revision(
  162. session=dbsession,
  163. tm=transaction.manager,
  164. content=firstly_created_but_recently_updated,
  165. ):
  166. firstly_created_but_recently_updated.description = 'Just an update'
  167. api.save(firstly_created_but_recently_updated)
  168. # comment change order
  169. firstly_created_but_recently_commented = api.create(ContentType.Page, workspace, main_folder, 'this is randomized label content', '', True) # nopep8
  170. sleep(1)
  171. secondly_created_but_not_commented = api.create(ContentType.Page, workspace, main_folder, 'this is another randomized label content', '', True) # nopep8
  172. sleep(1)
  173. comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True) # nopep8
  174. sleep(1)
  175. content_workspace_2 = api.create(ContentType.Page, workspace2,main_folder_workspace2, 'content_workspace_2', '',True) # nopep8
  176. dbsession.flush()
  177. transaction.commit()
  178. self.testapp.authorization = (
  179. 'Basic',
  180. (
  181. 'admin@admin.admin',
  182. 'admin@admin.admin'
  183. )
  184. )
  185. params = {
  186. 'limit': 2,
  187. }
  188. res = self.testapp.get(
  189. '/api/v2/users/1/workspaces/{}/contents/recently_active'.format(workspace.workspace_id), # nopep8
  190. status=200,
  191. params=params
  192. ) # nopep8
  193. res = res.json_body
  194. assert len(res) == 2
  195. for elem in res:
  196. assert isinstance(elem['content_id'], int)
  197. assert isinstance(elem['content_type'], str)
  198. assert elem['content_type'] != 'comments'
  199. assert isinstance(elem['is_archived'], bool)
  200. assert isinstance(elem['is_deleted'], bool)
  201. assert isinstance(elem['label'], str)
  202. assert isinstance(elem['parent_id'], int) or elem['parent_id'] is None
  203. assert isinstance(elem['show_in_ui'], bool)
  204. assert isinstance(elem['slug'], str)
  205. assert isinstance(elem['status'], str)
  206. assert isinstance(elem['sub_content_types'], list)
  207. for sub_content_type in elem['sub_content_types']:
  208. assert isinstance(sub_content_type, str)
  209. assert isinstance(elem['workspace_id'], int)
  210. # comment is newest than page2
  211. assert res[0]['content_id'] == firstly_created_but_recently_commented.content_id
  212. assert res[1]['content_id'] == secondly_created_but_not_commented.content_id
  213. params = {
  214. 'limit': 2,
  215. 'before_datetime': secondly_created_but_not_commented.get_last_activity_date().strftime('%Y-%m-%dT%H:%M:%SZ'), # nopep8
  216. }
  217. res = self.testapp.get(
  218. '/api/v2/users/1/workspaces/{}/contents/recently_active'.format(workspace.workspace_id), # nopep8
  219. status=200,
  220. params=params
  221. )
  222. res = res.json_body
  223. assert len(res) == 2
  224. # last updated content is newer than other one despite creation
  225. # of the other is more recent
  226. assert res[0]['content_id'] == firstly_created_but_recently_updated.content_id
  227. assert res[1]['content_id'] == secondly_created_but_not_updated.content_id
  228. class TestUserReadStatusEndpoint(FunctionalTest):
  229. """
  230. Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status # nopep8
  231. """
  232. def test_api__get_read_status__ok__200__all(self):
  233. # init DB
  234. dbsession = get_tm_session(self.session_factory, transaction.manager)
  235. admin = dbsession.query(models.User) \
  236. .filter(models.User.email == 'admin@admin.admin') \
  237. .one()
  238. workspace_api = WorkspaceApi(
  239. current_user=admin,
  240. session=dbsession,
  241. config=self.app_config
  242. )
  243. workspace = WorkspaceApi(
  244. current_user=admin,
  245. session=dbsession,
  246. config=self.app_config,
  247. ).create_workspace(
  248. 'test workspace',
  249. save_now=True
  250. )
  251. workspace2 = WorkspaceApi(
  252. current_user=admin,
  253. session=dbsession,
  254. config=self.app_config,
  255. ).create_workspace(
  256. 'test workspace2',
  257. save_now=True
  258. )
  259. api = ContentApi(
  260. current_user=admin,
  261. session=dbsession,
  262. config=self.app_config,
  263. )
  264. main_folder_workspace2 = api.create(ContentType.Folder, workspace2, None, 'Hepla', '', True) # nopep8
  265. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  266. # creation order test
  267. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  268. secondly_created = api.create(ContentType.Page, workspace, main_folder, 'another creation_order_test', '', True) # nopep8
  269. # update order test
  270. firstly_created_but_recently_updated = api.create(ContentType.Page, workspace, main_folder, 'update_order_test', '', True) # nopep8
  271. secondly_created_but_not_updated = api.create(ContentType.Page, workspace, main_folder, 'another update_order_test', '', True) # nopep8
  272. with new_revision(
  273. session=dbsession,
  274. tm=transaction.manager,
  275. content=firstly_created_but_recently_updated,
  276. ):
  277. firstly_created_but_recently_updated.description = 'Just an update'
  278. api.save(firstly_created_but_recently_updated)
  279. # comment change order
  280. firstly_created_but_recently_commented = api.create(ContentType.Page, workspace, main_folder, 'this is randomized label content', '', True) # nopep8
  281. secondly_created_but_not_commented = api.create(ContentType.Page, workspace, main_folder, 'this is another randomized label content', '', True) # nopep8
  282. comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True) # nopep8
  283. content_workspace_2 = api.create(ContentType.Page, workspace2,main_folder_workspace2, 'content_workspace_2', '',True) # nopep8
  284. dbsession.flush()
  285. transaction.commit()
  286. self.testapp.authorization = (
  287. 'Basic',
  288. (
  289. 'admin@admin.admin',
  290. 'admin@admin.admin'
  291. )
  292. )
  293. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  294. res = res.json_body
  295. assert len(res) == 7
  296. for elem in res:
  297. assert isinstance(elem['content_id'], int)
  298. assert isinstance(elem['read_by_user'], bool)
  299. # comment is newest than page2
  300. assert res[0]['content_id'] == firstly_created_but_recently_commented.content_id
  301. assert res[1]['content_id'] == secondly_created_but_not_commented.content_id
  302. # last updated content is newer than other one despite creation
  303. # of the other is more recent
  304. assert res[2]['content_id'] == firstly_created_but_recently_updated.content_id
  305. assert res[3]['content_id'] == secondly_created_but_not_updated.content_id
  306. # creation order is inverted here as last created is last active
  307. assert res[4]['content_id'] == secondly_created.content_id
  308. assert res[5]['content_id'] == firstly_created.content_id
  309. # folder subcontent modification does not change folder order
  310. assert res[6]['content_id'] == main_folder.content_id
  311. def test_api__get_read_status__ok__200__nominal_case(self):
  312. # init DB
  313. dbsession = get_tm_session(self.session_factory, transaction.manager)
  314. admin = dbsession.query(models.User) \
  315. .filter(models.User.email == 'admin@admin.admin') \
  316. .one()
  317. workspace_api = WorkspaceApi(
  318. current_user=admin,
  319. session=dbsession,
  320. config=self.app_config
  321. )
  322. workspace = WorkspaceApi(
  323. current_user=admin,
  324. session=dbsession,
  325. config=self.app_config,
  326. ).create_workspace(
  327. 'test workspace',
  328. save_now=True
  329. )
  330. workspace2 = WorkspaceApi(
  331. current_user=admin,
  332. session=dbsession,
  333. config=self.app_config,
  334. ).create_workspace(
  335. 'test workspace2',
  336. save_now=True
  337. )
  338. api = ContentApi(
  339. current_user=admin,
  340. session=dbsession,
  341. config=self.app_config,
  342. )
  343. main_folder_workspace2 = api.create(ContentType.Folder, workspace2, None, 'Hepla', '', True) # nopep8
  344. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  345. # creation order test
  346. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  347. secondly_created = api.create(ContentType.Page, workspace, main_folder, 'another creation_order_test', '', True) # nopep8
  348. # update order test
  349. firstly_created_but_recently_updated = api.create(ContentType.Page, workspace, main_folder, 'update_order_test', '', True) # nopep8
  350. secondly_created_but_not_updated = api.create(ContentType.Page, workspace, main_folder, 'another update_order_test', '', True) # nopep8
  351. with new_revision(
  352. session=dbsession,
  353. tm=transaction.manager,
  354. content=firstly_created_but_recently_updated,
  355. ):
  356. firstly_created_but_recently_updated.description = 'Just an update'
  357. api.save(firstly_created_but_recently_updated)
  358. # comment change order
  359. firstly_created_but_recently_commented = api.create(ContentType.Page, workspace, main_folder, 'this is randomized label content', '', True) # nopep8
  360. secondly_created_but_not_commented = api.create(ContentType.Page, workspace, main_folder, 'this is another randomized label content', '', True) # nopep8
  361. comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True) # nopep8
  362. content_workspace_2 = api.create(ContentType.Page, workspace2,main_folder_workspace2, 'content_workspace_2', '',True) # nopep8
  363. dbsession.flush()
  364. transaction.commit()
  365. self.testapp.authorization = (
  366. 'Basic',
  367. (
  368. 'admin@admin.admin',
  369. 'admin@admin.admin'
  370. )
  371. )
  372. selected_contents_id = [
  373. firstly_created_but_recently_commented.content_id,
  374. firstly_created_but_recently_updated.content_id,
  375. firstly_created.content_id,
  376. main_folder.content_id,
  377. ]
  378. url = '/api/v2/users/1/workspaces/{workspace_id}/contents/read_status?contents_ids={cid1}&contents_ids={cid2}&contents_ids={cid3}&contents_ids={cid4}'.format( # nopep8
  379. workspace_id=workspace.workspace_id,
  380. cid1=selected_contents_id[0],
  381. cid2=selected_contents_id[1],
  382. cid3=selected_contents_id[2],
  383. cid4=selected_contents_id[3],
  384. )
  385. res = self.testapp.get(
  386. url=url,
  387. status=200,
  388. )
  389. res = res.json_body
  390. assert len(res) == 4
  391. for elem in res:
  392. assert isinstance(elem['content_id'], int)
  393. assert isinstance(elem['read_by_user'], bool)
  394. # comment is newest than page2
  395. assert res[0]['content_id'] == firstly_created_but_recently_commented.content_id
  396. # last updated content is newer than other one despite creation
  397. # of the other is more recent
  398. assert res[1]['content_id'] == firstly_created_but_recently_updated.content_id
  399. # creation order is inverted here as last created is last active
  400. assert res[2]['content_id'] == firstly_created.content_id
  401. # folder subcontent modification does not change folder order
  402. assert res[3]['content_id'] == main_folder.content_id
  403. class TestUserSetContentAsRead(FunctionalTest):
  404. """
  405. Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/read # nopep8
  406. """
  407. def test_api_set_content_as_read__ok__200__nominal_case(self):
  408. # init DB
  409. dbsession = get_tm_session(self.session_factory, transaction.manager)
  410. admin = dbsession.query(models.User) \
  411. .filter(models.User.email == 'admin@admin.admin') \
  412. .one()
  413. workspace_api = WorkspaceApi(
  414. current_user=admin,
  415. session=dbsession,
  416. config=self.app_config
  417. )
  418. workspace = WorkspaceApi(
  419. current_user=admin,
  420. session=dbsession,
  421. config=self.app_config,
  422. ).create_workspace(
  423. 'test workspace',
  424. save_now=True
  425. )
  426. api = ContentApi(
  427. current_user=admin,
  428. session=dbsession,
  429. config=self.app_config,
  430. )
  431. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  432. # creation order test
  433. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  434. api.mark_unread(firstly_created)
  435. dbsession.flush()
  436. transaction.commit()
  437. self.testapp.authorization = (
  438. 'Basic',
  439. (
  440. 'admin@admin.admin',
  441. 'admin@admin.admin'
  442. )
  443. )
  444. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  445. assert res.json_body[0]['content_id'] == firstly_created.content_id
  446. assert res.json_body[0]['read_by_user'] is False
  447. self.testapp.put(
  448. '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/read'.format( # nopep8
  449. workspace_id=workspace.workspace_id,
  450. content_id=firstly_created.content_id,
  451. user_id=admin.user_id,
  452. )
  453. )
  454. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  455. assert res.json_body[0]['content_id'] == firstly_created.content_id
  456. assert res.json_body[0]['read_by_user'] is True
  457. def test_api_set_content_as_read__ok__200__with_comments(self):
  458. # init DB
  459. dbsession = get_tm_session(self.session_factory, transaction.manager)
  460. admin = dbsession.query(models.User) \
  461. .filter(models.User.email == 'admin@admin.admin') \
  462. .one()
  463. workspace_api = WorkspaceApi(
  464. current_user=admin,
  465. session=dbsession,
  466. config=self.app_config
  467. )
  468. workspace = WorkspaceApi(
  469. current_user=admin,
  470. session=dbsession,
  471. config=self.app_config,
  472. ).create_workspace(
  473. 'test workspace',
  474. save_now=True
  475. )
  476. api = ContentApi(
  477. current_user=admin,
  478. session=dbsession,
  479. config=self.app_config,
  480. )
  481. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  482. # creation order test
  483. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  484. comments = api.create_comment(workspace, firstly_created, 'juste a super comment', True) # nopep8
  485. api.mark_unread(firstly_created)
  486. api.mark_unread(comments)
  487. dbsession.flush()
  488. transaction.commit()
  489. self.testapp.authorization = (
  490. 'Basic',
  491. (
  492. 'admin@admin.admin',
  493. 'admin@admin.admin'
  494. )
  495. )
  496. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  497. assert res.json_body[0]['content_id'] == firstly_created.content_id
  498. assert res.json_body[0]['read_by_user'] is False
  499. self.testapp.put(
  500. '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/read'.format( # nopep8
  501. workspace_id=workspace.workspace_id,
  502. content_id=firstly_created.content_id,
  503. user_id=admin.user_id,
  504. )
  505. )
  506. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  507. assert res.json_body[0]['content_id'] == firstly_created.content_id
  508. assert res.json_body[0]['read_by_user'] is True
  509. # comment is also set as read
  510. assert comments.has_new_information_for(admin) is False
  511. class TestUserSetContentAsUnread(FunctionalTest):
  512. """
  513. Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/unread # nopep8
  514. """
  515. def test_api_set_content_as_unread__ok__200__nominal_case(self):
  516. # init DB
  517. dbsession = get_tm_session(self.session_factory, transaction.manager)
  518. admin = dbsession.query(models.User) \
  519. .filter(models.User.email == 'admin@admin.admin') \
  520. .one()
  521. workspace_api = WorkspaceApi(
  522. current_user=admin,
  523. session=dbsession,
  524. config=self.app_config
  525. )
  526. workspace = WorkspaceApi(
  527. current_user=admin,
  528. session=dbsession,
  529. config=self.app_config,
  530. ).create_workspace(
  531. 'test workspace',
  532. save_now=True
  533. )
  534. api = ContentApi(
  535. current_user=admin,
  536. session=dbsession,
  537. config=self.app_config,
  538. )
  539. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  540. # creation order test
  541. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  542. api.mark_read(firstly_created)
  543. dbsession.flush()
  544. transaction.commit()
  545. self.testapp.authorization = (
  546. 'Basic',
  547. (
  548. 'admin@admin.admin',
  549. 'admin@admin.admin'
  550. )
  551. )
  552. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  553. assert res.json_body[0]['content_id'] == firstly_created.content_id
  554. assert res.json_body[0]['read_by_user'] is True
  555. self.testapp.put(
  556. '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/unread'.format( # nopep8
  557. workspace_id=workspace.workspace_id,
  558. content_id=firstly_created.content_id,
  559. user_id=admin.user_id,
  560. )
  561. )
  562. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  563. assert res.json_body[0]['content_id'] == firstly_created.content_id
  564. assert res.json_body[0]['read_by_user'] is False
  565. def test_api_set_content_as_unread__ok__200__with_comments(self):
  566. # init DB
  567. dbsession = get_tm_session(self.session_factory, transaction.manager)
  568. admin = dbsession.query(models.User) \
  569. .filter(models.User.email == 'admin@admin.admin') \
  570. .one()
  571. workspace_api = WorkspaceApi(
  572. current_user=admin,
  573. session=dbsession,
  574. config=self.app_config
  575. )
  576. workspace = WorkspaceApi(
  577. current_user=admin,
  578. session=dbsession,
  579. config=self.app_config,
  580. ).create_workspace(
  581. 'test workspace',
  582. save_now=True
  583. )
  584. api = ContentApi(
  585. current_user=admin,
  586. session=dbsession,
  587. config=self.app_config,
  588. )
  589. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  590. # creation order test
  591. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  592. comments = api.create_comment(workspace, firstly_created, 'juste a super comment', True) # nopep8
  593. api.mark_read(firstly_created)
  594. api.mark_read(comments)
  595. dbsession.flush()
  596. transaction.commit()
  597. self.testapp.authorization = (
  598. 'Basic',
  599. (
  600. 'admin@admin.admin',
  601. 'admin@admin.admin'
  602. )
  603. )
  604. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  605. assert res.json_body[0]['content_id'] == firstly_created.content_id
  606. assert res.json_body[0]['read_by_user'] is True
  607. self.testapp.put(
  608. '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/unread'.format( # nopep8
  609. workspace_id=workspace.workspace_id,
  610. content_id=firstly_created.content_id,
  611. user_id=admin.user_id,
  612. )
  613. )
  614. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  615. assert res.json_body[0]['content_id'] == firstly_created.content_id
  616. assert res.json_body[0]['read_by_user'] is False
  617. assert comments.has_new_information_for(admin) is True
  618. class TestUserSetWorkspaceAsRead(FunctionalTest):
  619. """
  620. Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/read
  621. """
  622. def test_api_set_content_as_read__ok__200__nominal_case(self):
  623. # init DB
  624. dbsession = get_tm_session(self.session_factory, transaction.manager)
  625. admin = dbsession.query(models.User) \
  626. .filter(models.User.email == 'admin@admin.admin') \
  627. .one()
  628. workspace_api = WorkspaceApi(
  629. current_user=admin,
  630. session=dbsession,
  631. config=self.app_config
  632. )
  633. workspace = WorkspaceApi(
  634. current_user=admin,
  635. session=dbsession,
  636. config=self.app_config,
  637. ).create_workspace(
  638. 'test workspace',
  639. save_now=True
  640. )
  641. api = ContentApi(
  642. current_user=admin,
  643. session=dbsession,
  644. config=self.app_config,
  645. )
  646. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  647. # creation order test
  648. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  649. api.mark_unread(main_folder)
  650. api.mark_unread(firstly_created)
  651. dbsession.flush()
  652. transaction.commit()
  653. self.testapp.authorization = (
  654. 'Basic',
  655. (
  656. 'admin@admin.admin',
  657. 'admin@admin.admin'
  658. )
  659. )
  660. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  661. assert res.json_body[0]['content_id'] == firstly_created.content_id
  662. assert res.json_body[0]['read_by_user'] is False
  663. assert res.json_body[1]['content_id'] == main_folder.content_id
  664. assert res.json_body[1]['read_by_user'] is False
  665. self.testapp.put(
  666. '/api/v2/users/{user_id}/workspaces/{workspace_id}/read'.format( # nopep8
  667. workspace_id=workspace.workspace_id,
  668. content_id=firstly_created.content_id,
  669. user_id=admin.user_id,
  670. )
  671. )
  672. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  673. assert res.json_body[0]['content_id'] == firstly_created.content_id
  674. assert res.json_body[0]['read_by_user'] is True
  675. assert res.json_body[1]['content_id'] == main_folder.content_id
  676. assert res.json_body[1]['read_by_user'] is True
  677. class TestUserWorkspaceEndpoint(FunctionalTest):
  678. """
  679. Tests for /api/v2/users/{user_id}/workspaces
  680. """
  681. fixtures = [BaseFixture, ContentFixtures]
  682. def test_api__get_user_workspaces__ok_200__nominal_case(self):
  683. """
  684. Check obtain all workspaces reachables for user with user auth.
  685. """
  686. self.testapp.authorization = (
  687. 'Basic',
  688. (
  689. 'admin@admin.admin',
  690. 'admin@admin.admin'
  691. )
  692. )
  693. res = self.testapp.get('/api/v2/users/1/workspaces', status=200)
  694. res = res.json_body
  695. workspace = res[0]
  696. assert workspace['workspace_id'] == 1
  697. assert workspace['label'] == 'Business'
  698. assert workspace['slug'] == 'business'
  699. assert len(workspace['sidebar_entries']) == 7
  700. sidebar_entry = workspace['sidebar_entries'][0]
  701. assert sidebar_entry['slug'] == 'dashboard'
  702. assert sidebar_entry['label'] == 'Dashboard'
  703. assert sidebar_entry['route'] == '/#/workspaces/1/dashboard' # nopep8
  704. assert sidebar_entry['hexcolor'] == "#252525"
  705. assert sidebar_entry['fa_icon'] == "signal"
  706. sidebar_entry = workspace['sidebar_entries'][1]
  707. assert sidebar_entry['slug'] == 'contents/all'
  708. assert sidebar_entry['label'] == 'All Contents'
  709. assert sidebar_entry['route'] == "/#/workspaces/1/contents" # nopep8
  710. assert sidebar_entry['hexcolor'] == "#fdfdfd"
  711. assert sidebar_entry['fa_icon'] == "th"
  712. sidebar_entry = workspace['sidebar_entries'][2]
  713. assert sidebar_entry['slug'] == 'contents/html-documents'
  714. assert sidebar_entry['label'] == 'Text Documents'
  715. assert sidebar_entry['route'] == '/#/workspaces/1/contents?type=html-documents' # nopep8
  716. assert sidebar_entry['hexcolor'] == "#3f52e3"
  717. assert sidebar_entry['fa_icon'] == "file-text-o"
  718. sidebar_entry = workspace['sidebar_entries'][3]
  719. assert sidebar_entry['slug'] == 'contents/markdownpluspage'
  720. assert sidebar_entry['label'] == 'Markdown Plus Documents'
  721. assert sidebar_entry['route'] == "/#/workspaces/1/contents?type=markdownpluspage" # nopep8
  722. assert sidebar_entry['hexcolor'] == "#f12d2d"
  723. assert sidebar_entry['fa_icon'] == "file-code-o"
  724. sidebar_entry = workspace['sidebar_entries'][4]
  725. assert sidebar_entry['slug'] == 'contents/files'
  726. assert sidebar_entry['label'] == 'Files'
  727. assert sidebar_entry['route'] == "/#/workspaces/1/contents?type=file" # nopep8
  728. assert sidebar_entry['hexcolor'] == "#FF9900"
  729. assert sidebar_entry['fa_icon'] == "paperclip"
  730. sidebar_entry = workspace['sidebar_entries'][5]
  731. assert sidebar_entry['slug'] == 'contents/threads'
  732. assert sidebar_entry['label'] == 'Threads'
  733. assert sidebar_entry['route'] == "/#/workspaces/1/contents?type=thread" # nopep8
  734. assert sidebar_entry['hexcolor'] == "#ad4cf9"
  735. assert sidebar_entry['fa_icon'] == "comments-o"
  736. sidebar_entry = workspace['sidebar_entries'][6]
  737. assert sidebar_entry['slug'] == 'calendar'
  738. assert sidebar_entry['label'] == 'Calendar'
  739. assert sidebar_entry['route'] == "/#/workspaces/1/calendar" # nopep8
  740. assert sidebar_entry['hexcolor'] == "#757575"
  741. assert sidebar_entry['fa_icon'] == "calendar"
  742. def test_api__get_user_workspaces__err_403__unallowed_user(self):
  743. """
  744. Check obtain all workspaces reachables for one user
  745. with another non-admin user auth.
  746. """
  747. self.testapp.authorization = (
  748. 'Basic',
  749. (
  750. 'lawrence-not-real-email@fsf.local',
  751. 'foobarbaz'
  752. )
  753. )
  754. res = self.testapp.get('/api/v2/users/1/workspaces', status=403)
  755. assert isinstance(res.json, dict)
  756. assert 'code' in res.json.keys()
  757. assert 'message' in res.json.keys()
  758. assert 'details' in res.json.keys()
  759. def test_api__get_user_workspaces__err_401__unregistered_user(self):
  760. """
  761. Check obtain all workspaces reachables for one user
  762. without correct user auth (user unregistered).
  763. """
  764. self.testapp.authorization = (
  765. 'Basic',
  766. (
  767. 'john@doe.doe',
  768. 'lapin'
  769. )
  770. )
  771. res = self.testapp.get('/api/v2/users/1/workspaces', status=401)
  772. assert isinstance(res.json, dict)
  773. assert 'code' in res.json.keys()
  774. assert 'message' in res.json.keys()
  775. assert 'details' in res.json.keys()
  776. def test_api__get_user_workspaces__err_400__user_does_not_exist(self):
  777. """
  778. Check obtain all workspaces reachables for one user who does
  779. not exist
  780. with a correct user auth.
  781. """
  782. self.testapp.authorization = (
  783. 'Basic',
  784. (
  785. 'admin@admin.admin',
  786. 'admin@admin.admin'
  787. )
  788. )
  789. res = self.testapp.get('/api/v2/users/5/workspaces', status=400)
  790. assert isinstance(res.json, dict)
  791. assert 'code' in res.json.keys()
  792. assert 'message' in res.json.keys()
  793. assert 'details' in res.json.keys()