test_workspaces.py 104KB

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