test_workspaces.py 109KB

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