test_content_api.py 102KB

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