test_contents.py 73KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182
  1. # -*- coding: utf-8 -*-
  2. import transaction
  3. from tracim_backend import models
  4. from tracim_backend.lib.core.content import ContentApi
  5. from tracim_backend.lib.core.workspace import WorkspaceApi
  6. from tracim_backend.models import get_tm_session
  7. from tracim_backend.models.contents import CONTENT_TYPES
  8. from tracim_backend.models.revision_protection import new_revision
  9. import io
  10. import transaction
  11. from PIL import Image
  12. from depot.io.utils import FileIntent
  13. from tracim_backend import models
  14. from tracim_backend.lib.core.content import ContentApi
  15. from tracim_backend.lib.core.workspace import WorkspaceApi
  16. from tracim_backend.models import get_tm_session
  17. from tracim_backend.models.revision_protection import new_revision
  18. from tracim_backend.tests import FunctionalTest
  19. from tracim_backend.tests import create_1000px_png_test_image
  20. from tracim_backend.tests import set_html_document_slug_to_legacy
  21. from tracim_backend.fixtures.content import Content as ContentFixtures
  22. from tracim_backend.fixtures.users_and_groups import Base as BaseFixture
  23. class TestHtmlDocuments(FunctionalTest):
  24. """
  25. Tests for /api/v2/workspaces/{workspace_id}/html-documents/{content_id}
  26. endpoint
  27. """
  28. fixtures = [BaseFixture, ContentFixtures]
  29. def test_api__get_html_document__ok_200__legacy_slug(self) -> None:
  30. """
  31. Get one html document of a content
  32. """
  33. self.testapp.authorization = (
  34. 'Basic',
  35. (
  36. 'admin@admin.admin',
  37. 'admin@admin.admin'
  38. )
  39. )
  40. set_html_document_slug_to_legacy(self.session_factory)
  41. res = self.testapp.get(
  42. '/api/v2/workspaces/2/html-documents/6',
  43. status=200
  44. )
  45. content = res.json_body
  46. assert content['content_type'] == 'html-document'
  47. assert content['content_id'] == 6
  48. assert content['is_archived'] is False
  49. assert content['is_deleted'] is False
  50. assert content['label'] == 'Tiramisu Recipe'
  51. assert content['parent_id'] == 3
  52. assert content['show_in_ui'] is True
  53. assert content['slug'] == 'tiramisu-recipe'
  54. assert content['status'] == 'open'
  55. assert content['workspace_id'] == 2
  56. assert content['current_revision_id'] == 27
  57. # TODO - G.M - 2018-06-173 - check date format
  58. assert content['created']
  59. assert content['author']
  60. assert content['author']['user_id'] == 1
  61. assert content['author']['avatar_url'] is None
  62. assert content['author']['public_name'] == 'Global manager'
  63. # TODO - G.M - 2018-06-173 - check date format
  64. assert content['modified']
  65. assert content['last_modifier'] != content['author']
  66. assert content['last_modifier']['user_id'] == 3
  67. assert content['last_modifier']['public_name'] == 'Bob i.'
  68. assert content['last_modifier']['avatar_url'] is None
  69. assert content['raw_content'] == '<p>To cook a great Tiramisu, you need many ingredients.</p>' # nopep8
  70. def test_api__get_html_document__ok_200__nominal_case(self) -> None:
  71. """
  72. Get one html document of a content
  73. """
  74. self.testapp.authorization = (
  75. 'Basic',
  76. (
  77. 'admin@admin.admin',
  78. 'admin@admin.admin'
  79. )
  80. )
  81. res = self.testapp.get(
  82. '/api/v2/workspaces/2/html-documents/6',
  83. status=200
  84. )
  85. content = res.json_body
  86. assert content['content_type'] == 'html-document'
  87. assert content['content_id'] == 6
  88. assert content['is_archived'] is False
  89. assert content['is_deleted'] is False
  90. assert content['label'] == 'Tiramisu Recipe'
  91. assert content['parent_id'] == 3
  92. assert content['show_in_ui'] is True
  93. assert content['slug'] == 'tiramisu-recipe'
  94. assert content['status'] == 'open'
  95. assert content['workspace_id'] == 2
  96. assert content['current_revision_id'] == 27
  97. # TODO - G.M - 2018-06-173 - check date format
  98. assert content['created']
  99. assert content['author']
  100. assert content['author']['user_id'] == 1
  101. assert content['author']['avatar_url'] is None
  102. assert content['author']['public_name'] == 'Global manager'
  103. # TODO - G.M - 2018-06-173 - check date format
  104. assert content['modified']
  105. assert content['last_modifier'] != content['author']
  106. assert content['last_modifier']['user_id'] == 3
  107. assert content['last_modifier']['public_name'] == 'Bob i.'
  108. assert content['last_modifier']['avatar_url'] is None
  109. assert content['raw_content'] == '<p>To cook a great Tiramisu, you need many ingredients.</p>' # nopep8
  110. def test_api__get_html_document__ok_200__archived_content(self) -> None:
  111. """
  112. Get one html document of a content
  113. """
  114. self.testapp.authorization = (
  115. 'Basic',
  116. (
  117. 'admin@admin.admin',
  118. 'admin@admin.admin'
  119. )
  120. )
  121. res = self.testapp.put_json(
  122. '/api/v2/workspaces/2/contents/6/archive',
  123. status=204
  124. )
  125. res = self.testapp.get(
  126. '/api/v2/workspaces/2/html-documents/6',
  127. status=200
  128. )
  129. content = res.json_body
  130. assert content['content_type'] == 'html-document'
  131. assert content['content_id'] == 6
  132. assert content['is_archived'] is True
  133. def test_api__get_html_document__ok_200__deleted_content(self) -> None:
  134. """
  135. Get one html document of a content
  136. """
  137. self.testapp.authorization = (
  138. 'Basic',
  139. (
  140. 'admin@admin.admin',
  141. 'admin@admin.admin'
  142. )
  143. )
  144. res = self.testapp.put_json(
  145. '/api/v2/workspaces/2/contents/6/delete',
  146. status=204
  147. )
  148. res = self.testapp.get(
  149. '/api/v2/workspaces/2/html-documents/6',
  150. status=200
  151. )
  152. content = res.json_body
  153. assert content['content_type'] == 'html-document'
  154. assert content['content_id'] == 6
  155. assert content['is_deleted'] is True
  156. def test_api__get_html_document__err_400__wrong_content_type(self) -> None:
  157. """
  158. Get one html document of a content content 7 is not html_document
  159. """
  160. self.testapp.authorization = (
  161. 'Basic',
  162. (
  163. 'admin@admin.admin',
  164. 'admin@admin.admin'
  165. )
  166. )
  167. self.testapp.get(
  168. '/api/v2/workspaces/2/html-documents/7',
  169. status=400
  170. )
  171. def test_api__get_html_document__err_400__content_does_not_exist(self) -> None: # nopep8
  172. """
  173. Get one html document of a content (content 170 does not exist in db
  174. """
  175. self.testapp.authorization = (
  176. 'Basic',
  177. (
  178. 'admin@admin.admin',
  179. 'admin@admin.admin'
  180. )
  181. )
  182. self.testapp.get(
  183. '/api/v2/workspaces/2/html-documents/170',
  184. status=400
  185. )
  186. def test_api__get_html_document__err_400__content_not_in_workspace(self) -> None: # nopep8
  187. """
  188. Get one html document of a content (content 6 is in workspace 2)
  189. """
  190. self.testapp.authorization = (
  191. 'Basic',
  192. (
  193. 'admin@admin.admin',
  194. 'admin@admin.admin'
  195. )
  196. )
  197. self.testapp.get(
  198. '/api/v2/workspaces/1/html-documents/6',
  199. status=400
  200. )
  201. def test_api__get_html_document__err_400__workspace_does_not_exist(self) -> None: # nopep8
  202. """
  203. Get one html document of a content (Workspace 40 does not exist)
  204. """
  205. self.testapp.authorization = (
  206. 'Basic',
  207. (
  208. 'admin@admin.admin',
  209. 'admin@admin.admin'
  210. )
  211. )
  212. self.testapp.get(
  213. '/api/v2/workspaces/40/html-documents/6',
  214. status=400
  215. )
  216. def test_api__get_html_document__err_400__workspace_id_is_not_int(self) -> None: # nopep8
  217. """
  218. Get one html document of a content, workspace id is not int
  219. """
  220. self.testapp.authorization = (
  221. 'Basic',
  222. (
  223. 'admin@admin.admin',
  224. 'admin@admin.admin'
  225. )
  226. )
  227. self.testapp.get(
  228. '/api/v2/workspaces/coucou/html-documents/6',
  229. status=400
  230. )
  231. def test_api__get_html_document__err_400__content_id_is_not_int(self) -> None: # nopep8
  232. """
  233. Get one html document of a content, content_id is not int
  234. """
  235. self.testapp.authorization = (
  236. 'Basic',
  237. (
  238. 'admin@admin.admin',
  239. 'admin@admin.admin'
  240. )
  241. )
  242. self.testapp.get(
  243. '/api/v2/workspaces/2/html-documents/coucou',
  244. status=400
  245. )
  246. def test_api__update_html_document__err_400__empty_label(self) -> None: # nopep8
  247. """
  248. Update(put) one html document of a content
  249. """
  250. self.testapp.authorization = (
  251. 'Basic',
  252. (
  253. 'admin@admin.admin',
  254. 'admin@admin.admin'
  255. )
  256. )
  257. params = {
  258. 'label': '',
  259. 'raw_content': '<p> Le nouveau contenu </p>',
  260. }
  261. self.testapp.put_json(
  262. '/api/v2/workspaces/2/html-documents/6',
  263. params=params,
  264. status=400
  265. )
  266. def test_api__update_html_document__ok_200__nominal_case(self) -> None:
  267. """
  268. Update(put) one html document of a content
  269. """
  270. self.testapp.authorization = (
  271. 'Basic',
  272. (
  273. 'admin@admin.admin',
  274. 'admin@admin.admin'
  275. )
  276. )
  277. params = {
  278. 'label': 'My New label',
  279. 'raw_content': '<p> Le nouveau contenu </p>',
  280. }
  281. res = self.testapp.put_json(
  282. '/api/v2/workspaces/2/html-documents/6',
  283. params=params,
  284. status=200
  285. )
  286. content = res.json_body
  287. assert content['content_type'] == 'html-document'
  288. assert content['content_id'] == 6
  289. assert content['is_archived'] is False
  290. assert content['is_deleted'] is False
  291. assert content['label'] == 'My New label'
  292. assert content['parent_id'] == 3
  293. assert content['show_in_ui'] is True
  294. assert content['slug'] == 'my-new-label'
  295. assert content['status'] == 'open'
  296. assert content['workspace_id'] == 2
  297. assert content['current_revision_id'] == 28
  298. # TODO - G.M - 2018-06-173 - check date format
  299. assert content['created']
  300. assert content['author']
  301. assert content['author']['user_id'] == 1
  302. assert content['author']['avatar_url'] is None
  303. assert content['author']['public_name'] == 'Global manager'
  304. # TODO - G.M - 2018-06-173 - check date format
  305. assert content['modified']
  306. assert content['last_modifier'] == content['author']
  307. assert content['raw_content'] == '<p> Le nouveau contenu </p>'
  308. res = self.testapp.get(
  309. '/api/v2/workspaces/2/html-documents/6',
  310. status=200
  311. )
  312. content = res.json_body
  313. assert content['content_type'] == 'html-document'
  314. assert content['content_id'] == 6
  315. assert content['is_archived'] is False
  316. assert content['is_deleted'] is False
  317. assert content['label'] == 'My New label'
  318. assert content['parent_id'] == 3
  319. assert content['show_in_ui'] is True
  320. assert content['slug'] == 'my-new-label'
  321. assert content['status'] == 'open'
  322. assert content['workspace_id'] == 2
  323. assert content['current_revision_id'] == 28
  324. # TODO - G.M - 2018-06-173 - check date format
  325. assert content['created']
  326. assert content['author']
  327. assert content['author']['user_id'] == 1
  328. assert content['author']['avatar_url'] is None
  329. assert content['author']['public_name'] == 'Global manager'
  330. # TODO - G.M - 2018-06-173 - check date format
  331. assert content['modified']
  332. assert content['last_modifier'] == content['author']
  333. assert content['raw_content'] == '<p> Le nouveau contenu </p>'
  334. def test_api__get_html_document_revisions__ok_200__nominal_case(
  335. self
  336. ) -> None:
  337. """
  338. Get one html document of a content
  339. """
  340. self.testapp.authorization = (
  341. 'Basic',
  342. (
  343. 'admin@admin.admin',
  344. 'admin@admin.admin'
  345. )
  346. )
  347. res = self.testapp.get(
  348. '/api/v2/workspaces/2/html-documents/6/revisions',
  349. status=200
  350. )
  351. revisions = res.json_body
  352. assert len(revisions) == 3
  353. revision = revisions[0]
  354. assert revision['content_type'] == 'html-document'
  355. assert revision['content_id'] == 6
  356. assert revision['is_archived'] is False
  357. assert revision['is_deleted'] is False
  358. assert revision['label'] == 'Tiramisu Recipes!!!'
  359. assert revision['parent_id'] == 3
  360. assert revision['show_in_ui'] is True
  361. assert revision['slug'] == 'tiramisu-recipes'
  362. assert revision['status'] == 'open'
  363. assert revision['workspace_id'] == 2
  364. assert revision['revision_id'] == 6
  365. assert revision['revision_type'] == 'creation'
  366. assert revision['sub_content_types']
  367. # TODO - G.M - 2018-06-173 - Test with real comments
  368. assert revision['comment_ids'] == []
  369. # TODO - G.M - 2018-06-173 - check date format
  370. assert revision['created']
  371. assert revision['author']
  372. assert revision['author']['user_id'] == 1
  373. assert revision['author']['avatar_url'] is None
  374. assert revision['author']['public_name'] == 'Global manager'
  375. revision = revisions[1]
  376. assert revision['content_type'] == 'html-document'
  377. assert revision['content_id'] == 6
  378. assert revision['is_archived'] is False
  379. assert revision['is_deleted'] is False
  380. assert revision['label'] == 'Tiramisu Recipes!!!'
  381. assert revision['parent_id'] == 3
  382. assert revision['show_in_ui'] is True
  383. assert revision['slug'] == 'tiramisu-recipes'
  384. assert revision['status'] == 'open'
  385. assert revision['workspace_id'] == 2
  386. assert revision['revision_id'] == 7
  387. assert revision['revision_type'] == 'edition'
  388. assert revision['sub_content_types']
  389. # TODO - G.M - 2018-06-173 - Test with real comments
  390. assert revision['comment_ids'] == []
  391. # TODO - G.M - 2018-06-173 - check date format
  392. assert revision['created']
  393. assert revision['author']
  394. assert revision['author']['user_id'] == 1
  395. assert revision['author']['avatar_url'] is None
  396. assert revision['author']['public_name'] == 'Global manager'
  397. revision = revisions[2]
  398. assert revision['content_type'] == 'html-document'
  399. assert revision['content_id'] == 6
  400. assert revision['is_archived'] is False
  401. assert revision['is_deleted'] is False
  402. assert revision['label'] == 'Tiramisu Recipe'
  403. assert revision['parent_id'] == 3
  404. assert revision['show_in_ui'] is True
  405. assert revision['slug'] == 'tiramisu-recipe'
  406. assert revision['status'] == 'open'
  407. assert revision['workspace_id'] == 2
  408. assert revision['revision_id'] == 27
  409. assert revision['revision_type'] == 'edition'
  410. assert revision['sub_content_types']
  411. # TODO - G.M - 2018-06-173 - Test with real comments
  412. assert revision['comment_ids'] == []
  413. # TODO - G.M - 2018-06-173 - check date format
  414. assert revision['created']
  415. assert revision['author']
  416. assert revision['author']['user_id'] == 3
  417. assert revision['author']['avatar_url'] is None
  418. assert revision['author']['public_name'] == 'Bob i.'
  419. def test_api__set_html_document_status__ok_200__nominal_case(self) -> None:
  420. """
  421. Get one html document of a content
  422. """
  423. self.testapp.authorization = (
  424. 'Basic',
  425. (
  426. 'admin@admin.admin',
  427. 'admin@admin.admin'
  428. )
  429. )
  430. params = {
  431. 'status': 'closed-deprecated',
  432. }
  433. # before
  434. res = self.testapp.get(
  435. '/api/v2/workspaces/2/html-documents/6',
  436. status=200
  437. )
  438. content = res.json_body
  439. assert content['content_type'] == 'html-document'
  440. assert content['content_id'] == 6
  441. assert content['status'] == 'open'
  442. # set status
  443. self.testapp.put_json(
  444. '/api/v2/workspaces/2/html-documents/6/status',
  445. params=params,
  446. status=204
  447. )
  448. # after
  449. res = self.testapp.get(
  450. '/api/v2/workspaces/2/html-documents/6',
  451. status=200
  452. )
  453. content = res.json_body
  454. assert content['content_type'] == 'html-document'
  455. assert content['content_id'] == 6
  456. assert content['status'] == 'closed-deprecated'
  457. def test_api__set_html_document_status__err_400__wrong_status(self) -> None:
  458. """
  459. Get one html document of a content
  460. """
  461. self.testapp.authorization = (
  462. 'Basic',
  463. (
  464. 'admin@admin.admin',
  465. 'admin@admin.admin'
  466. )
  467. )
  468. params = {
  469. 'status': 'unexistant-status',
  470. }
  471. self.testapp.put_json(
  472. '/api/v2/workspaces/2/html-documents/6/status',
  473. params=params,
  474. status=400
  475. )
  476. class TestFiles(FunctionalTest):
  477. """
  478. Tests for /api/v2/workspaces/{workspace_id}/files/{content_id}
  479. endpoint
  480. """
  481. fixtures = [BaseFixture, ContentFixtures]
  482. def test_api__get_file__ok_200__nominal_case(self) -> None:
  483. """
  484. Get one file of a content
  485. """
  486. dbsession = get_tm_session(self.session_factory, transaction.manager)
  487. admin = dbsession.query(models.User) \
  488. .filter(models.User.email == 'admin@admin.admin') \
  489. .one()
  490. workspace_api = WorkspaceApi(
  491. current_user=admin,
  492. session=dbsession,
  493. config=self.app_config
  494. )
  495. content_api = ContentApi(
  496. current_user=admin,
  497. session=dbsession,
  498. config=self.app_config
  499. )
  500. business_workspace = workspace_api.get_one(1)
  501. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  502. test_file = content_api.create(
  503. content_type_slug=CONTENT_TYPES.File.slug,
  504. workspace=business_workspace,
  505. parent=tool_folder,
  506. label='Test file',
  507. do_save=False,
  508. do_notify=False,
  509. )
  510. test_file.file_extension = '.txt'
  511. test_file.depot_file = FileIntent(
  512. b'Test file',
  513. 'Test_file.txt',
  514. 'text/plain',
  515. )
  516. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  517. dbsession.flush()
  518. transaction.commit()
  519. self.testapp.authorization = (
  520. 'Basic',
  521. (
  522. 'admin@admin.admin',
  523. 'admin@admin.admin'
  524. )
  525. )
  526. res = self.testapp.get(
  527. '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
  528. status=200
  529. )
  530. content = res.json_body
  531. assert content['content_type'] == 'file'
  532. assert content['content_id'] == test_file.content_id
  533. assert content['is_archived'] is False
  534. assert content['is_deleted'] is False
  535. assert content['label'] == 'Test_file'
  536. assert content['parent_id'] == 1
  537. assert content['show_in_ui'] is True
  538. assert content['slug'] == 'test-file'
  539. assert content['status'] == 'open'
  540. assert content['workspace_id'] == 1
  541. assert content['current_revision_id']
  542. # TODO - G.M - 2018-06-173 - check date format
  543. assert content['created']
  544. assert content['author']
  545. assert content['author']['user_id'] == 1
  546. assert content['author']['avatar_url'] is None
  547. assert content['author']['public_name'] == 'Global manager'
  548. # TODO - G.M - 2018-06-173 - check date format
  549. assert content['modified']
  550. assert content['last_modifier'] == content['author']
  551. assert content['raw_content'] == '<p>description</p>' # nopep8
  552. def test_api__get_files__err_400__wrong_content_type(self) -> None:
  553. """
  554. Get one file of a content content
  555. """
  556. self.testapp.authorization = (
  557. 'Basic',
  558. (
  559. 'admin@admin.admin',
  560. 'admin@admin.admin'
  561. )
  562. )
  563. self.testapp.get(
  564. '/api/v2/workspaces/2/files/6',
  565. status=400
  566. )
  567. def test_api__get_file__err_400__content_does_not_exist(self) -> None: # nopep8
  568. """
  569. Get one file (content 170 does not exist in db
  570. """
  571. self.testapp.authorization = (
  572. 'Basic',
  573. (
  574. 'admin@admin.admin',
  575. 'admin@admin.admin'
  576. )
  577. )
  578. self.testapp.get(
  579. '/api/v2/workspaces/1/files/170',
  580. status=400
  581. )
  582. def test_api__get_file__err_400__content_not_in_workspace(self) -> None: # nopep8
  583. """
  584. Get one file (content 9 is in workspace 2)
  585. """
  586. self.testapp.authorization = (
  587. 'Basic',
  588. (
  589. 'admin@admin.admin',
  590. 'admin@admin.admin'
  591. )
  592. )
  593. self.testapp.get(
  594. '/api/v2/workspaces/1/files/9',
  595. status=400
  596. )
  597. def test_api__get_file__err_400__workspace_does_not_exist(self) -> None: # nopep8
  598. """
  599. Get one file (Workspace 40 does not exist)
  600. """
  601. self.testapp.authorization = (
  602. 'Basic',
  603. (
  604. 'admin@admin.admin',
  605. 'admin@admin.admin'
  606. )
  607. )
  608. self.testapp.get(
  609. '/api/v2/workspaces/40/files/9',
  610. status=400
  611. )
  612. def test_api__get_file__err_400__workspace_id_is_not_int(self) -> None: # nopep8
  613. """
  614. Get one file, workspace id is not int
  615. """
  616. self.testapp.authorization = (
  617. 'Basic',
  618. (
  619. 'admin@admin.admin',
  620. 'admin@admin.admin'
  621. )
  622. )
  623. self.testapp.get(
  624. '/api/v2/workspaces/coucou/files/9',
  625. status=400
  626. )
  627. def test_api__get_file__err_400__content_id_is_not_int(self) -> None: # nopep8
  628. """
  629. Get one file, content_id is not int
  630. """
  631. self.testapp.authorization = (
  632. 'Basic',
  633. (
  634. 'admin@admin.admin',
  635. 'admin@admin.admin'
  636. )
  637. )
  638. self.testapp.get(
  639. '/api/v2/workspaces/2/files/coucou',
  640. status=400
  641. )
  642. def test_api__update_file_info_err_400__empty_label(self) -> None: # nopep8
  643. """
  644. Update(put) one file
  645. """
  646. dbsession = get_tm_session(self.session_factory, transaction.manager)
  647. admin = dbsession.query(models.User) \
  648. .filter(models.User.email == 'admin@admin.admin') \
  649. .one()
  650. workspace_api = WorkspaceApi(
  651. current_user=admin,
  652. session=dbsession,
  653. config=self.app_config
  654. )
  655. content_api = ContentApi(
  656. current_user=admin,
  657. session=dbsession,
  658. config=self.app_config
  659. )
  660. business_workspace = workspace_api.get_one(1)
  661. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  662. test_file = content_api.create(
  663. content_type_slug=CONTENT_TYPES.File.slug,
  664. workspace=business_workspace,
  665. parent=tool_folder,
  666. label='Test file',
  667. do_save=False,
  668. do_notify=False,
  669. )
  670. test_file.file_extension = '.txt'
  671. test_file.depot_file = FileIntent(
  672. b'Test file',
  673. 'Test_file.txt',
  674. 'text/plain',
  675. )
  676. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  677. dbsession.flush()
  678. transaction.commit()
  679. self.testapp.authorization = (
  680. 'Basic',
  681. (
  682. 'admin@admin.admin',
  683. 'admin@admin.admin'
  684. )
  685. )
  686. params = {
  687. 'label': '',
  688. 'raw_content': '<p> Le nouveau contenu </p>',
  689. }
  690. self.testapp.put_json(
  691. '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
  692. params=params,
  693. status=400
  694. )
  695. def test_api__update_file_info__ok_200__nominal_case(self) -> None:
  696. """
  697. Update(put) one file
  698. """
  699. dbsession = get_tm_session(self.session_factory, transaction.manager)
  700. admin = dbsession.query(models.User) \
  701. .filter(models.User.email == 'admin@admin.admin') \
  702. .one()
  703. workspace_api = WorkspaceApi(
  704. current_user=admin,
  705. session=dbsession,
  706. config=self.app_config
  707. )
  708. content_api = ContentApi(
  709. current_user=admin,
  710. session=dbsession,
  711. config=self.app_config
  712. )
  713. business_workspace = workspace_api.get_one(1)
  714. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  715. test_file = content_api.create(
  716. content_type_slug=CONTENT_TYPES.File.slug,
  717. workspace=business_workspace,
  718. parent=tool_folder,
  719. label='Test file',
  720. do_save=False,
  721. do_notify=False,
  722. )
  723. test_file.file_extension = '.txt'
  724. test_file.depot_file = FileIntent(
  725. b'Test file',
  726. 'Test_file.txt',
  727. 'text/plain',
  728. )
  729. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  730. dbsession.flush()
  731. transaction.commit()
  732. self.testapp.authorization = (
  733. 'Basic',
  734. (
  735. 'admin@admin.admin',
  736. 'admin@admin.admin'
  737. )
  738. )
  739. params = {
  740. 'label': 'My New label',
  741. 'raw_content': '<p> Le nouveau contenu </p>',
  742. }
  743. res = self.testapp.put_json(
  744. '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
  745. params=params,
  746. status=200
  747. )
  748. content = res.json_body
  749. assert content['content_type'] == 'file'
  750. assert content['content_id'] == test_file.content_id
  751. assert content['is_archived'] is False
  752. assert content['is_deleted'] is False
  753. assert content['label'] == 'My New label'
  754. assert content['parent_id'] == 1
  755. assert content['show_in_ui'] is True
  756. assert content['slug'] == 'my-new-label'
  757. assert content['status'] == 'open'
  758. assert content['workspace_id'] == 1
  759. assert content['current_revision_id']
  760. # TODO - G.M - 2018-06-173 - check date format
  761. assert content['created']
  762. assert content['author']
  763. assert content['author']['user_id'] == 1
  764. assert content['author']['avatar_url'] is None
  765. assert content['author']['public_name'] == 'Global manager'
  766. # TODO - G.M - 2018-06-173 - check date format
  767. assert content['modified']
  768. assert content['last_modifier'] == content['author']
  769. assert content['raw_content'] == '<p> Le nouveau contenu </p>'
  770. res = self.testapp.get(
  771. '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
  772. status=200
  773. )
  774. content = res.json_body
  775. assert content['content_type'] == 'file'
  776. assert content['content_id'] == test_file.content_id
  777. assert content['is_archived'] is False
  778. assert content['is_deleted'] is False
  779. assert content['label'] == 'My New label'
  780. assert content['parent_id'] == 1
  781. assert content['show_in_ui'] is True
  782. assert content['slug'] == 'my-new-label'
  783. assert content['status'] == 'open'
  784. assert content['workspace_id'] == 1
  785. assert content['current_revision_id']
  786. # TODO - G.M - 2018-06-173 - check date format
  787. assert content['created']
  788. assert content['author']
  789. assert content['author']['user_id'] == 1
  790. assert content['author']['avatar_url'] is None
  791. assert content['author']['public_name'] == 'Global manager'
  792. # TODO - G.M - 2018-06-173 - check date format
  793. assert content['modified']
  794. assert content['last_modifier'] == content['author']
  795. assert content['raw_content'] == '<p> Le nouveau contenu </p>'
  796. def test_api__get_file_revisions__ok_200__nominal_case(
  797. self
  798. ) -> None:
  799. """
  800. Get file revisions
  801. """
  802. dbsession = get_tm_session(self.session_factory, transaction.manager)
  803. admin = dbsession.query(models.User) \
  804. .filter(models.User.email == 'admin@admin.admin') \
  805. .one()
  806. workspace_api = WorkspaceApi(
  807. current_user=admin,
  808. session=dbsession,
  809. config=self.app_config
  810. )
  811. content_api = ContentApi(
  812. current_user=admin,
  813. session=dbsession,
  814. config=self.app_config
  815. )
  816. business_workspace = workspace_api.get_one(1)
  817. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  818. test_file = content_api.create(
  819. content_type_slug=CONTENT_TYPES.File.slug,
  820. workspace=business_workspace,
  821. parent=tool_folder,
  822. label='Test file',
  823. do_save=False,
  824. do_notify=False,
  825. )
  826. test_file.file_extension = '.txt'
  827. test_file.depot_file = FileIntent(
  828. b'Test file',
  829. 'Test_file.txt',
  830. 'text/plain',
  831. )
  832. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  833. dbsession.flush()
  834. transaction.commit()
  835. self.testapp.authorization = (
  836. 'Basic',
  837. (
  838. 'admin@admin.admin',
  839. 'admin@admin.admin'
  840. )
  841. )
  842. res = self.testapp.get(
  843. '/api/v2/workspaces/1/files/{}/revisions'.format(test_file.content_id), # nopep8
  844. status=200
  845. )
  846. revisions = res.json_body
  847. assert len(revisions) == 1
  848. revision = revisions[0]
  849. assert revision['content_type'] == 'file'
  850. assert revision['content_id'] == test_file.content_id
  851. assert revision['is_archived'] is False
  852. assert revision['is_deleted'] is False
  853. assert revision['label'] == 'Test_file'
  854. assert revision['parent_id'] == 1
  855. assert revision['show_in_ui'] is True
  856. assert revision['slug'] == 'test-file'
  857. assert revision['status'] == 'open'
  858. assert revision['workspace_id'] == 1
  859. assert revision['revision_id']
  860. assert revision['sub_content_types']
  861. # TODO - G.M - 2018-06-173 - Test with real comments
  862. assert revision['comment_ids'] == []
  863. # TODO - G.M - 2018-06-173 - check date format
  864. assert revision['created']
  865. assert revision['author']
  866. assert revision['author']['user_id'] == 1
  867. assert revision['author']['avatar_url'] is None
  868. assert revision['author']['public_name'] == 'Global manager'
  869. def test_api__set_file_status__ok_200__nominal_case(self) -> None:
  870. """
  871. set file status
  872. """
  873. dbsession = get_tm_session(self.session_factory, transaction.manager)
  874. admin = dbsession.query(models.User) \
  875. .filter(models.User.email == 'admin@admin.admin') \
  876. .one()
  877. workspace_api = WorkspaceApi(
  878. current_user=admin,
  879. session=dbsession,
  880. config=self.app_config
  881. )
  882. content_api = ContentApi(
  883. current_user=admin,
  884. session=dbsession,
  885. config=self.app_config
  886. )
  887. business_workspace = workspace_api.get_one(1)
  888. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  889. test_file = content_api.create(
  890. content_type_slug=CONTENT_TYPES.File.slug,
  891. workspace=business_workspace,
  892. parent=tool_folder,
  893. label='Test file',
  894. do_save=False,
  895. do_notify=False,
  896. )
  897. test_file.file_extension = '.txt'
  898. test_file.depot_file = FileIntent(
  899. b'Test file',
  900. 'Test_file.txt',
  901. 'text/plain',
  902. )
  903. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  904. dbsession.flush()
  905. transaction.commit()
  906. self.testapp.authorization = (
  907. 'Basic',
  908. (
  909. 'admin@admin.admin',
  910. 'admin@admin.admin'
  911. )
  912. )
  913. params = {
  914. 'status': 'closed-deprecated',
  915. }
  916. # before
  917. res = self.testapp.get(
  918. '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
  919. status=200
  920. )
  921. content = res.json_body
  922. assert content['content_type'] == 'file'
  923. assert content['content_id'] == test_file.content_id
  924. assert content['status'] == 'open'
  925. # set status
  926. self.testapp.put_json(
  927. '/api/v2/workspaces/1/files/{}/status'.format(test_file.content_id),
  928. params=params,
  929. status=204
  930. )
  931. # after
  932. res = self.testapp.get(
  933. '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
  934. status=200
  935. )
  936. content = res.json_body
  937. assert content['content_type'] == 'file'
  938. assert content['content_id'] == test_file.content_id
  939. assert content['status'] == 'closed-deprecated'
  940. def test_api__set_file_status__err_400__wrong_status(self) -> None:
  941. """
  942. set file status
  943. """
  944. self.testapp.authorization = (
  945. 'Basic',
  946. (
  947. 'admin@admin.admin',
  948. 'admin@admin.admin'
  949. )
  950. )
  951. params = {
  952. 'status': 'unexistant-status',
  953. }
  954. self.testapp.put_json(
  955. '/api/v2/workspaces/2/files/6/status',
  956. params=params,
  957. status=400
  958. )
  959. def test_api__get_file_raw__ok_200__nominal_case(self) -> None:
  960. """
  961. Get one file of a content
  962. """
  963. dbsession = get_tm_session(self.session_factory, transaction.manager)
  964. admin = dbsession.query(models.User) \
  965. .filter(models.User.email == 'admin@admin.admin') \
  966. .one()
  967. workspace_api = WorkspaceApi(
  968. current_user=admin,
  969. session=dbsession,
  970. config=self.app_config
  971. )
  972. content_api = ContentApi(
  973. current_user=admin,
  974. session=dbsession,
  975. config=self.app_config
  976. )
  977. business_workspace = workspace_api.get_one(1)
  978. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  979. test_file = content_api.create(
  980. content_type_slug=CONTENT_TYPES.File.slug,
  981. workspace=business_workspace,
  982. parent=tool_folder,
  983. label='Test file',
  984. do_save=False,
  985. do_notify=False,
  986. )
  987. test_file.file_extension = '.txt'
  988. test_file.depot_file = FileIntent(
  989. b'Test file',
  990. 'Test_file.txt',
  991. 'text/plain',
  992. )
  993. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  994. dbsession.flush()
  995. transaction.commit()
  996. content_id = int(test_file.content_id)
  997. self.testapp.authorization = (
  998. 'Basic',
  999. (
  1000. 'admin@admin.admin',
  1001. 'admin@admin.admin'
  1002. )
  1003. )
  1004. res = self.testapp.get(
  1005. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  1006. status=200
  1007. )
  1008. assert res.body == b'Test file'
  1009. assert res.content_type == 'text/plain'
  1010. assert res.content_length == len(b'Test file')
  1011. def test_api__set_file_raw__ok_200__nominal_case(self) -> None:
  1012. """
  1013. Set one file of a content
  1014. """
  1015. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1016. admin = dbsession.query(models.User) \
  1017. .filter(models.User.email == 'admin@admin.admin') \
  1018. .one()
  1019. workspace_api = WorkspaceApi(
  1020. current_user=admin,
  1021. session=dbsession,
  1022. config=self.app_config
  1023. )
  1024. content_api = ContentApi(
  1025. current_user=admin,
  1026. session=dbsession,
  1027. config=self.app_config
  1028. )
  1029. business_workspace = workspace_api.get_one(1)
  1030. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1031. test_file = content_api.create(
  1032. content_type_slug=CONTENT_TYPES.File.slug,
  1033. workspace=business_workspace,
  1034. parent=tool_folder,
  1035. label='Test file',
  1036. do_save=True,
  1037. do_notify=False,
  1038. )
  1039. dbsession.flush()
  1040. transaction.commit()
  1041. content_id = int(test_file.content_id)
  1042. image = create_1000px_png_test_image()
  1043. self.testapp.authorization = (
  1044. 'Basic',
  1045. (
  1046. 'admin@admin.admin',
  1047. 'admin@admin.admin'
  1048. )
  1049. )
  1050. self.testapp.put(
  1051. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  1052. upload_files=[
  1053. ('files', image.name, image.getvalue())
  1054. ],
  1055. status=204,
  1056. )
  1057. res = self.testapp.get(
  1058. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  1059. status=200
  1060. )
  1061. assert res.body == image.getvalue()
  1062. assert res.content_type == 'image/png'
  1063. assert res.content_length == len(image.getvalue())
  1064. def test_api__get_allowed_size_dim__ok__nominal_case(self) -> None:
  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=CONTENT_TYPES.Any_SLUG)
  1081. test_file = content_api.create(
  1082. content_type_slug=CONTENT_TYPES.File.slug,
  1083. workspace=business_workspace,
  1084. parent=tool_folder,
  1085. label='Test file',
  1086. do_save=False,
  1087. do_notify=False,
  1088. )
  1089. test_file.file_extension = '.txt'
  1090. test_file.depot_file = FileIntent(
  1091. b'Test file',
  1092. 'Test_file.txt',
  1093. 'text/plain',
  1094. )
  1095. dbsession.flush()
  1096. transaction.commit()
  1097. self.testapp.authorization = (
  1098. 'Basic',
  1099. (
  1100. 'admin@admin.admin',
  1101. 'admin@admin.admin'
  1102. )
  1103. )
  1104. content_id = int(test_file.content_id)
  1105. res = self.testapp.get(
  1106. '/api/v2/workspaces/1/files/{}/preview/jpg/allowed_dims'.format(content_id), # nopep8
  1107. status=200,
  1108. )
  1109. res = res.json_body
  1110. assert res['restricted'] is True
  1111. assert len(res['dimensions']) == 1
  1112. dim = res['dimensions'][0]
  1113. assert dim['width'] == 256
  1114. assert dim['height'] == 256
  1115. def test_api__get_jpeg_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=CONTENT_TYPES.Any_SLUG)
  1135. test_file = content_api.create(
  1136. content_type_slug=CONTENT_TYPES.File.slug,
  1137. workspace=business_workspace,
  1138. parent=tool_folder,
  1139. label='Test file',
  1140. do_save=False,
  1141. do_notify=False,
  1142. )
  1143. test_file.file_extension = '.txt'
  1144. test_file.depot_file = FileIntent(
  1145. b'Test file',
  1146. 'Test_file.txt',
  1147. 'text/plain',
  1148. )
  1149. dbsession.flush()
  1150. transaction.commit()
  1151. content_id = int(test_file.content_id)
  1152. image = create_1000px_png_test_image()
  1153. self.testapp.authorization = (
  1154. 'Basic',
  1155. (
  1156. 'admin@admin.admin',
  1157. 'admin@admin.admin'
  1158. )
  1159. )
  1160. self.testapp.put(
  1161. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  1162. upload_files=[
  1163. ('files', image.name, image.getvalue())
  1164. ],
  1165. status=204,
  1166. )
  1167. res = self.testapp.get(
  1168. '/api/v2/workspaces/1/files/{}/preview/jpg'.format(content_id),
  1169. status=200
  1170. )
  1171. assert res.body != image.getvalue()
  1172. assert res.content_type == 'image/jpeg'
  1173. def test_api__get_sized_jpeg_preview__ok__200__nominal_case(self) -> None:
  1174. """
  1175. get 256x256 preview of a txt file
  1176. """
  1177. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1178. admin = dbsession.query(models.User) \
  1179. .filter(models.User.email == 'admin@admin.admin') \
  1180. .one()
  1181. workspace_api = WorkspaceApi(
  1182. current_user=admin,
  1183. session=dbsession,
  1184. config=self.app_config
  1185. )
  1186. content_api = ContentApi(
  1187. current_user=admin,
  1188. session=dbsession,
  1189. config=self.app_config
  1190. )
  1191. business_workspace = workspace_api.get_one(1)
  1192. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1193. test_file = content_api.create(
  1194. content_type_slug=CONTENT_TYPES.File.slug,
  1195. workspace=business_workspace,
  1196. parent=tool_folder,
  1197. label='Test file',
  1198. do_save=True,
  1199. do_notify=False,
  1200. )
  1201. dbsession.flush()
  1202. transaction.commit()
  1203. content_id = int(test_file.content_id)
  1204. image = create_1000px_png_test_image()
  1205. self.testapp.authorization = (
  1206. 'Basic',
  1207. (
  1208. 'admin@admin.admin',
  1209. 'admin@admin.admin'
  1210. )
  1211. )
  1212. self.testapp.put(
  1213. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  1214. upload_files=[
  1215. ('files', image.name, image.getvalue())
  1216. ],
  1217. status=204,
  1218. )
  1219. res = self.testapp.get(
  1220. '/api/v2/workspaces/1/files/{}/preview/jpg/256x256'.format(content_id), # nopep8
  1221. status=200
  1222. )
  1223. assert res.body != image.getvalue()
  1224. assert res.content_type == 'image/jpeg'
  1225. new_image = Image.open(io.BytesIO(res.body))
  1226. assert 256, 256 == new_image.size
  1227. def test_api__get_sized_jpeg_preview__err__400__SizeNotAllowed(self) -> None: # nopep8
  1228. """
  1229. get 256x256 preview of a txt file
  1230. """
  1231. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1232. admin = dbsession.query(models.User) \
  1233. .filter(models.User.email == 'admin@admin.admin') \
  1234. .one()
  1235. workspace_api = WorkspaceApi(
  1236. current_user=admin,
  1237. session=dbsession,
  1238. config=self.app_config
  1239. )
  1240. content_api = ContentApi(
  1241. current_user=admin,
  1242. session=dbsession,
  1243. config=self.app_config
  1244. )
  1245. business_workspace = workspace_api.get_one(1)
  1246. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1247. test_file = content_api.create(
  1248. content_type_slug=CONTENT_TYPES.File.slug,
  1249. workspace=business_workspace,
  1250. parent=tool_folder,
  1251. label='Test file',
  1252. do_save=True,
  1253. do_notify=False,
  1254. )
  1255. dbsession.flush()
  1256. transaction.commit()
  1257. content_id = int(test_file.content_id)
  1258. image = create_1000px_png_test_image()
  1259. self.testapp.authorization = (
  1260. 'Basic',
  1261. (
  1262. 'admin@admin.admin',
  1263. 'admin@admin.admin'
  1264. )
  1265. )
  1266. self.testapp.put(
  1267. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  1268. upload_files=[
  1269. ('files', image.name, image.getvalue())
  1270. ],
  1271. status=204,
  1272. )
  1273. self.testapp.get(
  1274. '/api/v2/workspaces/1/files/{}/preview/jpg/512x512'.format(content_id), # nopep8
  1275. status=400
  1276. )
  1277. def test_api__get_sized_jpeg_revision_preview__ok__200__nominal_case(self) -> None: # nopep8
  1278. """
  1279. get 256x256 revision preview of a txt file
  1280. """
  1281. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1282. admin = dbsession.query(models.User) \
  1283. .filter(models.User.email == 'admin@admin.admin') \
  1284. .one()
  1285. workspace_api = WorkspaceApi(
  1286. current_user=admin,
  1287. session=dbsession,
  1288. config=self.app_config
  1289. )
  1290. content_api = ContentApi(
  1291. current_user=admin,
  1292. session=dbsession,
  1293. config=self.app_config
  1294. )
  1295. business_workspace = workspace_api.get_one(1)
  1296. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1297. test_file = content_api.create(
  1298. content_type_slug=CONTENT_TYPES.File.slug,
  1299. workspace=business_workspace,
  1300. parent=tool_folder,
  1301. label='Test file',
  1302. do_save=False,
  1303. do_notify=False,
  1304. )
  1305. test_file.file_extension = '.txt'
  1306. test_file.depot_file = FileIntent(
  1307. b'Test file',
  1308. 'Test_file.txt',
  1309. 'text/plain',
  1310. )
  1311. dbsession.flush()
  1312. transaction.commit()
  1313. content_id = int(test_file.content_id)
  1314. revision_id = int(test_file.revision_id)
  1315. image = create_1000px_png_test_image()
  1316. self.testapp.authorization = (
  1317. 'Basic',
  1318. (
  1319. 'admin@admin.admin',
  1320. 'admin@admin.admin'
  1321. )
  1322. )
  1323. self.testapp.put(
  1324. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  1325. upload_files=[
  1326. ('files', image.name, image.getvalue())
  1327. ],
  1328. status=204,
  1329. )
  1330. res = self.testapp.get(
  1331. '/api/v2/workspaces/1/files/{content_id}/revisions/{revision_id}/raw'.format( # nopep8
  1332. content_id=content_id,
  1333. revision_id=revision_id,
  1334. ),
  1335. status=200
  1336. )
  1337. assert res.content_type == 'text/plain'
  1338. res = self.testapp.get(
  1339. '/api/v2/workspaces/1/files/{content_id}/revisions/{revision_id}/preview/jpg/256x256'.format( # nopep8
  1340. content_id=content_id,
  1341. revision_id=revision_id,
  1342. ),
  1343. status=200
  1344. )
  1345. assert res.body != image.getvalue()
  1346. assert res.content_type == 'image/jpeg'
  1347. new_image = Image.open(io.BytesIO(res.body))
  1348. assert 256, 256 == new_image.size
  1349. def test_api__get_full_pdf_preview__ok__200__nominal_case(self) -> None:
  1350. """
  1351. get full pdf preview of a txt file
  1352. """
  1353. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1354. admin = dbsession.query(models.User) \
  1355. .filter(models.User.email == 'admin@admin.admin') \
  1356. .one()
  1357. workspace_api = WorkspaceApi(
  1358. current_user=admin,
  1359. session=dbsession,
  1360. config=self.app_config
  1361. )
  1362. content_api = ContentApi(
  1363. current_user=admin,
  1364. session=dbsession,
  1365. config=self.app_config
  1366. )
  1367. business_workspace = workspace_api.get_one(1)
  1368. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1369. test_file = content_api.create(
  1370. content_type_slug=CONTENT_TYPES.File.slug,
  1371. workspace=business_workspace,
  1372. parent=tool_folder,
  1373. label='Test file',
  1374. do_save=True,
  1375. do_notify=False,
  1376. )
  1377. with new_revision(
  1378. session=dbsession,
  1379. tm=transaction.manager,
  1380. content=test_file,
  1381. ):
  1382. test_file.file_extension = '.txt'
  1383. test_file.depot_file = FileIntent(
  1384. b'Test file',
  1385. 'Test_file.txt',
  1386. 'text/plain',
  1387. )
  1388. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  1389. dbsession.flush()
  1390. transaction.commit()
  1391. content_id = int(test_file.content_id)
  1392. self.testapp.authorization = (
  1393. 'Basic',
  1394. (
  1395. 'admin@admin.admin',
  1396. 'admin@admin.admin'
  1397. )
  1398. )
  1399. self.testapp.put(
  1400. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  1401. upload_files=[
  1402. ('files', test_file.file_name, test_file.depot_file.file.read())
  1403. ],
  1404. status=204,
  1405. )
  1406. res = self.testapp.get(
  1407. '/api/v2/workspaces/1/files/{}/preview/pdf/full'.format(content_id), # nopep8
  1408. status=200
  1409. )
  1410. assert res.content_type == 'application/pdf'
  1411. def test_api__get_full_pdf_preview__err__400__png_UnavailablePreviewType(self) -> None: # nopep8
  1412. """
  1413. get full pdf preview of a png image -> error UnavailablePreviewType
  1414. """
  1415. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1416. admin = dbsession.query(models.User) \
  1417. .filter(models.User.email == 'admin@admin.admin') \
  1418. .one()
  1419. workspace_api = WorkspaceApi(
  1420. current_user=admin,
  1421. session=dbsession,
  1422. config=self.app_config
  1423. )
  1424. content_api = ContentApi(
  1425. current_user=admin,
  1426. session=dbsession,
  1427. config=self.app_config
  1428. )
  1429. business_workspace = workspace_api.get_one(1)
  1430. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1431. test_file = content_api.create(
  1432. content_type_slug=CONTENT_TYPES.File.slug,
  1433. workspace=business_workspace,
  1434. parent=tool_folder,
  1435. label='Test file',
  1436. do_save=True,
  1437. do_notify=False,
  1438. )
  1439. dbsession.flush()
  1440. transaction.commit()
  1441. content_id = int(test_file.content_id)
  1442. image = create_1000px_png_test_image()
  1443. self.testapp.authorization = (
  1444. 'Basic',
  1445. (
  1446. 'admin@admin.admin',
  1447. 'admin@admin.admin'
  1448. )
  1449. )
  1450. self.testapp.put(
  1451. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  1452. upload_files=[
  1453. ('files', image.name, image.getvalue())
  1454. ],
  1455. status=204,
  1456. )
  1457. self.testapp.get(
  1458. '/api/v2/workspaces/1/files/{}/preview/pdf/full'.format(content_id), # nopep8
  1459. status=400
  1460. )
  1461. def test_api__get_pdf_preview__ok__200__nominal_case(self) -> None:
  1462. """
  1463. get full pdf preview of a txt file
  1464. """
  1465. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1466. admin = dbsession.query(models.User) \
  1467. .filter(models.User.email == 'admin@admin.admin') \
  1468. .one()
  1469. workspace_api = WorkspaceApi(
  1470. current_user=admin,
  1471. session=dbsession,
  1472. config=self.app_config
  1473. )
  1474. content_api = ContentApi(
  1475. current_user=admin,
  1476. session=dbsession,
  1477. config=self.app_config
  1478. )
  1479. business_workspace = workspace_api.get_one(1)
  1480. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1481. test_file = content_api.create(
  1482. content_type_slug=CONTENT_TYPES.File.slug,
  1483. workspace=business_workspace,
  1484. parent=tool_folder,
  1485. label='Test file',
  1486. do_save=True,
  1487. do_notify=False,
  1488. )
  1489. with new_revision(
  1490. session=dbsession,
  1491. tm=transaction.manager,
  1492. content=test_file,
  1493. ):
  1494. test_file.file_extension = '.txt'
  1495. test_file.depot_file = FileIntent(
  1496. b'Test file',
  1497. 'Test_file.txt',
  1498. 'text/plain',
  1499. )
  1500. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  1501. dbsession.flush()
  1502. transaction.commit()
  1503. content_id = int(test_file.content_id)
  1504. self.testapp.authorization = (
  1505. 'Basic',
  1506. (
  1507. 'admin@admin.admin',
  1508. 'admin@admin.admin'
  1509. )
  1510. )
  1511. self.testapp.put(
  1512. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  1513. upload_files=[
  1514. ('files', test_file.file_name, test_file.depot_file.file.read())
  1515. ],
  1516. status=204,
  1517. )
  1518. params = {'page': 0}
  1519. res = self.testapp.get(
  1520. '/api/v2/workspaces/1/files/{}/preview/pdf'.format(content_id),
  1521. status=200,
  1522. params=params,
  1523. )
  1524. assert res.content_type == 'application/pdf'
  1525. def test_api__get_pdf_preview__ok__err__400_page_of_preview_not_found(self) -> None: # nopep8
  1526. """
  1527. get full pdf preview of a txt file
  1528. """
  1529. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1530. admin = dbsession.query(models.User) \
  1531. .filter(models.User.email == 'admin@admin.admin') \
  1532. .one()
  1533. workspace_api = WorkspaceApi(
  1534. current_user=admin,
  1535. session=dbsession,
  1536. config=self.app_config
  1537. )
  1538. content_api = ContentApi(
  1539. current_user=admin,
  1540. session=dbsession,
  1541. config=self.app_config
  1542. )
  1543. business_workspace = workspace_api.get_one(1)
  1544. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1545. test_file = content_api.create(
  1546. content_type_slug=CONTENT_TYPES.File.slug,
  1547. workspace=business_workspace,
  1548. parent=tool_folder,
  1549. label='Test file',
  1550. do_save=True,
  1551. do_notify=False,
  1552. )
  1553. with new_revision(
  1554. session=dbsession,
  1555. tm=transaction.manager,
  1556. content=test_file,
  1557. ):
  1558. test_file.file_extension = '.txt'
  1559. test_file.depot_file = FileIntent(
  1560. b'Test file',
  1561. 'Test_file.txt',
  1562. 'text/plain',
  1563. )
  1564. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  1565. dbsession.flush()
  1566. transaction.commit()
  1567. content_id = int(test_file.content_id)
  1568. self.testapp.authorization = (
  1569. 'Basic',
  1570. (
  1571. 'admin@admin.admin',
  1572. 'admin@admin.admin'
  1573. )
  1574. )
  1575. self.testapp.put(
  1576. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  1577. upload_files=[
  1578. ('files', test_file.file_name, test_file.depot_file.file.read())
  1579. ],
  1580. status=204,
  1581. )
  1582. params = {'page': 1}
  1583. self.testapp.get(
  1584. '/api/v2/workspaces/1/files/{}/preview/pdf'.format(content_id),
  1585. status=400,
  1586. params=params,
  1587. )
  1588. def test_api__get_pdf_revision_preview__ok__200__nominal_case(self) -> None:
  1589. """
  1590. get pdf revision preview of content
  1591. """
  1592. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1593. admin = dbsession.query(models.User) \
  1594. .filter(models.User.email == 'admin@admin.admin') \
  1595. .one()
  1596. workspace_api = WorkspaceApi(
  1597. current_user=admin,
  1598. session=dbsession,
  1599. config=self.app_config
  1600. )
  1601. content_api = ContentApi(
  1602. current_user=admin,
  1603. session=dbsession,
  1604. config=self.app_config
  1605. )
  1606. business_workspace = workspace_api.get_one(1)
  1607. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1608. test_file = content_api.create(
  1609. content_type_slug=CONTENT_TYPES.File.slug,
  1610. workspace=business_workspace,
  1611. parent=tool_folder,
  1612. label='Test file',
  1613. do_save=False,
  1614. do_notify=False,
  1615. )
  1616. test_file.file_extension = '.txt'
  1617. test_file.depot_file = FileIntent(
  1618. b'Test file',
  1619. 'Test_file.txt',
  1620. 'text/plain',
  1621. )
  1622. dbsession.flush()
  1623. transaction.commit()
  1624. content_id = int(test_file.content_id)
  1625. revision_id = int(test_file.revision_id)
  1626. image = create_1000px_png_test_image()
  1627. self.testapp.authorization = (
  1628. 'Basic',
  1629. (
  1630. 'admin@admin.admin',
  1631. 'admin@admin.admin'
  1632. )
  1633. )
  1634. self.testapp.put(
  1635. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  1636. upload_files=[
  1637. ('files', image.name, image.getvalue())
  1638. ],
  1639. status=204,
  1640. )
  1641. res = self.testapp.get(
  1642. '/api/v2/workspaces/1/files/{content_id}/revisions/{revision_id}/raw'.format( # nopep8
  1643. content_id=content_id,
  1644. revision_id=revision_id,
  1645. ),
  1646. status=200
  1647. )
  1648. assert res.content_type == 'text/plain'
  1649. params = {'page': 0}
  1650. res = self.testapp.get(
  1651. '/api/v2/workspaces/1/files/{content_id}/revisions/{revision_id}/preview/pdf'.format( # nopep8
  1652. content_id=content_id,
  1653. revision_id=revision_id,
  1654. params=params,
  1655. ),
  1656. status=200
  1657. )
  1658. assert res.content_type == 'application/pdf'
  1659. class TestThreads(FunctionalTest):
  1660. """
  1661. Tests for /api/v2/workspaces/{workspace_id}/threads/{content_id}
  1662. endpoint
  1663. """
  1664. fixtures = [BaseFixture, ContentFixtures]
  1665. def test_api__get_thread__err_400__wrong_content_type(self) -> None:
  1666. """
  1667. Get one html document of a content
  1668. """
  1669. self.testapp.authorization = (
  1670. 'Basic',
  1671. (
  1672. 'admin@admin.admin',
  1673. 'admin@admin.admin'
  1674. )
  1675. )
  1676. self.testapp.get(
  1677. '/api/v2/workspaces/2/threads/6',
  1678. status=400
  1679. )
  1680. def test_api__get_thread__ok_200__nominal_case(self) -> None:
  1681. """
  1682. Get one html document of a content
  1683. """
  1684. self.testapp.authorization = (
  1685. 'Basic',
  1686. (
  1687. 'admin@admin.admin',
  1688. 'admin@admin.admin'
  1689. )
  1690. )
  1691. res = self.testapp.get(
  1692. '/api/v2/workspaces/2/threads/7',
  1693. status=200
  1694. ) # nopep8
  1695. content = res.json_body
  1696. assert content['content_type'] == 'thread'
  1697. assert content['content_id'] == 7
  1698. assert content['is_archived'] is False
  1699. assert content['is_deleted'] is False
  1700. assert content['label'] == 'Best Cakes?'
  1701. assert content['parent_id'] == 3
  1702. assert content['show_in_ui'] is True
  1703. assert content['slug'] == 'best-cakes'
  1704. assert content['status'] == 'open'
  1705. assert content['workspace_id'] == 2
  1706. assert content['current_revision_id'] == 26
  1707. # TODO - G.M - 2018-06-173 - check date format
  1708. assert content['created']
  1709. assert content['author']
  1710. assert content['author']['user_id'] == 1
  1711. assert content['author']['avatar_url'] is None
  1712. assert content['author']['public_name'] == 'Global manager'
  1713. # TODO - G.M - 2018-06-173 - check date format
  1714. assert content['modified']
  1715. assert content['last_modifier'] != content['author']
  1716. assert content['last_modifier']['user_id'] == 3
  1717. assert content['last_modifier']['public_name'] == 'Bob i.'
  1718. assert content['last_modifier']['avatar_url'] is None
  1719. assert content['raw_content'] == 'What is the best cake?' # nopep8
  1720. def test_api__get_thread__err_400__content_does_not_exist(self) -> None:
  1721. """
  1722. Get one thread (content 170 does not exist)
  1723. """
  1724. self.testapp.authorization = (
  1725. 'Basic',
  1726. (
  1727. 'admin@admin.admin',
  1728. 'admin@admin.admin'
  1729. )
  1730. )
  1731. self.testapp.get(
  1732. '/api/v2/workspaces/2/threads/170',
  1733. status=400
  1734. )
  1735. def test_api__get_thread__err_400__content_not_in_workspace(self) -> None:
  1736. """
  1737. Get one thread(content 7 is in workspace 2)
  1738. """
  1739. self.testapp.authorization = (
  1740. 'Basic',
  1741. (
  1742. 'admin@admin.admin',
  1743. 'admin@admin.admin'
  1744. )
  1745. )
  1746. self.testapp.get(
  1747. '/api/v2/workspaces/1/threads/7',
  1748. status=400
  1749. )
  1750. def test_api__get_thread__err_400__workspace_does_not_exist(self) -> None: # nopep8
  1751. """
  1752. Get one thread (Workspace 40 does not exist)
  1753. """
  1754. self.testapp.authorization = (
  1755. 'Basic',
  1756. (
  1757. 'admin@admin.admin',
  1758. 'admin@admin.admin'
  1759. )
  1760. )
  1761. self.testapp.get(
  1762. '/api/v2/workspaces/40/threads/7',
  1763. status=400
  1764. )
  1765. def test_api__get_thread__err_400__workspace_id_is_not_int(self) -> None: # nopep8
  1766. """
  1767. Get one thread, workspace id is not int
  1768. """
  1769. self.testapp.authorization = (
  1770. 'Basic',
  1771. (
  1772. 'admin@admin.admin',
  1773. 'admin@admin.admin'
  1774. )
  1775. )
  1776. self.testapp.get(
  1777. '/api/v2/workspaces/coucou/threads/7',
  1778. status=400
  1779. )
  1780. def test_api__get_thread__err_400_content_id_is_not_int(self) -> None: # nopep8
  1781. """
  1782. Get one thread, content id is not int
  1783. """
  1784. self.testapp.authorization = (
  1785. 'Basic',
  1786. (
  1787. 'admin@admin.admin',
  1788. 'admin@admin.admin'
  1789. )
  1790. )
  1791. self.testapp.get(
  1792. '/api/v2/workspaces/2/threads/coucou',
  1793. status=400
  1794. )
  1795. def test_api__update_thread__ok_200__nominal_case(self) -> None:
  1796. """
  1797. Update(put) thread
  1798. """
  1799. self.testapp.authorization = (
  1800. 'Basic',
  1801. (
  1802. 'admin@admin.admin',
  1803. 'admin@admin.admin'
  1804. )
  1805. )
  1806. params = {
  1807. 'label': 'My New label',
  1808. 'raw_content': '<p> Le nouveau contenu </p>',
  1809. }
  1810. res = self.testapp.put_json(
  1811. '/api/v2/workspaces/2/threads/7',
  1812. params=params,
  1813. status=200
  1814. )
  1815. content = res.json_body
  1816. assert content['content_type'] == 'thread'
  1817. assert content['content_id'] == 7
  1818. assert content['is_archived'] is False
  1819. assert content['is_deleted'] is False
  1820. assert content['label'] == 'My New label'
  1821. assert content['parent_id'] == 3
  1822. assert content['show_in_ui'] is True
  1823. assert content['slug'] == 'my-new-label'
  1824. assert content['status'] == 'open'
  1825. assert content['workspace_id'] == 2
  1826. assert content['current_revision_id'] == 28
  1827. # TODO - G.M - 2018-06-173 - check date format
  1828. assert content['created']
  1829. assert content['author']
  1830. assert content['author']['user_id'] == 1
  1831. assert content['author']['avatar_url'] is None
  1832. assert content['author']['public_name'] == 'Global manager'
  1833. # TODO - G.M - 2018-06-173 - check date format
  1834. assert content['modified']
  1835. assert content['last_modifier'] == content['author']
  1836. assert content['raw_content'] == '<p> Le nouveau contenu </p>'
  1837. res = self.testapp.get(
  1838. '/api/v2/workspaces/2/threads/7',
  1839. status=200
  1840. ) # nopep8
  1841. content = res.json_body
  1842. assert content['content_type'] == 'thread'
  1843. assert content['content_id'] == 7
  1844. assert content['is_archived'] is False
  1845. assert content['is_deleted'] is False
  1846. assert content['label'] == 'My New label'
  1847. assert content['parent_id'] == 3
  1848. assert content['show_in_ui'] is True
  1849. assert content['slug'] == 'my-new-label'
  1850. assert content['status'] == 'open'
  1851. assert content['workspace_id'] == 2
  1852. assert content['current_revision_id'] == 28
  1853. # TODO - G.M - 2018-06-173 - check date format
  1854. assert content['created']
  1855. assert content['author']
  1856. assert content['author']['user_id'] == 1
  1857. assert content['author']['avatar_url'] is None
  1858. assert content['author']['public_name'] == 'Global manager'
  1859. # TODO - G.M - 2018-06-173 - check date format
  1860. assert content['modified']
  1861. assert content['last_modifier'] == content['author']
  1862. assert content['raw_content'] == '<p> Le nouveau contenu </p>'
  1863. def test_api__update_thread__err_400__empty_label(self) -> None:
  1864. """
  1865. Update(put) thread
  1866. """
  1867. self.testapp.authorization = (
  1868. 'Basic',
  1869. (
  1870. 'admin@admin.admin',
  1871. 'admin@admin.admin'
  1872. )
  1873. )
  1874. params = {
  1875. 'label': '',
  1876. 'raw_content': '<p> Le nouveau contenu </p>',
  1877. }
  1878. self.testapp.put_json(
  1879. '/api/v2/workspaces/2/threads/7',
  1880. params=params,
  1881. status=400
  1882. )
  1883. def test_api__get_thread_revisions__ok_200__nominal_case(
  1884. self
  1885. ) -> None:
  1886. """
  1887. Get threads revisions
  1888. """
  1889. self.testapp.authorization = (
  1890. 'Basic',
  1891. (
  1892. 'admin@admin.admin',
  1893. 'admin@admin.admin'
  1894. )
  1895. )
  1896. res = self.testapp.get(
  1897. '/api/v2/workspaces/2/threads/7/revisions',
  1898. status=200
  1899. )
  1900. revisions = res.json_body
  1901. assert len(revisions) == 2
  1902. revision = revisions[0]
  1903. assert revision['content_type'] == 'thread'
  1904. assert revision['content_id'] == 7
  1905. assert revision['is_archived'] is False
  1906. assert revision['is_deleted'] is False
  1907. assert revision['label'] == 'Best Cake'
  1908. assert revision['parent_id'] == 3
  1909. assert revision['show_in_ui'] is True
  1910. assert revision['slug'] == 'best-cake'
  1911. assert revision['status'] == 'open'
  1912. assert revision['workspace_id'] == 2
  1913. assert revision['revision_id'] == 8
  1914. assert revision['sub_content_types']
  1915. assert revision['revision_type'] == 'creation'
  1916. assert revision['comment_ids'] == [18, 19, 20]
  1917. # TODO - G.M - 2018-06-173 - check date format
  1918. assert revision['created']
  1919. assert revision['author']
  1920. assert revision['author']['user_id'] == 1
  1921. assert revision['author']['avatar_url'] is None
  1922. assert revision['author']['public_name'] == 'Global manager'
  1923. revision = revisions[1]
  1924. assert revision['content_type'] == 'thread'
  1925. assert revision['content_id'] == 7
  1926. assert revision['is_archived'] is False
  1927. assert revision['is_deleted'] is False
  1928. assert revision['label'] == 'Best Cakes?'
  1929. assert revision['parent_id'] == 3
  1930. assert revision['show_in_ui'] is True
  1931. assert revision['slug'] == 'best-cakes'
  1932. assert revision['status'] == 'open'
  1933. assert revision['workspace_id'] == 2
  1934. assert revision['revision_id'] == 26
  1935. assert revision['revision_type'] == 'edition'
  1936. assert revision['sub_content_types']
  1937. assert revision['comment_ids'] == []
  1938. # TODO - G.M - 2018-06-173 - check date format
  1939. assert revision['created']
  1940. assert revision['author']
  1941. assert revision['author']['user_id'] == 3
  1942. assert revision['author']['avatar_url'] is None
  1943. assert revision['author']['public_name'] == 'Bob i.'
  1944. def test_api__get_thread_revisions__ok_200__most_revision_type(self) -> None:
  1945. """
  1946. get threads revisions
  1947. """
  1948. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1949. admin = dbsession.query(models.User) \
  1950. .filter(models.User.email == 'admin@admin.admin') \
  1951. .one()
  1952. workspace_api = WorkspaceApi(
  1953. current_user=admin,
  1954. session=dbsession,
  1955. config=self.app_config
  1956. )
  1957. business_workspace = workspace_api.get_one(1)
  1958. content_api = ContentApi(
  1959. current_user=admin,
  1960. session=dbsession,
  1961. config=self.app_config
  1962. )
  1963. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1964. test_thread = content_api.create(
  1965. content_type_slug=CONTENT_TYPES.Thread.slug,
  1966. workspace=business_workspace,
  1967. parent=tool_folder,
  1968. label='Test Thread',
  1969. do_save=True,
  1970. do_notify=False,
  1971. )
  1972. with new_revision(
  1973. session=dbsession,
  1974. tm=transaction.manager,
  1975. content=test_thread,
  1976. ):
  1977. content_api.update_content(
  1978. test_thread,
  1979. new_label='test_thread_updated',
  1980. new_content='Just a test'
  1981. )
  1982. content_api.save(test_thread)
  1983. with new_revision(
  1984. session=dbsession,
  1985. tm=transaction.manager,
  1986. content=test_thread,
  1987. ):
  1988. content_api.archive(test_thread)
  1989. content_api.save(test_thread)
  1990. with new_revision(
  1991. session=dbsession,
  1992. tm=transaction.manager,
  1993. content=test_thread,
  1994. ):
  1995. content_api.unarchive(test_thread)
  1996. content_api.save(test_thread)
  1997. with new_revision(
  1998. session=dbsession,
  1999. tm=transaction.manager,
  2000. content=test_thread,
  2001. ):
  2002. content_api.delete(test_thread)
  2003. content_api.save(test_thread)
  2004. with new_revision(
  2005. session=dbsession,
  2006. tm=transaction.manager,
  2007. content=test_thread,
  2008. ):
  2009. content_api.undelete(test_thread)
  2010. content_api.save(test_thread)
  2011. dbsession.flush()
  2012. transaction.commit()
  2013. self.testapp.authorization = (
  2014. 'Basic',
  2015. (
  2016. 'admin@admin.admin',
  2017. 'admin@admin.admin'
  2018. )
  2019. )
  2020. res = self.testapp.get(
  2021. '/api/v2/workspaces/1/threads/{}/revisions'.format(test_thread.content_id), # nopep8
  2022. status=200
  2023. )
  2024. revisions = res.json_body
  2025. assert len(revisions) == 6
  2026. for revision in revisions:
  2027. revision['content_type'] == 'thread'
  2028. revision['workspace_id'] == 1
  2029. revision['content_id'] == test_thread.content_id
  2030. revision = revisions[0]
  2031. revision['revision_type'] == 'creation'
  2032. revision = revisions[1]
  2033. revision['revision_type'] == 'archiving'
  2034. revision = revisions[2]
  2035. revision['revision_type'] == 'unarchiving'
  2036. revision = revisions[3]
  2037. revision['revision_type'] == 'deletion'
  2038. revision = revisions[4]
  2039. revision['revision_type'] == 'undeletion'
  2040. def test_api__set_thread_status__ok_200__nominal_case(self) -> None:
  2041. """
  2042. Set thread status
  2043. """
  2044. self.testapp.authorization = (
  2045. 'Basic',
  2046. (
  2047. 'admin@admin.admin',
  2048. 'admin@admin.admin'
  2049. )
  2050. )
  2051. params = {
  2052. 'status': 'closed-deprecated',
  2053. }
  2054. # before
  2055. res = self.testapp.get(
  2056. '/api/v2/workspaces/2/threads/7',
  2057. status=200
  2058. ) # nopep8
  2059. content = res.json_body
  2060. assert content['content_type'] == 'thread'
  2061. assert content['content_id'] == 7
  2062. assert content['status'] == 'open'
  2063. # set status
  2064. self.testapp.put_json(
  2065. '/api/v2/workspaces/2/threads/7/status',
  2066. params=params,
  2067. status=204
  2068. )
  2069. # after
  2070. res = self.testapp.get(
  2071. '/api/v2/workspaces/2/threads/7',
  2072. status=200
  2073. ) # nopep8
  2074. content = res.json_body
  2075. assert content['content_type'] == 'thread'
  2076. assert content['content_id'] == 7
  2077. assert content['status'] == 'closed-deprecated'
  2078. def test_api__set_thread_status__ok_400__wrong_status(self) -> None:
  2079. """
  2080. Set thread status
  2081. """
  2082. self.testapp.authorization = (
  2083. 'Basic',
  2084. (
  2085. 'admin@admin.admin',
  2086. 'admin@admin.admin'
  2087. )
  2088. )
  2089. params = {
  2090. 'status': 'unexistant-status',
  2091. }
  2092. self.testapp.put_json(
  2093. '/api/v2/workspaces/2/threads/7/status',
  2094. params=params,
  2095. status=400
  2096. )