test_workspaces.py 102KB

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