test_workspaces.py 104KB

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