test_user.py 37KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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. @pytest.mark.xfail(reason='List of item in path bug need to be fixed')
  312. def test_api__get_read_status__ok__200__nominal_case(self):
  313. # init DB
  314. dbsession = get_tm_session(self.session_factory, transaction.manager)
  315. admin = dbsession.query(models.User) \
  316. .filter(models.User.email == 'admin@admin.admin') \
  317. .one()
  318. workspace_api = WorkspaceApi(
  319. current_user=admin,
  320. session=dbsession,
  321. config=self.app_config
  322. )
  323. workspace = WorkspaceApi(
  324. current_user=admin,
  325. session=dbsession,
  326. config=self.app_config,
  327. ).create_workspace(
  328. 'test workspace',
  329. save_now=True
  330. )
  331. workspace2 = WorkspaceApi(
  332. current_user=admin,
  333. session=dbsession,
  334. config=self.app_config,
  335. ).create_workspace(
  336. 'test workspace2',
  337. save_now=True
  338. )
  339. api = ContentApi(
  340. current_user=admin,
  341. session=dbsession,
  342. config=self.app_config,
  343. )
  344. main_folder_workspace2 = api.create(ContentType.Folder, workspace2, None, 'Hepla', '', True) # nopep8
  345. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  346. # creation order test
  347. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  348. secondly_created = api.create(ContentType.Page, workspace, main_folder, 'another creation_order_test', '', True) # nopep8
  349. # update order test
  350. firstly_created_but_recently_updated = api.create(ContentType.Page, workspace, main_folder, 'update_order_test', '', True) # nopep8
  351. secondly_created_but_not_updated = api.create(ContentType.Page, workspace, main_folder, 'another update_order_test', '', True) # nopep8
  352. with new_revision(
  353. session=dbsession,
  354. tm=transaction.manager,
  355. content=firstly_created_but_recently_updated,
  356. ):
  357. firstly_created_but_recently_updated.description = 'Just an update'
  358. api.save(firstly_created_but_recently_updated)
  359. # comment change order
  360. firstly_created_but_recently_commented = api.create(ContentType.Page, workspace, main_folder, 'this is randomized label content', '', True) # nopep8
  361. secondly_created_but_not_commented = api.create(ContentType.Page, workspace, main_folder, 'this is another randomized label content', '', True) # nopep8
  362. comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True) # nopep8
  363. content_workspace_2 = api.create(ContentType.Page, workspace2,main_folder_workspace2, 'content_workspace_2', '',True) # nopep8
  364. dbsession.flush()
  365. transaction.commit()
  366. self.testapp.authorization = (
  367. 'Basic',
  368. (
  369. 'admin@admin.admin',
  370. 'admin@admin.admin'
  371. )
  372. )
  373. selected_contents_id = [
  374. firstly_created_but_recently_commented.content_id,
  375. firstly_created_but_recently_updated.content_id,
  376. firstly_created.content_id,
  377. main_folder.content_id,
  378. ]
  379. content_id_str = '[{0},{1},{2},{3}]'.format(
  380. selected_contents_id[0],
  381. selected_contents_id[1],
  382. selected_contents_id[2],
  383. selected_contents_id[3],
  384. )
  385. params = {
  386. 'contents_ids': content_id_str
  387. }
  388. res = self.testapp.get(
  389. '/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), # nopep8
  390. status=200,
  391. params=params
  392. )
  393. res = res.json_body
  394. assert len(res) == 4
  395. for elem in res:
  396. assert isinstance(elem['content_id'], int)
  397. assert isinstance(elem['read_by_user'], bool)
  398. # comment is newest than page2
  399. assert res[0]['content_id'] == firstly_created_but_recently_commented.content_id
  400. assert res[1]['content_id'] == secondly_created_but_not_commented.content_id
  401. # last updated content is newer than other one despite creation
  402. # of the other is more recent
  403. assert res[2]['content_id'] == firstly_created_but_recently_updated.content_id
  404. assert res[3]['content_id'] == secondly_created_but_not_updated.content_id
  405. # creation order is inverted here as last created is last active
  406. assert res[4]['content_id'] == secondly_created.content_id
  407. assert res[5]['content_id'] == firstly_created.content_id
  408. # folder subcontent modification does not change folder order
  409. assert res[6]['content_id'] == main_folder.content_id
  410. class TestUserSetContentAsRead(FunctionalTest):
  411. """
  412. Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/read # nopep8
  413. """
  414. def test_api_set_content_as_read__ok__200__nominal_case(self):
  415. # init DB
  416. dbsession = get_tm_session(self.session_factory, transaction.manager)
  417. admin = dbsession.query(models.User) \
  418. .filter(models.User.email == 'admin@admin.admin') \
  419. .one()
  420. workspace_api = WorkspaceApi(
  421. current_user=admin,
  422. session=dbsession,
  423. config=self.app_config
  424. )
  425. workspace = WorkspaceApi(
  426. current_user=admin,
  427. session=dbsession,
  428. config=self.app_config,
  429. ).create_workspace(
  430. 'test workspace',
  431. save_now=True
  432. )
  433. api = ContentApi(
  434. current_user=admin,
  435. session=dbsession,
  436. config=self.app_config,
  437. )
  438. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  439. # creation order test
  440. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  441. api.mark_unread(firstly_created)
  442. dbsession.flush()
  443. transaction.commit()
  444. self.testapp.authorization = (
  445. 'Basic',
  446. (
  447. 'admin@admin.admin',
  448. 'admin@admin.admin'
  449. )
  450. )
  451. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  452. assert res.json_body[0]['content_id'] == firstly_created.content_id
  453. assert res.json_body[0]['read_by_user'] is False
  454. self.testapp.put(
  455. '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/read'.format( # nopep8
  456. workspace_id=workspace.workspace_id,
  457. content_id=firstly_created.content_id,
  458. user_id=admin.user_id,
  459. )
  460. )
  461. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  462. assert res.json_body[0]['content_id'] == firstly_created.content_id
  463. assert res.json_body[0]['read_by_user'] is True
  464. def test_api_set_content_as_read__ok__200__with_comments(self):
  465. # init DB
  466. dbsession = get_tm_session(self.session_factory, transaction.manager)
  467. admin = dbsession.query(models.User) \
  468. .filter(models.User.email == 'admin@admin.admin') \
  469. .one()
  470. workspace_api = WorkspaceApi(
  471. current_user=admin,
  472. session=dbsession,
  473. config=self.app_config
  474. )
  475. workspace = WorkspaceApi(
  476. current_user=admin,
  477. session=dbsession,
  478. config=self.app_config,
  479. ).create_workspace(
  480. 'test workspace',
  481. save_now=True
  482. )
  483. api = ContentApi(
  484. current_user=admin,
  485. session=dbsession,
  486. config=self.app_config,
  487. )
  488. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  489. # creation order test
  490. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  491. comments = api.create_comment(workspace, firstly_created, 'juste a super comment', True) # nopep8
  492. api.mark_unread(firstly_created)
  493. api.mark_unread(comments)
  494. dbsession.flush()
  495. transaction.commit()
  496. self.testapp.authorization = (
  497. 'Basic',
  498. (
  499. 'admin@admin.admin',
  500. 'admin@admin.admin'
  501. )
  502. )
  503. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  504. assert res.json_body[0]['content_id'] == firstly_created.content_id
  505. assert res.json_body[0]['read_by_user'] is False
  506. self.testapp.put(
  507. '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/read'.format( # nopep8
  508. workspace_id=workspace.workspace_id,
  509. content_id=firstly_created.content_id,
  510. user_id=admin.user_id,
  511. )
  512. )
  513. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  514. assert res.json_body[0]['content_id'] == firstly_created.content_id
  515. assert res.json_body[0]['read_by_user'] is True
  516. # comment is also set as read
  517. assert comments.has_new_information_for(admin) is False
  518. class TestUserSetContentAsUnread(FunctionalTest):
  519. """
  520. Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/unread # nopep8
  521. """
  522. def test_api_set_content_as_unread__ok__200__nominal_case(self):
  523. # init DB
  524. dbsession = get_tm_session(self.session_factory, transaction.manager)
  525. admin = dbsession.query(models.User) \
  526. .filter(models.User.email == 'admin@admin.admin') \
  527. .one()
  528. workspace_api = WorkspaceApi(
  529. current_user=admin,
  530. session=dbsession,
  531. config=self.app_config
  532. )
  533. workspace = WorkspaceApi(
  534. current_user=admin,
  535. session=dbsession,
  536. config=self.app_config,
  537. ).create_workspace(
  538. 'test workspace',
  539. save_now=True
  540. )
  541. api = ContentApi(
  542. current_user=admin,
  543. session=dbsession,
  544. config=self.app_config,
  545. )
  546. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  547. # creation order test
  548. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  549. api.mark_read(firstly_created)
  550. dbsession.flush()
  551. transaction.commit()
  552. self.testapp.authorization = (
  553. 'Basic',
  554. (
  555. 'admin@admin.admin',
  556. 'admin@admin.admin'
  557. )
  558. )
  559. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  560. assert res.json_body[0]['content_id'] == firstly_created.content_id
  561. assert res.json_body[0]['read_by_user'] is True
  562. self.testapp.put(
  563. '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/unread'.format( # nopep8
  564. workspace_id=workspace.workspace_id,
  565. content_id=firstly_created.content_id,
  566. user_id=admin.user_id,
  567. )
  568. )
  569. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  570. assert res.json_body[0]['content_id'] == firstly_created.content_id
  571. assert res.json_body[0]['read_by_user'] is False
  572. def test_api_set_content_as_unread__ok__200__with_comments(self):
  573. # init DB
  574. dbsession = get_tm_session(self.session_factory, transaction.manager)
  575. admin = dbsession.query(models.User) \
  576. .filter(models.User.email == 'admin@admin.admin') \
  577. .one()
  578. workspace_api = WorkspaceApi(
  579. current_user=admin,
  580. session=dbsession,
  581. config=self.app_config
  582. )
  583. workspace = WorkspaceApi(
  584. current_user=admin,
  585. session=dbsession,
  586. config=self.app_config,
  587. ).create_workspace(
  588. 'test workspace',
  589. save_now=True
  590. )
  591. api = ContentApi(
  592. current_user=admin,
  593. session=dbsession,
  594. config=self.app_config,
  595. )
  596. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  597. # creation order test
  598. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  599. comments = api.create_comment(workspace, firstly_created, 'juste a super comment', True) # nopep8
  600. api.mark_read(firstly_created)
  601. api.mark_read(comments)
  602. dbsession.flush()
  603. transaction.commit()
  604. self.testapp.authorization = (
  605. 'Basic',
  606. (
  607. 'admin@admin.admin',
  608. 'admin@admin.admin'
  609. )
  610. )
  611. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  612. assert res.json_body[0]['content_id'] == firstly_created.content_id
  613. assert res.json_body[0]['read_by_user'] is True
  614. self.testapp.put(
  615. '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/unread'.format( # nopep8
  616. workspace_id=workspace.workspace_id,
  617. content_id=firstly_created.content_id,
  618. user_id=admin.user_id,
  619. )
  620. )
  621. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  622. assert res.json_body[0]['content_id'] == firstly_created.content_id
  623. assert res.json_body[0]['read_by_user'] is False
  624. assert comments.has_new_information_for(admin) is True
  625. class TestUserSetWorkspaceAsRead(FunctionalTest):
  626. """
  627. Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/read
  628. """
  629. def test_api_set_content_as_read__ok__200__nominal_case(self):
  630. # init DB
  631. dbsession = get_tm_session(self.session_factory, transaction.manager)
  632. admin = dbsession.query(models.User) \
  633. .filter(models.User.email == 'admin@admin.admin') \
  634. .one()
  635. workspace_api = WorkspaceApi(
  636. current_user=admin,
  637. session=dbsession,
  638. config=self.app_config
  639. )
  640. workspace = WorkspaceApi(
  641. current_user=admin,
  642. session=dbsession,
  643. config=self.app_config,
  644. ).create_workspace(
  645. 'test workspace',
  646. save_now=True
  647. )
  648. api = ContentApi(
  649. current_user=admin,
  650. session=dbsession,
  651. config=self.app_config,
  652. )
  653. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  654. # creation order test
  655. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  656. api.mark_unread(main_folder)
  657. api.mark_unread(firstly_created)
  658. dbsession.flush()
  659. transaction.commit()
  660. self.testapp.authorization = (
  661. 'Basic',
  662. (
  663. 'admin@admin.admin',
  664. 'admin@admin.admin'
  665. )
  666. )
  667. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  668. assert res.json_body[0]['content_id'] == firstly_created.content_id
  669. assert res.json_body[0]['read_by_user'] is False
  670. assert res.json_body[1]['content_id'] == main_folder.content_id
  671. assert res.json_body[1]['read_by_user'] is False
  672. self.testapp.put(
  673. '/api/v2/users/{user_id}/workspaces/{workspace_id}/read'.format( # nopep8
  674. workspace_id=workspace.workspace_id,
  675. content_id=firstly_created.content_id,
  676. user_id=admin.user_id,
  677. )
  678. )
  679. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  680. assert res.json_body[0]['content_id'] == firstly_created.content_id
  681. assert res.json_body[0]['read_by_user'] is True
  682. assert res.json_body[1]['content_id'] == main_folder.content_id
  683. assert res.json_body[1]['read_by_user'] is True
  684. class TestUserWorkspaceEndpoint(FunctionalTest):
  685. """
  686. Tests for /api/v2/users/{user_id}/workspaces
  687. """
  688. fixtures = [BaseFixture, ContentFixtures]
  689. def test_api__get_user_workspaces__ok_200__nominal_case(self):
  690. """
  691. Check obtain all workspaces reachables for user with user auth.
  692. """
  693. self.testapp.authorization = (
  694. 'Basic',
  695. (
  696. 'admin@admin.admin',
  697. 'admin@admin.admin'
  698. )
  699. )
  700. res = self.testapp.get('/api/v2/users/1/workspaces', status=200)
  701. res = res.json_body
  702. workspace = res[0]
  703. assert workspace['workspace_id'] == 1
  704. assert workspace['label'] == 'Business'
  705. assert workspace['slug'] == 'business'
  706. assert len(workspace['sidebar_entries']) == 7
  707. sidebar_entry = workspace['sidebar_entries'][0]
  708. assert sidebar_entry['slug'] == 'dashboard'
  709. assert sidebar_entry['label'] == 'Dashboard'
  710. assert sidebar_entry['route'] == '/#/workspaces/1/dashboard' # nopep8
  711. assert sidebar_entry['hexcolor'] == "#252525"
  712. assert sidebar_entry['fa_icon'] == "signal"
  713. sidebar_entry = workspace['sidebar_entries'][1]
  714. assert sidebar_entry['slug'] == 'contents/all'
  715. assert sidebar_entry['label'] == 'All Contents'
  716. assert sidebar_entry['route'] == "/#/workspaces/1/contents" # nopep8
  717. assert sidebar_entry['hexcolor'] == "#fdfdfd"
  718. assert sidebar_entry['fa_icon'] == "th"
  719. sidebar_entry = workspace['sidebar_entries'][2]
  720. assert sidebar_entry['slug'] == 'contents/html-documents'
  721. assert sidebar_entry['label'] == 'Text Documents'
  722. assert sidebar_entry['route'] == '/#/workspaces/1/contents?type=html-documents' # nopep8
  723. assert sidebar_entry['hexcolor'] == "#3f52e3"
  724. assert sidebar_entry['fa_icon'] == "file-text-o"
  725. sidebar_entry = workspace['sidebar_entries'][3]
  726. assert sidebar_entry['slug'] == 'contents/markdownpluspage'
  727. assert sidebar_entry['label'] == 'Markdown Plus Documents'
  728. assert sidebar_entry['route'] == "/#/workspaces/1/contents?type=markdownpluspage" # nopep8
  729. assert sidebar_entry['hexcolor'] == "#f12d2d"
  730. assert sidebar_entry['fa_icon'] == "file-code-o"
  731. sidebar_entry = workspace['sidebar_entries'][4]
  732. assert sidebar_entry['slug'] == 'contents/files'
  733. assert sidebar_entry['label'] == 'Files'
  734. assert sidebar_entry['route'] == "/#/workspaces/1/contents?type=file" # nopep8
  735. assert sidebar_entry['hexcolor'] == "#FF9900"
  736. assert sidebar_entry['fa_icon'] == "paperclip"
  737. sidebar_entry = workspace['sidebar_entries'][5]
  738. assert sidebar_entry['slug'] == 'contents/threads'
  739. assert sidebar_entry['label'] == 'Threads'
  740. assert sidebar_entry['route'] == "/#/workspaces/1/contents?type=thread" # nopep8
  741. assert sidebar_entry['hexcolor'] == "#ad4cf9"
  742. assert sidebar_entry['fa_icon'] == "comments-o"
  743. sidebar_entry = workspace['sidebar_entries'][6]
  744. assert sidebar_entry['slug'] == 'calendar'
  745. assert sidebar_entry['label'] == 'Calendar'
  746. assert sidebar_entry['route'] == "/#/workspaces/1/calendar" # nopep8
  747. assert sidebar_entry['hexcolor'] == "#757575"
  748. assert sidebar_entry['fa_icon'] == "calendar"
  749. def test_api__get_user_workspaces__err_403__unallowed_user(self):
  750. """
  751. Check obtain all workspaces reachables for one user
  752. with another non-admin user auth.
  753. """
  754. self.testapp.authorization = (
  755. 'Basic',
  756. (
  757. 'lawrence-not-real-email@fsf.local',
  758. 'foobarbaz'
  759. )
  760. )
  761. res = self.testapp.get('/api/v2/users/1/workspaces', status=403)
  762. assert isinstance(res.json, dict)
  763. assert 'code' in res.json.keys()
  764. assert 'message' in res.json.keys()
  765. assert 'details' in res.json.keys()
  766. def test_api__get_user_workspaces__err_401__unregistered_user(self):
  767. """
  768. Check obtain all workspaces reachables for one user
  769. without correct user auth (user unregistered).
  770. """
  771. self.testapp.authorization = (
  772. 'Basic',
  773. (
  774. 'john@doe.doe',
  775. 'lapin'
  776. )
  777. )
  778. res = self.testapp.get('/api/v2/users/1/workspaces', status=401)
  779. assert isinstance(res.json, dict)
  780. assert 'code' in res.json.keys()
  781. assert 'message' in res.json.keys()
  782. assert 'details' in res.json.keys()
  783. def test_api__get_user_workspaces__err_400__user_does_not_exist(self):
  784. """
  785. Check obtain all workspaces reachables for one user who does
  786. not exist
  787. with a correct user auth.
  788. """
  789. self.testapp.authorization = (
  790. 'Basic',
  791. (
  792. 'admin@admin.admin',
  793. 'admin@admin.admin'
  794. )
  795. )
  796. res = self.testapp.get('/api/v2/users/5/workspaces', status=400)
  797. assert isinstance(res.json, dict)
  798. assert 'code' in res.json.keys()
  799. assert 'message' in res.json.keys()
  800. assert 'details' in res.json.keys()