test_user.py 37KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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. def test_api__get_recently_active_content__ok__200__limit_2_multiple(self):
  115. # TODO - G.M - 2018-07-20 - Better fix for this test, do not use sleep()
  116. # anymore to fix datetime lack of precision.
  117. # init DB
  118. dbsession = get_tm_session(self.session_factory, transaction.manager)
  119. admin = dbsession.query(models.User) \
  120. .filter(models.User.email == 'admin@admin.admin') \
  121. .one()
  122. workspace_api = WorkspaceApi(
  123. current_user=admin,
  124. session=dbsession,
  125. config=self.app_config
  126. )
  127. workspace = WorkspaceApi(
  128. current_user=admin,
  129. session=dbsession,
  130. config=self.app_config,
  131. ).create_workspace(
  132. 'test workspace',
  133. save_now=True
  134. )
  135. workspace2 = WorkspaceApi(
  136. current_user=admin,
  137. session=dbsession,
  138. config=self.app_config,
  139. ).create_workspace(
  140. 'test workspace2',
  141. save_now=True
  142. )
  143. api = ContentApi(
  144. current_user=admin,
  145. session=dbsession,
  146. config=self.app_config,
  147. )
  148. main_folder_workspace2 = api.create(ContentType.Folder, workspace2, None, 'Hepla', '', True) # nopep8
  149. sleep(1)
  150. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  151. # creation order test
  152. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  153. sleep(1)
  154. secondly_created = api.create(ContentType.Page, workspace, main_folder, 'another creation_order_test', '', True) # nopep8
  155. # update order test
  156. firstly_created_but_recently_updated = api.create(ContentType.Page, workspace, main_folder, 'update_order_test', '', True) # nopep8
  157. sleep(1)
  158. secondly_created_but_not_updated = api.create(ContentType.Page, workspace, main_folder, 'another update_order_test', '', True) # nopep8
  159. sleep(1)
  160. with new_revision(
  161. session=dbsession,
  162. tm=transaction.manager,
  163. content=firstly_created_but_recently_updated,
  164. ):
  165. firstly_created_but_recently_updated.description = 'Just an update'
  166. api.save(firstly_created_but_recently_updated)
  167. # comment change order
  168. firstly_created_but_recently_commented = api.create(ContentType.Page, workspace, main_folder, 'this is randomized label content', '', True) # nopep8
  169. sleep(1)
  170. secondly_created_but_not_commented = api.create(ContentType.Page, workspace, main_folder, 'this is another randomized label content', '', True) # nopep8
  171. sleep(1)
  172. comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True) # nopep8
  173. sleep(1)
  174. content_workspace_2 = api.create(ContentType.Page, workspace2,main_folder_workspace2, 'content_workspace_2', '',True) # nopep8
  175. dbsession.flush()
  176. transaction.commit()
  177. self.testapp.authorization = (
  178. 'Basic',
  179. (
  180. 'admin@admin.admin',
  181. 'admin@admin.admin'
  182. )
  183. )
  184. params = {
  185. 'limit': 2,
  186. }
  187. res = self.testapp.get(
  188. '/api/v2/users/1/workspaces/{}/contents/recently_active'.format(workspace.workspace_id), # nopep8
  189. status=200,
  190. params=params
  191. ) # nopep8
  192. res = res.json_body
  193. assert len(res) == 2
  194. for elem in res:
  195. assert isinstance(elem['content_id'], int)
  196. assert isinstance(elem['content_type'], str)
  197. assert elem['content_type'] != 'comments'
  198. assert isinstance(elem['is_archived'], bool)
  199. assert isinstance(elem['is_deleted'], bool)
  200. assert isinstance(elem['label'], str)
  201. assert isinstance(elem['parent_id'], int) or elem['parent_id'] is None
  202. assert isinstance(elem['show_in_ui'], bool)
  203. assert isinstance(elem['slug'], str)
  204. assert isinstance(elem['status'], str)
  205. assert isinstance(elem['sub_content_types'], list)
  206. for sub_content_type in elem['sub_content_types']:
  207. assert isinstance(sub_content_type, str)
  208. assert isinstance(elem['workspace_id'], int)
  209. # comment is newest than page2
  210. assert res[0]['content_id'] == firstly_created_but_recently_commented.content_id
  211. assert res[1]['content_id'] == secondly_created_but_not_commented.content_id
  212. params = {
  213. 'limit': 2,
  214. 'before_datetime': secondly_created_but_not_commented.get_last_activity_date().strftime('%Y-%m-%dT%H:%M:%SZ'), # nopep8
  215. }
  216. res = self.testapp.get(
  217. '/api/v2/users/1/workspaces/{}/contents/recently_active'.format(workspace.workspace_id), # nopep8
  218. status=200,
  219. params=params
  220. )
  221. res = res.json_body
  222. assert len(res) == 2
  223. # last updated content is newer than other one despite creation
  224. # of the other is more recent
  225. assert res[0]['content_id'] == firstly_created_but_recently_updated.content_id
  226. assert res[1]['content_id'] == secondly_created_but_not_updated.content_id
  227. class TestUserReadStatusEndpoint(FunctionalTest):
  228. """
  229. Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status # nopep8
  230. """
  231. def test_api__get_read_status__ok__200__all(self):
  232. # init DB
  233. dbsession = get_tm_session(self.session_factory, transaction.manager)
  234. admin = dbsession.query(models.User) \
  235. .filter(models.User.email == 'admin@admin.admin') \
  236. .one()
  237. workspace_api = WorkspaceApi(
  238. current_user=admin,
  239. session=dbsession,
  240. config=self.app_config
  241. )
  242. workspace = WorkspaceApi(
  243. current_user=admin,
  244. session=dbsession,
  245. config=self.app_config,
  246. ).create_workspace(
  247. 'test workspace',
  248. save_now=True
  249. )
  250. workspace2 = WorkspaceApi(
  251. current_user=admin,
  252. session=dbsession,
  253. config=self.app_config,
  254. ).create_workspace(
  255. 'test workspace2',
  256. save_now=True
  257. )
  258. api = ContentApi(
  259. current_user=admin,
  260. session=dbsession,
  261. config=self.app_config,
  262. )
  263. main_folder_workspace2 = api.create(ContentType.Folder, workspace2, None, 'Hepla', '', True) # nopep8
  264. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  265. # creation order test
  266. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  267. secondly_created = api.create(ContentType.Page, workspace, main_folder, 'another creation_order_test', '', True) # nopep8
  268. # update order test
  269. firstly_created_but_recently_updated = api.create(ContentType.Page, workspace, main_folder, 'update_order_test', '', True) # nopep8
  270. secondly_created_but_not_updated = api.create(ContentType.Page, workspace, main_folder, 'another update_order_test', '', True) # nopep8
  271. with new_revision(
  272. session=dbsession,
  273. tm=transaction.manager,
  274. content=firstly_created_but_recently_updated,
  275. ):
  276. firstly_created_but_recently_updated.description = 'Just an update'
  277. api.save(firstly_created_but_recently_updated)
  278. # comment change order
  279. firstly_created_but_recently_commented = api.create(ContentType.Page, workspace, main_folder, 'this is randomized label content', '', True) # nopep8
  280. secondly_created_but_not_commented = api.create(ContentType.Page, workspace, main_folder, 'this is another randomized label content', '', True) # nopep8
  281. comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True) # nopep8
  282. content_workspace_2 = api.create(ContentType.Page, workspace2,main_folder_workspace2, 'content_workspace_2', '',True) # nopep8
  283. dbsession.flush()
  284. transaction.commit()
  285. self.testapp.authorization = (
  286. 'Basic',
  287. (
  288. 'admin@admin.admin',
  289. 'admin@admin.admin'
  290. )
  291. )
  292. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  293. res = res.json_body
  294. assert len(res) == 7
  295. for elem in res:
  296. assert isinstance(elem['content_id'], int)
  297. assert isinstance(elem['read_by_user'], bool)
  298. # comment is newest than page2
  299. assert res[0]['content_id'] == firstly_created_but_recently_commented.content_id
  300. assert res[1]['content_id'] == secondly_created_but_not_commented.content_id
  301. # last updated content is newer than other one despite creation
  302. # of the other is more recent
  303. assert res[2]['content_id'] == firstly_created_but_recently_updated.content_id
  304. assert res[3]['content_id'] == secondly_created_but_not_updated.content_id
  305. # creation order is inverted here as last created is last active
  306. assert res[4]['content_id'] == secondly_created.content_id
  307. assert res[5]['content_id'] == firstly_created.content_id
  308. # folder subcontent modification does not change folder order
  309. assert res[6]['content_id'] == main_folder.content_id
  310. @pytest.mark.xfail(reason='List of item in path bug need to be fixed')
  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. content_id_str = '[{0},{1},{2},{3}]'.format(
  379. selected_contents_id[0],
  380. selected_contents_id[1],
  381. selected_contents_id[2],
  382. selected_contents_id[3],
  383. )
  384. params = {
  385. 'contents_ids': content_id_str
  386. }
  387. res = self.testapp.get(
  388. '/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), # nopep8
  389. status=200,
  390. params=params
  391. )
  392. res = res.json_body
  393. assert len(res) == 4
  394. for elem in res:
  395. assert isinstance(elem['content_id'], int)
  396. assert isinstance(elem['read_by_user'], bool)
  397. # comment is newest than page2
  398. assert res[0]['content_id'] == firstly_created_but_recently_commented.content_id
  399. assert res[1]['content_id'] == secondly_created_but_not_commented.content_id
  400. # last updated content is newer than other one despite creation
  401. # of the other is more recent
  402. assert res[2]['content_id'] == firstly_created_but_recently_updated.content_id
  403. assert res[3]['content_id'] == secondly_created_but_not_updated.content_id
  404. # creation order is inverted here as last created is last active
  405. assert res[4]['content_id'] == secondly_created.content_id
  406. assert res[5]['content_id'] == firstly_created.content_id
  407. # folder subcontent modification does not change folder order
  408. assert res[6]['content_id'] == main_folder.content_id
  409. class TestUserSetContentAsRead(FunctionalTest):
  410. """
  411. Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/read # nopep8
  412. """
  413. def test_api_set_content_as_read__ok__200__nominal_case(self):
  414. # init DB
  415. dbsession = get_tm_session(self.session_factory, transaction.manager)
  416. admin = dbsession.query(models.User) \
  417. .filter(models.User.email == 'admin@admin.admin') \
  418. .one()
  419. workspace_api = WorkspaceApi(
  420. current_user=admin,
  421. session=dbsession,
  422. config=self.app_config
  423. )
  424. workspace = WorkspaceApi(
  425. current_user=admin,
  426. session=dbsession,
  427. config=self.app_config,
  428. ).create_workspace(
  429. 'test workspace',
  430. save_now=True
  431. )
  432. api = ContentApi(
  433. current_user=admin,
  434. session=dbsession,
  435. config=self.app_config,
  436. )
  437. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  438. # creation order test
  439. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  440. api.mark_unread(firstly_created)
  441. dbsession.flush()
  442. transaction.commit()
  443. self.testapp.authorization = (
  444. 'Basic',
  445. (
  446. 'admin@admin.admin',
  447. 'admin@admin.admin'
  448. )
  449. )
  450. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  451. assert res.json_body[0]['content_id'] == firstly_created.content_id
  452. assert res.json_body[0]['read_by_user'] is False
  453. self.testapp.put(
  454. '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/read'.format( # nopep8
  455. workspace_id=workspace.workspace_id,
  456. content_id=firstly_created.content_id,
  457. user_id=admin.user_id,
  458. )
  459. )
  460. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  461. assert res.json_body[0]['content_id'] == firstly_created.content_id
  462. assert res.json_body[0]['read_by_user'] is True
  463. def test_api_set_content_as_read__ok__200__with_comments(self):
  464. # init DB
  465. dbsession = get_tm_session(self.session_factory, transaction.manager)
  466. admin = dbsession.query(models.User) \
  467. .filter(models.User.email == 'admin@admin.admin') \
  468. .one()
  469. workspace_api = WorkspaceApi(
  470. current_user=admin,
  471. session=dbsession,
  472. config=self.app_config
  473. )
  474. workspace = WorkspaceApi(
  475. current_user=admin,
  476. session=dbsession,
  477. config=self.app_config,
  478. ).create_workspace(
  479. 'test workspace',
  480. save_now=True
  481. )
  482. api = ContentApi(
  483. current_user=admin,
  484. session=dbsession,
  485. config=self.app_config,
  486. )
  487. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  488. # creation order test
  489. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  490. comments = api.create_comment(workspace, firstly_created, 'juste a super comment', True) # nopep8
  491. api.mark_unread(firstly_created)
  492. api.mark_unread(comments)
  493. dbsession.flush()
  494. transaction.commit()
  495. self.testapp.authorization = (
  496. 'Basic',
  497. (
  498. 'admin@admin.admin',
  499. 'admin@admin.admin'
  500. )
  501. )
  502. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  503. assert res.json_body[0]['content_id'] == firstly_created.content_id
  504. assert res.json_body[0]['read_by_user'] is False
  505. self.testapp.put(
  506. '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/read'.format( # nopep8
  507. workspace_id=workspace.workspace_id,
  508. content_id=firstly_created.content_id,
  509. user_id=admin.user_id,
  510. )
  511. )
  512. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  513. assert res.json_body[0]['content_id'] == firstly_created.content_id
  514. assert res.json_body[0]['read_by_user'] is True
  515. # comment is also set as read
  516. assert comments.has_new_information_for(admin) is False
  517. class TestUserSetContentAsUnread(FunctionalTest):
  518. """
  519. Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/unread # nopep8
  520. """
  521. def test_api_set_content_as_unread__ok__200__nominal_case(self):
  522. # init DB
  523. dbsession = get_tm_session(self.session_factory, transaction.manager)
  524. admin = dbsession.query(models.User) \
  525. .filter(models.User.email == 'admin@admin.admin') \
  526. .one()
  527. workspace_api = WorkspaceApi(
  528. current_user=admin,
  529. session=dbsession,
  530. config=self.app_config
  531. )
  532. workspace = WorkspaceApi(
  533. current_user=admin,
  534. session=dbsession,
  535. config=self.app_config,
  536. ).create_workspace(
  537. 'test workspace',
  538. save_now=True
  539. )
  540. api = ContentApi(
  541. current_user=admin,
  542. session=dbsession,
  543. config=self.app_config,
  544. )
  545. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  546. # creation order test
  547. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  548. api.mark_read(firstly_created)
  549. dbsession.flush()
  550. transaction.commit()
  551. self.testapp.authorization = (
  552. 'Basic',
  553. (
  554. 'admin@admin.admin',
  555. 'admin@admin.admin'
  556. )
  557. )
  558. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  559. assert res.json_body[0]['content_id'] == firstly_created.content_id
  560. assert res.json_body[0]['read_by_user'] is True
  561. self.testapp.put(
  562. '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/unread'.format( # nopep8
  563. workspace_id=workspace.workspace_id,
  564. content_id=firstly_created.content_id,
  565. user_id=admin.user_id,
  566. )
  567. )
  568. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  569. assert res.json_body[0]['content_id'] == firstly_created.content_id
  570. assert res.json_body[0]['read_by_user'] is False
  571. def test_api_set_content_as_unread__ok__200__with_comments(self):
  572. # init DB
  573. dbsession = get_tm_session(self.session_factory, transaction.manager)
  574. admin = dbsession.query(models.User) \
  575. .filter(models.User.email == 'admin@admin.admin') \
  576. .one()
  577. workspace_api = WorkspaceApi(
  578. current_user=admin,
  579. session=dbsession,
  580. config=self.app_config
  581. )
  582. workspace = WorkspaceApi(
  583. current_user=admin,
  584. session=dbsession,
  585. config=self.app_config,
  586. ).create_workspace(
  587. 'test workspace',
  588. save_now=True
  589. )
  590. api = ContentApi(
  591. current_user=admin,
  592. session=dbsession,
  593. config=self.app_config,
  594. )
  595. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  596. # creation order test
  597. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  598. comments = api.create_comment(workspace, firstly_created, 'juste a super comment', True) # nopep8
  599. api.mark_read(firstly_created)
  600. api.mark_read(comments)
  601. dbsession.flush()
  602. transaction.commit()
  603. self.testapp.authorization = (
  604. 'Basic',
  605. (
  606. 'admin@admin.admin',
  607. 'admin@admin.admin'
  608. )
  609. )
  610. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  611. assert res.json_body[0]['content_id'] == firstly_created.content_id
  612. assert res.json_body[0]['read_by_user'] is True
  613. self.testapp.put(
  614. '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/unread'.format( # nopep8
  615. workspace_id=workspace.workspace_id,
  616. content_id=firstly_created.content_id,
  617. user_id=admin.user_id,
  618. )
  619. )
  620. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  621. assert res.json_body[0]['content_id'] == firstly_created.content_id
  622. assert res.json_body[0]['read_by_user'] is False
  623. assert comments.has_new_information_for(admin) is True
  624. class TestUserSetWorkspaceAsRead(FunctionalTest):
  625. """
  626. Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/read
  627. """
  628. def test_api_set_content_as_read__ok__200__nominal_case(self):
  629. # init DB
  630. dbsession = get_tm_session(self.session_factory, transaction.manager)
  631. admin = dbsession.query(models.User) \
  632. .filter(models.User.email == 'admin@admin.admin') \
  633. .one()
  634. workspace_api = WorkspaceApi(
  635. current_user=admin,
  636. session=dbsession,
  637. config=self.app_config
  638. )
  639. workspace = WorkspaceApi(
  640. current_user=admin,
  641. session=dbsession,
  642. config=self.app_config,
  643. ).create_workspace(
  644. 'test workspace',
  645. save_now=True
  646. )
  647. api = ContentApi(
  648. current_user=admin,
  649. session=dbsession,
  650. config=self.app_config,
  651. )
  652. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  653. # creation order test
  654. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  655. api.mark_unread(main_folder)
  656. api.mark_unread(firstly_created)
  657. dbsession.flush()
  658. transaction.commit()
  659. self.testapp.authorization = (
  660. 'Basic',
  661. (
  662. 'admin@admin.admin',
  663. 'admin@admin.admin'
  664. )
  665. )
  666. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  667. assert res.json_body[0]['content_id'] == firstly_created.content_id
  668. assert res.json_body[0]['read_by_user'] is False
  669. assert res.json_body[1]['content_id'] == main_folder.content_id
  670. assert res.json_body[1]['read_by_user'] is False
  671. self.testapp.put(
  672. '/api/v2/users/{user_id}/workspaces/{workspace_id}/read'.format( # nopep8
  673. workspace_id=workspace.workspace_id,
  674. content_id=firstly_created.content_id,
  675. user_id=admin.user_id,
  676. )
  677. )
  678. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  679. assert res.json_body[0]['content_id'] == firstly_created.content_id
  680. assert res.json_body[0]['read_by_user'] is True
  681. assert res.json_body[1]['content_id'] == main_folder.content_id
  682. assert res.json_body[1]['read_by_user'] is True
  683. class TestUserWorkspaceEndpoint(FunctionalTest):
  684. """
  685. Tests for /api/v2/users/{user_id}/workspaces
  686. """
  687. fixtures = [BaseFixture, ContentFixtures]
  688. def test_api__get_user_workspaces__ok_200__nominal_case(self):
  689. """
  690. Check obtain all workspaces reachables for user with user auth.
  691. """
  692. self.testapp.authorization = (
  693. 'Basic',
  694. (
  695. 'admin@admin.admin',
  696. 'admin@admin.admin'
  697. )
  698. )
  699. res = self.testapp.get('/api/v2/users/1/workspaces', status=200)
  700. res = res.json_body
  701. workspace = res[0]
  702. assert workspace['workspace_id'] == 1
  703. assert workspace['label'] == 'Business'
  704. assert workspace['slug'] == 'business'
  705. assert len(workspace['sidebar_entries']) == 7
  706. sidebar_entry = workspace['sidebar_entries'][0]
  707. assert sidebar_entry['slug'] == 'dashboard'
  708. assert sidebar_entry['label'] == 'Dashboard'
  709. assert sidebar_entry['route'] == '/#/workspaces/1/dashboard' # nopep8
  710. assert sidebar_entry['hexcolor'] == "#252525"
  711. assert sidebar_entry['fa_icon'] == "signal"
  712. sidebar_entry = workspace['sidebar_entries'][1]
  713. assert sidebar_entry['slug'] == 'contents/all'
  714. assert sidebar_entry['label'] == 'All Contents'
  715. assert sidebar_entry['route'] == "/#/workspaces/1/contents" # nopep8
  716. assert sidebar_entry['hexcolor'] == "#fdfdfd"
  717. assert sidebar_entry['fa_icon'] == "th"
  718. sidebar_entry = workspace['sidebar_entries'][2]
  719. assert sidebar_entry['slug'] == 'contents/html-documents'
  720. assert sidebar_entry['label'] == 'Text Documents'
  721. assert sidebar_entry['route'] == '/#/workspaces/1/contents?type=html-documents' # nopep8
  722. assert sidebar_entry['hexcolor'] == "#3f52e3"
  723. assert sidebar_entry['fa_icon'] == "file-text-o"
  724. sidebar_entry = workspace['sidebar_entries'][3]
  725. assert sidebar_entry['slug'] == 'contents/markdownpluspage'
  726. assert sidebar_entry['label'] == 'Markdown Plus Documents'
  727. assert sidebar_entry['route'] == "/#/workspaces/1/contents?type=markdownpluspage" # nopep8
  728. assert sidebar_entry['hexcolor'] == "#f12d2d"
  729. assert sidebar_entry['fa_icon'] == "file-code-o"
  730. sidebar_entry = workspace['sidebar_entries'][4]
  731. assert sidebar_entry['slug'] == 'contents/files'
  732. assert sidebar_entry['label'] == 'Files'
  733. assert sidebar_entry['route'] == "/#/workspaces/1/contents?type=file" # nopep8
  734. assert sidebar_entry['hexcolor'] == "#FF9900"
  735. assert sidebar_entry['fa_icon'] == "paperclip"
  736. sidebar_entry = workspace['sidebar_entries'][5]
  737. assert sidebar_entry['slug'] == 'contents/threads'
  738. assert sidebar_entry['label'] == 'Threads'
  739. assert sidebar_entry['route'] == "/#/workspaces/1/contents?type=thread" # nopep8
  740. assert sidebar_entry['hexcolor'] == "#ad4cf9"
  741. assert sidebar_entry['fa_icon'] == "comments-o"
  742. sidebar_entry = workspace['sidebar_entries'][6]
  743. assert sidebar_entry['slug'] == 'calendar'
  744. assert sidebar_entry['label'] == 'Calendar'
  745. assert sidebar_entry['route'] == "/#/workspaces/1/calendar" # nopep8
  746. assert sidebar_entry['hexcolor'] == "#757575"
  747. assert sidebar_entry['fa_icon'] == "calendar"
  748. def test_api__get_user_workspaces__err_403__unallowed_user(self):
  749. """
  750. Check obtain all workspaces reachables for one user
  751. with another non-admin user auth.
  752. """
  753. self.testapp.authorization = (
  754. 'Basic',
  755. (
  756. 'lawrence-not-real-email@fsf.local',
  757. 'foobarbaz'
  758. )
  759. )
  760. res = self.testapp.get('/api/v2/users/1/workspaces', status=403)
  761. assert isinstance(res.json, dict)
  762. assert 'code' in res.json.keys()
  763. assert 'message' in res.json.keys()
  764. assert 'details' in res.json.keys()
  765. def test_api__get_user_workspaces__err_401__unregistered_user(self):
  766. """
  767. Check obtain all workspaces reachables for one user
  768. without correct user auth (user unregistered).
  769. """
  770. self.testapp.authorization = (
  771. 'Basic',
  772. (
  773. 'john@doe.doe',
  774. 'lapin'
  775. )
  776. )
  777. res = self.testapp.get('/api/v2/users/1/workspaces', status=401)
  778. assert isinstance(res.json, dict)
  779. assert 'code' in res.json.keys()
  780. assert 'message' in res.json.keys()
  781. assert 'details' in res.json.keys()
  782. def test_api__get_user_workspaces__err_400__user_does_not_exist(self):
  783. """
  784. Check obtain all workspaces reachables for one user who does
  785. not exist
  786. with a correct user auth.
  787. """
  788. self.testapp.authorization = (
  789. 'Basic',
  790. (
  791. 'admin@admin.admin',
  792. 'admin@admin.admin'
  793. )
  794. )
  795. res = self.testapp.get('/api/v2/users/5/workspaces', status=400)
  796. assert isinstance(res.json, dict)
  797. assert 'code' in res.json.keys()
  798. assert 'message' in res.json.keys()
  799. assert 'details' in res.json.keys()