test_workspaces.py 112KB

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