test_content_api.py 99KB

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