test_contents.py 55KB

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