test_contents.py 72KB

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