test_content_api.py 94KB

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