test_contents.py 98KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895
  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 TestFolder(FunctionalTest):
  24. """
  25. Tests for /api/v2/workspaces/{workspace_id}/folders/{content_id}
  26. endpoint
  27. """
  28. fixtures = [BaseFixture]
  29. def test_api__get_folder__ok_200__nominal_case(self) -> None:
  30. """
  31. Get one folder content
  32. """
  33. dbsession = get_tm_session(self.session_factory, transaction.manager)
  34. admin = dbsession.query(models.User) \
  35. .filter(models.User.email == 'admin@admin.admin') \
  36. .one()
  37. workspace_api = WorkspaceApi(
  38. current_user=admin,
  39. session=dbsession,
  40. config=self.app_config
  41. )
  42. content_api = ContentApi(
  43. current_user=admin,
  44. session=dbsession,
  45. config=self.app_config
  46. )
  47. test_workspace = workspace_api.create_workspace(
  48. label='test',
  49. save_now=True,
  50. )
  51. folder = content_api.create(
  52. label='test-folder',
  53. content_type_slug=CONTENT_TYPES.Folder.slug,
  54. workspace=test_workspace,
  55. do_save=True,
  56. do_notify=False
  57. )
  58. transaction.commit()
  59. self.testapp.authorization = (
  60. 'Basic',
  61. (
  62. 'admin@admin.admin',
  63. 'admin@admin.admin'
  64. )
  65. )
  66. res = self.testapp.get(
  67. '/api/v2/workspaces/{workspace_id}/folders/{content_id}'.format(
  68. workspace_id=test_workspace.workspace_id,
  69. content_id=folder.content_id,
  70. ),
  71. status=200
  72. )
  73. content = res.json_body
  74. assert content['content_type'] == 'folder'
  75. assert content['content_id'] == folder.content_id
  76. assert content['is_archived'] is False
  77. assert content['is_deleted'] is False
  78. assert content['label'] == 'test-folder'
  79. assert content['parent_id'] is None
  80. assert content['show_in_ui'] is True
  81. assert content['slug'] == 'test-folder'
  82. assert content['status'] == 'open'
  83. assert content['workspace_id'] == test_workspace.workspace_id
  84. assert content['current_revision_id'] == folder.revision_id
  85. # TODO - G.M - 2018-06-173 - check date format
  86. assert content['created']
  87. assert content['author']
  88. assert content['author']['user_id'] == 1
  89. assert content['author']['avatar_url'] is None
  90. assert content['author']['public_name'] == 'Global manager'
  91. # TODO - G.M - 2018-06-173 - check date format
  92. assert content['modified']
  93. assert content['last_modifier']['user_id'] == 1
  94. assert content['last_modifier']['public_name'] == 'Global manager'
  95. assert content['last_modifier']['avatar_url'] is None
  96. assert content['raw_content'] == ''
  97. def test_api__get_folder__err_400__wrong_content_type(self) -> None:
  98. """
  99. Get one folder of a content content 7 is not folder
  100. """
  101. self.testapp.authorization = (
  102. 'Basic',
  103. (
  104. 'admin@admin.admin',
  105. 'admin@admin.admin'
  106. )
  107. )
  108. dbsession = get_tm_session(self.session_factory, transaction.manager)
  109. admin = dbsession.query(models.User) \
  110. .filter(models.User.email == 'admin@admin.admin') \
  111. .one()
  112. workspace_api = WorkspaceApi(
  113. current_user=admin,
  114. session=dbsession,
  115. config=self.app_config
  116. )
  117. content_api = ContentApi(
  118. current_user=admin,
  119. session=dbsession,
  120. config=self.app_config
  121. )
  122. test_workspace = workspace_api.create_workspace(
  123. label='test',
  124. save_now=True,
  125. )
  126. thread = content_api.create(
  127. label='thread',
  128. content_type_slug=CONTENT_TYPES.Thread.slug,
  129. workspace=test_workspace,
  130. do_save=True,
  131. do_notify=False
  132. )
  133. transaction.commit()
  134. self.testapp.get(
  135. '/api/v2/workspaces/2/folders/7',
  136. status=400
  137. )
  138. def test_api__get_folder__err_400__content_does_not_exist(self) -> None: # nopep8
  139. """
  140. Get one folder content (content 170 does not exist in db)
  141. """
  142. self.testapp.authorization = (
  143. 'Basic',
  144. (
  145. 'admin@admin.admin',
  146. 'admin@admin.admin'
  147. )
  148. )
  149. dbsession = get_tm_session(self.session_factory, transaction.manager)
  150. admin = dbsession.query(models.User) \
  151. .filter(models.User.email == 'admin@admin.admin') \
  152. .one()
  153. workspace_api = WorkspaceApi(
  154. current_user=admin,
  155. session=dbsession,
  156. config=self.app_config
  157. )
  158. content_api = ContentApi(
  159. current_user=admin,
  160. session=dbsession,
  161. config=self.app_config
  162. )
  163. test_workspace = workspace_api.create_workspace(
  164. label='test',
  165. save_now=True,
  166. )
  167. transaction.commit()
  168. self.testapp.get(
  169. '/api/v2/workspaces/{workspace_id}/folders/170'.format(workspace_id=test_workspace.workspace_id), # nopep8
  170. status=400
  171. )
  172. def test_api__get_folder__err_400__content_not_in_workspace(self) -> None: # nopep8
  173. """
  174. Get one folders of a content (content is in another workspace)
  175. """
  176. self.testapp.authorization = (
  177. 'Basic',
  178. (
  179. 'admin@admin.admin',
  180. 'admin@admin.admin'
  181. )
  182. )
  183. dbsession = get_tm_session(self.session_factory, transaction.manager)
  184. admin = dbsession.query(models.User) \
  185. .filter(models.User.email == 'admin@admin.admin') \
  186. .one()
  187. workspace_api = WorkspaceApi(
  188. current_user=admin,
  189. session=dbsession,
  190. config=self.app_config
  191. )
  192. content_api = ContentApi(
  193. current_user=admin,
  194. session=dbsession,
  195. config=self.app_config
  196. )
  197. test_workspace = workspace_api.create_workspace(
  198. label='test',
  199. save_now=True,
  200. )
  201. folder = content_api.create(
  202. label='test_folder',
  203. content_type_slug=CONTENT_TYPES.Folder.slug,
  204. workspace=test_workspace,
  205. do_save=True,
  206. do_notify=False
  207. )
  208. test_workspace2 = workspace_api.create_workspace(
  209. label='test2',
  210. save_now=True,
  211. )
  212. transaction.commit()
  213. self.testapp.authorization = (
  214. 'Basic',
  215. (
  216. 'admin@admin.admin',
  217. 'admin@admin.admin'
  218. )
  219. )
  220. self.testapp.get(
  221. '/api/v2/workspaces/{workspace_id}/folders/{content_id}'.format(
  222. workspace_id=test_workspace2.workspace_id,
  223. content_id=folder.content_id,
  224. ),
  225. status=400
  226. )
  227. def test_api__get_folder__err_400__workspace_does_not_exist(self) -> None: # nopep8
  228. """
  229. Get one folder content (Workspace 40 does not exist)
  230. """
  231. dbsession = get_tm_session(self.session_factory, transaction.manager)
  232. admin = dbsession.query(models.User) \
  233. .filter(models.User.email == 'admin@admin.admin') \
  234. .one()
  235. workspace_api = WorkspaceApi(
  236. current_user=admin,
  237. session=dbsession,
  238. config=self.app_config
  239. )
  240. content_api = ContentApi(
  241. current_user=admin,
  242. session=dbsession,
  243. config=self.app_config
  244. )
  245. test_workspace = workspace_api.create_workspace(
  246. label='test',
  247. save_now=True,
  248. )
  249. folder = content_api.create(
  250. label='test_folder',
  251. content_type_slug=CONTENT_TYPES.Folder.slug,
  252. workspace=test_workspace,
  253. do_save=True,
  254. do_notify=False
  255. )
  256. transaction.commit()
  257. self.testapp.authorization = (
  258. 'Basic',
  259. (
  260. 'admin@admin.admin',
  261. 'admin@admin.admin'
  262. )
  263. )
  264. self.testapp.get(
  265. '/api/v2/workspaces/40/folders/{content_id}'.format(content_id=folder.content_id), # nopep8
  266. status=400
  267. )
  268. def test_api__get_folder__err_400__workspace_id_is_not_int(self) -> None: # nopep8
  269. """
  270. Get one folder content, workspace id is not int
  271. """
  272. dbsession = get_tm_session(self.session_factory, transaction.manager)
  273. admin = dbsession.query(models.User) \
  274. .filter(models.User.email == 'admin@admin.admin') \
  275. .one()
  276. workspace_api = WorkspaceApi(
  277. current_user=admin,
  278. session=dbsession,
  279. config=self.app_config
  280. )
  281. content_api = ContentApi(
  282. current_user=admin,
  283. session=dbsession,
  284. config=self.app_config
  285. )
  286. test_workspace = workspace_api.create_workspace(
  287. label='test',
  288. save_now=True,
  289. )
  290. folder = content_api.create(
  291. label='test_folder',
  292. content_type_slug=CONTENT_TYPES.Folder.slug,
  293. workspace=test_workspace,
  294. do_save=True,
  295. do_notify=False
  296. )
  297. transaction.commit()
  298. self.testapp.authorization = (
  299. 'Basic',
  300. (
  301. 'admin@admin.admin',
  302. 'admin@admin.admin'
  303. )
  304. )
  305. self.testapp.get(
  306. '/api/v2/workspaces/coucou/folders/{content_id}'.format(content_id=folder.content_id), # nopep8
  307. status=400
  308. )
  309. def test_api__get_folder__err_400__content_id_is_not_int(self) -> None: # nopep8
  310. """
  311. Get one folder content, content_id is not int
  312. """
  313. dbsession = get_tm_session(self.session_factory, transaction.manager)
  314. admin = dbsession.query(models.User) \
  315. .filter(models.User.email == 'admin@admin.admin') \
  316. .one()
  317. workspace_api = WorkspaceApi(
  318. current_user=admin,
  319. session=dbsession,
  320. config=self.app_config
  321. )
  322. content_api = ContentApi(
  323. current_user=admin,
  324. session=dbsession,
  325. config=self.app_config
  326. )
  327. test_workspace = workspace_api.create_workspace(
  328. label='test',
  329. save_now=True,
  330. )
  331. folder = content_api.create(
  332. label='test_folder',
  333. content_type_slug=CONTENT_TYPES.Folder.slug,
  334. workspace=test_workspace,
  335. do_save=True,
  336. do_notify=False
  337. )
  338. transaction.commit()
  339. self.testapp.authorization = (
  340. 'Basic',
  341. (
  342. 'admin@admin.admin',
  343. 'admin@admin.admin'
  344. )
  345. )
  346. self.testapp.get(
  347. '/api/v2/workspaces/{workspace_id}/folders/coucou'.format(workspace_id=test_workspace.workspace_id), # nopep8
  348. status=400
  349. )
  350. def test_api__update_folder__err_400__empty_label(self) -> None: # nopep8
  351. """
  352. Update(put) one folder content
  353. """
  354. dbsession = get_tm_session(self.session_factory, transaction.manager)
  355. admin = dbsession.query(models.User) \
  356. .filter(models.User.email == 'admin@admin.admin') \
  357. .one()
  358. workspace_api = WorkspaceApi(
  359. current_user=admin,
  360. session=dbsession,
  361. config=self.app_config
  362. )
  363. content_api = ContentApi(
  364. current_user=admin,
  365. session=dbsession,
  366. config=self.app_config
  367. )
  368. test_workspace = workspace_api.create_workspace(
  369. label='test',
  370. save_now=True,
  371. )
  372. folder = content_api.create(
  373. label='test_folder',
  374. content_type_slug=CONTENT_TYPES.Folder.slug,
  375. workspace=test_workspace,
  376. do_save=True,
  377. do_notify=False
  378. )
  379. transaction.commit()
  380. self.testapp.authorization = (
  381. 'Basic',
  382. (
  383. 'admin@admin.admin',
  384. 'admin@admin.admin'
  385. )
  386. )
  387. params = {
  388. 'label': '',
  389. 'raw_content': '<p> Le nouveau contenu </p>',
  390. 'sub_content_types': [CONTENT_TYPES.Folder.slug]
  391. }
  392. self.testapp.put_json(
  393. '/api/v2/workspaces/{workspace_id}/folders/{content_id}'.format(
  394. workspace_id=test_workspace.workspace_id,
  395. content_id=folder.content_id,
  396. ),
  397. params=params,
  398. status=400
  399. )
  400. def test_api__update_folder__ok_200__nominal_case(self) -> None:
  401. """
  402. Update(put) one html document of a content
  403. """
  404. dbsession = get_tm_session(self.session_factory, transaction.manager)
  405. admin = dbsession.query(models.User) \
  406. .filter(models.User.email == 'admin@admin.admin') \
  407. .one()
  408. workspace_api = WorkspaceApi(
  409. current_user=admin,
  410. session=dbsession,
  411. config=self.app_config
  412. )
  413. content_api = ContentApi(
  414. current_user=admin,
  415. session=dbsession,
  416. config=self.app_config
  417. )
  418. test_workspace = workspace_api.create_workspace(
  419. label='test',
  420. save_now=True,
  421. )
  422. folder = content_api.create(
  423. label='test_folder',
  424. content_type_slug=CONTENT_TYPES.Folder.slug,
  425. workspace=test_workspace,
  426. do_save=True,
  427. do_notify=False
  428. )
  429. transaction.commit()
  430. self.testapp.authorization = (
  431. 'Basic',
  432. (
  433. 'admin@admin.admin',
  434. 'admin@admin.admin'
  435. )
  436. )
  437. params = {
  438. 'label': 'My New label',
  439. 'raw_content': '<p> Le nouveau contenu </p>',
  440. 'sub_content_types': [CONTENT_TYPES.Folder.slug]
  441. }
  442. res = self.testapp.put_json(
  443. '/api/v2/workspaces/{workspace_id}/folders/{content_id}'.format(
  444. workspace_id=test_workspace.workspace_id,
  445. content_id=folder.content_id,
  446. ),
  447. params=params,
  448. status=200
  449. )
  450. content = res.json_body
  451. assert content['content_type'] == 'folder'
  452. assert content['content_id'] == folder.content_id
  453. assert content['is_archived'] is False
  454. assert content['is_deleted'] is False
  455. assert content['label'] == 'My New label'
  456. assert content['parent_id'] is None
  457. assert content['show_in_ui'] is True
  458. assert content['slug'] == 'my-new-label'
  459. assert content['status'] == 'open'
  460. assert content['workspace_id'] == test_workspace.workspace_id
  461. assert content['current_revision_id']
  462. # TODO - G.M - 2018-06-173 - check date format
  463. assert content['created']
  464. assert content['author']
  465. assert content['author']['user_id'] == 1
  466. assert content['author']['avatar_url'] is None
  467. assert content['author']['public_name'] == 'Global manager'
  468. # TODO - G.M - 2018-06-173 - check date format
  469. assert content['modified']
  470. assert content['last_modifier'] == content['author']
  471. assert content['raw_content'] == '<p> Le nouveau contenu </p>'
  472. assert content['sub_content_types'] == [CONTENT_TYPES.Folder.slug]
  473. def test_api__get_folder_revisions__ok_200__nominal_case(
  474. self
  475. ) -> None:
  476. """
  477. Get one html document of a content
  478. """
  479. dbsession = get_tm_session(self.session_factory, transaction.manager)
  480. admin = dbsession.query(models.User) \
  481. .filter(models.User.email == 'admin@admin.admin') \
  482. .one()
  483. workspace_api = WorkspaceApi(
  484. current_user=admin,
  485. session=dbsession,
  486. config=self.app_config
  487. )
  488. content_api = ContentApi(
  489. current_user=admin,
  490. session=dbsession,
  491. config=self.app_config
  492. )
  493. test_workspace = workspace_api.create_workspace(
  494. label='test',
  495. save_now=True,
  496. )
  497. folder = content_api.create(
  498. label='test-folder',
  499. content_type_slug=CONTENT_TYPES.Folder.slug,
  500. workspace=test_workspace,
  501. do_save=True,
  502. do_notify=False
  503. )
  504. with new_revision(
  505. session=dbsession,
  506. tm=transaction.manager,
  507. content=folder,
  508. ):
  509. content_api.update_content(
  510. folder,
  511. new_label='test-folder-updated',
  512. new_content='Just a test'
  513. )
  514. content_api.save(folder)
  515. with new_revision(
  516. session=dbsession,
  517. tm=transaction.manager,
  518. content=folder,
  519. ):
  520. content_api.archive(
  521. folder,
  522. )
  523. content_api.save(folder)
  524. with new_revision(
  525. session=dbsession,
  526. tm=transaction.manager,
  527. content=folder,
  528. ):
  529. content_api.unarchive(
  530. folder,
  531. )
  532. content_api.save(folder)
  533. transaction.commit()
  534. self.testapp.authorization = (
  535. 'Basic',
  536. (
  537. 'admin@admin.admin',
  538. 'admin@admin.admin'
  539. )
  540. )
  541. res = self.testapp.get(
  542. '/api/v2/workspaces/{workspace_id}/folders/{content_id}/revisions'.format( # nopep8
  543. workspace_id=test_workspace.workspace_id,
  544. content_id=folder.content_id,
  545. ),
  546. status=200
  547. )
  548. revisions = res.json_body
  549. assert len(revisions) == 4
  550. revision = revisions[0]
  551. assert revision['content_type'] == 'folder'
  552. assert revision['content_id'] == folder.content_id
  553. assert revision['is_archived'] is False
  554. assert revision['is_deleted'] is False
  555. assert revision['label'] == 'test-folder'
  556. assert revision['parent_id'] is None
  557. assert revision['show_in_ui'] is True
  558. assert revision['slug'] == 'test-folder'
  559. assert revision['status'] == 'open'
  560. assert revision['workspace_id'] == test_workspace.workspace_id
  561. assert revision['revision_id']
  562. assert revision['revision_type'] == 'creation'
  563. assert revision['sub_content_types']
  564. # TODO - G.M - 2018-06-173 - Test with real comments
  565. assert revision['comment_ids'] == []
  566. # TODO - G.M - 2018-06-173 - check date format
  567. assert revision['created']
  568. assert revision['author']
  569. assert revision['author']['user_id'] == 1
  570. assert revision['author']['avatar_url'] is None
  571. assert revision['author']['public_name'] == 'Global manager'
  572. revision = revisions[1]
  573. assert revision['content_type'] == 'folder'
  574. assert revision['content_id'] == folder.content_id
  575. assert revision['is_archived'] is False
  576. assert revision['is_deleted'] is False
  577. assert revision['label'] == 'test-folder-updated'
  578. assert revision['parent_id'] is None
  579. assert revision['show_in_ui'] is True
  580. assert revision['slug'] == 'test-folder-updated'
  581. assert revision['status'] == 'open'
  582. assert revision['workspace_id'] == test_workspace.workspace_id
  583. assert revision['revision_id']
  584. assert revision['revision_type'] == 'edition'
  585. assert revision['sub_content_types']
  586. # TODO - G.M - 2018-06-173 - Test with real comments
  587. assert revision['comment_ids'] == []
  588. # TODO - G.M - 2018-06-173 - check date format
  589. assert revision['created']
  590. assert revision['author']
  591. assert revision['author']['user_id'] == 1
  592. assert revision['author']['avatar_url'] is None
  593. assert revision['author']['public_name'] == 'Global manager'
  594. revision = revisions[2]
  595. assert revision['content_type'] == 'folder'
  596. assert revision['content_id'] == folder.content_id
  597. assert revision['is_archived'] is True
  598. assert revision['is_deleted'] is False
  599. assert revision['label'] != 'test-folder-updated'
  600. assert revision['label'].startswith('test-folder-updated')
  601. assert revision['parent_id'] is None
  602. assert revision['show_in_ui'] is True
  603. assert revision['slug'] != 'test-folder-updated'
  604. assert revision['slug'].startswith('test-folder-updated')
  605. assert revision['status'] == 'open'
  606. assert revision['workspace_id'] == test_workspace.workspace_id
  607. assert revision['revision_id']
  608. assert revision['revision_type'] == 'archiving'
  609. assert revision['sub_content_types']
  610. # TODO - G.M - 2018-06-173 - Test with real comments
  611. assert revision['comment_ids'] == []
  612. # TODO - G.M - 2018-06-173 - check date format
  613. assert revision['created']
  614. assert revision['author']
  615. assert revision['author']['user_id'] == 1
  616. assert revision['author']['avatar_url'] is None
  617. assert revision['author']['public_name'] == 'Global manager'
  618. revision = revisions[3]
  619. assert revision['content_type'] == 'folder'
  620. assert revision['content_id'] == folder.content_id
  621. assert revision['is_archived'] is False
  622. assert revision['is_deleted'] is False
  623. assert revision['label'].startswith('test-folder-updated')
  624. assert revision['parent_id'] is None
  625. assert revision['show_in_ui'] is True
  626. assert revision['slug'].startswith('test-folder-updated')
  627. assert revision['status'] == 'open'
  628. assert revision['workspace_id'] == test_workspace.workspace_id
  629. assert revision['revision_id']
  630. assert revision['revision_type'] == 'unarchiving'
  631. assert revision['sub_content_types']
  632. # TODO - G.M - 2018-06-173 - Test with real comments
  633. assert revision['comment_ids'] == []
  634. # TODO - G.M - 2018-06-173 - check date format
  635. assert revision['created']
  636. assert revision['author']
  637. assert revision['author']['user_id'] == 1
  638. assert revision['author']['avatar_url'] is None
  639. assert revision['author']['public_name'] == 'Global manager'
  640. def test_api__set_folder_status__ok_200__nominal_case(self) -> None:
  641. """
  642. Get one folder content
  643. """
  644. dbsession = get_tm_session(self.session_factory, transaction.manager)
  645. admin = dbsession.query(models.User) \
  646. .filter(models.User.email == 'admin@admin.admin') \
  647. .one()
  648. workspace_api = WorkspaceApi(
  649. current_user=admin,
  650. session=dbsession,
  651. config=self.app_config
  652. )
  653. content_api = ContentApi(
  654. current_user=admin,
  655. session=dbsession,
  656. config=self.app_config
  657. )
  658. test_workspace = workspace_api.create_workspace(
  659. label='test',
  660. save_now=True,
  661. )
  662. folder = content_api.create(
  663. label='test_folder',
  664. content_type_slug=CONTENT_TYPES.Folder.slug,
  665. workspace=test_workspace,
  666. do_save=True,
  667. do_notify=False
  668. )
  669. transaction.commit()
  670. self.testapp.authorization = (
  671. 'Basic',
  672. (
  673. 'admin@admin.admin',
  674. 'admin@admin.admin'
  675. )
  676. )
  677. params = {
  678. 'status': 'closed-deprecated',
  679. }
  680. # before
  681. res = self.testapp.get(
  682. '/api/v2/workspaces/{workspace_id}/folders/{content_id}'.format( # nopep8
  683. # nopep8
  684. workspace_id=test_workspace.workspace_id,
  685. content_id=folder.content_id,
  686. ),
  687. status=200
  688. )
  689. content = res.json_body
  690. assert content['content_type'] == 'folder'
  691. assert content['content_id'] == folder.content_id
  692. assert content['status'] == 'open'
  693. # set status
  694. self.testapp.put_json(
  695. '/api/v2/workspaces/{workspace_id}/folders/{content_id}/status'.format( # nopep8
  696. workspace_id=test_workspace.workspace_id,
  697. content_id=folder.content_id,
  698. ),
  699. params=params,
  700. status=204
  701. )
  702. # after
  703. res = self.testapp.get(
  704. '/api/v2/workspaces/{workspace_id}/folders/{content_id}'.format(
  705. workspace_id=test_workspace.workspace_id,
  706. content_id=folder.content_id,
  707. ),
  708. status=200
  709. )
  710. content = res.json_body
  711. assert content['content_type'] == 'folder'
  712. assert content['content_id'] == folder.content_id
  713. assert content['status'] == 'closed-deprecated'
  714. def test_api__set_folder_status__err_400__wrong_status(self) -> None:
  715. """
  716. Get one folder content
  717. """
  718. self.testapp.authorization = (
  719. 'Basic',
  720. (
  721. 'admin@admin.admin',
  722. 'admin@admin.admin'
  723. )
  724. )
  725. params = {
  726. 'status': 'unexistant-status',
  727. }
  728. dbsession = get_tm_session(self.session_factory, transaction.manager)
  729. admin = dbsession.query(models.User) \
  730. .filter(models.User.email == 'admin@admin.admin') \
  731. .one()
  732. workspace_api = WorkspaceApi(
  733. current_user=admin,
  734. session=dbsession,
  735. config=self.app_config
  736. )
  737. content_api = ContentApi(
  738. current_user=admin,
  739. session=dbsession,
  740. config=self.app_config
  741. )
  742. test_workspace = workspace_api.create_workspace(
  743. label='test',
  744. save_now=True,
  745. )
  746. folder = content_api.create(
  747. label='test_folder',
  748. content_type_slug=CONTENT_TYPES.Folder.slug,
  749. workspace=test_workspace,
  750. do_save=True,
  751. do_notify=False
  752. )
  753. transaction.commit()
  754. self.testapp.put_json(
  755. '/api/v2/workspaces/{workspace_id}/folders/{content_id}/status'.format( # nopep8
  756. workspace_id=test_workspace.workspace_id,
  757. content_id=folder.content_id,
  758. ),
  759. params=params,
  760. status=400
  761. )
  762. class TestHtmlDocuments(FunctionalTest):
  763. """
  764. Tests for /api/v2/workspaces/{workspace_id}/html-documents/{content_id}
  765. endpoint
  766. """
  767. fixtures = [BaseFixture, ContentFixtures]
  768. def test_api__get_html_document__ok_200__legacy_slug(self) -> None:
  769. """
  770. Get one html document of a content
  771. """
  772. self.testapp.authorization = (
  773. 'Basic',
  774. (
  775. 'admin@admin.admin',
  776. 'admin@admin.admin'
  777. )
  778. )
  779. set_html_document_slug_to_legacy(self.session_factory)
  780. res = self.testapp.get(
  781. '/api/v2/workspaces/2/html-documents/6',
  782. status=200
  783. )
  784. content = res.json_body
  785. assert content['content_type'] == 'html-document'
  786. assert content['content_id'] == 6
  787. assert content['is_archived'] is False
  788. assert content['is_deleted'] is False
  789. assert content['label'] == 'Tiramisu Recipe'
  790. assert content['parent_id'] == 3
  791. assert content['show_in_ui'] is True
  792. assert content['slug'] == 'tiramisu-recipe'
  793. assert content['status'] == 'open'
  794. assert content['workspace_id'] == 2
  795. assert content['current_revision_id'] == 27
  796. # TODO - G.M - 2018-06-173 - check date format
  797. assert content['created']
  798. assert content['author']
  799. assert content['author']['user_id'] == 1
  800. assert content['author']['avatar_url'] is None
  801. assert content['author']['public_name'] == 'Global manager'
  802. # TODO - G.M - 2018-06-173 - check date format
  803. assert content['modified']
  804. assert content['last_modifier'] != content['author']
  805. assert content['last_modifier']['user_id'] == 3
  806. assert content['last_modifier']['public_name'] == 'Bob i.'
  807. assert content['last_modifier']['avatar_url'] is None
  808. assert content['raw_content'] == '<p>To cook a great Tiramisu, you need many ingredients.</p>' # nopep8
  809. def test_api__get_html_document__ok_200__nominal_case(self) -> None:
  810. """
  811. Get one html document of a content
  812. """
  813. self.testapp.authorization = (
  814. 'Basic',
  815. (
  816. 'admin@admin.admin',
  817. 'admin@admin.admin'
  818. )
  819. )
  820. res = self.testapp.get(
  821. '/api/v2/workspaces/2/html-documents/6',
  822. status=200
  823. )
  824. content = res.json_body
  825. assert content['content_type'] == 'html-document'
  826. assert content['content_id'] == 6
  827. assert content['is_archived'] is False
  828. assert content['is_deleted'] is False
  829. assert content['label'] == 'Tiramisu Recipe'
  830. assert content['parent_id'] == 3
  831. assert content['show_in_ui'] is True
  832. assert content['slug'] == 'tiramisu-recipe'
  833. assert content['status'] == 'open'
  834. assert content['workspace_id'] == 2
  835. assert content['current_revision_id'] == 27
  836. # TODO - G.M - 2018-06-173 - check date format
  837. assert content['created']
  838. assert content['author']
  839. assert content['author']['user_id'] == 1
  840. assert content['author']['avatar_url'] is None
  841. assert content['author']['public_name'] == 'Global manager'
  842. # TODO - G.M - 2018-06-173 - check date format
  843. assert content['modified']
  844. assert content['last_modifier'] != content['author']
  845. assert content['last_modifier']['user_id'] == 3
  846. assert content['last_modifier']['public_name'] == 'Bob i.'
  847. assert content['last_modifier']['avatar_url'] is None
  848. assert content['raw_content'] == '<p>To cook a great Tiramisu, you need many ingredients.</p>' # nopep8
  849. def test_api__get_html_document__err_400__wrong_content_type(self) -> None:
  850. """
  851. Get one html document of a content content 7 is not html_document
  852. """
  853. self.testapp.authorization = (
  854. 'Basic',
  855. (
  856. 'admin@admin.admin',
  857. 'admin@admin.admin'
  858. )
  859. )
  860. self.testapp.get(
  861. '/api/v2/workspaces/2/html-documents/7',
  862. status=400
  863. )
  864. def test_api__get_html_document__err_400__content_does_not_exist(self) -> None: # nopep8
  865. """
  866. Get one html document of a content (content 170 does not exist in db
  867. """
  868. self.testapp.authorization = (
  869. 'Basic',
  870. (
  871. 'admin@admin.admin',
  872. 'admin@admin.admin'
  873. )
  874. )
  875. self.testapp.get(
  876. '/api/v2/workspaces/2/html-documents/170',
  877. status=400
  878. )
  879. def test_api__get_html_document__err_400__content_not_in_workspace(self) -> None: # nopep8
  880. """
  881. Get one html document of a content (content 6 is in workspace 2)
  882. """
  883. self.testapp.authorization = (
  884. 'Basic',
  885. (
  886. 'admin@admin.admin',
  887. 'admin@admin.admin'
  888. )
  889. )
  890. self.testapp.get(
  891. '/api/v2/workspaces/1/html-documents/6',
  892. status=400
  893. )
  894. def test_api__get_html_document__err_400__workspace_does_not_exist(self) -> None: # nopep8
  895. """
  896. Get one html document of a content (Workspace 40 does not exist)
  897. """
  898. self.testapp.authorization = (
  899. 'Basic',
  900. (
  901. 'admin@admin.admin',
  902. 'admin@admin.admin'
  903. )
  904. )
  905. self.testapp.get(
  906. '/api/v2/workspaces/40/html-documents/6',
  907. status=400
  908. )
  909. def test_api__get_html_document__err_400__workspace_id_is_not_int(self) -> None: # nopep8
  910. """
  911. Get one html document of a content, workspace id is not int
  912. """
  913. self.testapp.authorization = (
  914. 'Basic',
  915. (
  916. 'admin@admin.admin',
  917. 'admin@admin.admin'
  918. )
  919. )
  920. self.testapp.get(
  921. '/api/v2/workspaces/coucou/html-documents/6',
  922. status=400
  923. )
  924. def test_api__get_html_document__err_400__content_id_is_not_int(self) -> None: # nopep8
  925. """
  926. Get one html document of a content, content_id is not int
  927. """
  928. self.testapp.authorization = (
  929. 'Basic',
  930. (
  931. 'admin@admin.admin',
  932. 'admin@admin.admin'
  933. )
  934. )
  935. self.testapp.get(
  936. '/api/v2/workspaces/2/html-documents/coucou',
  937. status=400
  938. )
  939. def test_api__update_html_document__err_400__empty_label(self) -> None: # nopep8
  940. """
  941. Update(put) one html document of a content
  942. """
  943. self.testapp.authorization = (
  944. 'Basic',
  945. (
  946. 'admin@admin.admin',
  947. 'admin@admin.admin'
  948. )
  949. )
  950. params = {
  951. 'label': '',
  952. 'raw_content': '<p> Le nouveau contenu </p>',
  953. }
  954. self.testapp.put_json(
  955. '/api/v2/workspaces/2/html-documents/6',
  956. params=params,
  957. status=400
  958. )
  959. def test_api__update_html_document__ok_200__nominal_case(self) -> None:
  960. """
  961. Update(put) one html document of a content
  962. """
  963. self.testapp.authorization = (
  964. 'Basic',
  965. (
  966. 'admin@admin.admin',
  967. 'admin@admin.admin'
  968. )
  969. )
  970. params = {
  971. 'label': 'My New label',
  972. 'raw_content': '<p> Le nouveau contenu </p>',
  973. }
  974. res = self.testapp.put_json(
  975. '/api/v2/workspaces/2/html-documents/6',
  976. params=params,
  977. status=200
  978. )
  979. content = res.json_body
  980. assert content['content_type'] == 'html-document'
  981. assert content['content_id'] == 6
  982. assert content['is_archived'] is False
  983. assert content['is_deleted'] is False
  984. assert content['label'] == 'My New label'
  985. assert content['parent_id'] == 3
  986. assert content['show_in_ui'] is True
  987. assert content['slug'] == 'my-new-label'
  988. assert content['status'] == 'open'
  989. assert content['workspace_id'] == 2
  990. assert content['current_revision_id'] == 28
  991. # TODO - G.M - 2018-06-173 - check date format
  992. assert content['created']
  993. assert content['author']
  994. assert content['author']['user_id'] == 1
  995. assert content['author']['avatar_url'] is None
  996. assert content['author']['public_name'] == 'Global manager'
  997. # TODO - G.M - 2018-06-173 - check date format
  998. assert content['modified']
  999. assert content['last_modifier'] == content['author']
  1000. assert content['raw_content'] == '<p> Le nouveau contenu </p>'
  1001. res = self.testapp.get(
  1002. '/api/v2/workspaces/2/html-documents/6',
  1003. status=200
  1004. )
  1005. content = res.json_body
  1006. assert content['content_type'] == 'html-document'
  1007. assert content['content_id'] == 6
  1008. assert content['is_archived'] is False
  1009. assert content['is_deleted'] is False
  1010. assert content['label'] == 'My New label'
  1011. assert content['parent_id'] == 3
  1012. assert content['show_in_ui'] is True
  1013. assert content['slug'] == 'my-new-label'
  1014. assert content['status'] == 'open'
  1015. assert content['workspace_id'] == 2
  1016. assert content['current_revision_id'] == 28
  1017. # TODO - G.M - 2018-06-173 - check date format
  1018. assert content['created']
  1019. assert content['author']
  1020. assert content['author']['user_id'] == 1
  1021. assert content['author']['avatar_url'] is None
  1022. assert content['author']['public_name'] == 'Global manager'
  1023. # TODO - G.M - 2018-06-173 - check date format
  1024. assert content['modified']
  1025. assert content['last_modifier'] == content['author']
  1026. assert content['raw_content'] == '<p> Le nouveau contenu </p>'
  1027. def test_api__get_html_document_revisions__ok_200__nominal_case(
  1028. self
  1029. ) -> None:
  1030. """
  1031. Get one html document of a content
  1032. """
  1033. self.testapp.authorization = (
  1034. 'Basic',
  1035. (
  1036. 'admin@admin.admin',
  1037. 'admin@admin.admin'
  1038. )
  1039. )
  1040. res = self.testapp.get(
  1041. '/api/v2/workspaces/2/html-documents/6/revisions',
  1042. status=200
  1043. )
  1044. revisions = res.json_body
  1045. assert len(revisions) == 3
  1046. revision = revisions[0]
  1047. assert revision['content_type'] == 'html-document'
  1048. assert revision['content_id'] == 6
  1049. assert revision['is_archived'] is False
  1050. assert revision['is_deleted'] is False
  1051. assert revision['label'] == 'Tiramisu Recipes!!!'
  1052. assert revision['parent_id'] == 3
  1053. assert revision['show_in_ui'] is True
  1054. assert revision['slug'] == 'tiramisu-recipes'
  1055. assert revision['status'] == 'open'
  1056. assert revision['workspace_id'] == 2
  1057. assert revision['revision_id'] == 6
  1058. assert revision['revision_type'] == 'creation'
  1059. assert revision['sub_content_types']
  1060. # TODO - G.M - 2018-06-173 - Test with real comments
  1061. assert revision['comment_ids'] == []
  1062. # TODO - G.M - 2018-06-173 - check date format
  1063. assert revision['created']
  1064. assert revision['author']
  1065. assert revision['author']['user_id'] == 1
  1066. assert revision['author']['avatar_url'] is None
  1067. assert revision['author']['public_name'] == 'Global manager'
  1068. revision = revisions[1]
  1069. assert revision['content_type'] == 'html-document'
  1070. assert revision['content_id'] == 6
  1071. assert revision['is_archived'] is False
  1072. assert revision['is_deleted'] is False
  1073. assert revision['label'] == 'Tiramisu Recipes!!!'
  1074. assert revision['parent_id'] == 3
  1075. assert revision['show_in_ui'] is True
  1076. assert revision['slug'] == 'tiramisu-recipes'
  1077. assert revision['status'] == 'open'
  1078. assert revision['workspace_id'] == 2
  1079. assert revision['revision_id'] == 7
  1080. assert revision['revision_type'] == 'edition'
  1081. assert revision['sub_content_types']
  1082. # TODO - G.M - 2018-06-173 - Test with real comments
  1083. assert revision['comment_ids'] == []
  1084. # TODO - G.M - 2018-06-173 - check date format
  1085. assert revision['created']
  1086. assert revision['author']
  1087. assert revision['author']['user_id'] == 1
  1088. assert revision['author']['avatar_url'] is None
  1089. assert revision['author']['public_name'] == 'Global manager'
  1090. revision = revisions[2]
  1091. assert revision['content_type'] == 'html-document'
  1092. assert revision['content_id'] == 6
  1093. assert revision['is_archived'] is False
  1094. assert revision['is_deleted'] is False
  1095. assert revision['label'] == 'Tiramisu Recipe'
  1096. assert revision['parent_id'] == 3
  1097. assert revision['show_in_ui'] is True
  1098. assert revision['slug'] == 'tiramisu-recipe'
  1099. assert revision['status'] == 'open'
  1100. assert revision['workspace_id'] == 2
  1101. assert revision['revision_id'] == 27
  1102. assert revision['revision_type'] == 'edition'
  1103. assert revision['sub_content_types']
  1104. # TODO - G.M - 2018-06-173 - Test with real comments
  1105. assert revision['comment_ids'] == []
  1106. # TODO - G.M - 2018-06-173 - check date format
  1107. assert revision['created']
  1108. assert revision['author']
  1109. assert revision['author']['user_id'] == 3
  1110. assert revision['author']['avatar_url'] is None
  1111. assert revision['author']['public_name'] == 'Bob i.'
  1112. def test_api__set_html_document_status__ok_200__nominal_case(self) -> None:
  1113. """
  1114. Get one html document of a content
  1115. """
  1116. self.testapp.authorization = (
  1117. 'Basic',
  1118. (
  1119. 'admin@admin.admin',
  1120. 'admin@admin.admin'
  1121. )
  1122. )
  1123. params = {
  1124. 'status': 'closed-deprecated',
  1125. }
  1126. # before
  1127. res = self.testapp.get(
  1128. '/api/v2/workspaces/2/html-documents/6',
  1129. status=200
  1130. )
  1131. content = res.json_body
  1132. assert content['content_type'] == 'html-document'
  1133. assert content['content_id'] == 6
  1134. assert content['status'] == 'open'
  1135. # set status
  1136. self.testapp.put_json(
  1137. '/api/v2/workspaces/2/html-documents/6/status',
  1138. params=params,
  1139. status=204
  1140. )
  1141. # after
  1142. res = self.testapp.get(
  1143. '/api/v2/workspaces/2/html-documents/6',
  1144. status=200
  1145. )
  1146. content = res.json_body
  1147. assert content['content_type'] == 'html-document'
  1148. assert content['content_id'] == 6
  1149. assert content['status'] == 'closed-deprecated'
  1150. def test_api__set_html_document_status__err_400__wrong_status(self) -> None:
  1151. """
  1152. Get one html document of a content
  1153. """
  1154. self.testapp.authorization = (
  1155. 'Basic',
  1156. (
  1157. 'admin@admin.admin',
  1158. 'admin@admin.admin'
  1159. )
  1160. )
  1161. params = {
  1162. 'status': 'unexistant-status',
  1163. }
  1164. self.testapp.put_json(
  1165. '/api/v2/workspaces/2/html-documents/6/status',
  1166. params=params,
  1167. status=400
  1168. )
  1169. class TestFiles(FunctionalTest):
  1170. """
  1171. Tests for /api/v2/workspaces/{workspace_id}/files/{content_id}
  1172. endpoint
  1173. """
  1174. fixtures = [BaseFixture, ContentFixtures]
  1175. def test_api__get_file__ok_200__nominal_case(self) -> None:
  1176. """
  1177. Get one file of a content
  1178. """
  1179. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1180. admin = dbsession.query(models.User) \
  1181. .filter(models.User.email == 'admin@admin.admin') \
  1182. .one()
  1183. workspace_api = WorkspaceApi(
  1184. current_user=admin,
  1185. session=dbsession,
  1186. config=self.app_config
  1187. )
  1188. content_api = ContentApi(
  1189. current_user=admin,
  1190. session=dbsession,
  1191. config=self.app_config
  1192. )
  1193. business_workspace = workspace_api.get_one(1)
  1194. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1195. test_file = content_api.create(
  1196. content_type_slug=CONTENT_TYPES.File.slug,
  1197. workspace=business_workspace,
  1198. parent=tool_folder,
  1199. label='Test file',
  1200. do_save=False,
  1201. do_notify=False,
  1202. )
  1203. test_file.file_extension = '.txt'
  1204. test_file.depot_file = FileIntent(
  1205. b'Test file',
  1206. 'Test_file.txt',
  1207. 'text/plain',
  1208. )
  1209. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  1210. dbsession.flush()
  1211. transaction.commit()
  1212. self.testapp.authorization = (
  1213. 'Basic',
  1214. (
  1215. 'admin@admin.admin',
  1216. 'admin@admin.admin'
  1217. )
  1218. )
  1219. res = self.testapp.get(
  1220. '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
  1221. status=200
  1222. )
  1223. content = res.json_body
  1224. assert content['content_type'] == 'file'
  1225. assert content['content_id'] == test_file.content_id
  1226. assert content['is_archived'] is False
  1227. assert content['is_deleted'] is False
  1228. assert content['label'] == 'Test_file'
  1229. assert content['parent_id'] == 1
  1230. assert content['show_in_ui'] is True
  1231. assert content['slug'] == 'test-file'
  1232. assert content['status'] == 'open'
  1233. assert content['workspace_id'] == 1
  1234. assert content['current_revision_id']
  1235. # TODO - G.M - 2018-06-173 - check date format
  1236. assert content['created']
  1237. assert content['author']
  1238. assert content['author']['user_id'] == 1
  1239. assert content['author']['avatar_url'] is None
  1240. assert content['author']['public_name'] == 'Global manager'
  1241. # TODO - G.M - 2018-06-173 - check date format
  1242. assert content['modified']
  1243. assert content['last_modifier'] == content['author']
  1244. assert content['raw_content'] == '<p>description</p>' # nopep8
  1245. def test_api__get_files__err_400__wrong_content_type(self) -> None:
  1246. """
  1247. Get one file of a content content
  1248. """
  1249. self.testapp.authorization = (
  1250. 'Basic',
  1251. (
  1252. 'admin@admin.admin',
  1253. 'admin@admin.admin'
  1254. )
  1255. )
  1256. self.testapp.get(
  1257. '/api/v2/workspaces/2/files/6',
  1258. status=400
  1259. )
  1260. def test_api__get_file__err_400__content_does_not_exist(self) -> None: # nopep8
  1261. """
  1262. Get one file (content 170 does not exist in db
  1263. """
  1264. self.testapp.authorization = (
  1265. 'Basic',
  1266. (
  1267. 'admin@admin.admin',
  1268. 'admin@admin.admin'
  1269. )
  1270. )
  1271. self.testapp.get(
  1272. '/api/v2/workspaces/1/files/170',
  1273. status=400
  1274. )
  1275. def test_api__get_file__err_400__content_not_in_workspace(self) -> None: # nopep8
  1276. """
  1277. Get one file (content 9 is in workspace 2)
  1278. """
  1279. self.testapp.authorization = (
  1280. 'Basic',
  1281. (
  1282. 'admin@admin.admin',
  1283. 'admin@admin.admin'
  1284. )
  1285. )
  1286. self.testapp.get(
  1287. '/api/v2/workspaces/1/files/9',
  1288. status=400
  1289. )
  1290. def test_api__get_file__err_400__workspace_does_not_exist(self) -> None: # nopep8
  1291. """
  1292. Get one file (Workspace 40 does not exist)
  1293. """
  1294. self.testapp.authorization = (
  1295. 'Basic',
  1296. (
  1297. 'admin@admin.admin',
  1298. 'admin@admin.admin'
  1299. )
  1300. )
  1301. self.testapp.get(
  1302. '/api/v2/workspaces/40/files/9',
  1303. status=400
  1304. )
  1305. def test_api__get_file__err_400__workspace_id_is_not_int(self) -> None: # nopep8
  1306. """
  1307. Get one file, workspace id is not int
  1308. """
  1309. self.testapp.authorization = (
  1310. 'Basic',
  1311. (
  1312. 'admin@admin.admin',
  1313. 'admin@admin.admin'
  1314. )
  1315. )
  1316. self.testapp.get(
  1317. '/api/v2/workspaces/coucou/files/9',
  1318. status=400
  1319. )
  1320. def test_api__get_file__err_400__content_id_is_not_int(self) -> None: # nopep8
  1321. """
  1322. Get one file, content_id is not int
  1323. """
  1324. self.testapp.authorization = (
  1325. 'Basic',
  1326. (
  1327. 'admin@admin.admin',
  1328. 'admin@admin.admin'
  1329. )
  1330. )
  1331. self.testapp.get(
  1332. '/api/v2/workspaces/2/files/coucou',
  1333. status=400
  1334. )
  1335. def test_api__update_file_info_err_400__empty_label(self) -> None: # nopep8
  1336. """
  1337. Update(put) one file
  1338. """
  1339. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1340. admin = dbsession.query(models.User) \
  1341. .filter(models.User.email == 'admin@admin.admin') \
  1342. .one()
  1343. workspace_api = WorkspaceApi(
  1344. current_user=admin,
  1345. session=dbsession,
  1346. config=self.app_config
  1347. )
  1348. content_api = ContentApi(
  1349. current_user=admin,
  1350. session=dbsession,
  1351. config=self.app_config
  1352. )
  1353. business_workspace = workspace_api.get_one(1)
  1354. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1355. test_file = content_api.create(
  1356. content_type_slug=CONTENT_TYPES.File.slug,
  1357. workspace=business_workspace,
  1358. parent=tool_folder,
  1359. label='Test file',
  1360. do_save=False,
  1361. do_notify=False,
  1362. )
  1363. test_file.file_extension = '.txt'
  1364. test_file.depot_file = FileIntent(
  1365. b'Test file',
  1366. 'Test_file.txt',
  1367. 'text/plain',
  1368. )
  1369. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  1370. dbsession.flush()
  1371. transaction.commit()
  1372. self.testapp.authorization = (
  1373. 'Basic',
  1374. (
  1375. 'admin@admin.admin',
  1376. 'admin@admin.admin'
  1377. )
  1378. )
  1379. params = {
  1380. 'label': '',
  1381. 'raw_content': '<p> Le nouveau contenu </p>',
  1382. }
  1383. self.testapp.put_json(
  1384. '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
  1385. params=params,
  1386. status=400
  1387. )
  1388. def test_api__update_file_info__ok_200__nominal_case(self) -> None:
  1389. """
  1390. Update(put) one file
  1391. """
  1392. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1393. admin = dbsession.query(models.User) \
  1394. .filter(models.User.email == 'admin@admin.admin') \
  1395. .one()
  1396. workspace_api = WorkspaceApi(
  1397. current_user=admin,
  1398. session=dbsession,
  1399. config=self.app_config
  1400. )
  1401. content_api = ContentApi(
  1402. current_user=admin,
  1403. session=dbsession,
  1404. config=self.app_config
  1405. )
  1406. business_workspace = workspace_api.get_one(1)
  1407. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1408. test_file = content_api.create(
  1409. content_type_slug=CONTENT_TYPES.File.slug,
  1410. workspace=business_workspace,
  1411. parent=tool_folder,
  1412. label='Test file',
  1413. do_save=False,
  1414. do_notify=False,
  1415. )
  1416. test_file.file_extension = '.txt'
  1417. test_file.depot_file = FileIntent(
  1418. b'Test file',
  1419. 'Test_file.txt',
  1420. 'text/plain',
  1421. )
  1422. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  1423. dbsession.flush()
  1424. transaction.commit()
  1425. self.testapp.authorization = (
  1426. 'Basic',
  1427. (
  1428. 'admin@admin.admin',
  1429. 'admin@admin.admin'
  1430. )
  1431. )
  1432. params = {
  1433. 'label': 'My New label',
  1434. 'raw_content': '<p> Le nouveau contenu </p>',
  1435. }
  1436. res = self.testapp.put_json(
  1437. '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
  1438. params=params,
  1439. status=200
  1440. )
  1441. content = res.json_body
  1442. assert content['content_type'] == 'file'
  1443. assert content['content_id'] == test_file.content_id
  1444. assert content['is_archived'] is False
  1445. assert content['is_deleted'] is False
  1446. assert content['label'] == 'My New label'
  1447. assert content['parent_id'] == 1
  1448. assert content['show_in_ui'] is True
  1449. assert content['slug'] == 'my-new-label'
  1450. assert content['status'] == 'open'
  1451. assert content['workspace_id'] == 1
  1452. assert content['current_revision_id']
  1453. # TODO - G.M - 2018-06-173 - check date format
  1454. assert content['created']
  1455. assert content['author']
  1456. assert content['author']['user_id'] == 1
  1457. assert content['author']['avatar_url'] is None
  1458. assert content['author']['public_name'] == 'Global manager'
  1459. # TODO - G.M - 2018-06-173 - check date format
  1460. assert content['modified']
  1461. assert content['last_modifier'] == content['author']
  1462. assert content['raw_content'] == '<p> Le nouveau contenu </p>'
  1463. res = self.testapp.get(
  1464. '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
  1465. status=200
  1466. )
  1467. content = res.json_body
  1468. assert content['content_type'] == 'file'
  1469. assert content['content_id'] == test_file.content_id
  1470. assert content['is_archived'] is False
  1471. assert content['is_deleted'] is False
  1472. assert content['label'] == 'My New label'
  1473. assert content['parent_id'] == 1
  1474. assert content['show_in_ui'] is True
  1475. assert content['slug'] == 'my-new-label'
  1476. assert content['status'] == 'open'
  1477. assert content['workspace_id'] == 1
  1478. assert content['current_revision_id']
  1479. # TODO - G.M - 2018-06-173 - check date format
  1480. assert content['created']
  1481. assert content['author']
  1482. assert content['author']['user_id'] == 1
  1483. assert content['author']['avatar_url'] is None
  1484. assert content['author']['public_name'] == 'Global manager'
  1485. # TODO - G.M - 2018-06-173 - check date format
  1486. assert content['modified']
  1487. assert content['last_modifier'] == content['author']
  1488. assert content['raw_content'] == '<p> Le nouveau contenu </p>'
  1489. def test_api__get_file_revisions__ok_200__nominal_case(
  1490. self
  1491. ) -> None:
  1492. """
  1493. Get file revisions
  1494. """
  1495. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1496. admin = dbsession.query(models.User) \
  1497. .filter(models.User.email == 'admin@admin.admin') \
  1498. .one()
  1499. workspace_api = WorkspaceApi(
  1500. current_user=admin,
  1501. session=dbsession,
  1502. config=self.app_config
  1503. )
  1504. content_api = ContentApi(
  1505. current_user=admin,
  1506. session=dbsession,
  1507. config=self.app_config
  1508. )
  1509. business_workspace = workspace_api.get_one(1)
  1510. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1511. test_file = content_api.create(
  1512. content_type_slug=CONTENT_TYPES.File.slug,
  1513. workspace=business_workspace,
  1514. parent=tool_folder,
  1515. label='Test file',
  1516. do_save=False,
  1517. do_notify=False,
  1518. )
  1519. test_file.file_extension = '.txt'
  1520. test_file.depot_file = FileIntent(
  1521. b'Test file',
  1522. 'Test_file.txt',
  1523. 'text/plain',
  1524. )
  1525. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  1526. dbsession.flush()
  1527. transaction.commit()
  1528. self.testapp.authorization = (
  1529. 'Basic',
  1530. (
  1531. 'admin@admin.admin',
  1532. 'admin@admin.admin'
  1533. )
  1534. )
  1535. res = self.testapp.get(
  1536. '/api/v2/workspaces/1/files/{}/revisions'.format(test_file.content_id), # nopep8
  1537. status=200
  1538. )
  1539. revisions = res.json_body
  1540. assert len(revisions) == 1
  1541. revision = revisions[0]
  1542. assert revision['content_type'] == 'file'
  1543. assert revision['content_id'] == test_file.content_id
  1544. assert revision['is_archived'] is False
  1545. assert revision['is_deleted'] is False
  1546. assert revision['label'] == 'Test_file'
  1547. assert revision['parent_id'] == 1
  1548. assert revision['show_in_ui'] is True
  1549. assert revision['slug'] == 'test-file'
  1550. assert revision['status'] == 'open'
  1551. assert revision['workspace_id'] == 1
  1552. assert revision['revision_id']
  1553. assert revision['sub_content_types']
  1554. # TODO - G.M - 2018-06-173 - Test with real comments
  1555. assert revision['comment_ids'] == []
  1556. # TODO - G.M - 2018-06-173 - check date format
  1557. assert revision['created']
  1558. assert revision['author']
  1559. assert revision['author']['user_id'] == 1
  1560. assert revision['author']['avatar_url'] is None
  1561. assert revision['author']['public_name'] == 'Global manager'
  1562. def test_api__set_file_status__ok_200__nominal_case(self) -> None:
  1563. """
  1564. set file status
  1565. """
  1566. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1567. admin = dbsession.query(models.User) \
  1568. .filter(models.User.email == 'admin@admin.admin') \
  1569. .one()
  1570. workspace_api = WorkspaceApi(
  1571. current_user=admin,
  1572. session=dbsession,
  1573. config=self.app_config
  1574. )
  1575. content_api = ContentApi(
  1576. current_user=admin,
  1577. session=dbsession,
  1578. config=self.app_config
  1579. )
  1580. business_workspace = workspace_api.get_one(1)
  1581. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1582. test_file = content_api.create(
  1583. content_type_slug=CONTENT_TYPES.File.slug,
  1584. workspace=business_workspace,
  1585. parent=tool_folder,
  1586. label='Test file',
  1587. do_save=False,
  1588. do_notify=False,
  1589. )
  1590. test_file.file_extension = '.txt'
  1591. test_file.depot_file = FileIntent(
  1592. b'Test file',
  1593. 'Test_file.txt',
  1594. 'text/plain',
  1595. )
  1596. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  1597. dbsession.flush()
  1598. transaction.commit()
  1599. self.testapp.authorization = (
  1600. 'Basic',
  1601. (
  1602. 'admin@admin.admin',
  1603. 'admin@admin.admin'
  1604. )
  1605. )
  1606. params = {
  1607. 'status': 'closed-deprecated',
  1608. }
  1609. # before
  1610. res = self.testapp.get(
  1611. '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
  1612. status=200
  1613. )
  1614. content = res.json_body
  1615. assert content['content_type'] == 'file'
  1616. assert content['content_id'] == test_file.content_id
  1617. assert content['status'] == 'open'
  1618. # set status
  1619. self.testapp.put_json(
  1620. '/api/v2/workspaces/1/files/{}/status'.format(test_file.content_id),
  1621. params=params,
  1622. status=204
  1623. )
  1624. # after
  1625. res = self.testapp.get(
  1626. '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
  1627. status=200
  1628. )
  1629. content = res.json_body
  1630. assert content['content_type'] == 'file'
  1631. assert content['content_id'] == test_file.content_id
  1632. assert content['status'] == 'closed-deprecated'
  1633. def test_api__set_file_status__err_400__wrong_status(self) -> None:
  1634. """
  1635. set file status
  1636. """
  1637. self.testapp.authorization = (
  1638. 'Basic',
  1639. (
  1640. 'admin@admin.admin',
  1641. 'admin@admin.admin'
  1642. )
  1643. )
  1644. params = {
  1645. 'status': 'unexistant-status',
  1646. }
  1647. self.testapp.put_json(
  1648. '/api/v2/workspaces/2/files/6/status',
  1649. params=params,
  1650. status=400
  1651. )
  1652. def test_api__get_file_raw__ok_200__nominal_case(self) -> None:
  1653. """
  1654. Get one file of a content
  1655. """
  1656. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1657. admin = dbsession.query(models.User) \
  1658. .filter(models.User.email == 'admin@admin.admin') \
  1659. .one()
  1660. workspace_api = WorkspaceApi(
  1661. current_user=admin,
  1662. session=dbsession,
  1663. config=self.app_config
  1664. )
  1665. content_api = ContentApi(
  1666. current_user=admin,
  1667. session=dbsession,
  1668. config=self.app_config
  1669. )
  1670. business_workspace = workspace_api.get_one(1)
  1671. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1672. test_file = content_api.create(
  1673. content_type_slug=CONTENT_TYPES.File.slug,
  1674. workspace=business_workspace,
  1675. parent=tool_folder,
  1676. label='Test file',
  1677. do_save=False,
  1678. do_notify=False,
  1679. )
  1680. test_file.file_extension = '.txt'
  1681. test_file.depot_file = FileIntent(
  1682. b'Test file',
  1683. 'Test_file.txt',
  1684. 'text/plain',
  1685. )
  1686. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  1687. dbsession.flush()
  1688. transaction.commit()
  1689. content_id = int(test_file.content_id)
  1690. self.testapp.authorization = (
  1691. 'Basic',
  1692. (
  1693. 'admin@admin.admin',
  1694. 'admin@admin.admin'
  1695. )
  1696. )
  1697. res = self.testapp.get(
  1698. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  1699. status=200
  1700. )
  1701. assert res.body == b'Test file'
  1702. assert res.content_type == 'text/plain'
  1703. assert res.content_length == len(b'Test file')
  1704. def test_api__set_file_raw__ok_200__nominal_case(self) -> None:
  1705. """
  1706. Set one file of a content
  1707. """
  1708. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1709. admin = dbsession.query(models.User) \
  1710. .filter(models.User.email == 'admin@admin.admin') \
  1711. .one()
  1712. workspace_api = WorkspaceApi(
  1713. current_user=admin,
  1714. session=dbsession,
  1715. config=self.app_config
  1716. )
  1717. content_api = ContentApi(
  1718. current_user=admin,
  1719. session=dbsession,
  1720. config=self.app_config
  1721. )
  1722. business_workspace = workspace_api.get_one(1)
  1723. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1724. test_file = content_api.create(
  1725. content_type_slug=CONTENT_TYPES.File.slug,
  1726. workspace=business_workspace,
  1727. parent=tool_folder,
  1728. label='Test file',
  1729. do_save=True,
  1730. do_notify=False,
  1731. )
  1732. dbsession.flush()
  1733. transaction.commit()
  1734. content_id = int(test_file.content_id)
  1735. image = create_1000px_png_test_image()
  1736. self.testapp.authorization = (
  1737. 'Basic',
  1738. (
  1739. 'admin@admin.admin',
  1740. 'admin@admin.admin'
  1741. )
  1742. )
  1743. self.testapp.put(
  1744. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  1745. upload_files=[
  1746. ('files', image.name, image.getvalue())
  1747. ],
  1748. status=204,
  1749. )
  1750. res = self.testapp.get(
  1751. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  1752. status=200
  1753. )
  1754. assert res.body == image.getvalue()
  1755. assert res.content_type == 'image/png'
  1756. assert res.content_length == len(image.getvalue())
  1757. def test_api__get_allowed_size_dim__ok__nominal_case(self) -> None:
  1758. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1759. admin = dbsession.query(models.User) \
  1760. .filter(models.User.email == 'admin@admin.admin') \
  1761. .one()
  1762. workspace_api = WorkspaceApi(
  1763. current_user=admin,
  1764. session=dbsession,
  1765. config=self.app_config
  1766. )
  1767. content_api = ContentApi(
  1768. current_user=admin,
  1769. session=dbsession,
  1770. config=self.app_config
  1771. )
  1772. business_workspace = workspace_api.get_one(1)
  1773. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1774. test_file = content_api.create(
  1775. content_type_slug=CONTENT_TYPES.File.slug,
  1776. workspace=business_workspace,
  1777. parent=tool_folder,
  1778. label='Test file',
  1779. do_save=False,
  1780. do_notify=False,
  1781. )
  1782. test_file.file_extension = '.txt'
  1783. test_file.depot_file = FileIntent(
  1784. b'Test file',
  1785. 'Test_file.txt',
  1786. 'text/plain',
  1787. )
  1788. dbsession.flush()
  1789. transaction.commit()
  1790. self.testapp.authorization = (
  1791. 'Basic',
  1792. (
  1793. 'admin@admin.admin',
  1794. 'admin@admin.admin'
  1795. )
  1796. )
  1797. content_id = int(test_file.content_id)
  1798. res = self.testapp.get(
  1799. '/api/v2/workspaces/1/files/{}/preview/jpg/allowed_dims'.format(content_id), # nopep8
  1800. status=200,
  1801. )
  1802. res = res.json_body
  1803. assert res['restricted'] is True
  1804. assert len(res['dimensions']) == 1
  1805. dim = res['dimensions'][0]
  1806. assert dim['width'] == 256
  1807. assert dim['height'] == 256
  1808. def test_api__get_jpeg_preview__ok__200__nominal_case(self) -> None:
  1809. """
  1810. Set one file of a content
  1811. """
  1812. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1813. admin = dbsession.query(models.User) \
  1814. .filter(models.User.email == 'admin@admin.admin') \
  1815. .one()
  1816. workspace_api = WorkspaceApi(
  1817. current_user=admin,
  1818. session=dbsession,
  1819. config=self.app_config
  1820. )
  1821. content_api = ContentApi(
  1822. current_user=admin,
  1823. session=dbsession,
  1824. config=self.app_config
  1825. )
  1826. business_workspace = workspace_api.get_one(1)
  1827. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1828. test_file = content_api.create(
  1829. content_type_slug=CONTENT_TYPES.File.slug,
  1830. workspace=business_workspace,
  1831. parent=tool_folder,
  1832. label='Test file',
  1833. do_save=False,
  1834. do_notify=False,
  1835. )
  1836. test_file.file_extension = '.txt'
  1837. test_file.depot_file = FileIntent(
  1838. b'Test file',
  1839. 'Test_file.txt',
  1840. 'text/plain',
  1841. )
  1842. dbsession.flush()
  1843. transaction.commit()
  1844. content_id = int(test_file.content_id)
  1845. image = create_1000px_png_test_image()
  1846. self.testapp.authorization = (
  1847. 'Basic',
  1848. (
  1849. 'admin@admin.admin',
  1850. 'admin@admin.admin'
  1851. )
  1852. )
  1853. self.testapp.put(
  1854. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  1855. upload_files=[
  1856. ('files', image.name, image.getvalue())
  1857. ],
  1858. status=204,
  1859. )
  1860. res = self.testapp.get(
  1861. '/api/v2/workspaces/1/files/{}/preview/jpg'.format(content_id),
  1862. status=200
  1863. )
  1864. assert res.body != image.getvalue()
  1865. assert res.content_type == 'image/jpeg'
  1866. def test_api__get_sized_jpeg_preview__ok__200__nominal_case(self) -> None:
  1867. """
  1868. get 256x256 preview of a txt file
  1869. """
  1870. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1871. admin = dbsession.query(models.User) \
  1872. .filter(models.User.email == 'admin@admin.admin') \
  1873. .one()
  1874. workspace_api = WorkspaceApi(
  1875. current_user=admin,
  1876. session=dbsession,
  1877. config=self.app_config
  1878. )
  1879. content_api = ContentApi(
  1880. current_user=admin,
  1881. session=dbsession,
  1882. config=self.app_config
  1883. )
  1884. business_workspace = workspace_api.get_one(1)
  1885. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1886. test_file = content_api.create(
  1887. content_type_slug=CONTENT_TYPES.File.slug,
  1888. workspace=business_workspace,
  1889. parent=tool_folder,
  1890. label='Test file',
  1891. do_save=True,
  1892. do_notify=False,
  1893. )
  1894. dbsession.flush()
  1895. transaction.commit()
  1896. content_id = int(test_file.content_id)
  1897. image = create_1000px_png_test_image()
  1898. self.testapp.authorization = (
  1899. 'Basic',
  1900. (
  1901. 'admin@admin.admin',
  1902. 'admin@admin.admin'
  1903. )
  1904. )
  1905. self.testapp.put(
  1906. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  1907. upload_files=[
  1908. ('files', image.name, image.getvalue())
  1909. ],
  1910. status=204,
  1911. )
  1912. res = self.testapp.get(
  1913. '/api/v2/workspaces/1/files/{}/preview/jpg/256x256'.format(content_id), # nopep8
  1914. status=200
  1915. )
  1916. assert res.body != image.getvalue()
  1917. assert res.content_type == 'image/jpeg'
  1918. new_image = Image.open(io.BytesIO(res.body))
  1919. assert 256, 256 == new_image.size
  1920. def test_api__get_sized_jpeg_preview__err__400__SizeNotAllowed(self) -> None: # nopep8
  1921. """
  1922. get 256x256 preview of a txt file
  1923. """
  1924. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1925. admin = dbsession.query(models.User) \
  1926. .filter(models.User.email == 'admin@admin.admin') \
  1927. .one()
  1928. workspace_api = WorkspaceApi(
  1929. current_user=admin,
  1930. session=dbsession,
  1931. config=self.app_config
  1932. )
  1933. content_api = ContentApi(
  1934. current_user=admin,
  1935. session=dbsession,
  1936. config=self.app_config
  1937. )
  1938. business_workspace = workspace_api.get_one(1)
  1939. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1940. test_file = content_api.create(
  1941. content_type_slug=CONTENT_TYPES.File.slug,
  1942. workspace=business_workspace,
  1943. parent=tool_folder,
  1944. label='Test file',
  1945. do_save=True,
  1946. do_notify=False,
  1947. )
  1948. dbsession.flush()
  1949. transaction.commit()
  1950. content_id = int(test_file.content_id)
  1951. image = create_1000px_png_test_image()
  1952. self.testapp.authorization = (
  1953. 'Basic',
  1954. (
  1955. 'admin@admin.admin',
  1956. 'admin@admin.admin'
  1957. )
  1958. )
  1959. self.testapp.put(
  1960. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  1961. upload_files=[
  1962. ('files', image.name, image.getvalue())
  1963. ],
  1964. status=204,
  1965. )
  1966. self.testapp.get(
  1967. '/api/v2/workspaces/1/files/{}/preview/jpg/512x512'.format(content_id), # nopep8
  1968. status=400
  1969. )
  1970. def test_api__get_sized_jpeg_revision_preview__ok__200__nominal_case(self) -> None: # nopep8
  1971. """
  1972. get 256x256 revision preview of a txt file
  1973. """
  1974. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1975. admin = dbsession.query(models.User) \
  1976. .filter(models.User.email == 'admin@admin.admin') \
  1977. .one()
  1978. workspace_api = WorkspaceApi(
  1979. current_user=admin,
  1980. session=dbsession,
  1981. config=self.app_config
  1982. )
  1983. content_api = ContentApi(
  1984. current_user=admin,
  1985. session=dbsession,
  1986. config=self.app_config
  1987. )
  1988. business_workspace = workspace_api.get_one(1)
  1989. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1990. test_file = content_api.create(
  1991. content_type_slug=CONTENT_TYPES.File.slug,
  1992. workspace=business_workspace,
  1993. parent=tool_folder,
  1994. label='Test file',
  1995. do_save=False,
  1996. do_notify=False,
  1997. )
  1998. test_file.file_extension = '.txt'
  1999. test_file.depot_file = FileIntent(
  2000. b'Test file',
  2001. 'Test_file.txt',
  2002. 'text/plain',
  2003. )
  2004. dbsession.flush()
  2005. transaction.commit()
  2006. content_id = int(test_file.content_id)
  2007. revision_id = int(test_file.revision_id)
  2008. image = create_1000px_png_test_image()
  2009. self.testapp.authorization = (
  2010. 'Basic',
  2011. (
  2012. 'admin@admin.admin',
  2013. 'admin@admin.admin'
  2014. )
  2015. )
  2016. self.testapp.put(
  2017. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  2018. upload_files=[
  2019. ('files', image.name, image.getvalue())
  2020. ],
  2021. status=204,
  2022. )
  2023. res = self.testapp.get(
  2024. '/api/v2/workspaces/1/files/{content_id}/revisions/{revision_id}/raw'.format( # nopep8
  2025. content_id=content_id,
  2026. revision_id=revision_id,
  2027. ),
  2028. status=200
  2029. )
  2030. assert res.content_type == 'text/plain'
  2031. res = self.testapp.get(
  2032. '/api/v2/workspaces/1/files/{content_id}/revisions/{revision_id}/preview/jpg/256x256'.format( # nopep8
  2033. content_id=content_id,
  2034. revision_id=revision_id,
  2035. ),
  2036. status=200
  2037. )
  2038. assert res.body != image.getvalue()
  2039. assert res.content_type == 'image/jpeg'
  2040. new_image = Image.open(io.BytesIO(res.body))
  2041. assert 256, 256 == new_image.size
  2042. def test_api__get_full_pdf_preview__ok__200__nominal_case(self) -> None:
  2043. """
  2044. get full pdf preview of a txt file
  2045. """
  2046. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2047. admin = dbsession.query(models.User) \
  2048. .filter(models.User.email == 'admin@admin.admin') \
  2049. .one()
  2050. workspace_api = WorkspaceApi(
  2051. current_user=admin,
  2052. session=dbsession,
  2053. config=self.app_config
  2054. )
  2055. content_api = ContentApi(
  2056. current_user=admin,
  2057. session=dbsession,
  2058. config=self.app_config
  2059. )
  2060. business_workspace = workspace_api.get_one(1)
  2061. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  2062. test_file = content_api.create(
  2063. content_type_slug=CONTENT_TYPES.File.slug,
  2064. workspace=business_workspace,
  2065. parent=tool_folder,
  2066. label='Test file',
  2067. do_save=True,
  2068. do_notify=False,
  2069. )
  2070. with new_revision(
  2071. session=dbsession,
  2072. tm=transaction.manager,
  2073. content=test_file,
  2074. ):
  2075. test_file.file_extension = '.txt'
  2076. test_file.depot_file = FileIntent(
  2077. b'Test file',
  2078. 'Test_file.txt',
  2079. 'text/plain',
  2080. )
  2081. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  2082. dbsession.flush()
  2083. transaction.commit()
  2084. content_id = int(test_file.content_id)
  2085. self.testapp.authorization = (
  2086. 'Basic',
  2087. (
  2088. 'admin@admin.admin',
  2089. 'admin@admin.admin'
  2090. )
  2091. )
  2092. self.testapp.put(
  2093. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  2094. upload_files=[
  2095. ('files', test_file.file_name, test_file.depot_file.file.read())
  2096. ],
  2097. status=204,
  2098. )
  2099. res = self.testapp.get(
  2100. '/api/v2/workspaces/1/files/{}/preview/pdf/full'.format(content_id), # nopep8
  2101. status=200
  2102. )
  2103. assert res.content_type == 'application/pdf'
  2104. def test_api__get_full_pdf_preview__err__400__png_UnavailablePreviewType(self) -> None: # nopep8
  2105. """
  2106. get full pdf preview of a png image -> error UnavailablePreviewType
  2107. """
  2108. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2109. admin = dbsession.query(models.User) \
  2110. .filter(models.User.email == 'admin@admin.admin') \
  2111. .one()
  2112. workspace_api = WorkspaceApi(
  2113. current_user=admin,
  2114. session=dbsession,
  2115. config=self.app_config
  2116. )
  2117. content_api = ContentApi(
  2118. current_user=admin,
  2119. session=dbsession,
  2120. config=self.app_config
  2121. )
  2122. business_workspace = workspace_api.get_one(1)
  2123. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  2124. test_file = content_api.create(
  2125. content_type_slug=CONTENT_TYPES.File.slug,
  2126. workspace=business_workspace,
  2127. parent=tool_folder,
  2128. label='Test file',
  2129. do_save=True,
  2130. do_notify=False,
  2131. )
  2132. dbsession.flush()
  2133. transaction.commit()
  2134. content_id = int(test_file.content_id)
  2135. image = create_1000px_png_test_image()
  2136. self.testapp.authorization = (
  2137. 'Basic',
  2138. (
  2139. 'admin@admin.admin',
  2140. 'admin@admin.admin'
  2141. )
  2142. )
  2143. self.testapp.put(
  2144. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  2145. upload_files=[
  2146. ('files', image.name, image.getvalue())
  2147. ],
  2148. status=204,
  2149. )
  2150. self.testapp.get(
  2151. '/api/v2/workspaces/1/files/{}/preview/pdf/full'.format(content_id), # nopep8
  2152. status=400
  2153. )
  2154. def test_api__get_pdf_preview__ok__200__nominal_case(self) -> None:
  2155. """
  2156. get full pdf preview of a txt file
  2157. """
  2158. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2159. admin = dbsession.query(models.User) \
  2160. .filter(models.User.email == 'admin@admin.admin') \
  2161. .one()
  2162. workspace_api = WorkspaceApi(
  2163. current_user=admin,
  2164. session=dbsession,
  2165. config=self.app_config
  2166. )
  2167. content_api = ContentApi(
  2168. current_user=admin,
  2169. session=dbsession,
  2170. config=self.app_config
  2171. )
  2172. business_workspace = workspace_api.get_one(1)
  2173. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  2174. test_file = content_api.create(
  2175. content_type_slug=CONTENT_TYPES.File.slug,
  2176. workspace=business_workspace,
  2177. parent=tool_folder,
  2178. label='Test file',
  2179. do_save=True,
  2180. do_notify=False,
  2181. )
  2182. with new_revision(
  2183. session=dbsession,
  2184. tm=transaction.manager,
  2185. content=test_file,
  2186. ):
  2187. test_file.file_extension = '.txt'
  2188. test_file.depot_file = FileIntent(
  2189. b'Test file',
  2190. 'Test_file.txt',
  2191. 'text/plain',
  2192. )
  2193. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  2194. dbsession.flush()
  2195. transaction.commit()
  2196. content_id = int(test_file.content_id)
  2197. self.testapp.authorization = (
  2198. 'Basic',
  2199. (
  2200. 'admin@admin.admin',
  2201. 'admin@admin.admin'
  2202. )
  2203. )
  2204. self.testapp.put(
  2205. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  2206. upload_files=[
  2207. ('files', test_file.file_name, test_file.depot_file.file.read())
  2208. ],
  2209. status=204,
  2210. )
  2211. params = {'page': 0}
  2212. res = self.testapp.get(
  2213. '/api/v2/workspaces/1/files/{}/preview/pdf'.format(content_id),
  2214. status=200,
  2215. params=params,
  2216. )
  2217. assert res.content_type == 'application/pdf'
  2218. def test_api__get_pdf_preview__ok__err__400_page_of_preview_not_found(self) -> None: # nopep8
  2219. """
  2220. get full pdf preview of a txt file
  2221. """
  2222. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2223. admin = dbsession.query(models.User) \
  2224. .filter(models.User.email == 'admin@admin.admin') \
  2225. .one()
  2226. workspace_api = WorkspaceApi(
  2227. current_user=admin,
  2228. session=dbsession,
  2229. config=self.app_config
  2230. )
  2231. content_api = ContentApi(
  2232. current_user=admin,
  2233. session=dbsession,
  2234. config=self.app_config
  2235. )
  2236. business_workspace = workspace_api.get_one(1)
  2237. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  2238. test_file = content_api.create(
  2239. content_type_slug=CONTENT_TYPES.File.slug,
  2240. workspace=business_workspace,
  2241. parent=tool_folder,
  2242. label='Test file',
  2243. do_save=True,
  2244. do_notify=False,
  2245. )
  2246. with new_revision(
  2247. session=dbsession,
  2248. tm=transaction.manager,
  2249. content=test_file,
  2250. ):
  2251. test_file.file_extension = '.txt'
  2252. test_file.depot_file = FileIntent(
  2253. b'Test file',
  2254. 'Test_file.txt',
  2255. 'text/plain',
  2256. )
  2257. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  2258. dbsession.flush()
  2259. transaction.commit()
  2260. content_id = int(test_file.content_id)
  2261. self.testapp.authorization = (
  2262. 'Basic',
  2263. (
  2264. 'admin@admin.admin',
  2265. 'admin@admin.admin'
  2266. )
  2267. )
  2268. self.testapp.put(
  2269. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  2270. upload_files=[
  2271. ('files', test_file.file_name, test_file.depot_file.file.read())
  2272. ],
  2273. status=204,
  2274. )
  2275. params = {'page': 1}
  2276. self.testapp.get(
  2277. '/api/v2/workspaces/1/files/{}/preview/pdf'.format(content_id),
  2278. status=400,
  2279. params=params,
  2280. )
  2281. def test_api__get_pdf_revision_preview__ok__200__nominal_case(self) -> None:
  2282. """
  2283. get pdf revision preview of content
  2284. """
  2285. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2286. admin = dbsession.query(models.User) \
  2287. .filter(models.User.email == 'admin@admin.admin') \
  2288. .one()
  2289. workspace_api = WorkspaceApi(
  2290. current_user=admin,
  2291. session=dbsession,
  2292. config=self.app_config
  2293. )
  2294. content_api = ContentApi(
  2295. current_user=admin,
  2296. session=dbsession,
  2297. config=self.app_config
  2298. )
  2299. business_workspace = workspace_api.get_one(1)
  2300. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  2301. test_file = content_api.create(
  2302. content_type_slug=CONTENT_TYPES.File.slug,
  2303. workspace=business_workspace,
  2304. parent=tool_folder,
  2305. label='Test file',
  2306. do_save=False,
  2307. do_notify=False,
  2308. )
  2309. test_file.file_extension = '.txt'
  2310. test_file.depot_file = FileIntent(
  2311. b'Test file',
  2312. 'Test_file.txt',
  2313. 'text/plain',
  2314. )
  2315. dbsession.flush()
  2316. transaction.commit()
  2317. content_id = int(test_file.content_id)
  2318. revision_id = int(test_file.revision_id)
  2319. image = create_1000px_png_test_image()
  2320. self.testapp.authorization = (
  2321. 'Basic',
  2322. (
  2323. 'admin@admin.admin',
  2324. 'admin@admin.admin'
  2325. )
  2326. )
  2327. self.testapp.put(
  2328. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  2329. upload_files=[
  2330. ('files', image.name, image.getvalue())
  2331. ],
  2332. status=204,
  2333. )
  2334. res = self.testapp.get(
  2335. '/api/v2/workspaces/1/files/{content_id}/revisions/{revision_id}/raw'.format( # nopep8
  2336. content_id=content_id,
  2337. revision_id=revision_id,
  2338. ),
  2339. status=200
  2340. )
  2341. assert res.content_type == 'text/plain'
  2342. params = {'page': 0}
  2343. res = self.testapp.get(
  2344. '/api/v2/workspaces/1/files/{content_id}/revisions/{revision_id}/preview/pdf'.format( # nopep8
  2345. content_id=content_id,
  2346. revision_id=revision_id,
  2347. params=params,
  2348. ),
  2349. status=200
  2350. )
  2351. assert res.content_type == 'application/pdf'
  2352. class TestThreads(FunctionalTest):
  2353. """
  2354. Tests for /api/v2/workspaces/{workspace_id}/threads/{content_id}
  2355. endpoint
  2356. """
  2357. fixtures = [BaseFixture, ContentFixtures]
  2358. def test_api__get_thread__err_400__wrong_content_type(self) -> None:
  2359. """
  2360. Get one html document of a content
  2361. """
  2362. self.testapp.authorization = (
  2363. 'Basic',
  2364. (
  2365. 'admin@admin.admin',
  2366. 'admin@admin.admin'
  2367. )
  2368. )
  2369. self.testapp.get(
  2370. '/api/v2/workspaces/2/threads/6',
  2371. status=400
  2372. )
  2373. def test_api__get_thread__ok_200__nominal_case(self) -> None:
  2374. """
  2375. Get one html document of a content
  2376. """
  2377. self.testapp.authorization = (
  2378. 'Basic',
  2379. (
  2380. 'admin@admin.admin',
  2381. 'admin@admin.admin'
  2382. )
  2383. )
  2384. res = self.testapp.get(
  2385. '/api/v2/workspaces/2/threads/7',
  2386. status=200
  2387. ) # nopep8
  2388. content = res.json_body
  2389. assert content['content_type'] == 'thread'
  2390. assert content['content_id'] == 7
  2391. assert content['is_archived'] is False
  2392. assert content['is_deleted'] is False
  2393. assert content['label'] == 'Best Cakes?'
  2394. assert content['parent_id'] == 3
  2395. assert content['show_in_ui'] is True
  2396. assert content['slug'] == 'best-cakes'
  2397. assert content['status'] == 'open'
  2398. assert content['workspace_id'] == 2
  2399. assert content['current_revision_id'] == 26
  2400. # TODO - G.M - 2018-06-173 - check date format
  2401. assert content['created']
  2402. assert content['author']
  2403. assert content['author']['user_id'] == 1
  2404. assert content['author']['avatar_url'] is None
  2405. assert content['author']['public_name'] == 'Global manager'
  2406. # TODO - G.M - 2018-06-173 - check date format
  2407. assert content['modified']
  2408. assert content['last_modifier'] != content['author']
  2409. assert content['last_modifier']['user_id'] == 3
  2410. assert content['last_modifier']['public_name'] == 'Bob i.'
  2411. assert content['last_modifier']['avatar_url'] is None
  2412. assert content['raw_content'] == 'What is the best cake?' # nopep8
  2413. def test_api__get_thread__err_400__content_does_not_exist(self) -> None:
  2414. """
  2415. Get one thread (content 170 does not exist)
  2416. """
  2417. self.testapp.authorization = (
  2418. 'Basic',
  2419. (
  2420. 'admin@admin.admin',
  2421. 'admin@admin.admin'
  2422. )
  2423. )
  2424. self.testapp.get(
  2425. '/api/v2/workspaces/2/threads/170',
  2426. status=400
  2427. )
  2428. def test_api__get_thread__err_400__content_not_in_workspace(self) -> None:
  2429. """
  2430. Get one thread(content 7 is in workspace 2)
  2431. """
  2432. self.testapp.authorization = (
  2433. 'Basic',
  2434. (
  2435. 'admin@admin.admin',
  2436. 'admin@admin.admin'
  2437. )
  2438. )
  2439. self.testapp.get(
  2440. '/api/v2/workspaces/1/threads/7',
  2441. status=400
  2442. )
  2443. def test_api__get_thread__err_400__workspace_does_not_exist(self) -> None: # nopep8
  2444. """
  2445. Get one thread (Workspace 40 does not exist)
  2446. """
  2447. self.testapp.authorization = (
  2448. 'Basic',
  2449. (
  2450. 'admin@admin.admin',
  2451. 'admin@admin.admin'
  2452. )
  2453. )
  2454. self.testapp.get(
  2455. '/api/v2/workspaces/40/threads/7',
  2456. status=400
  2457. )
  2458. def test_api__get_thread__err_400__workspace_id_is_not_int(self) -> None: # nopep8
  2459. """
  2460. Get one thread, workspace id is not int
  2461. """
  2462. self.testapp.authorization = (
  2463. 'Basic',
  2464. (
  2465. 'admin@admin.admin',
  2466. 'admin@admin.admin'
  2467. )
  2468. )
  2469. self.testapp.get(
  2470. '/api/v2/workspaces/coucou/threads/7',
  2471. status=400
  2472. )
  2473. def test_api__get_thread__err_400_content_id_is_not_int(self) -> None: # nopep8
  2474. """
  2475. Get one thread, content id is not int
  2476. """
  2477. self.testapp.authorization = (
  2478. 'Basic',
  2479. (
  2480. 'admin@admin.admin',
  2481. 'admin@admin.admin'
  2482. )
  2483. )
  2484. self.testapp.get(
  2485. '/api/v2/workspaces/2/threads/coucou',
  2486. status=400
  2487. )
  2488. def test_api__update_thread__ok_200__nominal_case(self) -> None:
  2489. """
  2490. Update(put) thread
  2491. """
  2492. self.testapp.authorization = (
  2493. 'Basic',
  2494. (
  2495. 'admin@admin.admin',
  2496. 'admin@admin.admin'
  2497. )
  2498. )
  2499. params = {
  2500. 'label': 'My New label',
  2501. 'raw_content': '<p> Le nouveau contenu </p>',
  2502. }
  2503. res = self.testapp.put_json(
  2504. '/api/v2/workspaces/2/threads/7',
  2505. params=params,
  2506. status=200
  2507. )
  2508. content = res.json_body
  2509. assert content['content_type'] == 'thread'
  2510. assert content['content_id'] == 7
  2511. assert content['is_archived'] is False
  2512. assert content['is_deleted'] is False
  2513. assert content['label'] == 'My New label'
  2514. assert content['parent_id'] == 3
  2515. assert content['show_in_ui'] is True
  2516. assert content['slug'] == 'my-new-label'
  2517. assert content['status'] == 'open'
  2518. assert content['workspace_id'] == 2
  2519. assert content['current_revision_id'] == 28
  2520. # TODO - G.M - 2018-06-173 - check date format
  2521. assert content['created']
  2522. assert content['author']
  2523. assert content['author']['user_id'] == 1
  2524. assert content['author']['avatar_url'] is None
  2525. assert content['author']['public_name'] == 'Global manager'
  2526. # TODO - G.M - 2018-06-173 - check date format
  2527. assert content['modified']
  2528. assert content['last_modifier'] == content['author']
  2529. assert content['raw_content'] == '<p> Le nouveau contenu </p>'
  2530. res = self.testapp.get(
  2531. '/api/v2/workspaces/2/threads/7',
  2532. status=200
  2533. ) # nopep8
  2534. content = res.json_body
  2535. assert content['content_type'] == 'thread'
  2536. assert content['content_id'] == 7
  2537. assert content['is_archived'] is False
  2538. assert content['is_deleted'] is False
  2539. assert content['label'] == 'My New label'
  2540. assert content['parent_id'] == 3
  2541. assert content['show_in_ui'] is True
  2542. assert content['slug'] == 'my-new-label'
  2543. assert content['status'] == 'open'
  2544. assert content['workspace_id'] == 2
  2545. assert content['current_revision_id'] == 28
  2546. # TODO - G.M - 2018-06-173 - check date format
  2547. assert content['created']
  2548. assert content['author']
  2549. assert content['author']['user_id'] == 1
  2550. assert content['author']['avatar_url'] is None
  2551. assert content['author']['public_name'] == 'Global manager'
  2552. # TODO - G.M - 2018-06-173 - check date format
  2553. assert content['modified']
  2554. assert content['last_modifier'] == content['author']
  2555. assert content['raw_content'] == '<p> Le nouveau contenu </p>'
  2556. def test_api__update_thread__err_400__empty_label(self) -> None:
  2557. """
  2558. Update(put) thread
  2559. """
  2560. self.testapp.authorization = (
  2561. 'Basic',
  2562. (
  2563. 'admin@admin.admin',
  2564. 'admin@admin.admin'
  2565. )
  2566. )
  2567. params = {
  2568. 'label': '',
  2569. 'raw_content': '<p> Le nouveau contenu </p>',
  2570. }
  2571. self.testapp.put_json(
  2572. '/api/v2/workspaces/2/threads/7',
  2573. params=params,
  2574. status=400
  2575. )
  2576. def test_api__get_thread_revisions__ok_200__nominal_case(
  2577. self
  2578. ) -> None:
  2579. """
  2580. Get threads revisions
  2581. """
  2582. self.testapp.authorization = (
  2583. 'Basic',
  2584. (
  2585. 'admin@admin.admin',
  2586. 'admin@admin.admin'
  2587. )
  2588. )
  2589. res = self.testapp.get(
  2590. '/api/v2/workspaces/2/threads/7/revisions',
  2591. status=200
  2592. )
  2593. revisions = res.json_body
  2594. assert len(revisions) == 2
  2595. revision = revisions[0]
  2596. assert revision['content_type'] == 'thread'
  2597. assert revision['content_id'] == 7
  2598. assert revision['is_archived'] is False
  2599. assert revision['is_deleted'] is False
  2600. assert revision['label'] == 'Best Cake'
  2601. assert revision['parent_id'] == 3
  2602. assert revision['show_in_ui'] is True
  2603. assert revision['slug'] == 'best-cake'
  2604. assert revision['status'] == 'open'
  2605. assert revision['workspace_id'] == 2
  2606. assert revision['revision_id'] == 8
  2607. assert revision['sub_content_types']
  2608. assert revision['revision_type'] == 'creation'
  2609. assert revision['comment_ids'] == [18, 19, 20]
  2610. # TODO - G.M - 2018-06-173 - check date format
  2611. assert revision['created']
  2612. assert revision['author']
  2613. assert revision['author']['user_id'] == 1
  2614. assert revision['author']['avatar_url'] is None
  2615. assert revision['author']['public_name'] == 'Global manager'
  2616. revision = revisions[1]
  2617. assert revision['content_type'] == 'thread'
  2618. assert revision['content_id'] == 7
  2619. assert revision['is_archived'] is False
  2620. assert revision['is_deleted'] is False
  2621. assert revision['label'] == 'Best Cakes?'
  2622. assert revision['parent_id'] == 3
  2623. assert revision['show_in_ui'] is True
  2624. assert revision['slug'] == 'best-cakes'
  2625. assert revision['status'] == 'open'
  2626. assert revision['workspace_id'] == 2
  2627. assert revision['revision_id'] == 26
  2628. assert revision['revision_type'] == 'edition'
  2629. assert revision['sub_content_types']
  2630. assert revision['comment_ids'] == []
  2631. # TODO - G.M - 2018-06-173 - check date format
  2632. assert revision['created']
  2633. assert revision['author']
  2634. assert revision['author']['user_id'] == 3
  2635. assert revision['author']['avatar_url'] is None
  2636. assert revision['author']['public_name'] == 'Bob i.'
  2637. def test_api__get_thread_revisions__ok_200__most_revision_type(self) -> None:
  2638. """
  2639. get threads revisions
  2640. """
  2641. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2642. admin = dbsession.query(models.User) \
  2643. .filter(models.User.email == 'admin@admin.admin') \
  2644. .one()
  2645. workspace_api = WorkspaceApi(
  2646. current_user=admin,
  2647. session=dbsession,
  2648. config=self.app_config
  2649. )
  2650. business_workspace = workspace_api.get_one(1)
  2651. content_api = ContentApi(
  2652. current_user=admin,
  2653. session=dbsession,
  2654. config=self.app_config
  2655. )
  2656. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  2657. test_thread = content_api.create(
  2658. content_type_slug=CONTENT_TYPES.Thread.slug,
  2659. workspace=business_workspace,
  2660. parent=tool_folder,
  2661. label='Test Thread',
  2662. do_save=True,
  2663. do_notify=False,
  2664. )
  2665. with new_revision(
  2666. session=dbsession,
  2667. tm=transaction.manager,
  2668. content=test_thread,
  2669. ):
  2670. content_api.update_content(
  2671. test_thread,
  2672. new_label='test_thread_updated',
  2673. new_content='Just a test'
  2674. )
  2675. content_api.save(test_thread)
  2676. with new_revision(
  2677. session=dbsession,
  2678. tm=transaction.manager,
  2679. content=test_thread,
  2680. ):
  2681. content_api.archive(test_thread)
  2682. content_api.save(test_thread)
  2683. with new_revision(
  2684. session=dbsession,
  2685. tm=transaction.manager,
  2686. content=test_thread,
  2687. ):
  2688. content_api.unarchive(test_thread)
  2689. content_api.save(test_thread)
  2690. with new_revision(
  2691. session=dbsession,
  2692. tm=transaction.manager,
  2693. content=test_thread,
  2694. ):
  2695. content_api.delete(test_thread)
  2696. content_api.save(test_thread)
  2697. with new_revision(
  2698. session=dbsession,
  2699. tm=transaction.manager,
  2700. content=test_thread,
  2701. ):
  2702. content_api.undelete(test_thread)
  2703. content_api.save(test_thread)
  2704. dbsession.flush()
  2705. transaction.commit()
  2706. self.testapp.authorization = (
  2707. 'Basic',
  2708. (
  2709. 'admin@admin.admin',
  2710. 'admin@admin.admin'
  2711. )
  2712. )
  2713. res = self.testapp.get(
  2714. '/api/v2/workspaces/1/threads/{}/revisions'.format(test_thread.content_id), # nopep8
  2715. status=200
  2716. )
  2717. revisions = res.json_body
  2718. assert len(revisions) == 6
  2719. for revision in revisions:
  2720. revision['content_type'] == 'thread'
  2721. revision['workspace_id'] == 1
  2722. revision['content_id'] == test_thread.content_id
  2723. revision = revisions[0]
  2724. revision['revision_type'] == 'creation'
  2725. revision = revisions[1]
  2726. revision['revision_type'] == 'archiving'
  2727. revision = revisions[2]
  2728. revision['revision_type'] == 'unarchiving'
  2729. revision = revisions[3]
  2730. revision['revision_type'] == 'deletion'
  2731. revision = revisions[4]
  2732. revision['revision_type'] == 'undeletion'
  2733. def test_api__set_thread_status__ok_200__nominal_case(self) -> None:
  2734. """
  2735. Set thread status
  2736. """
  2737. self.testapp.authorization = (
  2738. 'Basic',
  2739. (
  2740. 'admin@admin.admin',
  2741. 'admin@admin.admin'
  2742. )
  2743. )
  2744. params = {
  2745. 'status': 'closed-deprecated',
  2746. }
  2747. # before
  2748. res = self.testapp.get(
  2749. '/api/v2/workspaces/2/threads/7',
  2750. status=200
  2751. ) # nopep8
  2752. content = res.json_body
  2753. assert content['content_type'] == 'thread'
  2754. assert content['content_id'] == 7
  2755. assert content['status'] == 'open'
  2756. # set status
  2757. self.testapp.put_json(
  2758. '/api/v2/workspaces/2/threads/7/status',
  2759. params=params,
  2760. status=204
  2761. )
  2762. # after
  2763. res = self.testapp.get(
  2764. '/api/v2/workspaces/2/threads/7',
  2765. status=200
  2766. ) # nopep8
  2767. content = res.json_body
  2768. assert content['content_type'] == 'thread'
  2769. assert content['content_id'] == 7
  2770. assert content['status'] == 'closed-deprecated'
  2771. def test_api__set_thread_status__ok_400__wrong_status(self) -> None:
  2772. """
  2773. Set thread status
  2774. """
  2775. self.testapp.authorization = (
  2776. 'Basic',
  2777. (
  2778. 'admin@admin.admin',
  2779. 'admin@admin.admin'
  2780. )
  2781. )
  2782. params = {
  2783. 'status': 'unexistant-status',
  2784. }
  2785. self.testapp.put_json(
  2786. '/api/v2/workspaces/2/threads/7/status',
  2787. params=params,
  2788. status=400
  2789. )