test_contents.py 72KB

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