test_contents.py 99KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943
  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.app_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__ok_200__archived_content(self) -> None:
  850. """
  851. Get one html document of a content
  852. """
  853. self.testapp.authorization = (
  854. 'Basic',
  855. (
  856. 'admin@admin.admin',
  857. 'admin@admin.admin'
  858. )
  859. )
  860. res = self.testapp.put_json(
  861. '/api/v2/workspaces/2/contents/6/archive',
  862. status=204
  863. )
  864. res = self.testapp.get(
  865. '/api/v2/workspaces/2/html-documents/6',
  866. status=200
  867. )
  868. content = res.json_body
  869. assert content['content_type'] == 'html-document'
  870. assert content['content_id'] == 6
  871. assert content['is_archived'] is True
  872. def test_api__get_html_document__ok_200__deleted_content(self) -> None:
  873. """
  874. Get one html document of a content
  875. """
  876. self.testapp.authorization = (
  877. 'Basic',
  878. (
  879. 'admin@admin.admin',
  880. 'admin@admin.admin'
  881. )
  882. )
  883. res = self.testapp.put_json(
  884. '/api/v2/workspaces/2/contents/6/delete',
  885. status=204
  886. )
  887. res = self.testapp.get(
  888. '/api/v2/workspaces/2/html-documents/6',
  889. status=200
  890. )
  891. content = res.json_body
  892. assert content['content_type'] == 'html-document'
  893. assert content['content_id'] == 6
  894. assert content['is_deleted'] is True
  895. def test_api__get_html_document__err_400__wrong_content_type(self) -> None:
  896. """
  897. Get one html document of a content content 7 is not html_document
  898. """
  899. self.testapp.authorization = (
  900. 'Basic',
  901. (
  902. 'admin@admin.admin',
  903. 'admin@admin.admin'
  904. )
  905. )
  906. self.testapp.get(
  907. '/api/v2/workspaces/2/html-documents/7',
  908. status=400
  909. )
  910. def test_api__get_html_document__err_400__content_does_not_exist(self) -> None: # nopep8
  911. """
  912. Get one html document of a content (content 170 does not exist in db
  913. """
  914. self.testapp.authorization = (
  915. 'Basic',
  916. (
  917. 'admin@admin.admin',
  918. 'admin@admin.admin'
  919. )
  920. )
  921. self.testapp.get(
  922. '/api/v2/workspaces/2/html-documents/170',
  923. status=400
  924. )
  925. def test_api__get_html_document__err_400__content_not_in_workspace(self) -> None: # nopep8
  926. """
  927. Get one html document of a content (content 6 is in workspace 2)
  928. """
  929. self.testapp.authorization = (
  930. 'Basic',
  931. (
  932. 'admin@admin.admin',
  933. 'admin@admin.admin'
  934. )
  935. )
  936. self.testapp.get(
  937. '/api/v2/workspaces/1/html-documents/6',
  938. status=400
  939. )
  940. def test_api__get_html_document__err_400__workspace_does_not_exist(self) -> None: # nopep8
  941. """
  942. Get one html document of a content (Workspace 40 does not exist)
  943. """
  944. self.testapp.authorization = (
  945. 'Basic',
  946. (
  947. 'admin@admin.admin',
  948. 'admin@admin.admin'
  949. )
  950. )
  951. self.testapp.get(
  952. '/api/v2/workspaces/40/html-documents/6',
  953. status=400
  954. )
  955. def test_api__get_html_document__err_400__workspace_id_is_not_int(self) -> None: # nopep8
  956. """
  957. Get one html document of a content, workspace id is not int
  958. """
  959. self.testapp.authorization = (
  960. 'Basic',
  961. (
  962. 'admin@admin.admin',
  963. 'admin@admin.admin'
  964. )
  965. )
  966. self.testapp.get(
  967. '/api/v2/workspaces/coucou/html-documents/6',
  968. status=400
  969. )
  970. def test_api__get_html_document__err_400__content_id_is_not_int(self) -> None: # nopep8
  971. """
  972. Get one html document of a content, content_id is not int
  973. """
  974. self.testapp.authorization = (
  975. 'Basic',
  976. (
  977. 'admin@admin.admin',
  978. 'admin@admin.admin'
  979. )
  980. )
  981. self.testapp.get(
  982. '/api/v2/workspaces/2/html-documents/coucou',
  983. status=400
  984. )
  985. def test_api__update_html_document__err_400__empty_label(self) -> None: # nopep8
  986. """
  987. Update(put) one html document of a content
  988. """
  989. self.testapp.authorization = (
  990. 'Basic',
  991. (
  992. 'admin@admin.admin',
  993. 'admin@admin.admin'
  994. )
  995. )
  996. params = {
  997. 'label': '',
  998. 'raw_content': '<p> Le nouveau contenu </p>',
  999. }
  1000. self.testapp.put_json(
  1001. '/api/v2/workspaces/2/html-documents/6',
  1002. params=params,
  1003. status=400
  1004. )
  1005. def test_api__update_html_document__ok_200__nominal_case(self) -> None:
  1006. """
  1007. Update(put) one html document of a content
  1008. """
  1009. self.testapp.authorization = (
  1010. 'Basic',
  1011. (
  1012. 'admin@admin.admin',
  1013. 'admin@admin.admin'
  1014. )
  1015. )
  1016. params = {
  1017. 'label': 'My New label',
  1018. 'raw_content': '<p> Le nouveau contenu </p>',
  1019. }
  1020. res = self.testapp.put_json(
  1021. '/api/v2/workspaces/2/html-documents/6',
  1022. params=params,
  1023. status=200
  1024. )
  1025. content = res.json_body
  1026. assert content['content_type'] == 'html-document'
  1027. assert content['content_id'] == 6
  1028. assert content['is_archived'] is False
  1029. assert content['is_deleted'] is False
  1030. assert content['label'] == 'My New label'
  1031. assert content['parent_id'] == 3
  1032. assert content['show_in_ui'] is True
  1033. assert content['slug'] == 'my-new-label'
  1034. assert content['status'] == 'open'
  1035. assert content['workspace_id'] == 2
  1036. assert content['current_revision_id'] == 28
  1037. # TODO - G.M - 2018-06-173 - check date format
  1038. assert content['created']
  1039. assert content['author']
  1040. assert content['author']['user_id'] == 1
  1041. assert content['author']['avatar_url'] is None
  1042. assert content['author']['public_name'] == 'Global manager'
  1043. # TODO - G.M - 2018-06-173 - check date format
  1044. assert content['modified']
  1045. assert content['last_modifier'] == content['author']
  1046. assert content['raw_content'] == '<p> Le nouveau contenu </p>'
  1047. res = self.testapp.get(
  1048. '/api/v2/workspaces/2/html-documents/6',
  1049. status=200
  1050. )
  1051. content = res.json_body
  1052. assert content['content_type'] == 'html-document'
  1053. assert content['content_id'] == 6
  1054. assert content['is_archived'] is False
  1055. assert content['is_deleted'] is False
  1056. assert content['label'] == 'My New label'
  1057. assert content['parent_id'] == 3
  1058. assert content['show_in_ui'] is True
  1059. assert content['slug'] == 'my-new-label'
  1060. assert content['status'] == 'open'
  1061. assert content['workspace_id'] == 2
  1062. assert content['current_revision_id'] == 28
  1063. # TODO - G.M - 2018-06-173 - check date format
  1064. assert content['created']
  1065. assert content['author']
  1066. assert content['author']['user_id'] == 1
  1067. assert content['author']['avatar_url'] is None
  1068. assert content['author']['public_name'] == 'Global manager'
  1069. # TODO - G.M - 2018-06-173 - check date format
  1070. assert content['modified']
  1071. assert content['last_modifier'] == content['author']
  1072. assert content['raw_content'] == '<p> Le nouveau contenu </p>'
  1073. def test_api__get_html_document_revisions__ok_200__nominal_case(
  1074. self
  1075. ) -> None:
  1076. """
  1077. Get one html document of a content
  1078. """
  1079. self.testapp.authorization = (
  1080. 'Basic',
  1081. (
  1082. 'admin@admin.admin',
  1083. 'admin@admin.admin'
  1084. )
  1085. )
  1086. res = self.testapp.get(
  1087. '/api/v2/workspaces/2/html-documents/6/revisions',
  1088. status=200
  1089. )
  1090. revisions = res.json_body
  1091. assert len(revisions) == 3
  1092. revision = revisions[0]
  1093. assert revision['content_type'] == 'html-document'
  1094. assert revision['content_id'] == 6
  1095. assert revision['is_archived'] is False
  1096. assert revision['is_deleted'] is False
  1097. assert revision['label'] == 'Tiramisu Recipes!!!'
  1098. assert revision['parent_id'] == 3
  1099. assert revision['show_in_ui'] is True
  1100. assert revision['slug'] == 'tiramisu-recipes'
  1101. assert revision['status'] == 'open'
  1102. assert revision['workspace_id'] == 2
  1103. assert revision['revision_id'] == 6
  1104. assert revision['revision_type'] == 'creation'
  1105. assert revision['sub_content_types']
  1106. # TODO - G.M - 2018-06-173 - Test with real comments
  1107. assert revision['comment_ids'] == []
  1108. # TODO - G.M - 2018-06-173 - check date format
  1109. assert revision['created']
  1110. assert revision['author']
  1111. assert revision['author']['user_id'] == 1
  1112. assert revision['author']['avatar_url'] is None
  1113. assert revision['author']['public_name'] == 'Global manager'
  1114. revision = revisions[1]
  1115. assert revision['content_type'] == 'html-document'
  1116. assert revision['content_id'] == 6
  1117. assert revision['is_archived'] is False
  1118. assert revision['is_deleted'] is False
  1119. assert revision['label'] == 'Tiramisu Recipes!!!'
  1120. assert revision['parent_id'] == 3
  1121. assert revision['show_in_ui'] is True
  1122. assert revision['slug'] == 'tiramisu-recipes'
  1123. assert revision['status'] == 'open'
  1124. assert revision['workspace_id'] == 2
  1125. assert revision['revision_id'] == 7
  1126. assert revision['revision_type'] == 'edition'
  1127. assert revision['sub_content_types']
  1128. # TODO - G.M - 2018-06-173 - Test with real comments
  1129. assert revision['comment_ids'] == []
  1130. # TODO - G.M - 2018-06-173 - check date format
  1131. assert revision['created']
  1132. assert revision['author']
  1133. assert revision['author']['user_id'] == 1
  1134. assert revision['author']['avatar_url'] is None
  1135. assert revision['author']['public_name'] == 'Global manager'
  1136. revision = revisions[2]
  1137. assert revision['content_type'] == 'html-document'
  1138. assert revision['content_id'] == 6
  1139. assert revision['is_archived'] is False
  1140. assert revision['is_deleted'] is False
  1141. assert revision['label'] == 'Tiramisu Recipe'
  1142. assert revision['parent_id'] == 3
  1143. assert revision['show_in_ui'] is True
  1144. assert revision['slug'] == 'tiramisu-recipe'
  1145. assert revision['status'] == 'open'
  1146. assert revision['workspace_id'] == 2
  1147. assert revision['revision_id'] == 27
  1148. assert revision['revision_type'] == 'edition'
  1149. assert revision['sub_content_types']
  1150. # TODO - G.M - 2018-06-173 - Test with real comments
  1151. assert revision['comment_ids'] == []
  1152. # TODO - G.M - 2018-06-173 - check date format
  1153. assert revision['created']
  1154. assert revision['author']
  1155. assert revision['author']['user_id'] == 3
  1156. assert revision['author']['avatar_url'] is None
  1157. assert revision['author']['public_name'] == 'Bob i.'
  1158. def test_api__set_html_document_status__ok_200__nominal_case(self) -> None:
  1159. """
  1160. Get one html document of a content
  1161. """
  1162. self.testapp.authorization = (
  1163. 'Basic',
  1164. (
  1165. 'admin@admin.admin',
  1166. 'admin@admin.admin'
  1167. )
  1168. )
  1169. params = {
  1170. 'status': 'closed-deprecated',
  1171. }
  1172. # before
  1173. res = self.testapp.get(
  1174. '/api/v2/workspaces/2/html-documents/6',
  1175. status=200
  1176. )
  1177. content = res.json_body
  1178. assert content['content_type'] == 'html-document'
  1179. assert content['content_id'] == 6
  1180. assert content['status'] == 'open'
  1181. # set status
  1182. self.testapp.put_json(
  1183. '/api/v2/workspaces/2/html-documents/6/status',
  1184. params=params,
  1185. status=204
  1186. )
  1187. # after
  1188. res = self.testapp.get(
  1189. '/api/v2/workspaces/2/html-documents/6',
  1190. status=200
  1191. )
  1192. content = res.json_body
  1193. assert content['content_type'] == 'html-document'
  1194. assert content['content_id'] == 6
  1195. assert content['status'] == 'closed-deprecated'
  1196. def test_api__set_html_document_status__err_400__wrong_status(self) -> None:
  1197. """
  1198. Get one html document of a content
  1199. """
  1200. self.testapp.authorization = (
  1201. 'Basic',
  1202. (
  1203. 'admin@admin.admin',
  1204. 'admin@admin.admin'
  1205. )
  1206. )
  1207. params = {
  1208. 'status': 'unexistant-status',
  1209. }
  1210. self.testapp.put_json(
  1211. '/api/v2/workspaces/2/html-documents/6/status',
  1212. params=params,
  1213. status=400
  1214. )
  1215. class TestFiles(FunctionalTest):
  1216. """
  1217. Tests for /api/v2/workspaces/{workspace_id}/files/{content_id}
  1218. endpoint
  1219. """
  1220. fixtures = [BaseFixture, ContentFixtures]
  1221. def test_api__get_file__ok_200__nominal_case(self) -> None:
  1222. """
  1223. Get one file of a content
  1224. """
  1225. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1226. admin = dbsession.query(models.User) \
  1227. .filter(models.User.email == 'admin@admin.admin') \
  1228. .one()
  1229. workspace_api = WorkspaceApi(
  1230. current_user=admin,
  1231. session=dbsession,
  1232. config=self.app_config
  1233. )
  1234. content_api = ContentApi(
  1235. current_user=admin,
  1236. session=dbsession,
  1237. config=self.app_config
  1238. )
  1239. business_workspace = workspace_api.get_one(1)
  1240. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1241. test_file = content_api.create(
  1242. content_type_slug=CONTENT_TYPES.File.slug,
  1243. workspace=business_workspace,
  1244. parent=tool_folder,
  1245. label='Test file',
  1246. do_save=False,
  1247. do_notify=False,
  1248. )
  1249. test_file.file_extension = '.txt'
  1250. test_file.depot_file = FileIntent(
  1251. b'Test file',
  1252. 'Test_file.txt',
  1253. 'text/plain',
  1254. )
  1255. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  1256. dbsession.flush()
  1257. transaction.commit()
  1258. self.testapp.authorization = (
  1259. 'Basic',
  1260. (
  1261. 'admin@admin.admin',
  1262. 'admin@admin.admin'
  1263. )
  1264. )
  1265. res = self.testapp.get(
  1266. '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
  1267. status=200
  1268. )
  1269. content = res.json_body
  1270. assert content['content_type'] == 'file'
  1271. assert content['content_id'] == test_file.content_id
  1272. assert content['is_archived'] is False
  1273. assert content['is_deleted'] is False
  1274. assert content['label'] == 'Test_file'
  1275. assert content['parent_id'] == 1
  1276. assert content['show_in_ui'] is True
  1277. assert content['slug'] == 'test-file'
  1278. assert content['status'] == 'open'
  1279. assert content['workspace_id'] == 1
  1280. assert content['current_revision_id']
  1281. # TODO - G.M - 2018-06-173 - check date format
  1282. assert content['created']
  1283. assert content['author']
  1284. assert content['author']['user_id'] == 1
  1285. assert content['author']['avatar_url'] is None
  1286. assert content['author']['public_name'] == 'Global manager'
  1287. # TODO - G.M - 2018-06-173 - check date format
  1288. assert content['modified']
  1289. assert content['last_modifier'] == content['author']
  1290. assert content['raw_content'] == '<p>description</p>' # nopep8
  1291. def test_api__get_files__err_400__wrong_content_type(self) -> None:
  1292. """
  1293. Get one file of a content content
  1294. """
  1295. self.testapp.authorization = (
  1296. 'Basic',
  1297. (
  1298. 'admin@admin.admin',
  1299. 'admin@admin.admin'
  1300. )
  1301. )
  1302. self.testapp.get(
  1303. '/api/v2/workspaces/2/files/6',
  1304. status=400
  1305. )
  1306. def test_api__get_file__err_400__content_does_not_exist(self) -> None: # nopep8
  1307. """
  1308. Get one file (content 170 does not exist in db
  1309. """
  1310. self.testapp.authorization = (
  1311. 'Basic',
  1312. (
  1313. 'admin@admin.admin',
  1314. 'admin@admin.admin'
  1315. )
  1316. )
  1317. self.testapp.get(
  1318. '/api/v2/workspaces/1/files/170',
  1319. status=400
  1320. )
  1321. def test_api__get_file__err_400__content_not_in_workspace(self) -> None: # nopep8
  1322. """
  1323. Get one file (content 9 is in workspace 2)
  1324. """
  1325. self.testapp.authorization = (
  1326. 'Basic',
  1327. (
  1328. 'admin@admin.admin',
  1329. 'admin@admin.admin'
  1330. )
  1331. )
  1332. self.testapp.get(
  1333. '/api/v2/workspaces/1/files/9',
  1334. status=400
  1335. )
  1336. def test_api__get_file__err_400__workspace_does_not_exist(self) -> None: # nopep8
  1337. """
  1338. Get one file (Workspace 40 does not exist)
  1339. """
  1340. self.testapp.authorization = (
  1341. 'Basic',
  1342. (
  1343. 'admin@admin.admin',
  1344. 'admin@admin.admin'
  1345. )
  1346. )
  1347. self.testapp.get(
  1348. '/api/v2/workspaces/40/files/9',
  1349. status=400
  1350. )
  1351. def test_api__get_file__err_400__workspace_id_is_not_int(self) -> None: # nopep8
  1352. """
  1353. Get one file, workspace id is not int
  1354. """
  1355. self.testapp.authorization = (
  1356. 'Basic',
  1357. (
  1358. 'admin@admin.admin',
  1359. 'admin@admin.admin'
  1360. )
  1361. )
  1362. self.testapp.get(
  1363. '/api/v2/workspaces/coucou/files/9',
  1364. status=400
  1365. )
  1366. def test_api__get_file__err_400__content_id_is_not_int(self) -> None: # nopep8
  1367. """
  1368. Get one file, content_id is not int
  1369. """
  1370. self.testapp.authorization = (
  1371. 'Basic',
  1372. (
  1373. 'admin@admin.admin',
  1374. 'admin@admin.admin'
  1375. )
  1376. )
  1377. self.testapp.get(
  1378. '/api/v2/workspaces/2/files/coucou',
  1379. status=400
  1380. )
  1381. def test_api__update_file_info_err_400__empty_label(self) -> None: # nopep8
  1382. """
  1383. Update(put) one file
  1384. """
  1385. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1386. admin = dbsession.query(models.User) \
  1387. .filter(models.User.email == 'admin@admin.admin') \
  1388. .one()
  1389. workspace_api = WorkspaceApi(
  1390. current_user=admin,
  1391. session=dbsession,
  1392. config=self.app_config
  1393. )
  1394. content_api = ContentApi(
  1395. current_user=admin,
  1396. session=dbsession,
  1397. config=self.app_config
  1398. )
  1399. business_workspace = workspace_api.get_one(1)
  1400. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1401. test_file = content_api.create(
  1402. content_type_slug=CONTENT_TYPES.File.slug,
  1403. workspace=business_workspace,
  1404. parent=tool_folder,
  1405. label='Test file',
  1406. do_save=False,
  1407. do_notify=False,
  1408. )
  1409. test_file.file_extension = '.txt'
  1410. test_file.depot_file = FileIntent(
  1411. b'Test file',
  1412. 'Test_file.txt',
  1413. 'text/plain',
  1414. )
  1415. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  1416. dbsession.flush()
  1417. transaction.commit()
  1418. self.testapp.authorization = (
  1419. 'Basic',
  1420. (
  1421. 'admin@admin.admin',
  1422. 'admin@admin.admin'
  1423. )
  1424. )
  1425. params = {
  1426. 'label': '',
  1427. 'raw_content': '<p> Le nouveau contenu </p>',
  1428. }
  1429. self.testapp.put_json(
  1430. '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
  1431. params=params,
  1432. status=400
  1433. )
  1434. def test_api__update_file_info__ok_200__nominal_case(self) -> None:
  1435. """
  1436. Update(put) one file
  1437. """
  1438. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1439. admin = dbsession.query(models.User) \
  1440. .filter(models.User.email == 'admin@admin.admin') \
  1441. .one()
  1442. workspace_api = WorkspaceApi(
  1443. current_user=admin,
  1444. session=dbsession,
  1445. config=self.app_config
  1446. )
  1447. content_api = ContentApi(
  1448. current_user=admin,
  1449. session=dbsession,
  1450. config=self.app_config
  1451. )
  1452. business_workspace = workspace_api.get_one(1)
  1453. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1454. test_file = content_api.create(
  1455. content_type_slug=CONTENT_TYPES.File.slug,
  1456. workspace=business_workspace,
  1457. parent=tool_folder,
  1458. label='Test file',
  1459. do_save=False,
  1460. do_notify=False,
  1461. )
  1462. test_file.file_extension = '.txt'
  1463. test_file.depot_file = FileIntent(
  1464. b'Test file',
  1465. 'Test_file.txt',
  1466. 'text/plain',
  1467. )
  1468. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  1469. dbsession.flush()
  1470. transaction.commit()
  1471. self.testapp.authorization = (
  1472. 'Basic',
  1473. (
  1474. 'admin@admin.admin',
  1475. 'admin@admin.admin'
  1476. )
  1477. )
  1478. params = {
  1479. 'label': 'My New label',
  1480. 'raw_content': '<p> Le nouveau contenu </p>',
  1481. }
  1482. res = self.testapp.put_json(
  1483. '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
  1484. params=params,
  1485. status=200
  1486. )
  1487. content = res.json_body
  1488. assert content['content_type'] == 'file'
  1489. assert content['content_id'] == test_file.content_id
  1490. assert content['is_archived'] is False
  1491. assert content['is_deleted'] is False
  1492. assert content['label'] == 'My New label'
  1493. assert content['parent_id'] == 1
  1494. assert content['show_in_ui'] is True
  1495. assert content['slug'] == 'my-new-label'
  1496. assert content['status'] == 'open'
  1497. assert content['workspace_id'] == 1
  1498. assert content['current_revision_id']
  1499. # TODO - G.M - 2018-06-173 - check date format
  1500. assert content['created']
  1501. assert content['author']
  1502. assert content['author']['user_id'] == 1
  1503. assert content['author']['avatar_url'] is None
  1504. assert content['author']['public_name'] == 'Global manager'
  1505. # TODO - G.M - 2018-06-173 - check date format
  1506. assert content['modified']
  1507. assert content['last_modifier'] == content['author']
  1508. assert content['raw_content'] == '<p> Le nouveau contenu </p>'
  1509. res = self.testapp.get(
  1510. '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
  1511. status=200
  1512. )
  1513. content = res.json_body
  1514. assert content['content_type'] == 'file'
  1515. assert content['content_id'] == test_file.content_id
  1516. assert content['is_archived'] is False
  1517. assert content['is_deleted'] is False
  1518. assert content['label'] == 'My New label'
  1519. assert content['parent_id'] == 1
  1520. assert content['show_in_ui'] is True
  1521. assert content['slug'] == 'my-new-label'
  1522. assert content['status'] == 'open'
  1523. assert content['workspace_id'] == 1
  1524. assert content['current_revision_id']
  1525. # TODO - G.M - 2018-06-173 - check date format
  1526. assert content['created']
  1527. assert content['author']
  1528. assert content['author']['user_id'] == 1
  1529. assert content['author']['avatar_url'] is None
  1530. assert content['author']['public_name'] == 'Global manager'
  1531. # TODO - G.M - 2018-06-173 - check date format
  1532. assert content['modified']
  1533. assert content['last_modifier'] == content['author']
  1534. assert content['raw_content'] == '<p> Le nouveau contenu </p>'
  1535. def test_api__get_file_revisions__ok_200__nominal_case(
  1536. self
  1537. ) -> None:
  1538. """
  1539. Get file revisions
  1540. """
  1541. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1542. admin = dbsession.query(models.User) \
  1543. .filter(models.User.email == 'admin@admin.admin') \
  1544. .one()
  1545. workspace_api = WorkspaceApi(
  1546. current_user=admin,
  1547. session=dbsession,
  1548. config=self.app_config
  1549. )
  1550. content_api = ContentApi(
  1551. current_user=admin,
  1552. session=dbsession,
  1553. config=self.app_config
  1554. )
  1555. business_workspace = workspace_api.get_one(1)
  1556. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1557. test_file = content_api.create(
  1558. content_type_slug=CONTENT_TYPES.File.slug,
  1559. workspace=business_workspace,
  1560. parent=tool_folder,
  1561. label='Test file',
  1562. do_save=False,
  1563. do_notify=False,
  1564. )
  1565. test_file.file_extension = '.txt'
  1566. test_file.depot_file = FileIntent(
  1567. b'Test file',
  1568. 'Test_file.txt',
  1569. 'text/plain',
  1570. )
  1571. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  1572. dbsession.flush()
  1573. transaction.commit()
  1574. self.testapp.authorization = (
  1575. 'Basic',
  1576. (
  1577. 'admin@admin.admin',
  1578. 'admin@admin.admin'
  1579. )
  1580. )
  1581. res = self.testapp.get(
  1582. '/api/v2/workspaces/1/files/{}/revisions'.format(test_file.content_id), # nopep8
  1583. status=200
  1584. )
  1585. revisions = res.json_body
  1586. assert len(revisions) == 1
  1587. revision = revisions[0]
  1588. assert revision['content_type'] == 'file'
  1589. assert revision['content_id'] == test_file.content_id
  1590. assert revision['is_archived'] is False
  1591. assert revision['is_deleted'] is False
  1592. assert revision['label'] == 'Test_file'
  1593. assert revision['parent_id'] == 1
  1594. assert revision['show_in_ui'] is True
  1595. assert revision['slug'] == 'test-file'
  1596. assert revision['status'] == 'open'
  1597. assert revision['workspace_id'] == 1
  1598. assert revision['revision_id']
  1599. assert revision['sub_content_types']
  1600. # TODO - G.M - 2018-06-173 - Test with real comments
  1601. assert revision['comment_ids'] == []
  1602. # TODO - G.M - 2018-06-173 - check date format
  1603. assert revision['created']
  1604. assert revision['author']
  1605. assert revision['author']['user_id'] == 1
  1606. assert revision['author']['avatar_url'] is None
  1607. assert revision['author']['public_name'] == 'Global manager'
  1608. def test_api__set_file_status__ok_200__nominal_case(self) -> None:
  1609. """
  1610. set file status
  1611. """
  1612. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1613. admin = dbsession.query(models.User) \
  1614. .filter(models.User.email == 'admin@admin.admin') \
  1615. .one()
  1616. workspace_api = WorkspaceApi(
  1617. current_user=admin,
  1618. session=dbsession,
  1619. config=self.app_config
  1620. )
  1621. content_api = ContentApi(
  1622. current_user=admin,
  1623. session=dbsession,
  1624. config=self.app_config
  1625. )
  1626. business_workspace = workspace_api.get_one(1)
  1627. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1628. test_file = content_api.create(
  1629. content_type_slug=CONTENT_TYPES.File.slug,
  1630. workspace=business_workspace,
  1631. parent=tool_folder,
  1632. label='Test file',
  1633. do_save=False,
  1634. do_notify=False,
  1635. )
  1636. test_file.file_extension = '.txt'
  1637. test_file.depot_file = FileIntent(
  1638. b'Test file',
  1639. 'Test_file.txt',
  1640. 'text/plain',
  1641. )
  1642. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  1643. dbsession.flush()
  1644. transaction.commit()
  1645. self.testapp.authorization = (
  1646. 'Basic',
  1647. (
  1648. 'admin@admin.admin',
  1649. 'admin@admin.admin'
  1650. )
  1651. )
  1652. params = {
  1653. 'status': 'closed-deprecated',
  1654. }
  1655. # before
  1656. res = self.testapp.get(
  1657. '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
  1658. status=200
  1659. )
  1660. content = res.json_body
  1661. assert content['content_type'] == 'file'
  1662. assert content['content_id'] == test_file.content_id
  1663. assert content['status'] == 'open'
  1664. # set status
  1665. self.testapp.put_json(
  1666. '/api/v2/workspaces/1/files/{}/status'.format(test_file.content_id),
  1667. params=params,
  1668. status=204
  1669. )
  1670. # after
  1671. res = self.testapp.get(
  1672. '/api/v2/workspaces/1/files/{}'.format(test_file.content_id),
  1673. status=200
  1674. )
  1675. content = res.json_body
  1676. assert content['content_type'] == 'file'
  1677. assert content['content_id'] == test_file.content_id
  1678. assert content['status'] == 'closed-deprecated'
  1679. def test_api__set_file_status__err_400__wrong_status(self) -> None:
  1680. """
  1681. set file status
  1682. """
  1683. self.testapp.authorization = (
  1684. 'Basic',
  1685. (
  1686. 'admin@admin.admin',
  1687. 'admin@admin.admin'
  1688. )
  1689. )
  1690. params = {
  1691. 'status': 'unexistant-status',
  1692. }
  1693. self.testapp.put_json(
  1694. '/api/v2/workspaces/2/files/6/status',
  1695. params=params,
  1696. status=400
  1697. )
  1698. def test_api__get_file_raw__ok_200__nominal_case(self) -> None:
  1699. """
  1700. Get one file of a content
  1701. """
  1702. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1703. admin = dbsession.query(models.User) \
  1704. .filter(models.User.email == 'admin@admin.admin') \
  1705. .one()
  1706. workspace_api = WorkspaceApi(
  1707. current_user=admin,
  1708. session=dbsession,
  1709. config=self.app_config
  1710. )
  1711. content_api = ContentApi(
  1712. current_user=admin,
  1713. session=dbsession,
  1714. config=self.app_config
  1715. )
  1716. business_workspace = workspace_api.get_one(1)
  1717. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1718. test_file = content_api.create(
  1719. content_type_slug=CONTENT_TYPES.File.slug,
  1720. workspace=business_workspace,
  1721. parent=tool_folder,
  1722. label='Test file',
  1723. do_save=False,
  1724. do_notify=False,
  1725. )
  1726. test_file.file_extension = '.txt'
  1727. test_file.depot_file = FileIntent(
  1728. b'Test file',
  1729. 'Test_file.txt',
  1730. 'text/plain',
  1731. )
  1732. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  1733. dbsession.flush()
  1734. transaction.commit()
  1735. content_id = int(test_file.content_id)
  1736. self.testapp.authorization = (
  1737. 'Basic',
  1738. (
  1739. 'admin@admin.admin',
  1740. 'admin@admin.admin'
  1741. )
  1742. )
  1743. res = self.testapp.get(
  1744. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  1745. status=200
  1746. )
  1747. assert res.body == b'Test file'
  1748. assert res.content_type == 'text/plain'
  1749. assert res.content_length == len(b'Test file')
  1750. def test_api__set_file_raw__ok_200__nominal_case(self) -> None:
  1751. """
  1752. Set one file of a content
  1753. """
  1754. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1755. admin = dbsession.query(models.User) \
  1756. .filter(models.User.email == 'admin@admin.admin') \
  1757. .one()
  1758. workspace_api = WorkspaceApi(
  1759. current_user=admin,
  1760. session=dbsession,
  1761. config=self.app_config
  1762. )
  1763. content_api = ContentApi(
  1764. current_user=admin,
  1765. session=dbsession,
  1766. config=self.app_config
  1767. )
  1768. business_workspace = workspace_api.get_one(1)
  1769. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1770. test_file = content_api.create(
  1771. content_type_slug=CONTENT_TYPES.File.slug,
  1772. workspace=business_workspace,
  1773. parent=tool_folder,
  1774. label='Test file',
  1775. do_save=True,
  1776. do_notify=False,
  1777. )
  1778. dbsession.flush()
  1779. transaction.commit()
  1780. content_id = int(test_file.content_id)
  1781. image = create_1000px_png_test_image()
  1782. self.testapp.authorization = (
  1783. 'Basic',
  1784. (
  1785. 'admin@admin.admin',
  1786. 'admin@admin.admin'
  1787. )
  1788. )
  1789. self.testapp.put(
  1790. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  1791. upload_files=[
  1792. ('files', image.name, image.getvalue())
  1793. ],
  1794. status=204,
  1795. )
  1796. res = self.testapp.get(
  1797. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  1798. status=200
  1799. )
  1800. assert res.body == image.getvalue()
  1801. assert res.content_type == 'image/png'
  1802. assert res.content_length == len(image.getvalue())
  1803. def test_api__get_allowed_size_dim__ok__nominal_case(self) -> None:
  1804. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1805. admin = dbsession.query(models.User) \
  1806. .filter(models.User.email == 'admin@admin.admin') \
  1807. .one()
  1808. workspace_api = WorkspaceApi(
  1809. current_user=admin,
  1810. session=dbsession,
  1811. config=self.app_config
  1812. )
  1813. content_api = ContentApi(
  1814. current_user=admin,
  1815. session=dbsession,
  1816. config=self.app_config
  1817. )
  1818. business_workspace = workspace_api.get_one(1)
  1819. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1820. test_file = content_api.create(
  1821. content_type_slug=CONTENT_TYPES.File.slug,
  1822. workspace=business_workspace,
  1823. parent=tool_folder,
  1824. label='Test file',
  1825. do_save=False,
  1826. do_notify=False,
  1827. )
  1828. test_file.file_extension = '.txt'
  1829. test_file.depot_file = FileIntent(
  1830. b'Test file',
  1831. 'Test_file.txt',
  1832. 'text/plain',
  1833. )
  1834. dbsession.flush()
  1835. transaction.commit()
  1836. self.testapp.authorization = (
  1837. 'Basic',
  1838. (
  1839. 'admin@admin.admin',
  1840. 'admin@admin.admin'
  1841. )
  1842. )
  1843. content_id = int(test_file.content_id)
  1844. res = self.testapp.get(
  1845. '/api/v2/workspaces/1/files/{}/preview/jpg/allowed_dims'.format(content_id), # nopep8
  1846. status=200,
  1847. )
  1848. res = res.json_body
  1849. assert res['restricted'] is True
  1850. assert len(res['dimensions']) == 1
  1851. dim = res['dimensions'][0]
  1852. assert dim['width'] == 256
  1853. assert dim['height'] == 256
  1854. def test_api__get_jpeg_preview__ok__200__nominal_case(self) -> None:
  1855. """
  1856. Set one file of a content
  1857. """
  1858. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1859. admin = dbsession.query(models.User) \
  1860. .filter(models.User.email == 'admin@admin.admin') \
  1861. .one()
  1862. workspace_api = WorkspaceApi(
  1863. current_user=admin,
  1864. session=dbsession,
  1865. config=self.app_config
  1866. )
  1867. content_api = ContentApi(
  1868. current_user=admin,
  1869. session=dbsession,
  1870. config=self.app_config
  1871. )
  1872. business_workspace = workspace_api.get_one(1)
  1873. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1874. test_file = content_api.create(
  1875. content_type_slug=CONTENT_TYPES.File.slug,
  1876. workspace=business_workspace,
  1877. parent=tool_folder,
  1878. label='Test file',
  1879. do_save=False,
  1880. do_notify=False,
  1881. )
  1882. test_file.file_extension = '.txt'
  1883. test_file.depot_file = FileIntent(
  1884. b'Test file',
  1885. 'Test_file.txt',
  1886. 'text/plain',
  1887. )
  1888. dbsession.flush()
  1889. transaction.commit()
  1890. content_id = int(test_file.content_id)
  1891. image = create_1000px_png_test_image()
  1892. self.testapp.authorization = (
  1893. 'Basic',
  1894. (
  1895. 'admin@admin.admin',
  1896. 'admin@admin.admin'
  1897. )
  1898. )
  1899. self.testapp.put(
  1900. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  1901. upload_files=[
  1902. ('files', image.name, image.getvalue())
  1903. ],
  1904. status=204,
  1905. )
  1906. res = self.testapp.get(
  1907. '/api/v2/workspaces/1/files/{}/preview/jpg'.format(content_id),
  1908. status=200
  1909. )
  1910. assert res.body != image.getvalue()
  1911. assert res.content_type == 'image/jpeg'
  1912. def test_api__get_sized_jpeg_preview__ok__200__nominal_case(self) -> None:
  1913. """
  1914. get 256x256 preview of a txt file
  1915. """
  1916. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1917. admin = dbsession.query(models.User) \
  1918. .filter(models.User.email == 'admin@admin.admin') \
  1919. .one()
  1920. workspace_api = WorkspaceApi(
  1921. current_user=admin,
  1922. session=dbsession,
  1923. config=self.app_config
  1924. )
  1925. content_api = ContentApi(
  1926. current_user=admin,
  1927. session=dbsession,
  1928. config=self.app_config
  1929. )
  1930. business_workspace = workspace_api.get_one(1)
  1931. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1932. test_file = content_api.create(
  1933. content_type_slug=CONTENT_TYPES.File.slug,
  1934. workspace=business_workspace,
  1935. parent=tool_folder,
  1936. label='Test file',
  1937. do_save=True,
  1938. do_notify=False,
  1939. )
  1940. dbsession.flush()
  1941. transaction.commit()
  1942. content_id = int(test_file.content_id)
  1943. image = create_1000px_png_test_image()
  1944. self.testapp.authorization = (
  1945. 'Basic',
  1946. (
  1947. 'admin@admin.admin',
  1948. 'admin@admin.admin'
  1949. )
  1950. )
  1951. self.testapp.put(
  1952. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  1953. upload_files=[
  1954. ('files', image.name, image.getvalue())
  1955. ],
  1956. status=204,
  1957. )
  1958. res = self.testapp.get(
  1959. '/api/v2/workspaces/1/files/{}/preview/jpg/256x256'.format(content_id), # nopep8
  1960. status=200
  1961. )
  1962. assert res.body != image.getvalue()
  1963. assert res.content_type == 'image/jpeg'
  1964. new_image = Image.open(io.BytesIO(res.body))
  1965. assert 256, 256 == new_image.size
  1966. def test_api__get_sized_jpeg_preview__err__400__SizeNotAllowed(self) -> None: # nopep8
  1967. """
  1968. get 256x256 preview of a txt file
  1969. """
  1970. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1971. admin = dbsession.query(models.User) \
  1972. .filter(models.User.email == 'admin@admin.admin') \
  1973. .one()
  1974. workspace_api = WorkspaceApi(
  1975. current_user=admin,
  1976. session=dbsession,
  1977. config=self.app_config
  1978. )
  1979. content_api = ContentApi(
  1980. current_user=admin,
  1981. session=dbsession,
  1982. config=self.app_config
  1983. )
  1984. business_workspace = workspace_api.get_one(1)
  1985. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  1986. test_file = content_api.create(
  1987. content_type_slug=CONTENT_TYPES.File.slug,
  1988. workspace=business_workspace,
  1989. parent=tool_folder,
  1990. label='Test file',
  1991. do_save=True,
  1992. do_notify=False,
  1993. )
  1994. dbsession.flush()
  1995. transaction.commit()
  1996. content_id = int(test_file.content_id)
  1997. image = create_1000px_png_test_image()
  1998. self.testapp.authorization = (
  1999. 'Basic',
  2000. (
  2001. 'admin@admin.admin',
  2002. 'admin@admin.admin'
  2003. )
  2004. )
  2005. self.testapp.put(
  2006. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  2007. upload_files=[
  2008. ('files', image.name, image.getvalue())
  2009. ],
  2010. status=204,
  2011. )
  2012. self.testapp.get(
  2013. '/api/v2/workspaces/1/files/{}/preview/jpg/512x512'.format(content_id), # nopep8
  2014. status=400
  2015. )
  2016. def test_api__get_sized_jpeg_revision_preview__ok__200__nominal_case(self) -> None: # nopep8
  2017. """
  2018. get 256x256 revision preview of a txt file
  2019. """
  2020. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2021. admin = dbsession.query(models.User) \
  2022. .filter(models.User.email == 'admin@admin.admin') \
  2023. .one()
  2024. workspace_api = WorkspaceApi(
  2025. current_user=admin,
  2026. session=dbsession,
  2027. config=self.app_config
  2028. )
  2029. content_api = ContentApi(
  2030. current_user=admin,
  2031. session=dbsession,
  2032. config=self.app_config
  2033. )
  2034. business_workspace = workspace_api.get_one(1)
  2035. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  2036. test_file = content_api.create(
  2037. content_type_slug=CONTENT_TYPES.File.slug,
  2038. workspace=business_workspace,
  2039. parent=tool_folder,
  2040. label='Test file',
  2041. do_save=False,
  2042. do_notify=False,
  2043. )
  2044. test_file.file_extension = '.txt'
  2045. test_file.depot_file = FileIntent(
  2046. b'Test file',
  2047. 'Test_file.txt',
  2048. 'text/plain',
  2049. )
  2050. dbsession.flush()
  2051. transaction.commit()
  2052. content_id = int(test_file.content_id)
  2053. revision_id = int(test_file.revision_id)
  2054. image = create_1000px_png_test_image()
  2055. self.testapp.authorization = (
  2056. 'Basic',
  2057. (
  2058. 'admin@admin.admin',
  2059. 'admin@admin.admin'
  2060. )
  2061. )
  2062. self.testapp.put(
  2063. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  2064. upload_files=[
  2065. ('files', image.name, image.getvalue())
  2066. ],
  2067. status=204,
  2068. )
  2069. res = self.testapp.get(
  2070. '/api/v2/workspaces/1/files/{content_id}/revisions/{revision_id}/raw'.format( # nopep8
  2071. content_id=content_id,
  2072. revision_id=revision_id,
  2073. ),
  2074. status=200
  2075. )
  2076. assert res.content_type == 'text/plain'
  2077. res = self.testapp.get(
  2078. '/api/v2/workspaces/1/files/{content_id}/revisions/{revision_id}/preview/jpg/256x256'.format( # nopep8
  2079. content_id=content_id,
  2080. revision_id=revision_id,
  2081. ),
  2082. status=200
  2083. )
  2084. assert res.body != image.getvalue()
  2085. assert res.content_type == 'image/jpeg'
  2086. new_image = Image.open(io.BytesIO(res.body))
  2087. assert 256, 256 == new_image.size
  2088. def test_api__get_full_pdf_preview__ok__200__nominal_case(self) -> None:
  2089. """
  2090. get full pdf preview of a txt file
  2091. """
  2092. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2093. admin = dbsession.query(models.User) \
  2094. .filter(models.User.email == 'admin@admin.admin') \
  2095. .one()
  2096. workspace_api = WorkspaceApi(
  2097. current_user=admin,
  2098. session=dbsession,
  2099. config=self.app_config
  2100. )
  2101. content_api = ContentApi(
  2102. current_user=admin,
  2103. session=dbsession,
  2104. config=self.app_config
  2105. )
  2106. business_workspace = workspace_api.get_one(1)
  2107. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  2108. test_file = content_api.create(
  2109. content_type_slug=CONTENT_TYPES.File.slug,
  2110. workspace=business_workspace,
  2111. parent=tool_folder,
  2112. label='Test file',
  2113. do_save=True,
  2114. do_notify=False,
  2115. )
  2116. with new_revision(
  2117. session=dbsession,
  2118. tm=transaction.manager,
  2119. content=test_file,
  2120. ):
  2121. test_file.file_extension = '.txt'
  2122. test_file.depot_file = FileIntent(
  2123. b'Test file',
  2124. 'Test_file.txt',
  2125. 'text/plain',
  2126. )
  2127. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  2128. dbsession.flush()
  2129. transaction.commit()
  2130. content_id = int(test_file.content_id)
  2131. self.testapp.authorization = (
  2132. 'Basic',
  2133. (
  2134. 'admin@admin.admin',
  2135. 'admin@admin.admin'
  2136. )
  2137. )
  2138. self.testapp.put(
  2139. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  2140. upload_files=[
  2141. ('files', test_file.file_name, test_file.depot_file.file.read())
  2142. ],
  2143. status=204,
  2144. )
  2145. res = self.testapp.get(
  2146. '/api/v2/workspaces/1/files/{}/preview/pdf/full'.format(content_id), # nopep8
  2147. status=200
  2148. )
  2149. assert res.content_type == 'application/pdf'
  2150. def test_api__get_full_pdf_preview__err__400__png_UnavailablePreviewType(self) -> None: # nopep8
  2151. """
  2152. get full pdf preview of a png image -> error UnavailablePreviewType
  2153. """
  2154. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2155. admin = dbsession.query(models.User) \
  2156. .filter(models.User.email == 'admin@admin.admin') \
  2157. .one()
  2158. workspace_api = WorkspaceApi(
  2159. current_user=admin,
  2160. session=dbsession,
  2161. config=self.app_config
  2162. )
  2163. content_api = ContentApi(
  2164. current_user=admin,
  2165. session=dbsession,
  2166. config=self.app_config
  2167. )
  2168. business_workspace = workspace_api.get_one(1)
  2169. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  2170. test_file = content_api.create(
  2171. content_type_slug=CONTENT_TYPES.File.slug,
  2172. workspace=business_workspace,
  2173. parent=tool_folder,
  2174. label='Test file',
  2175. do_save=True,
  2176. do_notify=False,
  2177. )
  2178. dbsession.flush()
  2179. transaction.commit()
  2180. content_id = int(test_file.content_id)
  2181. image = create_1000px_png_test_image()
  2182. self.testapp.authorization = (
  2183. 'Basic',
  2184. (
  2185. 'admin@admin.admin',
  2186. 'admin@admin.admin'
  2187. )
  2188. )
  2189. self.testapp.put(
  2190. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  2191. upload_files=[
  2192. ('files', image.name, image.getvalue())
  2193. ],
  2194. status=204,
  2195. )
  2196. self.testapp.get(
  2197. '/api/v2/workspaces/1/files/{}/preview/pdf/full'.format(content_id), # nopep8
  2198. status=400
  2199. )
  2200. def test_api__get_pdf_preview__ok__200__nominal_case(self) -> None:
  2201. """
  2202. get full pdf preview of a txt file
  2203. """
  2204. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2205. admin = dbsession.query(models.User) \
  2206. .filter(models.User.email == 'admin@admin.admin') \
  2207. .one()
  2208. workspace_api = WorkspaceApi(
  2209. current_user=admin,
  2210. session=dbsession,
  2211. config=self.app_config
  2212. )
  2213. content_api = ContentApi(
  2214. current_user=admin,
  2215. session=dbsession,
  2216. config=self.app_config
  2217. )
  2218. business_workspace = workspace_api.get_one(1)
  2219. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  2220. test_file = content_api.create(
  2221. content_type_slug=CONTENT_TYPES.File.slug,
  2222. workspace=business_workspace,
  2223. parent=tool_folder,
  2224. label='Test file',
  2225. do_save=True,
  2226. do_notify=False,
  2227. )
  2228. with new_revision(
  2229. session=dbsession,
  2230. tm=transaction.manager,
  2231. content=test_file,
  2232. ):
  2233. test_file.file_extension = '.txt'
  2234. test_file.depot_file = FileIntent(
  2235. b'Test file',
  2236. 'Test_file.txt',
  2237. 'text/plain',
  2238. )
  2239. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  2240. dbsession.flush()
  2241. transaction.commit()
  2242. content_id = int(test_file.content_id)
  2243. self.testapp.authorization = (
  2244. 'Basic',
  2245. (
  2246. 'admin@admin.admin',
  2247. 'admin@admin.admin'
  2248. )
  2249. )
  2250. self.testapp.put(
  2251. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  2252. upload_files=[
  2253. ('files', test_file.file_name, test_file.depot_file.file.read())
  2254. ],
  2255. status=204,
  2256. )
  2257. params = {'page': 0}
  2258. res = self.testapp.get(
  2259. '/api/v2/workspaces/1/files/{}/preview/pdf'.format(content_id),
  2260. status=200,
  2261. params=params,
  2262. )
  2263. assert res.content_type == 'application/pdf'
  2264. def test_api__get_pdf_preview__ok__err__400_page_of_preview_not_found(self) -> None: # nopep8
  2265. """
  2266. get full pdf preview of a txt file
  2267. """
  2268. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2269. admin = dbsession.query(models.User) \
  2270. .filter(models.User.email == 'admin@admin.admin') \
  2271. .one()
  2272. workspace_api = WorkspaceApi(
  2273. current_user=admin,
  2274. session=dbsession,
  2275. config=self.app_config
  2276. )
  2277. content_api = ContentApi(
  2278. current_user=admin,
  2279. session=dbsession,
  2280. config=self.app_config
  2281. )
  2282. business_workspace = workspace_api.get_one(1)
  2283. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  2284. test_file = content_api.create(
  2285. content_type_slug=CONTENT_TYPES.File.slug,
  2286. workspace=business_workspace,
  2287. parent=tool_folder,
  2288. label='Test file',
  2289. do_save=True,
  2290. do_notify=False,
  2291. )
  2292. with new_revision(
  2293. session=dbsession,
  2294. tm=transaction.manager,
  2295. content=test_file,
  2296. ):
  2297. test_file.file_extension = '.txt'
  2298. test_file.depot_file = FileIntent(
  2299. b'Test file',
  2300. 'Test_file.txt',
  2301. 'text/plain',
  2302. )
  2303. content_api.update_content(test_file, 'Test_file', '<p>description</p>') # nopep8
  2304. dbsession.flush()
  2305. transaction.commit()
  2306. content_id = int(test_file.content_id)
  2307. self.testapp.authorization = (
  2308. 'Basic',
  2309. (
  2310. 'admin@admin.admin',
  2311. 'admin@admin.admin'
  2312. )
  2313. )
  2314. self.testapp.put(
  2315. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  2316. upload_files=[
  2317. ('files', test_file.file_name, test_file.depot_file.file.read())
  2318. ],
  2319. status=204,
  2320. )
  2321. params = {'page': 1}
  2322. self.testapp.get(
  2323. '/api/v2/workspaces/1/files/{}/preview/pdf'.format(content_id),
  2324. status=400,
  2325. params=params,
  2326. )
  2327. def test_api__get_pdf_revision_preview__ok__200__nominal_case(self) -> None:
  2328. """
  2329. get pdf revision preview of content
  2330. """
  2331. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2332. admin = dbsession.query(models.User) \
  2333. .filter(models.User.email == 'admin@admin.admin') \
  2334. .one()
  2335. workspace_api = WorkspaceApi(
  2336. current_user=admin,
  2337. session=dbsession,
  2338. config=self.app_config
  2339. )
  2340. content_api = ContentApi(
  2341. current_user=admin,
  2342. session=dbsession,
  2343. config=self.app_config
  2344. )
  2345. business_workspace = workspace_api.get_one(1)
  2346. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  2347. test_file = content_api.create(
  2348. content_type_slug=CONTENT_TYPES.File.slug,
  2349. workspace=business_workspace,
  2350. parent=tool_folder,
  2351. label='Test file',
  2352. do_save=False,
  2353. do_notify=False,
  2354. )
  2355. test_file.file_extension = '.txt'
  2356. test_file.depot_file = FileIntent(
  2357. b'Test file',
  2358. 'Test_file.txt',
  2359. 'text/plain',
  2360. )
  2361. dbsession.flush()
  2362. transaction.commit()
  2363. content_id = int(test_file.content_id)
  2364. revision_id = int(test_file.revision_id)
  2365. image = create_1000px_png_test_image()
  2366. self.testapp.authorization = (
  2367. 'Basic',
  2368. (
  2369. 'admin@admin.admin',
  2370. 'admin@admin.admin'
  2371. )
  2372. )
  2373. self.testapp.put(
  2374. '/api/v2/workspaces/1/files/{}/raw'.format(content_id),
  2375. upload_files=[
  2376. ('files', image.name, image.getvalue())
  2377. ],
  2378. status=204,
  2379. )
  2380. res = self.testapp.get(
  2381. '/api/v2/workspaces/1/files/{content_id}/revisions/{revision_id}/raw'.format( # nopep8
  2382. content_id=content_id,
  2383. revision_id=revision_id,
  2384. ),
  2385. status=200
  2386. )
  2387. assert res.content_type == 'text/plain'
  2388. params = {'page': 0}
  2389. res = self.testapp.get(
  2390. '/api/v2/workspaces/1/files/{content_id}/revisions/{revision_id}/preview/pdf'.format( # nopep8
  2391. content_id=content_id,
  2392. revision_id=revision_id,
  2393. params=params,
  2394. ),
  2395. status=200
  2396. )
  2397. assert res.content_type == 'application/pdf'
  2398. class TestThreads(FunctionalTest):
  2399. """
  2400. Tests for /api/v2/workspaces/{workspace_id}/threads/{content_id}
  2401. endpoint
  2402. """
  2403. fixtures = [BaseFixture, ContentFixtures]
  2404. def test_api__get_thread__err_400__wrong_content_type(self) -> None:
  2405. """
  2406. Get one html document of a content
  2407. """
  2408. self.testapp.authorization = (
  2409. 'Basic',
  2410. (
  2411. 'admin@admin.admin',
  2412. 'admin@admin.admin'
  2413. )
  2414. )
  2415. self.testapp.get(
  2416. '/api/v2/workspaces/2/threads/6',
  2417. status=400
  2418. )
  2419. def test_api__get_thread__ok_200__nominal_case(self) -> None:
  2420. """
  2421. Get one html document of a content
  2422. """
  2423. self.testapp.authorization = (
  2424. 'Basic',
  2425. (
  2426. 'admin@admin.admin',
  2427. 'admin@admin.admin'
  2428. )
  2429. )
  2430. res = self.testapp.get(
  2431. '/api/v2/workspaces/2/threads/7',
  2432. status=200
  2433. ) # nopep8
  2434. content = res.json_body
  2435. assert content['content_type'] == 'thread'
  2436. assert content['content_id'] == 7
  2437. assert content['is_archived'] is False
  2438. assert content['is_deleted'] is False
  2439. assert content['label'] == 'Best Cakes?'
  2440. assert content['parent_id'] == 3
  2441. assert content['show_in_ui'] is True
  2442. assert content['slug'] == 'best-cakes'
  2443. assert content['status'] == 'open'
  2444. assert content['workspace_id'] == 2
  2445. assert content['current_revision_id'] == 26
  2446. # TODO - G.M - 2018-06-173 - check date format
  2447. assert content['created']
  2448. assert content['author']
  2449. assert content['author']['user_id'] == 1
  2450. assert content['author']['avatar_url'] is None
  2451. assert content['author']['public_name'] == 'Global manager'
  2452. # TODO - G.M - 2018-06-173 - check date format
  2453. assert content['modified']
  2454. assert content['last_modifier'] != content['author']
  2455. assert content['last_modifier']['user_id'] == 3
  2456. assert content['last_modifier']['public_name'] == 'Bob i.'
  2457. assert content['last_modifier']['avatar_url'] is None
  2458. assert content['raw_content'] == 'What is the best cake?' # nopep8
  2459. def test_api__get_thread__err_400__content_does_not_exist(self) -> None:
  2460. """
  2461. Get one thread (content 170 does not exist)
  2462. """
  2463. self.testapp.authorization = (
  2464. 'Basic',
  2465. (
  2466. 'admin@admin.admin',
  2467. 'admin@admin.admin'
  2468. )
  2469. )
  2470. self.testapp.get(
  2471. '/api/v2/workspaces/2/threads/170',
  2472. status=400
  2473. )
  2474. def test_api__get_thread__err_400__content_not_in_workspace(self) -> None:
  2475. """
  2476. Get one thread(content 7 is in workspace 2)
  2477. """
  2478. self.testapp.authorization = (
  2479. 'Basic',
  2480. (
  2481. 'admin@admin.admin',
  2482. 'admin@admin.admin'
  2483. )
  2484. )
  2485. self.testapp.get(
  2486. '/api/v2/workspaces/1/threads/7',
  2487. status=400
  2488. )
  2489. def test_api__get_thread__err_400__workspace_does_not_exist(self) -> None: # nopep8
  2490. """
  2491. Get one thread (Workspace 40 does not exist)
  2492. """
  2493. self.testapp.authorization = (
  2494. 'Basic',
  2495. (
  2496. 'admin@admin.admin',
  2497. 'admin@admin.admin'
  2498. )
  2499. )
  2500. self.testapp.get(
  2501. '/api/v2/workspaces/40/threads/7',
  2502. status=400
  2503. )
  2504. def test_api__get_thread__err_400__workspace_id_is_not_int(self) -> None: # nopep8
  2505. """
  2506. Get one thread, workspace id is not int
  2507. """
  2508. self.testapp.authorization = (
  2509. 'Basic',
  2510. (
  2511. 'admin@admin.admin',
  2512. 'admin@admin.admin'
  2513. )
  2514. )
  2515. self.testapp.get(
  2516. '/api/v2/workspaces/coucou/threads/7',
  2517. status=400
  2518. )
  2519. def test_api__get_thread__err_400_content_id_is_not_int(self) -> None: # nopep8
  2520. """
  2521. Get one thread, content id is not int
  2522. """
  2523. self.testapp.authorization = (
  2524. 'Basic',
  2525. (
  2526. 'admin@admin.admin',
  2527. 'admin@admin.admin'
  2528. )
  2529. )
  2530. self.testapp.get(
  2531. '/api/v2/workspaces/2/threads/coucou',
  2532. status=400
  2533. )
  2534. def test_api__update_thread__ok_200__nominal_case(self) -> None:
  2535. """
  2536. Update(put) thread
  2537. """
  2538. self.testapp.authorization = (
  2539. 'Basic',
  2540. (
  2541. 'admin@admin.admin',
  2542. 'admin@admin.admin'
  2543. )
  2544. )
  2545. params = {
  2546. 'label': 'My New label',
  2547. 'raw_content': '<p> Le nouveau contenu </p>',
  2548. }
  2549. res = self.testapp.put_json(
  2550. '/api/v2/workspaces/2/threads/7',
  2551. params=params,
  2552. status=200
  2553. )
  2554. content = res.json_body
  2555. assert content['content_type'] == 'thread'
  2556. assert content['content_id'] == 7
  2557. assert content['is_archived'] is False
  2558. assert content['is_deleted'] is False
  2559. assert content['label'] == 'My New label'
  2560. assert content['parent_id'] == 3
  2561. assert content['show_in_ui'] is True
  2562. assert content['slug'] == 'my-new-label'
  2563. assert content['status'] == 'open'
  2564. assert content['workspace_id'] == 2
  2565. assert content['current_revision_id'] == 28
  2566. # TODO - G.M - 2018-06-173 - check date format
  2567. assert content['created']
  2568. assert content['author']
  2569. assert content['author']['user_id'] == 1
  2570. assert content['author']['avatar_url'] is None
  2571. assert content['author']['public_name'] == 'Global manager'
  2572. # TODO - G.M - 2018-06-173 - check date format
  2573. assert content['modified']
  2574. assert content['last_modifier'] == content['author']
  2575. assert content['raw_content'] == '<p> Le nouveau contenu </p>'
  2576. res = self.testapp.get(
  2577. '/api/v2/workspaces/2/threads/7',
  2578. status=200
  2579. ) # nopep8
  2580. content = res.json_body
  2581. assert content['content_type'] == 'thread'
  2582. assert content['content_id'] == 7
  2583. assert content['is_archived'] is False
  2584. assert content['is_deleted'] is False
  2585. assert content['label'] == 'My New label'
  2586. assert content['parent_id'] == 3
  2587. assert content['show_in_ui'] is True
  2588. assert content['slug'] == 'my-new-label'
  2589. assert content['status'] == 'open'
  2590. assert content['workspace_id'] == 2
  2591. assert content['current_revision_id'] == 28
  2592. # TODO - G.M - 2018-06-173 - check date format
  2593. assert content['created']
  2594. assert content['author']
  2595. assert content['author']['user_id'] == 1
  2596. assert content['author']['avatar_url'] is None
  2597. assert content['author']['public_name'] == 'Global manager'
  2598. # TODO - G.M - 2018-06-173 - check date format
  2599. assert content['modified']
  2600. assert content['last_modifier'] == content['author']
  2601. assert content['raw_content'] == '<p> Le nouveau contenu </p>'
  2602. def test_api__update_thread__err_400__empty_label(self) -> None:
  2603. """
  2604. Update(put) thread
  2605. """
  2606. self.testapp.authorization = (
  2607. 'Basic',
  2608. (
  2609. 'admin@admin.admin',
  2610. 'admin@admin.admin'
  2611. )
  2612. )
  2613. params = {
  2614. 'label': '',
  2615. 'raw_content': '<p> Le nouveau contenu </p>',
  2616. }
  2617. self.testapp.put_json(
  2618. '/api/v2/workspaces/2/threads/7',
  2619. params=params,
  2620. status=400
  2621. )
  2622. def test_api__get_thread_revisions__ok_200__nominal_case(
  2623. self
  2624. ) -> None:
  2625. """
  2626. Get threads revisions
  2627. """
  2628. self.testapp.authorization = (
  2629. 'Basic',
  2630. (
  2631. 'admin@admin.admin',
  2632. 'admin@admin.admin'
  2633. )
  2634. )
  2635. res = self.testapp.get(
  2636. '/api/v2/workspaces/2/threads/7/revisions',
  2637. status=200
  2638. )
  2639. revisions = res.json_body
  2640. assert len(revisions) == 2
  2641. revision = revisions[0]
  2642. assert revision['content_type'] == 'thread'
  2643. assert revision['content_id'] == 7
  2644. assert revision['is_archived'] is False
  2645. assert revision['is_deleted'] is False
  2646. assert revision['label'] == 'Best Cake'
  2647. assert revision['parent_id'] == 3
  2648. assert revision['show_in_ui'] is True
  2649. assert revision['slug'] == 'best-cake'
  2650. assert revision['status'] == 'open'
  2651. assert revision['workspace_id'] == 2
  2652. assert revision['revision_id'] == 8
  2653. assert revision['sub_content_types']
  2654. assert revision['revision_type'] == 'creation'
  2655. assert revision['comment_ids'] == [18, 19, 20]
  2656. # TODO - G.M - 2018-06-173 - check date format
  2657. assert revision['created']
  2658. assert revision['author']
  2659. assert revision['author']['user_id'] == 1
  2660. assert revision['author']['avatar_url'] is None
  2661. assert revision['author']['public_name'] == 'Global manager'
  2662. revision = revisions[1]
  2663. assert revision['content_type'] == 'thread'
  2664. assert revision['content_id'] == 7
  2665. assert revision['is_archived'] is False
  2666. assert revision['is_deleted'] is False
  2667. assert revision['label'] == 'Best Cakes?'
  2668. assert revision['parent_id'] == 3
  2669. assert revision['show_in_ui'] is True
  2670. assert revision['slug'] == 'best-cakes'
  2671. assert revision['status'] == 'open'
  2672. assert revision['workspace_id'] == 2
  2673. assert revision['revision_id'] == 26
  2674. assert revision['revision_type'] == 'edition'
  2675. assert revision['sub_content_types']
  2676. assert revision['comment_ids'] == []
  2677. # TODO - G.M - 2018-06-173 - check date format
  2678. assert revision['created']
  2679. assert revision['author']
  2680. assert revision['author']['user_id'] == 3
  2681. assert revision['author']['avatar_url'] is None
  2682. assert revision['author']['public_name'] == 'Bob i.'
  2683. def test_api__get_thread_revisions__ok_200__most_revision_type(self) -> None:
  2684. """
  2685. get threads revisions
  2686. """
  2687. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2688. admin = dbsession.query(models.User) \
  2689. .filter(models.User.email == 'admin@admin.admin') \
  2690. .one()
  2691. workspace_api = WorkspaceApi(
  2692. current_user=admin,
  2693. session=dbsession,
  2694. config=self.app_config
  2695. )
  2696. business_workspace = workspace_api.get_one(1)
  2697. content_api = ContentApi(
  2698. current_user=admin,
  2699. session=dbsession,
  2700. config=self.app_config
  2701. )
  2702. tool_folder = content_api.get_one(1, content_type=CONTENT_TYPES.Any_SLUG)
  2703. test_thread = content_api.create(
  2704. content_type_slug=CONTENT_TYPES.Thread.slug,
  2705. workspace=business_workspace,
  2706. parent=tool_folder,
  2707. label='Test Thread',
  2708. do_save=True,
  2709. do_notify=False,
  2710. )
  2711. with new_revision(
  2712. session=dbsession,
  2713. tm=transaction.manager,
  2714. content=test_thread,
  2715. ):
  2716. content_api.update_content(
  2717. test_thread,
  2718. new_label='test_thread_updated',
  2719. new_content='Just a test'
  2720. )
  2721. content_api.save(test_thread)
  2722. with new_revision(
  2723. session=dbsession,
  2724. tm=transaction.manager,
  2725. content=test_thread,
  2726. ):
  2727. content_api.archive(test_thread)
  2728. content_api.save(test_thread)
  2729. with new_revision(
  2730. session=dbsession,
  2731. tm=transaction.manager,
  2732. content=test_thread,
  2733. ):
  2734. content_api.unarchive(test_thread)
  2735. content_api.save(test_thread)
  2736. with new_revision(
  2737. session=dbsession,
  2738. tm=transaction.manager,
  2739. content=test_thread,
  2740. ):
  2741. content_api.delete(test_thread)
  2742. content_api.save(test_thread)
  2743. with new_revision(
  2744. session=dbsession,
  2745. tm=transaction.manager,
  2746. content=test_thread,
  2747. ):
  2748. content_api.undelete(test_thread)
  2749. content_api.save(test_thread)
  2750. dbsession.flush()
  2751. transaction.commit()
  2752. self.testapp.authorization = (
  2753. 'Basic',
  2754. (
  2755. 'admin@admin.admin',
  2756. 'admin@admin.admin'
  2757. )
  2758. )
  2759. res = self.testapp.get(
  2760. '/api/v2/workspaces/1/threads/{}/revisions'.format(test_thread.content_id), # nopep8
  2761. status=200
  2762. )
  2763. revisions = res.json_body
  2764. assert len(revisions) == 6
  2765. for revision in revisions:
  2766. revision['content_type'] == 'thread'
  2767. revision['workspace_id'] == 1
  2768. revision['content_id'] == test_thread.content_id
  2769. revision = revisions[0]
  2770. revision['revision_type'] == 'creation'
  2771. revision = revisions[1]
  2772. revision['revision_type'] == 'archiving'
  2773. revision = revisions[2]
  2774. revision['revision_type'] == 'unarchiving'
  2775. revision = revisions[3]
  2776. revision['revision_type'] == 'deletion'
  2777. revision = revisions[4]
  2778. revision['revision_type'] == 'undeletion'
  2779. def test_api__set_thread_status__ok_200__nominal_case(self) -> None:
  2780. """
  2781. Set thread status
  2782. """
  2783. self.testapp.authorization = (
  2784. 'Basic',
  2785. (
  2786. 'admin@admin.admin',
  2787. 'admin@admin.admin'
  2788. )
  2789. )
  2790. params = {
  2791. 'status': 'closed-deprecated',
  2792. }
  2793. # before
  2794. res = self.testapp.get(
  2795. '/api/v2/workspaces/2/threads/7',
  2796. status=200
  2797. ) # nopep8
  2798. content = res.json_body
  2799. assert content['content_type'] == 'thread'
  2800. assert content['content_id'] == 7
  2801. assert content['status'] == 'open'
  2802. # set status
  2803. self.testapp.put_json(
  2804. '/api/v2/workspaces/2/threads/7/status',
  2805. params=params,
  2806. status=204
  2807. )
  2808. # after
  2809. res = self.testapp.get(
  2810. '/api/v2/workspaces/2/threads/7',
  2811. status=200
  2812. ) # nopep8
  2813. content = res.json_body
  2814. assert content['content_type'] == 'thread'
  2815. assert content['content_id'] == 7
  2816. assert content['status'] == 'closed-deprecated'
  2817. def test_api__set_thread_status__ok_400__wrong_status(self) -> None:
  2818. """
  2819. Set thread status
  2820. """
  2821. self.testapp.authorization = (
  2822. 'Basic',
  2823. (
  2824. 'admin@admin.admin',
  2825. 'admin@admin.admin'
  2826. )
  2827. )
  2828. params = {
  2829. 'status': 'unexistant-status',
  2830. }
  2831. self.testapp.put_json(
  2832. '/api/v2/workspaces/2/threads/7/status',
  2833. params=params,
  2834. status=400
  2835. )