test_workspaces.py 98KB

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