test_content_api.py 99KB

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