test_workspaces.py 104KB

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