test_content_api.py 64KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082
  1. # -*- coding: utf-8 -*-
  2. import transaction
  3. import pytest
  4. from tracim.config import CFG
  5. from tracim.lib.core.content import compare_content_for_sorting_by_type_and_name
  6. from tracim.lib.core.content import ContentApi
  7. # TODO - G.M - 28-03-2018 - [GroupApi] Re-enable GroupApi
  8. from tracim.lib.core.group import GroupApi
  9. from tracim.lib.core.user import UserApi
  10. from tracim.exceptions import SameValueError
  11. # TODO - G.M - 28-03-2018 - [RoleApi] Re-enable RoleApi
  12. from tracim.lib.core.workspace import RoleApi
  13. # TODO - G.M - 28-03-2018 - [WorkspaceApi] Re-enable WorkspaceApi
  14. from tracim.lib.core.workspace import WorkspaceApi
  15. from tracim.models.revision_protection import new_revision
  16. from tracim.models.auth import User
  17. from tracim.models.auth import Group
  18. from tracim.models.data import ActionDescription
  19. from tracim.models.data import ContentRevisionRO
  20. from tracim.models.data import Workspace
  21. from tracim.models.data import Content
  22. from tracim.models.data import ContentType
  23. from tracim.models.data import UserRoleInWorkspace
  24. from tracim.fixtures.users_and_groups import Test as FixtureTest
  25. from tracim.tests import DefaultTest
  26. from tracim.tests import eq_
  27. class TestContentApi(DefaultTest):
  28. def test_compare_content_for_sorting_by_type(self):
  29. c1 = Content()
  30. c1.label = ''
  31. c1.type = 'file'
  32. c2 = Content()
  33. c2.label = ''
  34. c2.type = 'folder'
  35. c11 = c1
  36. eq_(1, compare_content_for_sorting_by_type_and_name(c1, c2))
  37. eq_(-1, compare_content_for_sorting_by_type_and_name(c2, c1))
  38. eq_(0, compare_content_for_sorting_by_type_and_name(c1, c11))
  39. def test_compare_content_for_sorting_by_label(self):
  40. c1 = Content()
  41. c1.label = 'bbb'
  42. c1.type = 'file'
  43. c2 = Content()
  44. c2.label = 'aaa'
  45. c2.type = 'file'
  46. c11 = c1
  47. eq_(1, compare_content_for_sorting_by_type_and_name(c1, c2))
  48. eq_(-1, compare_content_for_sorting_by_type_and_name(c2, c1))
  49. eq_(0, compare_content_for_sorting_by_type_and_name(c1, c11))
  50. def test_sort_by_label_or_filename(self):
  51. c1 = Content()
  52. c1.label = 'ABCD'
  53. c1.type = 'file'
  54. c2 = Content()
  55. c2.label = ''
  56. c2.type = 'file'
  57. c2.file_name = 'AABC'
  58. c3 = Content()
  59. c3.label = 'BCDE'
  60. c3.type = 'file'
  61. items = [c1, c2, c3]
  62. sorteds = ContentApi.sort_content(items)
  63. eq_(sorteds[0], c2)
  64. eq_(sorteds[1], c1)
  65. eq_(sorteds[2], c3)
  66. def test_sort_by_content_type(self):
  67. c1 = Content()
  68. c1.label = 'AAAA'
  69. c1.type = 'file'
  70. c2 = Content()
  71. c2.label = 'BBBB'
  72. c2.type = 'folder'
  73. items = [c1, c2]
  74. sorteds = ContentApi.sort_content(items)
  75. eq_(sorteds[0], c2,
  76. 'value is {} instead of {}'.format(sorteds[0].content_id,
  77. c2.content_id))
  78. eq_(sorteds[1], c1,
  79. 'value is {} instead of {}'.format(sorteds[1].content_id,
  80. c1.content_id))
  81. def test_delete(self):
  82. uapi = UserApi(
  83. session=self.session,
  84. config=self.app_config,
  85. current_user=None,
  86. )
  87. group_api = GroupApi(current_user=None,session=self.session)
  88. groups = [group_api.get_one(Group.TIM_USER),
  89. group_api.get_one(Group.TIM_MANAGER),
  90. group_api.get_one(Group.TIM_ADMIN)]
  91. user = uapi.create_minimal_user(email='this.is@user',
  92. groups=groups, save_now=True)
  93. workspace = WorkspaceApi(
  94. current_user=user,
  95. session=self.session
  96. ).create_workspace('test workspace', save_now=True)
  97. api = ContentApi(
  98. current_user=user,
  99. session=self.session,
  100. config=self.app_config,
  101. )
  102. item = api.create(ContentType.Folder, workspace, None,
  103. 'not_deleted', True)
  104. item2 = api.create(ContentType.Folder, workspace, None,
  105. 'to_delete', True)
  106. uid = user.user_id
  107. wid = workspace.workspace_id
  108. transaction.commit()
  109. # Refresh instances after commit
  110. user = uapi.get_one(uid)
  111. workspace_api = WorkspaceApi(current_user=user, session=self.session)
  112. workspace = workspace_api.get_one(wid)
  113. api = ContentApi(
  114. current_user=user,
  115. session=self.session,
  116. config=self.app_config,
  117. )
  118. items = api.get_all(None, ContentType.Any, workspace)
  119. eq_(2, len(items))
  120. items = api.get_all(None, ContentType.Any, workspace)
  121. with new_revision(
  122. session=self.session,
  123. tm=transaction.manager,
  124. content=items[0]
  125. ):
  126. api.delete(items[0])
  127. transaction.commit()
  128. # Refresh instances after commit
  129. user = uapi.get_one(uid)
  130. workspace_api = WorkspaceApi(current_user=user, session=self.session)
  131. workspace = workspace_api.get_one(wid)
  132. api = ContentApi(
  133. current_user=user,
  134. session=self.session,
  135. config=self.app_config,
  136. )
  137. items = api.get_all(None, ContentType.Any, workspace)
  138. eq_(1, len(items))
  139. transaction.commit()
  140. # Test that the item is still available if "show deleted" is activated
  141. # Refresh instances after commit
  142. user = uapi.get_one(uid)
  143. workspace_api = WorkspaceApi(current_user=user, session=self.session)
  144. api = ContentApi(
  145. current_user=user,
  146. session=self.session,
  147. config=self.app_config,
  148. show_deleted=True,
  149. )
  150. items = api.get_all(None, ContentType.Any, workspace)
  151. eq_(2, len(items))
  152. def test_archive(self):
  153. uapi = UserApi(
  154. session=self.session,
  155. config=self.app_config,
  156. current_user=None,
  157. )
  158. group_api = GroupApi(current_user=None, session=self.session)
  159. groups = [group_api.get_one(Group.TIM_USER),
  160. group_api.get_one(Group.TIM_MANAGER),
  161. group_api.get_one(Group.TIM_ADMIN)]
  162. user = uapi.create_minimal_user(email='this.is@user',
  163. groups=groups, save_now=True)
  164. workspace_api = WorkspaceApi(current_user=user, session=self.session)
  165. workspace = workspace_api.create_workspace(
  166. 'test workspace',
  167. save_now=True
  168. )
  169. api = ContentApi(
  170. current_user=user,
  171. session=self.session,
  172. config=self.app_config,
  173. )
  174. item = api.create(ContentType.Folder, workspace, None,
  175. 'not_archived', True)
  176. item2 = api.create(ContentType.Folder, workspace, None,
  177. 'to_archive', True)
  178. uid = user.user_id
  179. wid = workspace.workspace_id
  180. transaction.commit()
  181. # Refresh instances after commit
  182. user = uapi.get_one(uid)
  183. workspace_api = WorkspaceApi(current_user=user, session=self.session)
  184. api = ContentApi(
  185. session=self.session,
  186. current_user=user,
  187. config=self.app_config,
  188. )
  189. items = api.get_all(None, ContentType.Any, workspace)
  190. eq_(2, len(items))
  191. items = api.get_all(None, ContentType.Any, workspace)
  192. with new_revision(
  193. session=self.session,
  194. tm=transaction.manager,
  195. content=items[0],
  196. ):
  197. api.archive(items[0])
  198. transaction.commit()
  199. # Refresh instances after commit
  200. user = uapi.get_one(uid)
  201. workspace_api = WorkspaceApi(current_user=user, session=self.session)
  202. workspace = workspace_api.get_one(wid)
  203. api = ContentApi(
  204. current_user=user,
  205. session=self.session,
  206. config=self.app_config,
  207. )
  208. items = api.get_all(None, ContentType.Any, workspace)
  209. eq_(1, len(items))
  210. transaction.commit()
  211. # Refresh instances after commit
  212. user = uapi.get_one(uid)
  213. workspace_api = WorkspaceApi(current_user=user, session=self.session)
  214. workspace = workspace_api.get_one(wid)
  215. api = ContentApi(
  216. current_user=user,
  217. session=self.session,
  218. config=self.app_config,
  219. )
  220. # Test that the item is still available if "show deleted" is activated
  221. api = ContentApi(
  222. current_user=None,
  223. session=self.session,
  224. config=self.app_config,
  225. show_archived=True,
  226. )
  227. items = api.get_all(None, ContentType.Any, workspace)
  228. eq_(2, len(items))
  229. def test_get_all_with_filter(self):
  230. uapi = UserApi(
  231. session=self.session,
  232. config=self.app_config,
  233. current_user=None,
  234. )
  235. group_api = GroupApi(current_user=None, session=self.session)
  236. groups = [group_api.get_one(Group.TIM_USER),
  237. group_api.get_one(Group.TIM_MANAGER),
  238. group_api.get_one(Group.TIM_ADMIN)]
  239. user = uapi.create_minimal_user(
  240. email='this.is@user',
  241. groups=groups,
  242. save_now=True
  243. )
  244. workspace = WorkspaceApi(
  245. current_user=user,
  246. session=self.session
  247. ).create_workspace(
  248. 'test workspace',
  249. save_now=True
  250. )
  251. api = ContentApi(
  252. current_user=user,
  253. session=self.session,
  254. config=self.app_config,
  255. )
  256. item = api.create(ContentType.Folder, workspace, None,
  257. 'thefolder', True)
  258. item2 = api.create(ContentType.File, workspace, None, 'thefile', True)
  259. uid = user.user_id
  260. wid = workspace.workspace_id
  261. transaction.commit()
  262. # Refresh instances after commit
  263. user = uapi.get_one(uid)
  264. workspace_api = WorkspaceApi(current_user=user, session=self.session)
  265. workspace = workspace_api.get_one(wid)
  266. api = ContentApi(
  267. current_user=user,
  268. session=self.session,
  269. config=self.app_config,
  270. )
  271. items = api.get_all(None, ContentType.Any, workspace)
  272. eq_(2, len(items))
  273. items2 = api.get_all(None, ContentType.File, workspace)
  274. eq_(1, len(items2))
  275. eq_('thefile', items2[0].label)
  276. items3 = api.get_all(None, ContentType.Folder, workspace)
  277. eq_(1, len(items3))
  278. eq_('thefolder', items3[0].label)
  279. def test_get_all_with_parent_id(self):
  280. uapi = UserApi(
  281. session=self.session,
  282. config=self.app_config,
  283. current_user=None,
  284. )
  285. group_api = GroupApi(current_user=None, session=self.session)
  286. groups = [group_api.get_one(Group.TIM_USER),
  287. group_api.get_one(Group.TIM_MANAGER),
  288. group_api.get_one(Group.TIM_ADMIN)]
  289. user = uapi.create_minimal_user(email='this.is@user',
  290. groups=groups, save_now=True)
  291. workspace = WorkspaceApi(
  292. current_user=user,
  293. session=self.session
  294. ).create_workspace('test workspace', save_now=True)
  295. api = ContentApi(
  296. current_user=user,
  297. session=self.session,
  298. config=self.app_config,
  299. )
  300. item = api.create(
  301. ContentType.Folder,
  302. workspace,
  303. None,
  304. 'parent',
  305. do_save=True,
  306. )
  307. item2 = api.create(
  308. ContentType.File,
  309. workspace,
  310. item,
  311. 'file1',
  312. do_save=True,
  313. )
  314. item3 = api.create(
  315. ContentType.File,
  316. workspace,
  317. None,
  318. 'file2',
  319. do_save=True,
  320. )
  321. parent_id = item.content_id
  322. child_id = item2.content_id
  323. uid = user.user_id
  324. wid = workspace.workspace_id
  325. transaction.commit()
  326. # Refresh instances after commit
  327. user = uapi.get_one(uid)
  328. workspace_api = WorkspaceApi(current_user=user, session=self.session)
  329. workspace = workspace_api.get_one(wid)
  330. api = ContentApi(
  331. current_user=user,
  332. session=self.session,
  333. config=self.app_config,
  334. )
  335. items = api.get_all(None, ContentType.Any, workspace)
  336. eq_(3, len(items))
  337. items2 = api.get_all(parent_id, ContentType.File, workspace)
  338. eq_(1, len(items2))
  339. eq_(child_id, items2[0].content_id)
  340. def test_set_status_unknown_status(self):
  341. uapi = UserApi(
  342. session=self.session,
  343. config=self.app_config,
  344. current_user=None,
  345. )
  346. group_api = GroupApi(
  347. current_user=None,
  348. session=self.session
  349. )
  350. groups = [group_api.get_one(Group.TIM_USER),
  351. group_api.get_one(Group.TIM_MANAGER),
  352. group_api.get_one(Group.TIM_ADMIN)]
  353. user = uapi.create_minimal_user(email='this.is@user',
  354. groups=groups, save_now=True)
  355. workspace = WorkspaceApi(
  356. current_user=user,
  357. session=self.session
  358. ).create_workspace(
  359. 'test workspace',
  360. save_now=True
  361. )
  362. api = ContentApi(
  363. current_user=user,
  364. session=self.session,
  365. config=self.app_config,
  366. )
  367. c = api.create(ContentType.Folder, workspace, None, 'parent', True)
  368. with new_revision(
  369. session=self.session,
  370. tm=transaction.manager,
  371. content=c,
  372. ):
  373. with pytest.raises(ValueError):
  374. api.set_status(c, 'unknown-status')
  375. def test_set_status_ok(self):
  376. uapi = UserApi(
  377. session=self.session,
  378. config=self.app_config,
  379. current_user=None,
  380. )
  381. group_api = GroupApi(
  382. current_user=None,
  383. session=self.session
  384. )
  385. groups = [group_api.get_one(Group.TIM_USER),
  386. group_api.get_one(Group.TIM_MANAGER),
  387. group_api.get_one(Group.TIM_ADMIN)]
  388. user = uapi.create_minimal_user(email='this.is@user',
  389. groups=groups, save_now=True)
  390. workspace = WorkspaceApi(
  391. current_user=user,
  392. session=self.session
  393. ).create_workspace(
  394. 'test workspace',
  395. save_now=True
  396. )
  397. api = ContentApi(
  398. current_user=user,
  399. session=self.session,
  400. config=self.app_config,
  401. )
  402. c = api.create(ContentType.Folder, workspace, None, 'parent', True)
  403. with new_revision(
  404. session=self.session,
  405. tm=transaction.manager,
  406. content=c,
  407. ):
  408. for new_status in ['open', 'closed-validated', 'closed-unvalidated',
  409. 'closed-deprecated']:
  410. api.set_status(c, new_status)
  411. eq_(new_status, c.status)
  412. eq_(ActionDescription.STATUS_UPDATE, c.revision_type)
  413. def test_create_comment_ok(self):
  414. uapi = UserApi(
  415. session=self.session,
  416. config=self.app_config,
  417. current_user=None,
  418. )
  419. group_api = GroupApi(
  420. current_user=None,
  421. session=self.session
  422. )
  423. groups = [group_api.get_one(Group.TIM_USER),
  424. group_api.get_one(Group.TIM_MANAGER),
  425. group_api.get_one(Group.TIM_ADMIN)]
  426. user = uapi.create_minimal_user(email='this.is@user',
  427. groups=groups, save_now=True)
  428. workspace = WorkspaceApi(
  429. current_user=user,
  430. session=self.session
  431. ).create_workspace(
  432. 'test workspace',
  433. save_now=True
  434. )
  435. api = ContentApi(
  436. current_user=user,
  437. session=self.session,
  438. config=self.app_config,
  439. )
  440. p = api.create(ContentType.Page, workspace, None, 'this_is_a_page')
  441. c = api.create_comment(workspace, p, 'this is the comment', True)
  442. eq_(Content, c.__class__)
  443. eq_(p.content_id, c.parent_id)
  444. eq_(user, c.owner)
  445. eq_(workspace, c.workspace)
  446. eq_(ContentType.Comment, c.type)
  447. eq_('this is the comment', c.description)
  448. eq_('', c.label)
  449. eq_(ActionDescription.COMMENT, c.revision_type)
  450. def test_unit_copy_file_different_label_different_parent_ok(self):
  451. uapi = UserApi(
  452. session=self.session,
  453. config=self.app_config,
  454. current_user=None,
  455. )
  456. group_api = GroupApi(
  457. current_user=None,
  458. session=self.session
  459. )
  460. groups = [group_api.get_one(Group.TIM_USER),
  461. group_api.get_one(Group.TIM_MANAGER),
  462. group_api.get_one(Group.TIM_ADMIN)]
  463. user = uapi.create_minimal_user(
  464. email='user1@user',
  465. groups=groups,
  466. save_now=True
  467. )
  468. user2 = uapi.create_minimal_user(
  469. email='user2@user',
  470. groups=groups,
  471. save_now=True
  472. )
  473. workspace = WorkspaceApi(
  474. current_user=user,
  475. session=self.session
  476. ).create_workspace(
  477. 'test workspace',
  478. save_now=True
  479. )
  480. RoleApi(current_user=user, session=self.session).create_one(
  481. user2,
  482. workspace,
  483. UserRoleInWorkspace.WORKSPACE_MANAGER,
  484. with_notif=False
  485. )
  486. api = ContentApi(
  487. current_user=user,
  488. session=self.session,
  489. config=self.app_config,
  490. )
  491. foldera = api.create(
  492. ContentType.Folder,
  493. workspace,
  494. None,
  495. 'folder a',
  496. True
  497. )
  498. with self.session.no_autoflush:
  499. text_file = api.create(
  500. content_type=ContentType.File,
  501. workspace=workspace,
  502. parent=foldera,
  503. label='test_file',
  504. do_save=False,
  505. )
  506. api.update_file_data(
  507. text_file,
  508. 'test_file',
  509. 'text/plain',
  510. b'test_content'
  511. )
  512. api.save(text_file, ActionDescription.CREATION)
  513. api2 = ContentApi(
  514. current_user=user2,
  515. session=self.session,
  516. config=self.app_config,
  517. )
  518. workspace2 = WorkspaceApi(
  519. current_user=user2,
  520. session=self.session,
  521. ).create_workspace(
  522. 'test workspace2',
  523. save_now=True
  524. )
  525. folderb = api2.create(
  526. ContentType.Folder,
  527. workspace2,
  528. None,
  529. 'folder b',
  530. True
  531. )
  532. api2.copy(
  533. item=text_file,
  534. new_parent=folderb,
  535. new_label='test_file_copy'
  536. )
  537. transaction.commit()
  538. text_file_copy = api2.get_one_by_label_and_parent(
  539. 'test_file_copy',
  540. folderb,
  541. )
  542. assert text_file != text_file_copy
  543. assert text_file_copy.content_id != text_file.content_id
  544. assert text_file_copy.workspace_id == workspace2.workspace_id
  545. assert text_file_copy.depot_file.file.read() == text_file.depot_file.file.read() # nopep8
  546. assert text_file_copy.depot_file.path != text_file.depot_file.path
  547. assert text_file_copy.label == 'test_file_copy'
  548. assert text_file_copy.type == text_file.type
  549. assert text_file_copy.parent.content_id == folderb.content_id
  550. assert text_file_copy.owner.user_id == user.user_id
  551. assert text_file_copy.description == text_file.description
  552. assert text_file_copy.file_extension == text_file.file_extension
  553. assert text_file_copy.file_mimetype == text_file.file_mimetype
  554. assert text_file_copy.revision_type == ActionDescription.COPY
  555. assert len(text_file_copy.revisions) == len(text_file.revisions) + 1
  556. def test_unit_copy_file__same_label_different_parent_ok(self):
  557. uapi = UserApi(
  558. session=self.session,
  559. config=self.app_config,
  560. current_user=None,
  561. )
  562. group_api = GroupApi(
  563. current_user=None,
  564. session=self.session
  565. )
  566. groups = [group_api.get_one(Group.TIM_USER),
  567. group_api.get_one(Group.TIM_MANAGER),
  568. group_api.get_one(Group.TIM_ADMIN)]
  569. user = uapi.create_minimal_user(
  570. email='user1@user',
  571. groups=groups,
  572. save_now=True
  573. )
  574. user2 = uapi.create_minimal_user(
  575. email='user2@user',
  576. groups=groups,
  577. save_now=True
  578. )
  579. workspace = WorkspaceApi(
  580. current_user=user,
  581. session=self.session
  582. ).create_workspace(
  583. 'test workspace',
  584. save_now=True
  585. )
  586. RoleApi(current_user=user, session=self.session).create_one(
  587. user2,
  588. workspace,
  589. UserRoleInWorkspace.WORKSPACE_MANAGER,
  590. with_notif=False
  591. )
  592. api = ContentApi(
  593. current_user=user,
  594. session=self.session,
  595. config=self.app_config,
  596. )
  597. foldera = api.create(
  598. ContentType.Folder,
  599. workspace,
  600. None,
  601. 'folder a',
  602. True
  603. )
  604. with self.session.no_autoflush:
  605. text_file = api.create(
  606. content_type=ContentType.File,
  607. workspace=workspace,
  608. parent=foldera,
  609. label='test_file',
  610. do_save=False,
  611. )
  612. api.update_file_data(
  613. text_file,
  614. 'test_file',
  615. 'text/plain',
  616. b'test_content'
  617. )
  618. api.save(text_file, ActionDescription.CREATION)
  619. api2 = ContentApi(
  620. current_user=user2,
  621. session=self.session,
  622. config=self.app_config,
  623. )
  624. workspace2 = WorkspaceApi(
  625. current_user=user2,
  626. session=self.session
  627. ).create_workspace(
  628. 'test workspace2',
  629. save_now=True
  630. )
  631. folderb = api2.create(
  632. ContentType.Folder,
  633. workspace2,
  634. None,
  635. 'folder b',
  636. True
  637. )
  638. api2.copy(
  639. item=text_file,
  640. new_parent=folderb,
  641. )
  642. transaction.commit()
  643. text_file_copy = api2.get_one_by_label_and_parent(
  644. 'test_file',
  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 == text_file.label
  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_different_label_same_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. )
  671. groups = [group_api.get_one(Group.TIM_USER),
  672. group_api.get_one(Group.TIM_MANAGER),
  673. group_api.get_one(Group.TIM_ADMIN)]
  674. user = uapi.create_minimal_user(
  675. email='user1@user',
  676. groups=groups,
  677. save_now=True,
  678. )
  679. user2 = uapi.create_minimal_user(
  680. email='user2@user',
  681. groups=groups,
  682. save_now=True
  683. )
  684. workspace = WorkspaceApi(
  685. current_user=user,
  686. session=self.session
  687. ).create_workspace(
  688. 'test workspace',
  689. save_now=True
  690. )
  691. RoleApi(current_user=user, session=self.session).create_one(
  692. user2, workspace,
  693. UserRoleInWorkspace.WORKSPACE_MANAGER,
  694. with_notif=False
  695. )
  696. api = ContentApi(
  697. current_user=user,
  698. session=self.session,
  699. config=self.app_config,
  700. )
  701. foldera = api.create(
  702. ContentType.Folder,
  703. workspace,
  704. None,
  705. 'folder a',
  706. True
  707. )
  708. with self.session.no_autoflush:
  709. text_file = api.create(
  710. content_type=ContentType.File,
  711. workspace=workspace,
  712. parent=foldera,
  713. label='test_file',
  714. do_save=False,
  715. )
  716. api.update_file_data(
  717. text_file,
  718. 'test_file',
  719. 'text/plain',
  720. b'test_content'
  721. )
  722. api.save(
  723. text_file,
  724. ActionDescription.CREATION
  725. )
  726. api2 = ContentApi(
  727. current_user=user2,
  728. session=self.session,
  729. config=self.app_config,
  730. )
  731. api2.copy(
  732. item=text_file,
  733. new_label='test_file_copy'
  734. )
  735. transaction.commit()
  736. text_file_copy = api2.get_one_by_label_and_parent(
  737. 'test_file_copy',
  738. foldera,
  739. )
  740. assert text_file != text_file_copy
  741. assert text_file_copy.content_id != text_file.content_id
  742. assert text_file_copy.workspace_id == workspace.workspace_id
  743. assert text_file_copy.depot_file.file.read() == text_file.depot_file.file.read() # nopep8
  744. assert text_file_copy.depot_file.path != text_file.depot_file.path
  745. assert text_file_copy.label == 'test_file_copy'
  746. assert text_file_copy.type == text_file.type
  747. assert text_file_copy.parent.content_id == foldera.content_id
  748. assert text_file_copy.owner.user_id == user.user_id
  749. assert text_file_copy.description == text_file.description
  750. assert text_file_copy.file_extension == text_file.file_extension
  751. assert text_file_copy.file_mimetype == text_file.file_mimetype
  752. assert text_file_copy.revision_type == ActionDescription.COPY
  753. assert len(text_file_copy.revisions) == len(text_file.revisions) + 1
  754. def test_mark_read__workspace(self):
  755. uapi = UserApi(
  756. session=self.session,
  757. config=self.app_config,
  758. current_user=None,
  759. )
  760. group_api = GroupApi(
  761. current_user=None,
  762. session=self.session
  763. )
  764. groups = [group_api.get_one(Group.TIM_USER),
  765. group_api.get_one(Group.TIM_MANAGER),
  766. group_api.get_one(Group.TIM_ADMIN)]
  767. user_a = uapi.create_minimal_user(email='this.is@user',
  768. groups=groups, save_now=True)
  769. user_b = uapi.create_minimal_user(email='this.is@another.user',
  770. groups=groups, save_now=True)
  771. wapi = WorkspaceApi(
  772. current_user=user_a,
  773. session=self.session,
  774. )
  775. workspace1 = wapi.create_workspace(
  776. 'test workspace n°1',
  777. save_now=True)
  778. workspace2 = wapi.create_workspace(
  779. 'test workspace n°2',
  780. save_now=True)
  781. role_api1 = RoleApi(
  782. current_user=user_a,
  783. session=self.session,
  784. )
  785. role_api1.create_one(
  786. user_b,
  787. workspace1,
  788. UserRoleInWorkspace.READER,
  789. False
  790. )
  791. role_api2 = RoleApi(
  792. current_user=user_b,
  793. session=self.session,
  794. )
  795. role_api2.create_one(user_b, workspace2, UserRoleInWorkspace.READER,
  796. False)
  797. cont_api_a = ContentApi(
  798. current_user=user_a,
  799. session=self.session,
  800. config=self.app_config,
  801. )
  802. cont_api_b = ContentApi(
  803. current_user=user_b,
  804. session=self.session,
  805. config=self.app_config,
  806. )
  807. # Creates page_1 & page_2 in workspace 1
  808. # and page_3 & page_4 in workspace 2
  809. page_1 = cont_api_a.create(ContentType.Page, workspace1, None,
  810. 'this is a page', do_save=True)
  811. page_2 = cont_api_a.create(ContentType.Page, workspace1, None,
  812. 'this is page1', do_save=True)
  813. page_3 = cont_api_a.create(ContentType.Thread, workspace2, None,
  814. 'this is page2', do_save=True)
  815. page_4 = cont_api_a.create(ContentType.File, workspace2, None,
  816. 'this is page3', do_save=True)
  817. for rev in page_1.revisions:
  818. eq_(user_b not in rev.read_by.keys(), True)
  819. for rev in page_2.revisions:
  820. eq_(user_b not in rev.read_by.keys(), True)
  821. for rev in page_3.revisions:
  822. eq_(user_b not in rev.read_by.keys(), True)
  823. for rev in page_4.revisions:
  824. eq_(user_b not in rev.read_by.keys(), True)
  825. # Set as read the workspace n°1
  826. cont_api_b.mark_read__workspace(workspace=workspace1)
  827. for rev in page_1.revisions:
  828. eq_(user_b in rev.read_by.keys(), True)
  829. for rev in page_2.revisions:
  830. eq_(user_b in rev.read_by.keys(), True)
  831. for rev in page_3.revisions:
  832. eq_(user_b not in rev.read_by.keys(), True)
  833. for rev in page_4.revisions:
  834. eq_(user_b not in rev.read_by.keys(), True)
  835. # Set as read the workspace n°2
  836. cont_api_b.mark_read__workspace(workspace=workspace2)
  837. for rev in page_1.revisions:
  838. eq_(user_b in rev.read_by.keys(), True)
  839. for rev in page_2.revisions:
  840. eq_(user_b in rev.read_by.keys(), True)
  841. for rev in page_3.revisions:
  842. eq_(user_b in rev.read_by.keys(), True)
  843. for rev in page_4.revisions:
  844. eq_(user_b in rev.read_by.keys(), True)
  845. def test_mark_read(self):
  846. uapi = UserApi(
  847. session=self.session,
  848. config=self.app_config,
  849. current_user=None,
  850. )
  851. group_api = GroupApi(
  852. current_user=None,
  853. session=self.session
  854. )
  855. groups = [group_api.get_one(Group.TIM_USER),
  856. group_api.get_one(Group.TIM_MANAGER),
  857. group_api.get_one(Group.TIM_ADMIN)]
  858. user_a = uapi.create_minimal_user(
  859. email='this.is@user',
  860. groups=groups,
  861. save_now=True
  862. )
  863. user_b = uapi.create_minimal_user(
  864. email='this.is@another.user',
  865. groups=groups,
  866. save_now=True
  867. )
  868. wapi = WorkspaceApi(current_user=user_a, session=self.session)
  869. workspace_api = WorkspaceApi(
  870. current_user=user_a,
  871. session=self.session
  872. )
  873. workspace = wapi.create_workspace(
  874. 'test workspace',
  875. save_now=True)
  876. role_api = RoleApi(
  877. current_user=user_a,
  878. session=self.session,
  879. )
  880. role_api.create_one(
  881. user_b,
  882. workspace,
  883. UserRoleInWorkspace.READER,
  884. False
  885. )
  886. cont_api_a = ContentApi(
  887. current_user=user_a,
  888. session=self.session,
  889. config=self.app_config,
  890. )
  891. cont_api_b = ContentApi(
  892. current_user=user_b,
  893. session=self.session,
  894. config=self.app_config,
  895. )
  896. page_1 = cont_api_a.create(ContentType.Page, workspace, None,
  897. 'this is a page', do_save=True)
  898. for rev in page_1.revisions:
  899. eq_(user_b not in rev.read_by.keys(), True)
  900. cont_api_b.mark_read(page_1)
  901. for rev in page_1.revisions:
  902. eq_(user_b in rev.read_by.keys(), True)
  903. def test_mark_read__all(self):
  904. uapi = UserApi(
  905. session=self.session,
  906. config=self.app_config,
  907. current_user=None,
  908. )
  909. group_api = GroupApi(current_user=None, session=self.session)
  910. groups = [group_api.get_one(Group.TIM_USER),
  911. group_api.get_one(Group.TIM_MANAGER),
  912. group_api.get_one(Group.TIM_ADMIN)]
  913. user_a = uapi.create_minimal_user(
  914. email='this.is@user',
  915. groups=groups,
  916. save_now=True
  917. )
  918. user_b = uapi.create_minimal_user(
  919. email='this.is@another.user',
  920. groups=groups,
  921. save_now=True
  922. )
  923. wapi = WorkspaceApi(
  924. current_user=user_a,
  925. session=self.session,
  926. )
  927. workspace = wapi.create_workspace(
  928. 'test workspace',
  929. save_now=True)
  930. role_api = RoleApi(
  931. current_user=user_a,
  932. session=self.session,
  933. )
  934. role_api.create_one(
  935. user_b,
  936. workspace,
  937. UserRoleInWorkspace.READER,
  938. False
  939. )
  940. cont_api_a = ContentApi(
  941. current_user=user_a,
  942. session=self.session,
  943. config=self.app_config,
  944. )
  945. cont_api_b = ContentApi(
  946. current_user=user_b,
  947. session=self.session,
  948. config=self.app_config,
  949. )
  950. page_2 = cont_api_a.create(
  951. ContentType.Page,
  952. workspace,
  953. None,
  954. 'this is page1',
  955. do_save=True
  956. )
  957. page_3 = cont_api_a.create(
  958. ContentType.Thread,
  959. workspace,
  960. None,
  961. 'this is page2',
  962. do_save=True
  963. )
  964. page_4 = cont_api_a.create(
  965. ContentType.File,
  966. workspace,
  967. None,
  968. 'this is page3',
  969. do_save=True
  970. )
  971. for rev in page_2.revisions:
  972. eq_(user_b not in rev.read_by.keys(), True)
  973. for rev in page_3.revisions:
  974. eq_(user_b not in rev.read_by.keys(), True)
  975. for rev in page_4.revisions:
  976. eq_(user_b not in rev.read_by.keys(), True)
  977. self.session.refresh(page_2)
  978. self.session.refresh(page_3)
  979. self.session.refresh(page_4)
  980. cont_api_b.mark_read__all()
  981. for rev in page_2.revisions:
  982. eq_(user_b in rev.read_by.keys(), True)
  983. for rev in page_3.revisions:
  984. eq_(user_b in rev.read_by.keys(), True)
  985. for rev in page_4.revisions:
  986. eq_(user_b in rev.read_by.keys(), True)
  987. def test_update(self):
  988. uapi = UserApi(
  989. session=self.session,
  990. config=self.app_config,
  991. current_user=None,
  992. )
  993. group_api = GroupApi(current_user=None, session=self.session)
  994. groups = [group_api.get_one(Group.TIM_USER),
  995. group_api.get_one(Group.TIM_MANAGER),
  996. group_api.get_one(Group.TIM_ADMIN)]
  997. user1 = uapi.create_minimal_user(
  998. email='this.is@user',
  999. groups=groups,
  1000. save_now=True
  1001. )
  1002. workspace_api = WorkspaceApi(current_user=user1, session=self.session)
  1003. workspace = workspace_api.create_workspace(
  1004. 'test workspace',
  1005. save_now=True
  1006. )
  1007. wid = workspace.workspace_id
  1008. user2 = uapi.create_minimal_user('this.is@another.user')
  1009. uapi.save(user2)
  1010. RoleApi(
  1011. current_user=user1,
  1012. session=self.session
  1013. ).create_one(
  1014. user2,
  1015. workspace,
  1016. UserRoleInWorkspace.CONTENT_MANAGER,
  1017. with_notif=False,
  1018. flush=True
  1019. )
  1020. # Test starts here
  1021. api = ContentApi(
  1022. current_user=user1,
  1023. session=self.session,
  1024. config=self.app_config,
  1025. )
  1026. p = api.create(ContentType.Page, workspace, None,
  1027. 'this_is_a_page', True)
  1028. u1id = user1.user_id
  1029. u2id = user2.user_id
  1030. pcid = p.content_id
  1031. poid = p.owner_id
  1032. transaction.commit()
  1033. # Refresh instances after commit
  1034. user1 = uapi.get_one(u1id)
  1035. workspace = WorkspaceApi(
  1036. current_user=user1,
  1037. session=self.session
  1038. ).get_one(wid)
  1039. api = ContentApi(
  1040. current_user=user1,
  1041. session=self.session,
  1042. config=self.app_config,
  1043. )
  1044. content = api.get_one(pcid, ContentType.Any, workspace)
  1045. eq_(u1id, content.owner_id)
  1046. eq_(poid, content.owner_id)
  1047. u2 = UserApi(
  1048. session=self.session,
  1049. config=self.app_config,
  1050. current_user=None,
  1051. ).get_one(u2id)
  1052. api2 = ContentApi(
  1053. current_user=u2,
  1054. session=self.session,
  1055. config=self.app_config,
  1056. )
  1057. content2 = api2.get_one(pcid, ContentType.Any, workspace)
  1058. with new_revision(
  1059. session=self.session,
  1060. tm=transaction.manager,
  1061. content=content2,
  1062. ):
  1063. api2.update_content(
  1064. content2,
  1065. 'this is an updated page',
  1066. 'new content'
  1067. )
  1068. api2.save(content2)
  1069. transaction.commit()
  1070. # Refresh instances after commit
  1071. user1 = uapi.get_one(u1id)
  1072. workspace = WorkspaceApi(
  1073. current_user=user1,
  1074. session=self.session,
  1075. ).get_one(wid)
  1076. api = ContentApi(
  1077. current_user=user1,
  1078. session=self.session,
  1079. config=self.app_config,
  1080. )
  1081. updated = api.get_one(pcid, ContentType.Any, workspace)
  1082. eq_(u2id, updated.owner_id,
  1083. 'the owner id should be {} (found {})'.format(u2id,
  1084. updated.owner_id))
  1085. eq_('this is an updated page', updated.label)
  1086. eq_('new content', updated.description)
  1087. eq_(ActionDescription.EDITION, updated.revision_type)
  1088. def test_update_no_change(self):
  1089. uapi = UserApi(
  1090. session=self.session,
  1091. config=self.app_config,
  1092. current_user=None,
  1093. )
  1094. group_api = GroupApi(
  1095. current_user=None,
  1096. session=self.session
  1097. )
  1098. groups = [group_api.get_one(Group.TIM_USER),
  1099. group_api.get_one(Group.TIM_MANAGER),
  1100. group_api.get_one(Group.TIM_ADMIN)]
  1101. user1 = uapi.create_minimal_user(
  1102. email='this.is@user',
  1103. groups=groups,
  1104. save_now=True,
  1105. )
  1106. workspace = WorkspaceApi(
  1107. current_user=user1,
  1108. session=self.session,
  1109. ).create_workspace(
  1110. 'test workspace',
  1111. save_now=True
  1112. )
  1113. user2 = uapi.create_minimal_user('this.is@another.user')
  1114. uapi.save(user2)
  1115. RoleApi(
  1116. current_user=user1,
  1117. session=self.session
  1118. ).create_one(
  1119. user2,
  1120. workspace,
  1121. UserRoleInWorkspace.CONTENT_MANAGER,
  1122. with_notif=False,
  1123. flush=True
  1124. )
  1125. api = ContentApi(
  1126. current_user=user1,
  1127. session=self.session,
  1128. config=self.app_config,
  1129. )
  1130. with self.session.no_autoflush:
  1131. page = api.create(
  1132. content_type=ContentType.Page,
  1133. workspace=workspace,
  1134. label="same_content",
  1135. do_save=False
  1136. )
  1137. page.description = "Same_content_here"
  1138. api.save(page, ActionDescription.CREATION, do_notify=True)
  1139. transaction.commit()
  1140. api2 = ContentApi(
  1141. current_user=user2,
  1142. session=self.session,
  1143. config=self.app_config,
  1144. )
  1145. content2 = api2.get_one(page.content_id, ContentType.Any, workspace)
  1146. with new_revision(
  1147. session=self.session,
  1148. tm=transaction.manager,
  1149. content=content2,
  1150. ):
  1151. with pytest.raises(SameValueError):
  1152. api2.update_content(
  1153. item=content2,
  1154. new_label='same_content',
  1155. new_content='Same_content_here'
  1156. )
  1157. api2.save(content2)
  1158. transaction.commit()
  1159. def test_update_file_data(self):
  1160. uapi = UserApi(
  1161. session=self.session,
  1162. config=self.app_config,
  1163. current_user=None,
  1164. )
  1165. group_api = GroupApi(
  1166. current_user=None,
  1167. session=self.session
  1168. )
  1169. groups = [group_api.get_one(Group.TIM_USER),
  1170. group_api.get_one(Group.TIM_MANAGER),
  1171. group_api.get_one(Group.TIM_ADMIN)]
  1172. user1 = uapi.create_minimal_user(
  1173. email='this.is@user',
  1174. groups=groups,
  1175. save_now=True
  1176. )
  1177. workspace_api = WorkspaceApi(current_user=user1, session=self.session)
  1178. workspace = workspace_api.create_workspace(
  1179. 'test workspace',
  1180. save_now=True
  1181. )
  1182. wid = workspace.workspace_id
  1183. user2 = uapi.create_minimal_user('this.is@another.user')
  1184. uapi.save(user2)
  1185. RoleApi(
  1186. current_user=user1,
  1187. session=self.session,
  1188. ).create_one(
  1189. user2,
  1190. workspace,
  1191. UserRoleInWorkspace.CONTENT_MANAGER,
  1192. with_notif=True,
  1193. flush=True
  1194. )
  1195. # Test starts here
  1196. api = ContentApi(
  1197. current_user=user1,
  1198. session=self.session,
  1199. config=self.app_config,
  1200. )
  1201. p = api.create(ContentType.File, workspace, None,
  1202. 'this_is_a_page', True)
  1203. u1id = user1.user_id
  1204. u2id = user2.user_id
  1205. pcid = p.content_id
  1206. poid = p.owner_id
  1207. api.save(p)
  1208. transaction.commit()
  1209. # Refresh instances after commit
  1210. user1 = uapi.get_one(u1id)
  1211. workspace_api2 = WorkspaceApi(current_user=user1, session=self.session)
  1212. workspace = workspace_api2.get_one(wid)
  1213. api = ContentApi(
  1214. current_user=user1,
  1215. session=self.session,
  1216. config=self.app_config,
  1217. )
  1218. content = api.get_one(pcid, ContentType.Any, workspace)
  1219. eq_(u1id, content.owner_id)
  1220. eq_(poid, content.owner_id)
  1221. u2 = UserApi(
  1222. current_user=None,
  1223. session=self.session,
  1224. config=self.app_config,
  1225. ).get_one(u2id)
  1226. api2 = ContentApi(
  1227. current_user=u2,
  1228. session=self.session,
  1229. config=self.app_config,
  1230. )
  1231. content2 = api2.get_one(pcid, ContentType.Any, workspace)
  1232. with new_revision(
  1233. session=self.session,
  1234. tm=transaction.manager,
  1235. content=content2,
  1236. ):
  1237. api2.update_file_data(
  1238. content2,
  1239. 'index.html',
  1240. 'text/html',
  1241. b'<html>hello world</html>'
  1242. )
  1243. api2.save(content2)
  1244. transaction.commit()
  1245. # Refresh instances after commit
  1246. user1 = uapi.get_one(u1id)
  1247. workspace = WorkspaceApi(
  1248. current_user=user1,
  1249. session=self.session,
  1250. ).get_one(wid)
  1251. updated = api.get_one(pcid, ContentType.Any, workspace)
  1252. eq_(u2id, updated.owner_id,
  1253. 'the owner id should be {} (found {})'.format(u2id,
  1254. updated.owner_id))
  1255. eq_('this_is_a_page.html', updated.file_name)
  1256. eq_('text/html', updated.file_mimetype)
  1257. eq_(b'<html>hello world</html>', updated.depot_file.file.read())
  1258. eq_(ActionDescription.REVISION, updated.revision_type)
  1259. def test_update_no_change(self):
  1260. uapi = UserApi(
  1261. session=self.session,
  1262. config=self.app_config,
  1263. current_user=None,
  1264. )
  1265. group_api = GroupApi(
  1266. current_user=None,
  1267. session=self.session,
  1268. )
  1269. groups = [group_api.get_one(Group.TIM_USER),
  1270. group_api.get_one(Group.TIM_MANAGER),
  1271. group_api.get_one(Group.TIM_ADMIN)]
  1272. user1 = uapi.create_minimal_user(
  1273. email='this.is@user',
  1274. groups=groups,
  1275. save_now=True,
  1276. )
  1277. workspace_api = WorkspaceApi(current_user=user1, session=self.session)
  1278. workspace = workspace_api.create_workspace(
  1279. 'test workspace',
  1280. save_now=True
  1281. )
  1282. user2 = uapi.create_minimal_user('this.is@another.user')
  1283. uapi.save(user2)
  1284. RoleApi(
  1285. current_user=user1,
  1286. session=self.session,
  1287. ).create_one(
  1288. user2,
  1289. workspace,
  1290. UserRoleInWorkspace.CONTENT_MANAGER,
  1291. with_notif=False,
  1292. flush=True
  1293. )
  1294. api = ContentApi(
  1295. current_user=user1,
  1296. session=self.session,
  1297. config=self.app_config,
  1298. )
  1299. with self.session.no_autoflush:
  1300. page = api.create(
  1301. content_type=ContentType.Page,
  1302. workspace=workspace,
  1303. label="same_content",
  1304. do_save=False
  1305. )
  1306. api.update_file_data(
  1307. page,
  1308. 'index.html',
  1309. 'text/html',
  1310. b'<html>Same Content Here</html>'
  1311. )
  1312. api.save(page, ActionDescription.CREATION, do_notify=True)
  1313. transaction.commit()
  1314. api2 = ContentApi(
  1315. current_user=user2,
  1316. session=self.session,
  1317. config=self.app_config,
  1318. )
  1319. content2 = api2.get_one(page.content_id, ContentType.Any, workspace)
  1320. with new_revision(
  1321. session=self.session,
  1322. tm=transaction.manager,
  1323. content=content2,
  1324. ):
  1325. with pytest.raises(SameValueError):
  1326. api2.update_file_data(
  1327. page,
  1328. 'index.html',
  1329. 'text/html',
  1330. b'<html>Same Content Here</html>'
  1331. )
  1332. api2.save(content2)
  1333. transaction.commit()
  1334. def test_archive_unarchive(self):
  1335. uapi = UserApi(
  1336. session=self.session,
  1337. config=self.app_config,
  1338. current_user=None,
  1339. )
  1340. group_api = GroupApi(current_user=None, session=self.session)
  1341. groups = [group_api.get_one(Group.TIM_USER),
  1342. group_api.get_one(Group.TIM_MANAGER),
  1343. group_api.get_one(Group.TIM_ADMIN)]
  1344. user1 = uapi.create_minimal_user(
  1345. email='this.is@user',
  1346. groups=groups,
  1347. save_now=True
  1348. )
  1349. u1id = user1.user_id
  1350. workspace_api = WorkspaceApi(current_user=user1, session=self.session)
  1351. workspace = workspace_api.create_workspace(
  1352. 'test workspace',
  1353. save_now=True
  1354. )
  1355. wid = workspace.workspace_id
  1356. user2 = uapi.create_minimal_user('this.is@another.user')
  1357. uapi.save(user2)
  1358. RoleApi(
  1359. current_user=user1,
  1360. session=self.session
  1361. ).create_one(
  1362. user2,
  1363. workspace,
  1364. UserRoleInWorkspace.CONTENT_MANAGER,
  1365. with_notif=True,
  1366. flush=True
  1367. )
  1368. # show archived is used at the top end of the test
  1369. api = ContentApi(
  1370. current_user=user1,
  1371. session=self.session,
  1372. show_archived=True,
  1373. config=self.app_config,
  1374. )
  1375. p = api.create(ContentType.File, workspace, None,
  1376. 'this_is_a_page', True)
  1377. u1id = user1.user_id
  1378. u2id = user2.user_id
  1379. pcid = p.content_id
  1380. poid = p.owner_id
  1381. transaction.commit()
  1382. ####
  1383. # refresh after commit
  1384. user1 = UserApi(
  1385. current_user=None,
  1386. config=self.app_config,
  1387. session=self.session
  1388. ).get_one(u1id)
  1389. workspace = WorkspaceApi(
  1390. current_user=user1,
  1391. session=self.session
  1392. ).get_one(wid)
  1393. content = api.get_one(pcid, ContentType.Any, workspace)
  1394. eq_(u1id, content.owner_id)
  1395. eq_(poid, content.owner_id)
  1396. u2api = UserApi(
  1397. session=self.session,
  1398. config=self.app_config,
  1399. current_user=None,
  1400. )
  1401. u2 = u2api.get_one(u2id)
  1402. api2 = ContentApi(
  1403. current_user=u2,
  1404. session=self.session,
  1405. config=self.app_config,
  1406. show_archived=True,
  1407. )
  1408. content2 = api2.get_one(pcid, ContentType.Any, workspace)
  1409. with new_revision(
  1410. session=self.session,
  1411. tm=transaction.manager,
  1412. content=content2,
  1413. ):
  1414. api2.archive(content2)
  1415. api2.save(content2)
  1416. transaction.commit()
  1417. # refresh after commit
  1418. user1 = UserApi(
  1419. current_user=None,
  1420. session=self.session,
  1421. config=self.app_config,
  1422. ).get_one(u1id)
  1423. workspace = WorkspaceApi(
  1424. current_user=user1,
  1425. session=self.session,
  1426. ).get_one(wid)
  1427. u2 = UserApi(
  1428. current_user=None,
  1429. session=self.session,
  1430. config=self.app_config,
  1431. ).get_one(u2id)
  1432. api = ContentApi(
  1433. current_user=user1,
  1434. session=self.session,
  1435. config=self.app_config,
  1436. show_archived=True,
  1437. )
  1438. api2 = ContentApi(
  1439. current_user=u2,
  1440. session=self.session,
  1441. config=self.app_config,
  1442. show_archived=True,
  1443. )
  1444. updated = api2.get_one(pcid, ContentType.Any, workspace)
  1445. eq_(u2id, updated.owner_id,
  1446. 'the owner id should be {} (found {})'.format(u2id,
  1447. updated.owner_id))
  1448. eq_(True, updated.is_archived)
  1449. eq_(ActionDescription.ARCHIVING, updated.revision_type)
  1450. ####
  1451. updated2 = api.get_one(pcid, ContentType.Any, workspace)
  1452. with new_revision(
  1453. session=self.session,
  1454. tm=transaction.manager,
  1455. content=updated,
  1456. ):
  1457. api.unarchive(updated)
  1458. api.save(updated2)
  1459. eq_(False, updated2.is_archived)
  1460. eq_(ActionDescription.UNARCHIVING, updated2.revision_type)
  1461. eq_(u1id, updated2.owner_id)
  1462. def test_delete_undelete(self):
  1463. uapi = UserApi(
  1464. session=self.session,
  1465. config=self.app_config,
  1466. current_user=None,
  1467. )
  1468. group_api = GroupApi(
  1469. current_user=None,
  1470. session=self.session
  1471. )
  1472. groups = [group_api.get_one(Group.TIM_USER),
  1473. group_api.get_one(Group.TIM_MANAGER),
  1474. group_api.get_one(Group.TIM_ADMIN)]
  1475. user1 = uapi.create_minimal_user(
  1476. email='this.is@user',
  1477. groups=groups,
  1478. save_now=True
  1479. )
  1480. u1id = user1.user_id
  1481. workspace_api = WorkspaceApi(current_user=user1, session=self.session)
  1482. workspace = workspace_api.create_workspace(
  1483. 'test workspace',
  1484. save_now=True
  1485. )
  1486. wid = workspace.workspace_id
  1487. user2 = uapi.create_minimal_user('this.is@another.user')
  1488. uapi.save(user2)
  1489. RoleApi(
  1490. current_user=user1,
  1491. session=self.session
  1492. ).create_one(
  1493. user2,
  1494. workspace,
  1495. UserRoleInWorkspace.CONTENT_MANAGER,
  1496. with_notif=True,
  1497. flush=True
  1498. )
  1499. # show archived is used at the top end of the test
  1500. api = ContentApi(
  1501. current_user=user1,
  1502. session=self.session,
  1503. config=self.app_config,
  1504. show_deleted=True,
  1505. )
  1506. p = api.create(ContentType.File, workspace, None,
  1507. 'this_is_a_page', True)
  1508. u1id = user1.user_id
  1509. u2id = user2.user_id
  1510. pcid = p.content_id
  1511. poid = p.owner_id
  1512. transaction.commit()
  1513. ####
  1514. user1 = UserApi(
  1515. current_user=None,
  1516. session=self.session,
  1517. config=self.app_config,
  1518. ).get_one(u1id)
  1519. workspace = WorkspaceApi(
  1520. current_user=user1,
  1521. session=self.session,
  1522. ).get_one(wid)
  1523. content = api.get_one(pcid, ContentType.Any, workspace)
  1524. eq_(u1id, content.owner_id)
  1525. eq_(poid, content.owner_id)
  1526. u2 = UserApi(
  1527. current_user=None,
  1528. session=self.session,
  1529. config=self.app_config,
  1530. ).get_one(u2id)
  1531. api2 = ContentApi(
  1532. current_user=u2,
  1533. session=self.session,
  1534. config=self.app_config,
  1535. show_deleted=True,
  1536. )
  1537. content2 = api2.get_one(pcid, ContentType.Any, workspace)
  1538. with new_revision(
  1539. session=self.session,
  1540. tm=transaction.manager,
  1541. content=content2,
  1542. ):
  1543. api2.delete(content2)
  1544. api2.save(content2)
  1545. transaction.commit()
  1546. ####
  1547. user1 = UserApi(
  1548. current_user=None,
  1549. session=self.session,
  1550. config=self.app_config,
  1551. ).get_one(u1id)
  1552. workspace = WorkspaceApi(
  1553. current_user=user1,
  1554. session=self.session,
  1555. ).get_one(wid)
  1556. # show archived is used at the top end of the test
  1557. api = ContentApi(
  1558. current_user=user1,
  1559. session=self.session,
  1560. config=self.app_config,
  1561. show_deleted=True,
  1562. )
  1563. u2 = UserApi(
  1564. current_user=None,
  1565. session=self.session,
  1566. config=self.app_config,
  1567. ).get_one(u2id)
  1568. api2 = ContentApi(
  1569. current_user=u2,
  1570. session=self.session,
  1571. config=self.app_config,
  1572. show_deleted=True
  1573. )
  1574. updated = api2.get_one(pcid, ContentType.Any, workspace)
  1575. eq_(u2id, updated.owner_id,
  1576. 'the owner id should be {} (found {})'.format(u2id,
  1577. updated.owner_id))
  1578. eq_(True, updated.is_deleted)
  1579. eq_(ActionDescription.DELETION, updated.revision_type)
  1580. ####
  1581. updated2 = api.get_one(pcid, ContentType.Any, workspace)
  1582. with new_revision(
  1583. tm=transaction.manager,
  1584. session=self.session,
  1585. content=updated2,
  1586. ):
  1587. api.undelete(updated2)
  1588. api.save(updated2)
  1589. eq_(False, updated2.is_deleted)
  1590. eq_(ActionDescription.UNDELETION, updated2.revision_type)
  1591. eq_(u1id, updated2.owner_id)
  1592. def test_search_in_label(self):
  1593. # HACK - D.A. - 2015-03-09
  1594. # This test is based on a bug which does NOT return results found
  1595. # at root of a workspace (eg a folder)
  1596. uapi = UserApi(
  1597. session=self.session,
  1598. config=self.app_config,
  1599. current_user=None,
  1600. )
  1601. group_api = GroupApi(
  1602. current_user=None,
  1603. session=self.session,
  1604. )
  1605. groups = [group_api.get_one(Group.TIM_USER),
  1606. group_api.get_one(Group.TIM_MANAGER),
  1607. group_api.get_one(Group.TIM_ADMIN)]
  1608. user = uapi.create_minimal_user(email='this.is@user',
  1609. groups=groups, save_now=True)
  1610. workspace = WorkspaceApi(
  1611. current_user=user,
  1612. session=self.session
  1613. ).create_workspace(
  1614. 'test workspace',
  1615. save_now=True
  1616. )
  1617. api = ContentApi(
  1618. current_user=user,
  1619. session=self.session,
  1620. config=self.app_config,
  1621. )
  1622. a = api.create(ContentType.Folder, workspace, None,
  1623. 'this is randomized folder', True)
  1624. p = api.create(ContentType.Page, workspace, a,
  1625. 'this is randomized label content', True)
  1626. with new_revision(
  1627. session=self.session,
  1628. tm=transaction.manager,
  1629. content=p,
  1630. ):
  1631. p.description = 'This is some amazing test'
  1632. api.save(p)
  1633. original_id = p.content_id
  1634. res = api.search(['randomized'])
  1635. eq_(1, len(res.all()))
  1636. item = res.all()[0]
  1637. eq_(original_id, item.content_id)
  1638. def test_search_in_description(self):
  1639. # HACK - D.A. - 2015-03-09
  1640. # This test is based on a bug which does NOT return results found
  1641. # at root of a workspace (eg a folder)
  1642. uapi = UserApi(
  1643. session=self.session,
  1644. config=self.app_config,
  1645. current_user=None,
  1646. )
  1647. group_api = GroupApi(
  1648. current_user=None,
  1649. session=self.session,
  1650. )
  1651. groups = [group_api.get_one(Group.TIM_USER),
  1652. group_api.get_one(Group.TIM_MANAGER),
  1653. group_api.get_one(Group.TIM_ADMIN)]
  1654. user = uapi.create_minimal_user(email='this.is@user',
  1655. groups=groups, save_now=True)
  1656. workspace = WorkspaceApi(
  1657. current_user=user,
  1658. session=self.session
  1659. ).create_workspace(
  1660. 'test workspace',
  1661. save_now=True,
  1662. )
  1663. api = ContentApi(
  1664. current_user=user,
  1665. session=self.session,
  1666. config=self.app_config,
  1667. )
  1668. a = api.create(ContentType.Folder, workspace, None,
  1669. 'this is randomized folder', True)
  1670. p = api.create(ContentType.Page, workspace, a,
  1671. 'this is dummy label content', True)
  1672. with new_revision(
  1673. tm=transaction.manager,
  1674. session=self.session,
  1675. content=p,
  1676. ):
  1677. p.description = 'This is some amazing test'
  1678. api.save(p)
  1679. original_id = p.content_id
  1680. res = api.search(['dummy'])
  1681. eq_(1, len(res.all()))
  1682. item = res.all()[0]
  1683. eq_(original_id, item.content_id)
  1684. def test_search_in_label_or_description(self):
  1685. # HACK - D.A. - 2015-03-09
  1686. # This test is based on a bug which does NOT return results found
  1687. # at root of a workspace (eg a folder)
  1688. uapi = UserApi(
  1689. session=self.session,
  1690. config=self.app_config,
  1691. current_user=None,
  1692. )
  1693. group_api = GroupApi(current_user=None, session=self.session)
  1694. groups = [group_api.get_one(Group.TIM_USER),
  1695. group_api.get_one(Group.TIM_MANAGER),
  1696. group_api.get_one(Group.TIM_ADMIN)]
  1697. user = uapi.create_minimal_user(email='this.is@user',
  1698. groups=groups, save_now=True)
  1699. workspace = WorkspaceApi(
  1700. current_user=user,
  1701. session=self.session
  1702. ).create_workspace('test workspace', save_now=True)
  1703. api = ContentApi(
  1704. current_user=user,
  1705. session=self.session,
  1706. config=self.app_config,
  1707. )
  1708. a = api.create(ContentType.Folder, workspace, None,
  1709. 'this is randomized folder', True)
  1710. p1 = api.create(ContentType.Page, workspace, a,
  1711. 'this is dummy label content', True)
  1712. p2 = api.create(ContentType.Page, workspace, a, 'Hey ! Jon !', True)
  1713. with new_revision(
  1714. session=self.session,
  1715. tm=transaction.manager,
  1716. content=p1,
  1717. ):
  1718. p1.description = 'This is some amazing test'
  1719. with new_revision(
  1720. session=self.session,
  1721. tm=transaction.manager,
  1722. content=p2,
  1723. ):
  1724. p2.description = 'What\'s up ?'
  1725. api.save(p1)
  1726. api.save(p2)
  1727. id1 = p1.content_id
  1728. id2 = p2.content_id
  1729. eq_(1, self.session.query(Workspace).filter(Workspace.label == 'test workspace').count())
  1730. eq_(1, self.session.query(ContentRevisionRO).filter(ContentRevisionRO.label == 'this is randomized folder').count())
  1731. eq_(2, self.session.query(ContentRevisionRO).filter(ContentRevisionRO.label == 'this is dummy label content').count())
  1732. eq_(1, self.session.query(ContentRevisionRO).filter(ContentRevisionRO.description == 'This is some amazing test').count())
  1733. eq_(2, self.session.query(ContentRevisionRO).filter(ContentRevisionRO.label == 'Hey ! Jon !').count())
  1734. eq_(1, self.session.query(ContentRevisionRO).filter(ContentRevisionRO.description == 'What\'s up ?').count())
  1735. res = api.search(['dummy', 'jon'])
  1736. eq_(2, len(res.all()))
  1737. eq_(True, id1 in [o.content_id for o in res.all()])
  1738. eq_(True, id2 in [o.content_id for o in res.all()])
  1739. def test_unit__search_exclude_content_under_deleted_or_archived_parents__ok(self): # nopep8
  1740. admin = self.session.query(User)\
  1741. .filter(User.email == 'admin@admin.admin').one()
  1742. workspace = self._create_workspace_and_test(
  1743. 'workspace_1',
  1744. admin
  1745. )
  1746. folder_1 = self._create_content_and_test(
  1747. 'folder_1',
  1748. workspace=workspace,
  1749. type=ContentType.Folder
  1750. )
  1751. folder_2 = self._create_content_and_test(
  1752. 'folder_2',
  1753. workspace=workspace,
  1754. type=ContentType.Folder
  1755. )
  1756. page_1 = self._create_content_and_test(
  1757. 'foo', workspace=workspace,
  1758. type=ContentType.Page,
  1759. parent=folder_1
  1760. )
  1761. page_2 = self._create_content_and_test(
  1762. 'bar',
  1763. workspace=workspace,
  1764. type=ContentType.Page,
  1765. parent=folder_2
  1766. )
  1767. api = ContentApi(
  1768. current_user=admin,
  1769. session=self.session,
  1770. config=self.app_config,
  1771. )
  1772. foo_result = api.search(['foo']).all()
  1773. eq_(1, len(foo_result))
  1774. assert page_1 in foo_result
  1775. bar_result = api.search(['bar']).all()
  1776. eq_(1, len(bar_result))
  1777. assert page_2 in bar_result
  1778. with new_revision(
  1779. session=self.session,
  1780. tm=transaction.manager,
  1781. content=folder_1,
  1782. ):
  1783. api.delete(folder_1)
  1784. with new_revision(
  1785. session=self.session,
  1786. tm=transaction.manager,
  1787. content=folder_2,
  1788. ):
  1789. api.archive(folder_2)
  1790. # Actually ContentApi.search don't filter it
  1791. foo_result = api.search(['foo']).all()
  1792. eq_(1, len(foo_result))
  1793. assert page_1 in foo_result
  1794. bar_result = api.search(['bar']).all()
  1795. eq_(1, len(bar_result))
  1796. assert page_2 in bar_result
  1797. # ContentApi offer exclude_unavailable method to do it
  1798. foo_result = api.search(['foo']).all()
  1799. api.exclude_unavailable(foo_result)
  1800. eq_(0, len(foo_result))
  1801. bar_result = api.search(['bar']).all()
  1802. api.exclude_unavailable(bar_result)
  1803. eq_(0, len(bar_result))
  1804. class TestContentApiSecurity(DefaultTest):
  1805. fixtures = [FixtureTest, ]
  1806. def test_unit__cant_get_non_access_content__ok__nominal_case(self):
  1807. admin = self.session.query(User)\
  1808. .filter(User.email == 'admin@admin.admin').one()
  1809. bob = self.session.query(User)\
  1810. .filter(User.email == 'bob@fsf.local').one()
  1811. bob_workspace = WorkspaceApi(
  1812. current_user=bob,
  1813. session=self.session,
  1814. ).create_workspace(
  1815. 'bob_workspace',
  1816. save_now=True,
  1817. )
  1818. admin_workspace = WorkspaceApi(
  1819. current_user=admin,
  1820. session=self.session,
  1821. ).create_workspace(
  1822. 'admin_workspace',
  1823. save_now=True,
  1824. )
  1825. bob_page = ContentApi(
  1826. current_user=bob,
  1827. session=self.session,
  1828. config=self.app_config,
  1829. ).create(
  1830. content_type=ContentType.Page,
  1831. workspace=bob_workspace,
  1832. label='bob_page',
  1833. do_save=True,
  1834. )
  1835. admin_page = ContentApi(
  1836. current_user=admin,
  1837. session=self.session,
  1838. config=self.app_config,
  1839. ).create(
  1840. content_type=ContentType.Page,
  1841. workspace=admin_workspace,
  1842. label='admin_page',
  1843. do_save=True,
  1844. )
  1845. bob_viewable = ContentApi(
  1846. current_user=bob,
  1847. session=self.session,
  1848. config=self.app_config,
  1849. ).get_all()
  1850. eq_(1, len(bob_viewable), 'Bob should view only one content')
  1851. eq_(
  1852. 'bob_page',
  1853. bob_viewable[0].label,
  1854. 'Bob should not view "{0}" content'.format(
  1855. bob_viewable[0].label,
  1856. )
  1857. )