test_content_api.py 91KB

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