test_workspaces.py 103KB

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