test_contents.py 44KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  1. # -*- coding: utf-8 -*-
  2. import transaction
  3. from depot.io.utils import FileIntent
  4. from tracim import models
  5. from tracim.lib.core.content import ContentApi
  6. from tracim.lib.core.workspace import WorkspaceApi
  7. from tracim.models.data import ContentType
  8. from tracim.models import get_tm_session
  9. from tracim.tests import FunctionalTest
  10. from tracim.tests import set_html_document_slug_to_legacy
  11. from tracim.fixtures.content import Content as ContentFixtures
  12. from tracim.fixtures.users_and_groups import Base as BaseFixture
  13. class TestHtmlDocuments(FunctionalTest):
  14. """
  15. Tests for /api/v2/workspaces/{workspace_id}/html-documents/{content_id}
  16. endpoint
  17. """
  18. fixtures = [BaseFixture, ContentFixtures]
  19. def test_api__get_html_document__ok_200__legacy_slug(self) -> None:
  20. """
  21. Get one html document of a content
  22. """
  23. self.testapp.authorization = (
  24. 'Basic',
  25. (
  26. 'admin@admin.admin',
  27. 'admin@admin.admin'
  28. )
  29. )
  30. set_html_document_slug_to_legacy(self.session_factory)
  31. res = self.testapp.get(
  32. '/api/v2/workspaces/2/html-documents/6',
  33. status=200
  34. )
  35. content = res.json_body
  36. assert content['content_type'] == 'html-documents'
  37. assert content['content_id'] == 6
  38. assert content['is_archived'] is False
  39. assert content['is_deleted'] is False
  40. assert content['label'] == 'Tiramisu Recipe'
  41. assert content['parent_id'] == 3
  42. assert content['show_in_ui'] is True
  43. assert content['slug'] == 'tiramisu-recipe'
  44. assert content['status'] == 'open'
  45. assert content['workspace_id'] == 2
  46. assert content['current_revision_id'] == 27
  47. # TODO - G.M - 2018-06-173 - check date format
  48. assert content['created']
  49. assert content['author']
  50. assert content['author']['user_id'] == 1
  51. assert content['author']['avatar_url'] is None
  52. assert content['author']['public_name'] == 'Global manager'
  53. # TODO - G.M - 2018-06-173 - check date format
  54. assert content['modified']
  55. assert content['last_modifier'] != content['author']
  56. assert content['last_modifier']['user_id'] == 3
  57. assert content['last_modifier']['public_name'] == 'Bob i.'
  58. assert content['last_modifier']['avatar_url'] is None
  59. assert content['raw_content'] == '<p>To cook a great Tiramisu, you need many ingredients.</p>' # nopep8
  60. def test_api__get_html_document__ok_200__nominal_case(self) -> None:
  61. """
  62. Get one html document of a content
  63. """
  64. self.testapp.authorization = (
  65. 'Basic',
  66. (
  67. 'admin@admin.admin',
  68. 'admin@admin.admin'
  69. )
  70. )
  71. res = self.testapp.get(
  72. '/api/v2/workspaces/2/html-documents/6',
  73. status=200
  74. )
  75. content = res.json_body
  76. assert content['content_type'] == 'html-documents'
  77. assert content['content_id'] == 6
  78. assert content['is_archived'] is False
  79. assert content['is_deleted'] is False
  80. assert content['label'] == 'Tiramisu Recipe'
  81. assert content['parent_id'] == 3
  82. assert content['show_in_ui'] is True
  83. assert content['slug'] == 'tiramisu-recipe'
  84. assert content['status'] == 'open'
  85. assert content['workspace_id'] == 2
  86. assert content['current_revision_id'] == 27
  87. # TODO - G.M - 2018-06-173 - check date format
  88. assert content['created']
  89. assert content['author']
  90. assert content['author']['user_id'] == 1
  91. assert content['author']['avatar_url'] is None
  92. assert content['author']['public_name'] == 'Global manager'
  93. # TODO - G.M - 2018-06-173 - check date format
  94. assert content['modified']
  95. assert content['last_modifier'] != content['author']
  96. assert content['last_modifier']['user_id'] == 3
  97. assert content['last_modifier']['public_name'] == 'Bob i.'
  98. assert content['last_modifier']['avatar_url'] is None
  99. assert content['raw_content'] == '<p>To cook a great Tiramisu, you need many ingredients.</p>' # nopep8
  100. def test_api__get_html_document__err_400__wrong_content_type(self) -> None:
  101. """
  102. Get one html document of a content content 7 is not html_document
  103. """
  104. self.testapp.authorization = (
  105. 'Basic',
  106. (
  107. 'admin@admin.admin',
  108. 'admin@admin.admin'
  109. )
  110. )
  111. res = self.testapp.get(
  112. '/api/v2/workspaces/2/html-documents/7',
  113. status=400
  114. )
  115. def test_api__get_html_document__err_400__content_does_not_exist(self) -> None: # nopep8
  116. """
  117. Get one html document of a content (content 170 does not exist in db
  118. """
  119. self.testapp.authorization = (
  120. 'Basic',
  121. (
  122. 'admin@admin.admin',
  123. 'admin@admin.admin'
  124. )
  125. )
  126. res = self.testapp.get(
  127. '/api/v2/workspaces/2/html-documents/170',
  128. status=400
  129. )
  130. def test_api__get_html_document__err_400__content_not_in_workspace(self) -> None: # nopep8
  131. """
  132. Get one html document of a content (content 6 is in workspace 2)
  133. """
  134. self.testapp.authorization = (
  135. 'Basic',
  136. (
  137. 'admin@admin.admin',
  138. 'admin@admin.admin'
  139. )
  140. )
  141. res = self.testapp.get(
  142. '/api/v2/workspaces/1/html-documents/6',
  143. status=400
  144. )
  145. def test_api__get_html_document__err_400__workspace_does_not_exist(self) -> None: # nopep8
  146. """
  147. Get one html document of a content (Workspace 40 does not exist)
  148. """
  149. self.testapp.authorization = (
  150. 'Basic',
  151. (
  152. 'admin@admin.admin',
  153. 'admin@admin.admin'
  154. )
  155. )
  156. res = self.testapp.get(
  157. '/api/v2/workspaces/40/html-documents/6',
  158. status=400
  159. )
  160. def test_api__get_html_document__err_400__workspace_id_is_not_int(self) -> None: # nopep8
  161. """
  162. Get one html document of a content, workspace id is not int
  163. """
  164. self.testapp.authorization = (
  165. 'Basic',
  166. (
  167. 'admin@admin.admin',
  168. 'admin@admin.admin'
  169. )
  170. )
  171. res = self.testapp.get(
  172. '/api/v2/workspaces/coucou/html-documents/6',
  173. status=400
  174. )
  175. def test_api__get_html_document__err_400__content_id_is_not_int(self) -> None: # nopep8
  176. """
  177. Get one html document of a content, content_id is not int
  178. """
  179. self.testapp.authorization = (
  180. 'Basic',
  181. (
  182. 'admin@admin.admin',
  183. 'admin@admin.admin'
  184. )
  185. )
  186. res = self.testapp.get(
  187. '/api/v2/workspaces/2/html-documents/coucou',
  188. status=400
  189. )
  190. def test_api__update_html_document__err_400__empty_label(self) -> None: # nopep8
  191. """
  192. Update(put) one html document of a content
  193. """
  194. self.testapp.authorization = (
  195. 'Basic',
  196. (
  197. 'admin@admin.admin',
  198. 'admin@admin.admin'
  199. )
  200. )
  201. params = {
  202. 'label': '',
  203. 'raw_content': '<p> Le nouveau contenu </p>',
  204. }
  205. res = self.testapp.put_json(
  206. '/api/v2/workspaces/2/html-documents/6',
  207. params=params,
  208. status=400
  209. )
  210. def test_api__update_html_document__ok_200__nominal_case(self) -> None:
  211. """
  212. Update(put) one html document of a content
  213. """
  214. self.testapp.authorization = (
  215. 'Basic',
  216. (
  217. 'admin@admin.admin',
  218. 'admin@admin.admin'
  219. )
  220. )
  221. params = {
  222. 'label': 'My New label',
  223. 'raw_content': '<p> Le nouveau contenu </p>',
  224. }
  225. res = self.testapp.put_json(
  226. '/api/v2/workspaces/2/html-documents/6',
  227. params=params,
  228. status=200
  229. )
  230. content = res.json_body
  231. assert content['content_type'] == 'html-documents'
  232. assert content['content_id'] == 6
  233. assert content['is_archived'] is False
  234. assert content['is_deleted'] is False
  235. assert content['label'] == 'My New label'
  236. assert content['parent_id'] == 3
  237. assert content['show_in_ui'] is True
  238. assert content['slug'] == 'my-new-label'
  239. assert content['status'] == 'open'
  240. assert content['workspace_id'] == 2
  241. assert content['current_revision_id'] == 28
  242. # TODO - G.M - 2018-06-173 - check date format
  243. assert content['created']
  244. assert content['author']
  245. assert content['author']['user_id'] == 1
  246. assert content['author']['avatar_url'] is None
  247. assert content['author']['public_name'] == 'Global manager'
  248. # TODO - G.M - 2018-06-173 - check date format
  249. assert content['modified']
  250. assert content['last_modifier'] == content['author']
  251. assert content['raw_content'] == '<p> Le nouveau contenu </p>'
  252. res = self.testapp.get(
  253. '/api/v2/workspaces/2/html-documents/6',
  254. status=200
  255. )
  256. content = res.json_body
  257. assert content['content_type'] == 'html-documents'
  258. assert content['content_id'] == 6
  259. assert content['is_archived'] is False
  260. assert content['is_deleted'] is False
  261. assert content['label'] == 'My New label'
  262. assert content['parent_id'] == 3
  263. assert content['show_in_ui'] is True
  264. assert content['slug'] == 'my-new-label'
  265. assert content['status'] == 'open'
  266. assert content['workspace_id'] == 2
  267. assert content['current_revision_id'] == 28
  268. # TODO - G.M - 2018-06-173 - check date format
  269. assert content['created']
  270. assert content['author']
  271. assert content['author']['user_id'] == 1
  272. assert content['author']['avatar_url'] is None
  273. assert content['author']['public_name'] == 'Global manager'
  274. # TODO - G.M - 2018-06-173 - check date format
  275. assert content['modified']
  276. assert content['last_modifier'] == content['author']
  277. assert content['raw_content'] == '<p> Le nouveau contenu </p>'
  278. def test_api__get_html_document_revisions__ok_200__nominal_case(
  279. self
  280. ) -> None:
  281. """
  282. Get one html document of a content
  283. """
  284. self.testapp.authorization = (
  285. 'Basic',
  286. (
  287. 'admin@admin.admin',
  288. 'admin@admin.admin'
  289. )
  290. )
  291. res = self.testapp.get(
  292. '/api/v2/workspaces/2/html-documents/6/revisions',
  293. status=200
  294. )
  295. revisions = res.json_body
  296. assert len(revisions) == 3
  297. revision = revisions[0]
  298. assert revision['content_type'] == 'html-documents'
  299. assert revision['content_id'] == 6
  300. assert revision['is_archived'] is False
  301. assert revision['is_deleted'] is False
  302. assert revision['label'] == 'Tiramisu Recipes!!!'
  303. assert revision['parent_id'] == 3
  304. assert revision['show_in_ui'] is True
  305. assert revision['slug'] == 'tiramisu-recipes'
  306. assert revision['status'] == 'open'
  307. assert revision['workspace_id'] == 2
  308. assert revision['revision_id'] == 6
  309. assert revision['sub_content_types']
  310. # TODO - G.M - 2018-06-173 - Test with real comments
  311. assert revision['comment_ids'] == []
  312. # TODO - G.M - 2018-06-173 - check date format
  313. assert revision['created']
  314. assert revision['author']
  315. assert revision['author']['user_id'] == 1
  316. assert revision['author']['avatar_url'] is None
  317. assert revision['author']['public_name'] == 'Global manager'
  318. revision = revisions[1]
  319. assert revision['content_type'] == 'html-documents'
  320. assert revision['content_id'] == 6
  321. assert revision['is_archived'] is False
  322. assert revision['is_deleted'] is False
  323. assert revision['label'] == 'Tiramisu Recipes!!!'
  324. assert revision['parent_id'] == 3
  325. assert revision['show_in_ui'] is True
  326. assert revision['slug'] == 'tiramisu-recipes'
  327. assert revision['status'] == 'open'
  328. assert revision['workspace_id'] == 2
  329. assert revision['revision_id'] == 7
  330. assert revision['sub_content_types']
  331. # TODO - G.M - 2018-06-173 - Test with real comments
  332. assert revision['comment_ids'] == []
  333. # TODO - G.M - 2018-06-173 - check date format
  334. assert revision['created']
  335. assert revision['author']
  336. assert revision['author']['user_id'] == 1
  337. assert revision['author']['avatar_url'] is None
  338. assert revision['author']['public_name'] == 'Global manager'
  339. revision = revisions[2]
  340. assert revision['content_type'] == 'html-documents'
  341. assert revision['content_id'] == 6
  342. assert revision['is_archived'] is False
  343. assert revision['is_deleted'] is False
  344. assert revision['label'] == 'Tiramisu Recipe'
  345. assert revision['parent_id'] == 3
  346. assert revision['show_in_ui'] is True
  347. assert revision['slug'] == 'tiramisu-recipe'
  348. assert revision['status'] == 'open'
  349. assert revision['workspace_id'] == 2
  350. assert revision['revision_id'] == 27
  351. assert revision['sub_content_types']
  352. # TODO - G.M - 2018-06-173 - Test with real comments
  353. assert revision['comment_ids'] == []
  354. # TODO - G.M - 2018-06-173 - check date format
  355. assert revision['created']
  356. assert revision['author']
  357. assert revision['author']['user_id'] == 3
  358. assert revision['author']['avatar_url'] is None
  359. assert revision['author']['public_name'] == 'Bob i.'
  360. def test_api__set_html_document_status__ok_200__nominal_case(self) -> None:
  361. """
  362. Get one html document of a content
  363. """
  364. self.testapp.authorization = (
  365. 'Basic',
  366. (
  367. 'admin@admin.admin',
  368. 'admin@admin.admin'
  369. )
  370. )
  371. params = {
  372. 'status': 'closed-deprecated',
  373. }
  374. # before
  375. res = self.testapp.get(
  376. '/api/v2/workspaces/2/html-documents/6',
  377. status=200
  378. )
  379. content = res.json_body
  380. assert content['content_type'] == 'html-documents'
  381. assert content['content_id'] == 6
  382. assert content['status'] == 'open'
  383. # set status
  384. res = self.testapp.put_json(
  385. '/api/v2/workspaces/2/html-documents/6/status',
  386. params=params,
  387. status=204
  388. )
  389. # after
  390. res = self.testapp.get(
  391. '/api/v2/workspaces/2/html-documents/6',
  392. status=200
  393. )
  394. content = res.json_body
  395. assert content['content_type'] == 'html-documents'
  396. assert content['content_id'] == 6
  397. assert content['status'] == 'closed-deprecated'
  398. def test_api__set_html_document_status__err_400__wrong_status(self) -> None:
  399. """
  400. Get one html document of a content
  401. """
  402. self.testapp.authorization = (
  403. 'Basic',
  404. (
  405. 'admin@admin.admin',
  406. 'admin@admin.admin'
  407. )
  408. )
  409. params = {
  410. 'status': 'unexistant-status',
  411. }
  412. res = self.testapp.put_json(
  413. '/api/v2/workspaces/2/html-documents/6/status',
  414. params=params,
  415. status=400
  416. )
  417. class TestFiles(FunctionalTest):
  418. """
  419. Tests for /api/v2/workspaces/{workspace_id}/files/{content_id}
  420. endpoint
  421. """
  422. fixtures = [BaseFixture, ContentFixtures]
  423. def test_api__get_file__ok_200__nominal_case(self) -> None:
  424. """
  425. Get one file of a content
  426. """
  427. dbsession = get_tm_session(self.session_factory, transaction.manager)
  428. admin = dbsession.query(models.User) \
  429. .filter(models.User.email == 'admin@admin.admin') \
  430. .one()
  431. workspace_api = WorkspaceApi(
  432. current_user=admin,
  433. session=dbsession,
  434. config=self.app_config
  435. )
  436. content_api = ContentApi(
  437. current_user=admin,
  438. session=dbsession,
  439. config=self.app_config
  440. )
  441. business_workspace = workspace_api.get_one(1)
  442. tool_folder = content_api.get_one(1, content_type=ContentType.Any)
  443. test_file = content_api.create(
  444. content_type=ContentType.File,
  445. workspace=business_workspace,
  446. parent=tool_folder,
  447. label='Test file',
  448. do_save=False,
  449. do_notify=False,
  450. )
  451. test_file.file_extension = '.txt'
  452. test_file.depot_file = FileIntent(
  453. b'Test file',
  454. 'Test_file.txt',
  455. 'text/plain',
  456. )
  457. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  458. dbsession.flush()
  459. transaction.commit()
  460. self.testapp.authorization = (
  461. 'Basic',
  462. (
  463. 'admin@admin.admin',
  464. 'admin@admin.admin'
  465. )
  466. )
  467. res = self.testapp.get(
  468. '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
  469. status=200
  470. )
  471. content = res.json_body
  472. assert content['content_type'] == 'file'
  473. assert content['content_id'] == test_file.content_id
  474. assert content['is_archived'] is False
  475. assert content['is_deleted'] is False
  476. assert content['label'] == 'Test_file'
  477. assert content['parent_id'] == 1
  478. assert content['show_in_ui'] is True
  479. assert content['slug'] == 'test-file'
  480. assert content['status'] == 'open'
  481. assert content['workspace_id'] == 1
  482. assert content['current_revision_id']
  483. # TODO - G.M - 2018-06-173 - check date format
  484. assert content['created']
  485. assert content['author']
  486. assert content['author']['user_id'] == 1
  487. assert content['author']['avatar_url'] is None
  488. assert content['author']['public_name'] == 'Global manager'
  489. # TODO - G.M - 2018-06-173 - check date format
  490. assert content['modified']
  491. assert content['last_modifier'] == content['author']
  492. assert content['raw_content'] == '<p>description</p>' # nopep8
  493. def test_api__get_files__err_400__wrong_content_type(self) -> None:
  494. """
  495. Get one file of a content content
  496. """
  497. self.testapp.authorization = (
  498. 'Basic',
  499. (
  500. 'admin@admin.admin',
  501. 'admin@admin.admin'
  502. )
  503. )
  504. res = self.testapp.get(
  505. '/api/v2/workspaces/2/files/6',
  506. status=400
  507. )
  508. def test_api__get_file__err_400__content_does_not_exist(self) -> None: # nopep8
  509. """
  510. Get one file (content 170 does not exist in db
  511. """
  512. self.testapp.authorization = (
  513. 'Basic',
  514. (
  515. 'admin@admin.admin',
  516. 'admin@admin.admin'
  517. )
  518. )
  519. res = self.testapp.get(
  520. '/api/v2/workspaces/1/files/170',
  521. status=400
  522. )
  523. def test_api__get_file__err_400__content_not_in_workspace(self) -> None: # nopep8
  524. """
  525. Get one file (content 9 is in workspace 2)
  526. """
  527. self.testapp.authorization = (
  528. 'Basic',
  529. (
  530. 'admin@admin.admin',
  531. 'admin@admin.admin'
  532. )
  533. )
  534. res = self.testapp.get(
  535. '/api/v2/workspaces/1/files/9',
  536. status=400
  537. )
  538. def test_api__get_file__err_400__workspace_does_not_exist(self) -> None: # nopep8
  539. """
  540. Get one file (Workspace 40 does not exist)
  541. """
  542. self.testapp.authorization = (
  543. 'Basic',
  544. (
  545. 'admin@admin.admin',
  546. 'admin@admin.admin'
  547. )
  548. )
  549. res = self.testapp.get(
  550. '/api/v2/workspaces/40/files/9',
  551. status=400
  552. )
  553. def test_api__get_file__err_400__workspace_id_is_not_int(self) -> None: # nopep8
  554. """
  555. Get one file, workspace id is not int
  556. """
  557. self.testapp.authorization = (
  558. 'Basic',
  559. (
  560. 'admin@admin.admin',
  561. 'admin@admin.admin'
  562. )
  563. )
  564. res = self.testapp.get(
  565. '/api/v2/workspaces/coucou/files/9',
  566. status=400
  567. )
  568. def test_api__get_file__err_400__content_id_is_not_int(self) -> None: # nopep8
  569. """
  570. Get one file, content_id is not int
  571. """
  572. self.testapp.authorization = (
  573. 'Basic',
  574. (
  575. 'admin@admin.admin',
  576. 'admin@admin.admin'
  577. )
  578. )
  579. res = self.testapp.get(
  580. '/api/v2/workspaces/2/files/coucou',
  581. status=400
  582. )
  583. def test_api__update_file_info_err_400__empty_label(self) -> None: # nopep8
  584. """
  585. Update(put) one file
  586. """
  587. dbsession = get_tm_session(self.session_factory, transaction.manager)
  588. admin = dbsession.query(models.User) \
  589. .filter(models.User.email == 'admin@admin.admin') \
  590. .one()
  591. workspace_api = WorkspaceApi(
  592. current_user=admin,
  593. session=dbsession,
  594. config=self.app_config
  595. )
  596. content_api = ContentApi(
  597. current_user=admin,
  598. session=dbsession,
  599. config=self.app_config
  600. )
  601. business_workspace = workspace_api.get_one(1)
  602. tool_folder = content_api.get_one(1, content_type=ContentType.Any)
  603. test_file = content_api.create(
  604. content_type=ContentType.File,
  605. workspace=business_workspace,
  606. parent=tool_folder,
  607. label='Test file',
  608. do_save=False,
  609. do_notify=False,
  610. )
  611. test_file.file_extension = '.txt'
  612. test_file.depot_file = FileIntent(
  613. b'Test file',
  614. 'Test_file.txt',
  615. 'text/plain',
  616. )
  617. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  618. dbsession.flush()
  619. transaction.commit()
  620. self.testapp.authorization = (
  621. 'Basic',
  622. (
  623. 'admin@admin.admin',
  624. 'admin@admin.admin'
  625. )
  626. )
  627. params = {
  628. 'label': '',
  629. 'raw_content': '<p> Le nouveau contenu </p>',
  630. }
  631. res = self.testapp.put_json(
  632. '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
  633. params=params,
  634. status=400
  635. )
  636. def test_api__update_file_info__ok_200__nominal_case(self) -> None:
  637. """
  638. Update(put) one file
  639. """
  640. dbsession = get_tm_session(self.session_factory, transaction.manager)
  641. admin = dbsession.query(models.User) \
  642. .filter(models.User.email == 'admin@admin.admin') \
  643. .one()
  644. workspace_api = WorkspaceApi(
  645. current_user=admin,
  646. session=dbsession,
  647. config=self.app_config
  648. )
  649. content_api = ContentApi(
  650. current_user=admin,
  651. session=dbsession,
  652. config=self.app_config
  653. )
  654. business_workspace = workspace_api.get_one(1)
  655. tool_folder = content_api.get_one(1, content_type=ContentType.Any)
  656. test_file = content_api.create(
  657. content_type=ContentType.File,
  658. workspace=business_workspace,
  659. parent=tool_folder,
  660. label='Test file',
  661. do_save=False,
  662. do_notify=False,
  663. )
  664. test_file.file_extension = '.txt'
  665. test_file.depot_file = FileIntent(
  666. b'Test file',
  667. 'Test_file.txt',
  668. 'text/plain',
  669. )
  670. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  671. dbsession.flush()
  672. transaction.commit()
  673. self.testapp.authorization = (
  674. 'Basic',
  675. (
  676. 'admin@admin.admin',
  677. 'admin@admin.admin'
  678. )
  679. )
  680. params = {
  681. 'label': 'My New label',
  682. 'raw_content': '<p> Le nouveau contenu </p>',
  683. }
  684. res = self.testapp.put_json(
  685. '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
  686. params=params,
  687. status=200
  688. )
  689. content = res.json_body
  690. assert content['content_type'] == 'file'
  691. assert content['content_id'] == test_file.content_id
  692. assert content['is_archived'] is False
  693. assert content['is_deleted'] is False
  694. assert content['label'] == 'My New label'
  695. assert content['parent_id'] == 1
  696. assert content['show_in_ui'] is True
  697. assert content['slug'] == 'my-new-label'
  698. assert content['status'] == 'open'
  699. assert content['workspace_id'] == 1
  700. assert content['current_revision_id']
  701. # TODO - G.M - 2018-06-173 - check date format
  702. assert content['created']
  703. assert content['author']
  704. assert content['author']['user_id'] == 1
  705. assert content['author']['avatar_url'] is None
  706. assert content['author']['public_name'] == 'Global manager'
  707. # TODO - G.M - 2018-06-173 - check date format
  708. assert content['modified']
  709. assert content['last_modifier'] == content['author']
  710. assert content['raw_content'] == '<p> Le nouveau contenu </p>'
  711. res = self.testapp.get(
  712. '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
  713. status=200
  714. )
  715. content = res.json_body
  716. assert content['content_type'] == 'file'
  717. assert content['content_id'] == test_file.content_id
  718. assert content['is_archived'] is False
  719. assert content['is_deleted'] is False
  720. assert content['label'] == 'My New label'
  721. assert content['parent_id'] == 1
  722. assert content['show_in_ui'] is True
  723. assert content['slug'] == 'my-new-label'
  724. assert content['status'] == 'open'
  725. assert content['workspace_id'] == 1
  726. assert content['current_revision_id']
  727. # TODO - G.M - 2018-06-173 - check date format
  728. assert content['created']
  729. assert content['author']
  730. assert content['author']['user_id'] == 1
  731. assert content['author']['avatar_url'] is None
  732. assert content['author']['public_name'] == 'Global manager'
  733. # TODO - G.M - 2018-06-173 - check date format
  734. assert content['modified']
  735. assert content['last_modifier'] == content['author']
  736. assert content['raw_content'] == '<p> Le nouveau contenu </p>'
  737. def test_api__get_file_revisions__ok_200__nominal_case(
  738. self
  739. ) -> None:
  740. """
  741. Get file revisions
  742. """
  743. dbsession = get_tm_session(self.session_factory, transaction.manager)
  744. admin = dbsession.query(models.User) \
  745. .filter(models.User.email == 'admin@admin.admin') \
  746. .one()
  747. workspace_api = WorkspaceApi(
  748. current_user=admin,
  749. session=dbsession,
  750. config=self.app_config
  751. )
  752. content_api = ContentApi(
  753. current_user=admin,
  754. session=dbsession,
  755. config=self.app_config
  756. )
  757. business_workspace = workspace_api.get_one(1)
  758. tool_folder = content_api.get_one(1, content_type=ContentType.Any)
  759. test_file = content_api.create(
  760. content_type=ContentType.File,
  761. workspace=business_workspace,
  762. parent=tool_folder,
  763. label='Test file',
  764. do_save=False,
  765. do_notify=False,
  766. )
  767. test_file.file_extension = '.txt'
  768. test_file.depot_file = FileIntent(
  769. b'Test file',
  770. 'Test_file.txt',
  771. 'text/plain',
  772. )
  773. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  774. dbsession.flush()
  775. transaction.commit()
  776. self.testapp.authorization = (
  777. 'Basic',
  778. (
  779. 'admin@admin.admin',
  780. 'admin@admin.admin'
  781. )
  782. )
  783. res = self.testapp.get(
  784. '/api/v2/workspaces/1/files/{}/revisions'.format(test_file.content_id),
  785. status=200
  786. )
  787. revisions = res.json_body
  788. assert len(revisions) == 1
  789. revision = revisions[0]
  790. assert revision['content_type'] == 'file'
  791. assert revision['content_id'] == test_file.content_id
  792. assert revision['is_archived'] is False
  793. assert revision['is_deleted'] is False
  794. assert revision['label'] == 'Test_file'
  795. assert revision['parent_id'] == 1
  796. assert revision['show_in_ui'] is True
  797. assert revision['slug'] == 'test-file'
  798. assert revision['status'] == 'open'
  799. assert revision['workspace_id'] == 1
  800. assert revision['revision_id']
  801. assert revision['sub_content_types']
  802. # TODO - G.M - 2018-06-173 - Test with real comments
  803. assert revision['comment_ids'] == []
  804. # TODO - G.M - 2018-06-173 - check date format
  805. assert revision['created']
  806. assert revision['author']
  807. assert revision['author']['user_id'] == 1
  808. assert revision['author']['avatar_url'] is None
  809. assert revision['author']['public_name'] == 'Global manager'
  810. def test_api__set_file_status__ok_200__nominal_case(self) -> None:
  811. """
  812. set file status
  813. """
  814. dbsession = get_tm_session(self.session_factory, transaction.manager)
  815. admin = dbsession.query(models.User) \
  816. .filter(models.User.email == 'admin@admin.admin') \
  817. .one()
  818. workspace_api = WorkspaceApi(
  819. current_user=admin,
  820. session=dbsession,
  821. config=self.app_config
  822. )
  823. content_api = ContentApi(
  824. current_user=admin,
  825. session=dbsession,
  826. config=self.app_config
  827. )
  828. business_workspace = workspace_api.get_one(1)
  829. tool_folder = content_api.get_one(1, content_type=ContentType.Any)
  830. test_file = content_api.create(
  831. content_type=ContentType.File,
  832. workspace=business_workspace,
  833. parent=tool_folder,
  834. label='Test file',
  835. do_save=False,
  836. do_notify=False,
  837. )
  838. test_file.file_extension = '.txt'
  839. test_file.depot_file = FileIntent(
  840. b'Test file',
  841. 'Test_file.txt',
  842. 'text/plain',
  843. )
  844. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  845. dbsession.flush()
  846. transaction.commit()
  847. self.testapp.authorization = (
  848. 'Basic',
  849. (
  850. 'admin@admin.admin',
  851. 'admin@admin.admin'
  852. )
  853. )
  854. params = {
  855. 'status': 'closed-deprecated',
  856. }
  857. # before
  858. res = self.testapp.get(
  859. '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
  860. status=200
  861. )
  862. content = res.json_body
  863. assert content['content_type'] == 'file'
  864. assert content['content_id'] == test_file.content_id
  865. assert content['status'] == 'open'
  866. # set status
  867. res = self.testapp.put_json(
  868. '/api/v2/workspaces/1/files/{}/status'.format(test_file.content_id),
  869. params=params,
  870. status=204
  871. )
  872. # after
  873. res = self.testapp.get(
  874. '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
  875. status=200
  876. )
  877. content = res.json_body
  878. assert content['content_type'] == 'file'
  879. assert content['content_id'] == test_file.content_id
  880. assert content['status'] == 'closed-deprecated'
  881. def test_api__set_file_status__err_400__wrong_status(self) -> None:
  882. """
  883. set file status
  884. """
  885. self.testapp.authorization = (
  886. 'Basic',
  887. (
  888. 'admin@admin.admin',
  889. 'admin@admin.admin'
  890. )
  891. )
  892. params = {
  893. 'status': 'unexistant-status',
  894. }
  895. res = self.testapp.put_json(
  896. '/api/v2/workspaces/2/files/6/status',
  897. params=params,
  898. status=400
  899. )
  900. class TestThreads(FunctionalTest):
  901. """
  902. Tests for /api/v2/workspaces/{workspace_id}/threads/{content_id}
  903. endpoint
  904. """
  905. fixtures = [BaseFixture, ContentFixtures]
  906. def test_api__get_thread__err_400__wrong_content_type(self) -> None:
  907. """
  908. Get one html document of a content
  909. """
  910. self.testapp.authorization = (
  911. 'Basic',
  912. (
  913. 'admin@admin.admin',
  914. 'admin@admin.admin'
  915. )
  916. )
  917. res = self.testapp.get(
  918. '/api/v2/workspaces/2/threads/6',
  919. status=400
  920. )
  921. def test_api__get_thread__ok_200__nominal_case(self) -> None:
  922. """
  923. Get one html document of a content
  924. """
  925. self.testapp.authorization = (
  926. 'Basic',
  927. (
  928. 'admin@admin.admin',
  929. 'admin@admin.admin'
  930. )
  931. )
  932. res = self.testapp.get(
  933. '/api/v2/workspaces/2/threads/7',
  934. status=200
  935. ) # nopep8
  936. content = res.json_body
  937. assert content['content_type'] == 'thread'
  938. assert content['content_id'] == 7
  939. assert content['is_archived'] is False
  940. assert content['is_deleted'] is False
  941. assert content['label'] == 'Best Cakes?'
  942. assert content['parent_id'] == 3
  943. assert content['show_in_ui'] is True
  944. assert content['slug'] == 'best-cakes'
  945. assert content['status'] == 'open'
  946. assert content['workspace_id'] == 2
  947. assert content['current_revision_id'] == 26
  948. # TODO - G.M - 2018-06-173 - check date format
  949. assert content['created']
  950. assert content['author']
  951. assert content['author']['user_id'] == 1
  952. assert content['author']['avatar_url'] is None
  953. assert content['author']['public_name'] == 'Global manager'
  954. # TODO - G.M - 2018-06-173 - check date format
  955. assert content['modified']
  956. assert content['last_modifier'] != content['author']
  957. assert content['last_modifier']['user_id'] == 3
  958. assert content['last_modifier']['public_name'] == 'Bob i.'
  959. assert content['last_modifier']['avatar_url'] is None
  960. assert content['raw_content'] == 'What is the best cake?' # nopep8
  961. def test_api__get_thread__err_400__content_does_not_exist(self) -> None:
  962. """
  963. Get one thread (content 170 does not exist)
  964. """
  965. self.testapp.authorization = (
  966. 'Basic',
  967. (
  968. 'admin@admin.admin',
  969. 'admin@admin.admin'
  970. )
  971. )
  972. res = self.testapp.get(
  973. '/api/v2/workspaces/2/threads/170',
  974. status=400
  975. )
  976. def test_api__get_thread__err_400__content_not_in_workspace(self) -> None:
  977. """
  978. Get one thread(content 7 is in workspace 2)
  979. """
  980. self.testapp.authorization = (
  981. 'Basic',
  982. (
  983. 'admin@admin.admin',
  984. 'admin@admin.admin'
  985. )
  986. )
  987. res = self.testapp.get(
  988. '/api/v2/workspaces/1/threads/7',
  989. status=400
  990. )
  991. def test_api__get_thread__err_400__workspace_does_not_exist(self) -> None: # nopep8
  992. """
  993. Get one thread (Workspace 40 does not exist)
  994. """
  995. self.testapp.authorization = (
  996. 'Basic',
  997. (
  998. 'admin@admin.admin',
  999. 'admin@admin.admin'
  1000. )
  1001. )
  1002. res = self.testapp.get(
  1003. '/api/v2/workspaces/40/threads/7',
  1004. status=400
  1005. )
  1006. def test_api__get_thread__err_400__workspace_id_is_not_int(self) -> None: # nopep8
  1007. """
  1008. Get one thread, workspace id is not int
  1009. """
  1010. self.testapp.authorization = (
  1011. 'Basic',
  1012. (
  1013. 'admin@admin.admin',
  1014. 'admin@admin.admin'
  1015. )
  1016. )
  1017. res = self.testapp.get(
  1018. '/api/v2/workspaces/coucou/threads/7',
  1019. status=400
  1020. )
  1021. def test_api__get_thread__err_400_content_id_is_not_int(self) -> None: # nopep8
  1022. """
  1023. Get one thread, content id is not int
  1024. """
  1025. self.testapp.authorization = (
  1026. 'Basic',
  1027. (
  1028. 'admin@admin.admin',
  1029. 'admin@admin.admin'
  1030. )
  1031. )
  1032. res = self.testapp.get(
  1033. '/api/v2/workspaces/2/threads/coucou',
  1034. status=400
  1035. )
  1036. def test_api__update_thread__ok_200__nominal_case(self) -> None:
  1037. """
  1038. Update(put) thread
  1039. """
  1040. self.testapp.authorization = (
  1041. 'Basic',
  1042. (
  1043. 'admin@admin.admin',
  1044. 'admin@admin.admin'
  1045. )
  1046. )
  1047. params = {
  1048. 'label': 'My New label',
  1049. 'raw_content': '<p> Le nouveau contenu </p>',
  1050. }
  1051. res = self.testapp.put_json(
  1052. '/api/v2/workspaces/2/threads/7',
  1053. params=params,
  1054. status=200
  1055. )
  1056. content = res.json_body
  1057. assert content['content_type'] == 'thread'
  1058. assert content['content_id'] == 7
  1059. assert content['is_archived'] is False
  1060. assert content['is_deleted'] is False
  1061. assert content['label'] == 'My New label'
  1062. assert content['parent_id'] == 3
  1063. assert content['show_in_ui'] is True
  1064. assert content['slug'] == 'my-new-label'
  1065. assert content['status'] == 'open'
  1066. assert content['workspace_id'] == 2
  1067. assert content['current_revision_id'] == 28
  1068. # TODO - G.M - 2018-06-173 - check date format
  1069. assert content['created']
  1070. assert content['author']
  1071. assert content['author']['user_id'] == 1
  1072. assert content['author']['avatar_url'] is None
  1073. assert content['author']['public_name'] == 'Global manager'
  1074. # TODO - G.M - 2018-06-173 - check date format
  1075. assert content['modified']
  1076. assert content['last_modifier'] == content['author']
  1077. assert content['raw_content'] == '<p> Le nouveau contenu </p>'
  1078. res = self.testapp.get(
  1079. '/api/v2/workspaces/2/threads/7',
  1080. status=200
  1081. ) # nopep8
  1082. content = res.json_body
  1083. assert content['content_type'] == 'thread'
  1084. assert content['content_id'] == 7
  1085. assert content['is_archived'] is False
  1086. assert content['is_deleted'] is False
  1087. assert content['label'] == 'My New label'
  1088. assert content['parent_id'] == 3
  1089. assert content['show_in_ui'] is True
  1090. assert content['slug'] == 'my-new-label'
  1091. assert content['status'] == 'open'
  1092. assert content['workspace_id'] == 2
  1093. assert content['current_revision_id'] == 28
  1094. # TODO - G.M - 2018-06-173 - check date format
  1095. assert content['created']
  1096. assert content['author']
  1097. assert content['author']['user_id'] == 1
  1098. assert content['author']['avatar_url'] is None
  1099. assert content['author']['public_name'] == 'Global manager'
  1100. # TODO - G.M - 2018-06-173 - check date format
  1101. assert content['modified']
  1102. assert content['last_modifier'] == content['author']
  1103. assert content['raw_content'] == '<p> Le nouveau contenu </p>'
  1104. def test_api__update_thread__err_400__empty_label(self) -> None:
  1105. """
  1106. Update(put) thread
  1107. """
  1108. self.testapp.authorization = (
  1109. 'Basic',
  1110. (
  1111. 'admin@admin.admin',
  1112. 'admin@admin.admin'
  1113. )
  1114. )
  1115. params = {
  1116. 'label': '',
  1117. 'raw_content': '<p> Le nouveau contenu </p>',
  1118. }
  1119. res = self.testapp.put_json(
  1120. '/api/v2/workspaces/2/threads/7',
  1121. params=params,
  1122. status=400
  1123. )
  1124. def test_api__get_thread_revisions__ok_200__nominal_case(
  1125. self
  1126. ) -> None:
  1127. """
  1128. Get threads revisions
  1129. """
  1130. self.testapp.authorization = (
  1131. 'Basic',
  1132. (
  1133. 'admin@admin.admin',
  1134. 'admin@admin.admin'
  1135. )
  1136. )
  1137. res = self.testapp.get(
  1138. '/api/v2/workspaces/2/threads/7/revisions',
  1139. status=200
  1140. )
  1141. revisions = res.json_body
  1142. assert len(revisions) == 2
  1143. revision = revisions[0]
  1144. assert revision['content_type'] == 'thread'
  1145. assert revision['content_id'] == 7
  1146. assert revision['is_archived'] is False
  1147. assert revision['is_deleted'] is False
  1148. assert revision['label'] == 'Best Cake'
  1149. assert revision['parent_id'] == 3
  1150. assert revision['show_in_ui'] is True
  1151. assert revision['slug'] == 'best-cake'
  1152. assert revision['status'] == 'open'
  1153. assert revision['workspace_id'] == 2
  1154. assert revision['revision_id'] == 8
  1155. assert revision['sub_content_types']
  1156. assert revision['comment_ids'] == [18, 19, 20]
  1157. # TODO - G.M - 2018-06-173 - check date format
  1158. assert revision['created']
  1159. assert revision['author']
  1160. assert revision['author']['user_id'] == 1
  1161. assert revision['author']['avatar_url'] is None
  1162. assert revision['author']['public_name'] == 'Global manager'
  1163. revision = revisions[1]
  1164. assert revision['content_type'] == 'thread'
  1165. assert revision['content_id'] == 7
  1166. assert revision['is_archived'] is False
  1167. assert revision['is_deleted'] is False
  1168. assert revision['label'] == 'Best Cakes?'
  1169. assert revision['parent_id'] == 3
  1170. assert revision['show_in_ui'] is True
  1171. assert revision['slug'] == 'best-cakes'
  1172. assert revision['status'] == 'open'
  1173. assert revision['workspace_id'] == 2
  1174. assert revision['revision_id'] == 26
  1175. assert revision['sub_content_types']
  1176. assert revision['comment_ids'] == []
  1177. # TODO - G.M - 2018-06-173 - check date format
  1178. assert revision['created']
  1179. assert revision['author']
  1180. assert revision['author']['user_id'] == 3
  1181. assert revision['author']['avatar_url'] is None
  1182. assert revision['author']['public_name'] == 'Bob i.'
  1183. def test_api__set_thread_status__ok_200__nominal_case(self) -> None:
  1184. """
  1185. Set thread status
  1186. """
  1187. self.testapp.authorization = (
  1188. 'Basic',
  1189. (
  1190. 'admin@admin.admin',
  1191. 'admin@admin.admin'
  1192. )
  1193. )
  1194. params = {
  1195. 'status': 'closed-deprecated',
  1196. }
  1197. # before
  1198. res = self.testapp.get(
  1199. '/api/v2/workspaces/2/threads/7',
  1200. status=200
  1201. ) # nopep8
  1202. content = res.json_body
  1203. assert content['content_type'] == 'thread'
  1204. assert content['content_id'] == 7
  1205. assert content['status'] == 'open'
  1206. # set status
  1207. res = self.testapp.put_json(
  1208. '/api/v2/workspaces/2/threads/7/status',
  1209. params=params,
  1210. status=204
  1211. )
  1212. # after
  1213. res = self.testapp.get(
  1214. '/api/v2/workspaces/2/threads/7',
  1215. status=200
  1216. ) # nopep8
  1217. content = res.json_body
  1218. assert content['content_type'] == 'thread'
  1219. assert content['content_id'] == 7
  1220. assert content['status'] == 'closed-deprecated'
  1221. def test_api__set_thread_status__ok_400__wrong_status(self) -> None:
  1222. """
  1223. Set thread status
  1224. """
  1225. self.testapp.authorization = (
  1226. 'Basic',
  1227. (
  1228. 'admin@admin.admin',
  1229. 'admin@admin.admin'
  1230. )
  1231. )
  1232. params = {
  1233. 'status': 'unexistant-status',
  1234. }
  1235. res = self.testapp.put_json(
  1236. '/api/v2/workspaces/2/threads/7/status',
  1237. params=params,
  1238. status=400
  1239. )