test_content_api.py 87KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669
  1. # -*- coding: utf-8 -*-
  2. import datetime
  3. import transaction
  4. import pytest
  5. from tracim.config import CFG
  6. from tracim.lib.core.content import compare_content_for_sorting_by_type_and_name
  7. from tracim.lib.core.content import ContentApi
  8. # TODO - G.M - 28-03-2018 - [GroupApi] Re-enable GroupApi
  9. from tracim.lib.core.group import GroupApi
  10. from tracim.lib.core.user import UserApi
  11. from tracim.exceptions import SameValueError
  12. # TODO - G.M - 28-03-2018 - [RoleApi] Re-enable RoleApi
  13. from tracim.lib.core.workspace import RoleApi
  14. # TODO - G.M - 28-03-2018 - [WorkspaceApi] Re-enable WorkspaceApi
  15. from tracim.lib.core.workspace import WorkspaceApi
  16. from tracim.models.revision_protection import new_revision
  17. from tracim.models.auth import User
  18. from tracim.models.auth import Group
  19. from tracim.models.data import ActionDescription
  20. from tracim.models.data import ContentRevisionRO
  21. from tracim.models.data import Workspace
  22. from tracim.models.data import Content
  23. from tracim.models.data import ContentType
  24. from tracim.models.data import UserRoleInWorkspace
  25. from tracim.fixtures.users_and_groups import Test as FixtureTest
  26. from tracim.tests import DefaultTest
  27. from tracim.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=ContentType.Folder,
  110. workspace=workspace,
  111. parent=None,
  112. label='not_deleted',
  113. do_save=True
  114. )
  115. item2 = api.create(
  116. content_type=ContentType.Folder,
  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, ContentType.Any, workspace)
  139. eq_(2, len(items))
  140. items = api.get_all(None, ContentType.Any, 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, ContentType.Any, 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, ContentType.Any, 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=ContentType.Folder,
  215. workspace=workspace,
  216. parent=None,
  217. label='not_archived',
  218. do_save=True
  219. )
  220. item2 = api.create(
  221. content_type=ContentType.Folder,
  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, ContentType.Any, workspace)
  243. eq_(2, len(items))
  244. items = api.get_all(None, ContentType.Any, 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, ContentType.Any, 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, ContentType.Any, 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=ContentType.Folder,
  324. workspace=workspace,
  325. parent=None,
  326. label='thefolder',
  327. do_save=True
  328. )
  329. item2 = api.create(
  330. content_type=ContentType.File,
  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, ContentType.Any, workspace)
  353. eq_(2, len(items))
  354. items2 = api.get_all(None, ContentType.File, workspace)
  355. eq_(1, len(items2))
  356. eq_('thefile', items2[0].label)
  357. items3 = api.get_all(None, ContentType.Folder, 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. ContentType.Folder,
  388. workspace,
  389. None,
  390. 'parent',
  391. do_save=True,
  392. )
  393. item2 = api.create(
  394. ContentType.File,
  395. workspace,
  396. item,
  397. 'file1',
  398. do_save=True,
  399. )
  400. item3 = api.create(
  401. ContentType.File,
  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, ContentType.Any, workspace)
  426. eq_(3, len(items))
  427. items2 = api.get_all(parent_id, ContentType.File, 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(ContentType.Folder, 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(ContentType.Folder, 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(ContentType.Page, 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_(ContentType.Comment, 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. ContentType.Folder,
  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=ContentType.File,
  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. ContentType.Folder,
  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. ContentType.Folder,
  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=ContentType.File,
  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. ContentType.Folder,
  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. ContentType.Folder,
  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=ContentType.File,
  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(ContentType.Page, workspace1, None,
  935. 'this is a page', do_save=True)
  936. page_2 = cont_api_a.create(ContentType.Page, workspace1, None,
  937. 'this is page1', do_save=True)
  938. page_3 = cont_api_a.create(ContentType.Thread, workspace2, None,
  939. 'this is page2', do_save=True)
  940. page_4 = cont_api_a.create(ContentType.File, 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(ContentType.Page, 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. ContentType.Page,
  1090. workspace,
  1091. None,
  1092. 'this is page1',
  1093. do_save=True
  1094. )
  1095. page_3 = cont_api_a.create(
  1096. ContentType.Thread,
  1097. workspace,
  1098. None,
  1099. 'this is page2',
  1100. do_save=True
  1101. )
  1102. page_4 = cont_api_a.create(
  1103. ContentType.File,
  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=ContentType.Page,
  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, ContentType.Any, 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, ContentType.Any, 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, ContentType.Any, 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=ContentType.Page,
  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, ContentType.Any, 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=ContentType.File,
  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, ContentType.Any, 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, ContentType.Any, 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, ContentType.Any, 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=ContentType.Page,
  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, ContentType.Any, 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=ContentType.File,
  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, ContentType.Any, 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, ContentType.Any, 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, ContentType.Any, 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, ContentType.Any, 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=ContentType.File,
  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, ContentType.Any, 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, ContentType.Any, 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, ContentType.Any, 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, ContentType.Any, 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(ContentType.Folder, workspace2, None, 'Hepla', '', True) # nopep8
  1838. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  1839. # creation order test
  1840. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  1841. secondly_created = api.create(ContentType.Page, workspace, main_folder, 'another creation_order_test', '', True) # nopep8
  1842. # update order test
  1843. firstly_created_but_recently_updated = api.create(ContentType.Page, workspace, main_folder, 'update_order_test', '', True) # nopep8
  1844. secondly_created_but_not_updated = api.create(ContentType.Page, 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(ContentType.Page, workspace, main_folder, 'this is randomized label content', '', True) # nopep8
  1854. secondly_created_but_not_commented = api.create(ContentType.Page, 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(ContentType.Page, 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__workspace_filter_workspace_full(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. api = ContentApi(
  1901. current_user=user,
  1902. session=self.session,
  1903. config=self.app_config,
  1904. )
  1905. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  1906. # creation order test
  1907. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  1908. secondly_created = api.create(ContentType.Page, workspace, main_folder, 'another creation_order_test', '', True) # nopep8
  1909. # update order test
  1910. firstly_created_but_recently_updated = api.create(ContentType.Page, workspace, main_folder, 'update_order_test', '', True) # nopep8
  1911. secondly_created_but_not_updated = api.create(ContentType.Page, workspace, main_folder, 'another update_order_test', '', True) # nopep8
  1912. with new_revision(
  1913. session=self.session,
  1914. tm=transaction.manager,
  1915. content=firstly_created_but_recently_updated,
  1916. ):
  1917. firstly_created_but_recently_updated.description = 'Just an update'
  1918. api.save(firstly_created_but_recently_updated)
  1919. # comment change order
  1920. firstly_created_but_recently_commented = api.create(ContentType.Page, workspace, main_folder, 'this is randomized label content', '', True) # nopep8
  1921. secondly_created_but_not_commented = api.create(ContentType.Page, workspace, main_folder, 'this is another randomized label content', '', True) # nopep8
  1922. comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True) # nopep8
  1923. last_actives = api.get_last_active(workspace=workspace)
  1924. assert len(last_actives) == 7
  1925. # comment is newest than page2
  1926. assert last_actives[0] == firstly_created_but_recently_commented
  1927. assert last_actives[1] == secondly_created_but_not_commented
  1928. # last updated content is newer than other one despite creation
  1929. # of the other is more recent
  1930. assert last_actives[2] == firstly_created_but_recently_updated
  1931. assert last_actives[3] == secondly_created_but_not_updated
  1932. # creation order is inverted here as last created is last active
  1933. assert last_actives[4] == secondly_created
  1934. assert last_actives[5] == firstly_created
  1935. # folder subcontent modification does not change folder order
  1936. assert last_actives[6] == main_folder
  1937. def test_unit__get_last_active__ok__workspace_filter_workspace_content_ids(self):
  1938. uapi = UserApi(
  1939. session=self.session,
  1940. config=self.app_config,
  1941. current_user=None,
  1942. )
  1943. group_api = GroupApi(
  1944. current_user=None,
  1945. session=self.session,
  1946. config=self.app_config,
  1947. )
  1948. groups = [group_api.get_one(Group.TIM_USER),
  1949. group_api.get_one(Group.TIM_MANAGER),
  1950. group_api.get_one(Group.TIM_ADMIN)]
  1951. user = uapi.create_minimal_user(email='this.is@user',
  1952. groups=groups, save_now=True)
  1953. workspace = WorkspaceApi(
  1954. current_user=user,
  1955. session=self.session,
  1956. config=self.app_config,
  1957. ).create_workspace(
  1958. 'test workspace',
  1959. save_now=True
  1960. )
  1961. api = ContentApi(
  1962. current_user=user,
  1963. session=self.session,
  1964. config=self.app_config,
  1965. )
  1966. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  1967. # creation order test
  1968. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  1969. secondly_created = api.create(ContentType.Page, workspace, main_folder, 'another creation_order_test', '', True) # nopep8
  1970. # update order test
  1971. firstly_created_but_recently_updated = api.create(ContentType.Page, workspace, main_folder, 'update_order_test', '', True) # nopep8
  1972. secondly_created_but_not_updated = api.create(ContentType.Page, workspace, main_folder, 'another update_order_test', '', True) # nopep8
  1973. with new_revision(
  1974. session=self.session,
  1975. tm=transaction.manager,
  1976. content=firstly_created_but_recently_updated,
  1977. ):
  1978. firstly_created_but_recently_updated.description = 'Just an update'
  1979. api.save(firstly_created_but_recently_updated)
  1980. # comment change order
  1981. firstly_created_but_recently_commented = api.create(ContentType.Page, workspace, main_folder, 'this is randomized label content', '', True) # nopep8
  1982. secondly_created_but_not_commented = api.create(ContentType.Page, workspace, main_folder, 'this is another randomized label content', '', True) # nopep8
  1983. comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True) # nopep8
  1984. selected_contents = [
  1985. firstly_created_but_recently_commented,
  1986. firstly_created_but_recently_updated,
  1987. firstly_created,
  1988. main_folder,
  1989. ]
  1990. content_ids = [content.content_id for content in selected_contents]
  1991. last_actives = api.get_last_active(
  1992. workspace=workspace,
  1993. content_ids=content_ids,
  1994. )
  1995. assert len(last_actives) == 4
  1996. # comment is newest than page2
  1997. assert last_actives[0] == firstly_created_but_recently_commented
  1998. assert secondly_created_but_not_commented not in last_actives
  1999. # last updated content is newer than other one despite creation
  2000. # of the other is more recent
  2001. assert last_actives[1] == firstly_created_but_recently_updated
  2002. assert secondly_created_but_not_updated not in last_actives
  2003. # creation order is inverted here as last created is last active
  2004. assert secondly_created not in last_actives
  2005. assert last_actives[2] == firstly_created
  2006. # folder subcontent modification does not change folder order
  2007. assert last_actives[3] == main_folder
  2008. def test_unit__get_last_active__ok__workspace_filter_workspace_limit_2_multiples_times(self): # nopep8
  2009. uapi = UserApi(
  2010. session=self.session,
  2011. config=self.app_config,
  2012. current_user=None,
  2013. )
  2014. group_api = GroupApi(
  2015. current_user=None,
  2016. session=self.session,
  2017. config=self.app_config,
  2018. )
  2019. groups = [group_api.get_one(Group.TIM_USER),
  2020. group_api.get_one(Group.TIM_MANAGER),
  2021. group_api.get_one(Group.TIM_ADMIN)]
  2022. user = uapi.create_minimal_user(email='this.is@user',
  2023. groups=groups, save_now=True)
  2024. workspace = WorkspaceApi(
  2025. current_user=user,
  2026. session=self.session,
  2027. config=self.app_config,
  2028. ).create_workspace(
  2029. 'test workspace',
  2030. save_now=True
  2031. )
  2032. api = ContentApi(
  2033. current_user=user,
  2034. session=self.session,
  2035. config=self.app_config,
  2036. )
  2037. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  2038. # creation order test
  2039. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  2040. secondly_created = api.create(ContentType.Page, workspace, main_folder, 'another creation_order_test', '', True) # nopep8
  2041. # update order test
  2042. firstly_created_but_recently_updated = api.create(ContentType.Page, workspace, main_folder, 'update_order_test', '', True) # nopep8
  2043. secondly_created_but_not_updated = api.create(ContentType.Page, workspace, main_folder, 'another update_order_test', '', True) # nopep8
  2044. with new_revision(
  2045. session=self.session,
  2046. tm=transaction.manager,
  2047. content=firstly_created_but_recently_updated,
  2048. ):
  2049. firstly_created_but_recently_updated.description = 'Just an update'
  2050. api.save(firstly_created_but_recently_updated)
  2051. # comment change order
  2052. firstly_created_but_recently_commented = api.create(ContentType.Page, workspace, main_folder, 'this is randomized label content', '', True) # nopep8
  2053. secondly_created_but_not_commented = api.create(ContentType.Page, workspace, main_folder, 'this is another randomized label content', '', True) # nopep8
  2054. comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True) # nopep8
  2055. last_actives = api.get_last_active(workspace=workspace, limit=2, before_datetime=datetime.datetime.now()) # nopep8
  2056. assert len(last_actives) == 2
  2057. # comment is newest than page2
  2058. assert last_actives[0] == firstly_created_but_recently_commented
  2059. assert last_actives[1] == secondly_created_but_not_commented
  2060. last_actives = api.get_last_active(workspace=workspace, limit=2, before_datetime=last_actives[1].get_simple_last_activity_date()) # nopep8
  2061. assert len(last_actives) == 2
  2062. # last updated content is newer than other one despite creation
  2063. # of the other is more recent
  2064. assert last_actives[0] == firstly_created_but_recently_updated
  2065. assert last_actives[1] == secondly_created_but_not_updated
  2066. last_actives = api.get_last_active(workspace=workspace, limit=2, before_datetime=last_actives[1].get_simple_last_activity_date()) # nopep8
  2067. assert len(last_actives) == 2
  2068. # creation order is inverted here as last created is last active
  2069. assert last_actives[0] == secondly_created
  2070. assert last_actives[1] == firstly_created
  2071. last_actives = api.get_last_active(workspace=workspace, limit=2, before_datetime=last_actives[1].get_simple_last_activity_date()) # nopep8
  2072. assert len(last_actives) == 1
  2073. # folder subcontent modification does not change folder order
  2074. assert last_actives[0] == main_folder
  2075. def test_unit__get_last_active__ok__workspace_filter_workspace_empty(self):
  2076. uapi = UserApi(
  2077. session=self.session,
  2078. config=self.app_config,
  2079. current_user=None,
  2080. )
  2081. group_api = GroupApi(
  2082. current_user=None,
  2083. session=self.session,
  2084. config=self.app_config,
  2085. )
  2086. groups = [group_api.get_one(Group.TIM_USER),
  2087. group_api.get_one(Group.TIM_MANAGER),
  2088. group_api.get_one(Group.TIM_ADMIN)]
  2089. user = uapi.create_minimal_user(email='this.is@user',
  2090. groups=groups, save_now=True)
  2091. workspace = WorkspaceApi(
  2092. current_user=user,
  2093. session=self.session,
  2094. config=self.app_config,
  2095. ).create_workspace(
  2096. 'test workspace',
  2097. save_now=True
  2098. )
  2099. workspace2 = WorkspaceApi(
  2100. current_user=user,
  2101. session=self.session,
  2102. config=self.app_config,
  2103. ).create_workspace(
  2104. 'test workspace2',
  2105. save_now=True
  2106. )
  2107. api = ContentApi(
  2108. current_user=user,
  2109. session=self.session,
  2110. config=self.app_config,
  2111. )
  2112. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  2113. # creation order test
  2114. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  2115. secondly_created = api.create(ContentType.Page, workspace, main_folder, 'another creation_order_test', '', True) # nopep8
  2116. # update order test
  2117. firstly_created_but_recently_updated = api.create(ContentType.Page, workspace, main_folder, 'update_order_test', '', True) # nopep8
  2118. secondly_created_but_not_updated = api.create(ContentType.Page, workspace, main_folder, 'another update_order_test', '', True) # nopep8
  2119. with new_revision(
  2120. session=self.session,
  2121. tm=transaction.manager,
  2122. content=firstly_created_but_recently_updated,
  2123. ):
  2124. firstly_created_but_recently_updated.description = 'Just an update'
  2125. api.save(firstly_created_but_recently_updated)
  2126. # comment change order
  2127. firstly_created_but_recently_commented = api.create(ContentType.Page, workspace, main_folder, 'this is randomized label content', '', True) # nopep8
  2128. secondly_created_but_not_commented = api.create(ContentType.Page, workspace, main_folder, 'this is another randomized label content', '', True) # nopep8
  2129. comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True) # nopep8
  2130. last_actives = api.get_last_active(workspace=workspace2)
  2131. assert len(last_actives) == 0
  2132. def test_search_in_label(self):
  2133. # HACK - D.A. - 2015-03-09
  2134. # This test is based on a bug which does NOT return results found
  2135. # at root of a workspace (eg a folder)
  2136. uapi = UserApi(
  2137. session=self.session,
  2138. config=self.app_config,
  2139. current_user=None,
  2140. )
  2141. group_api = GroupApi(
  2142. current_user=None,
  2143. session=self.session,
  2144. config=self.app_config,
  2145. )
  2146. groups = [group_api.get_one(Group.TIM_USER),
  2147. group_api.get_one(Group.TIM_MANAGER),
  2148. group_api.get_one(Group.TIM_ADMIN)]
  2149. user = uapi.create_minimal_user(email='this.is@user',
  2150. groups=groups, save_now=True)
  2151. workspace = WorkspaceApi(
  2152. current_user=user,
  2153. session=self.session,
  2154. config=self.app_config,
  2155. ).create_workspace(
  2156. 'test workspace',
  2157. save_now=True
  2158. )
  2159. api = ContentApi(
  2160. current_user=user,
  2161. session=self.session,
  2162. config=self.app_config,
  2163. )
  2164. a = api.create(ContentType.Folder, workspace, None,
  2165. 'this is randomized folder', '', True)
  2166. p = api.create(ContentType.Page, workspace, a,
  2167. 'this is randomized label content', '', True)
  2168. with new_revision(
  2169. session=self.session,
  2170. tm=transaction.manager,
  2171. content=p,
  2172. ):
  2173. p.description = 'This is some amazing test'
  2174. api.save(p)
  2175. original_id = p.content_id
  2176. res = api.search(['randomized'])
  2177. eq_(1, len(res.all()))
  2178. item = res.all()[0]
  2179. eq_(original_id, item.content_id)
  2180. def test_search_in_description(self):
  2181. # HACK - D.A. - 2015-03-09
  2182. # This test is based on a bug which does NOT return results found
  2183. # at root of a workspace (eg a folder)
  2184. uapi = UserApi(
  2185. session=self.session,
  2186. config=self.app_config,
  2187. current_user=None,
  2188. )
  2189. group_api = GroupApi(
  2190. current_user=None,
  2191. session=self.session,
  2192. config=self.app_config,
  2193. )
  2194. groups = [group_api.get_one(Group.TIM_USER),
  2195. group_api.get_one(Group.TIM_MANAGER),
  2196. group_api.get_one(Group.TIM_ADMIN)]
  2197. user = uapi.create_minimal_user(email='this.is@user',
  2198. groups=groups, save_now=True)
  2199. workspace = WorkspaceApi(
  2200. current_user=user,
  2201. session=self.session,
  2202. config=self.app_config,
  2203. ).create_workspace(
  2204. 'test workspace',
  2205. save_now=True,
  2206. )
  2207. api = ContentApi(
  2208. current_user=user,
  2209. session=self.session,
  2210. config=self.app_config,
  2211. )
  2212. a = api.create(ContentType.Folder, workspace, None,
  2213. 'this is randomized folder', '', True)
  2214. p = api.create(ContentType.Page, workspace, a,
  2215. 'this is dummy label content', '', True)
  2216. with new_revision(
  2217. tm=transaction.manager,
  2218. session=self.session,
  2219. content=p,
  2220. ):
  2221. p.description = 'This is some amazing test'
  2222. api.save(p)
  2223. original_id = p.content_id
  2224. res = api.search(['dummy'])
  2225. eq_(1, len(res.all()))
  2226. item = res.all()[0]
  2227. eq_(original_id, item.content_id)
  2228. def test_search_in_label_or_description(self):
  2229. # HACK - D.A. - 2015-03-09
  2230. # This test is based on a bug which does NOT return results found
  2231. # at root of a workspace (eg a folder)
  2232. uapi = UserApi(
  2233. session=self.session,
  2234. config=self.app_config,
  2235. current_user=None,
  2236. )
  2237. group_api = GroupApi(
  2238. current_user=None,
  2239. session=self.session,
  2240. config=self.app_config,
  2241. )
  2242. groups = [group_api.get_one(Group.TIM_USER),
  2243. group_api.get_one(Group.TIM_MANAGER),
  2244. group_api.get_one(Group.TIM_ADMIN)]
  2245. user = uapi.create_minimal_user(email='this.is@user',
  2246. groups=groups, save_now=True)
  2247. workspace = WorkspaceApi(
  2248. current_user=user,
  2249. session=self.session,
  2250. config=self.app_config,
  2251. ).create_workspace('test workspace', save_now=True)
  2252. api = ContentApi(
  2253. current_user=user,
  2254. session=self.session,
  2255. config=self.app_config,
  2256. )
  2257. a = api.create(
  2258. content_type=ContentType.Folder,
  2259. workspace=workspace,
  2260. parent=None,
  2261. label='this is randomized folder',
  2262. do_save=True
  2263. )
  2264. p1 = api.create(
  2265. content_type=ContentType.Page,
  2266. workspace=workspace,
  2267. parent=a,
  2268. label='this is dummy label content',
  2269. do_save=True
  2270. )
  2271. p2 = api.create(
  2272. content_type=ContentType.Page,
  2273. workspace=workspace,
  2274. parent=a,
  2275. label='Hey ! Jon !',
  2276. do_save=True
  2277. )
  2278. with new_revision(
  2279. session=self.session,
  2280. tm=transaction.manager,
  2281. content=p1,
  2282. ):
  2283. p1.description = 'This is some amazing test'
  2284. with new_revision(
  2285. session=self.session,
  2286. tm=transaction.manager,
  2287. content=p2,
  2288. ):
  2289. p2.description = 'What\'s up?'
  2290. api.save(p1)
  2291. api.save(p2)
  2292. id1 = p1.content_id
  2293. id2 = p2.content_id
  2294. eq_(1, self.session.query(Workspace).filter(Workspace.label == 'test workspace').count())
  2295. eq_(1, self.session.query(ContentRevisionRO).filter(ContentRevisionRO.label == 'this is randomized folder').count())
  2296. eq_(2, self.session.query(ContentRevisionRO).filter(ContentRevisionRO.label == 'this is dummy label content').count())
  2297. eq_(1, self.session.query(ContentRevisionRO).filter(ContentRevisionRO.description == 'This is some amazing test').count())
  2298. eq_(2, self.session.query(ContentRevisionRO).filter(ContentRevisionRO.label == 'Hey ! Jon !').count())
  2299. eq_(1, self.session.query(ContentRevisionRO).filter(ContentRevisionRO.description == 'What\'s up?').count())
  2300. res = api.search(['dummy', 'jon'])
  2301. eq_(2, len(res.all()))
  2302. eq_(True, id1 in [o.content_id for o in res.all()])
  2303. eq_(True, id2 in [o.content_id for o in res.all()])
  2304. def test_unit__search_exclude_content_under_deleted_or_archived_parents__ok(self): # nopep8
  2305. admin = self.session.query(User)\
  2306. .filter(User.email == 'admin@admin.admin').one()
  2307. workspace = self._create_workspace_and_test(
  2308. 'workspace_1',
  2309. admin
  2310. )
  2311. folder_1 = self._create_content_and_test(
  2312. 'folder_1',
  2313. workspace=workspace,
  2314. type=ContentType.Folder
  2315. )
  2316. folder_2 = self._create_content_and_test(
  2317. 'folder_2',
  2318. workspace=workspace,
  2319. type=ContentType.Folder
  2320. )
  2321. page_1 = self._create_content_and_test(
  2322. 'foo', workspace=workspace,
  2323. type=ContentType.Page,
  2324. parent=folder_1
  2325. )
  2326. page_2 = self._create_content_and_test(
  2327. 'bar',
  2328. workspace=workspace,
  2329. type=ContentType.Page,
  2330. parent=folder_2
  2331. )
  2332. api = ContentApi(
  2333. current_user=admin,
  2334. session=self.session,
  2335. config=self.app_config,
  2336. )
  2337. foo_result = api.search(['foo']).all()
  2338. eq_(1, len(foo_result))
  2339. assert page_1 in foo_result
  2340. bar_result = api.search(['bar']).all()
  2341. eq_(1, len(bar_result))
  2342. assert page_2 in bar_result
  2343. with new_revision(
  2344. session=self.session,
  2345. tm=transaction.manager,
  2346. content=folder_1,
  2347. ):
  2348. api.delete(folder_1)
  2349. with new_revision(
  2350. session=self.session,
  2351. tm=transaction.manager,
  2352. content=folder_2,
  2353. ):
  2354. api.archive(folder_2)
  2355. # Actually ContentApi.search don't filter it
  2356. foo_result = api.search(['foo']).all()
  2357. eq_(1, len(foo_result))
  2358. assert page_1 in foo_result
  2359. bar_result = api.search(['bar']).all()
  2360. eq_(1, len(bar_result))
  2361. assert page_2 in bar_result
  2362. # ContentApi offer exclude_unavailable method to do it
  2363. foo_result = api.search(['foo']).all()
  2364. api.exclude_unavailable(foo_result)
  2365. eq_(0, len(foo_result))
  2366. bar_result = api.search(['bar']).all()
  2367. api.exclude_unavailable(bar_result)
  2368. eq_(0, len(bar_result))
  2369. class TestContentApiSecurity(DefaultTest):
  2370. fixtures = [FixtureTest, ]
  2371. def test_unit__cant_get_non_access_content__ok__nominal_case(self):
  2372. admin = self.session.query(User)\
  2373. .filter(User.email == 'admin@admin.admin').one()
  2374. bob = self.session.query(User)\
  2375. .filter(User.email == 'bob@fsf.local').one()
  2376. bob_workspace = WorkspaceApi(
  2377. current_user=bob,
  2378. session=self.session,
  2379. config=self.app_config,
  2380. ).create_workspace(
  2381. 'bob_workspace',
  2382. save_now=True,
  2383. )
  2384. admin_workspace = WorkspaceApi(
  2385. current_user=admin,
  2386. session=self.session,
  2387. config=self.app_config,
  2388. ).create_workspace(
  2389. 'admin_workspace',
  2390. save_now=True,
  2391. )
  2392. bob_page = ContentApi(
  2393. current_user=bob,
  2394. session=self.session,
  2395. config=self.app_config,
  2396. ).create(
  2397. content_type=ContentType.Page,
  2398. workspace=bob_workspace,
  2399. label='bob_page',
  2400. do_save=True,
  2401. )
  2402. admin_page = ContentApi(
  2403. current_user=admin,
  2404. session=self.session,
  2405. config=self.app_config,
  2406. ).create(
  2407. content_type=ContentType.Page,
  2408. workspace=admin_workspace,
  2409. label='admin_page',
  2410. do_save=True,
  2411. )
  2412. bob_viewable = ContentApi(
  2413. current_user=bob,
  2414. session=self.session,
  2415. config=self.app_config,
  2416. ).get_all()
  2417. eq_(1, len(bob_viewable), 'Bob should view only one content')
  2418. eq_(
  2419. 'bob_page',
  2420. bob_viewable[0].label,
  2421. 'Bob should not view "{0}" content'.format(
  2422. bob_viewable[0].label,
  2423. )
  2424. )