test_pagure_lib_gitolite_config.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2017 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import datetime
  9. import os
  10. import shutil
  11. import sys
  12. import tempfile
  13. import time
  14. import unittest
  15. from io import open
  16. import pygit2
  17. from mock import patch, MagicMock
  18. sys.path.insert(0, os.path.join(os.path.dirname(
  19. os.path.abspath(__file__)), '..'))
  20. import pagure.lib.git
  21. import pagure.lib.query
  22. import tests
  23. from pagure.lib.repo import PagureRepo
  24. CORE_CONFIG = """repo test
  25. R = @all
  26. RW+ = pingou
  27. repo docs/test
  28. R = @all
  29. RW+ = pingou
  30. repo tickets/test
  31. RW+ = pingou
  32. repo requests/test
  33. RW+ = pingou
  34. repo test2
  35. R = @all
  36. RW+ = pingou
  37. repo docs/test2
  38. R = @all
  39. RW+ = pingou
  40. repo tickets/test2
  41. RW+ = pingou
  42. repo requests/test2
  43. RW+ = pingou
  44. repo somenamespace/test3
  45. R = @all
  46. RW+ = pingou
  47. repo docs/somenamespace/test3
  48. R = @all
  49. RW+ = pingou
  50. repo tickets/somenamespace/test3
  51. RW+ = pingou
  52. repo requests/somenamespace/test3
  53. RW+ = pingou"""
  54. class PagureLibGitoliteConfigtests(tests.Modeltests):
  55. """ Tests for pagure.lib.git """
  56. maxDiff = None
  57. def setUp(self):
  58. """ Set up the environnment, ran before every tests. """
  59. super(PagureLibGitoliteConfigtests, self).setUp()
  60. tests.create_projects(self.session)
  61. self.outputconf = os.path.join(self.path, 'test_gitolite.conf')
  62. self.preconf = os.path.join(self.path, 'header_gitolite')
  63. with open(self.preconf, 'w') as stream:
  64. stream.write('# this is a header that is manually added\n')
  65. stream.write('\n')
  66. stream.write('@group1 = foo bar baz\n')
  67. stream.write('@group2 = threebean puiterwijk kevin pingou\n')
  68. self.postconf = os.path.join(self.path, 'footer_gitolite')
  69. with open(self.postconf, 'w', encoding="utf-8") as stream:
  70. stream.write('# end of generated configuration\n')
  71. stream.write(r'# \ó/')
  72. stream.write('\n# end of footer\n')
  73. def tearDown(self):
  74. """ Tearn down the environnment, ran before every tests. """
  75. super(PagureLibGitoliteConfigtests, self).tearDown()
  76. if os.path.exists(self.outputconf):
  77. os.unlink(self.outputconf)
  78. self.assertFalse(os.path.exists(self.outputconf))
  79. def test_write_gitolite_pre_post_projectNone(self):
  80. """ Test the write_gitolite_acls function of pagure.lib.git with
  81. a postconf set """
  82. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  83. helper.write_gitolite_acls(
  84. self.session,
  85. self.outputconf,
  86. project=None,
  87. preconf=self.preconf,
  88. postconf=self.postconf
  89. )
  90. self.assertTrue(os.path.exists(self.outputconf))
  91. with open(self.outputconf, 'r') as stream:
  92. data = stream.read()
  93. exp = r"""# this is a header that is manually added
  94. @group1 = foo bar baz
  95. @group2 = threebean puiterwijk kevin pingou
  96. # end of header
  97. %s
  98. # end of body
  99. # end of generated configuration
  100. # \ó/
  101. # end of footer
  102. """ % CORE_CONFIG
  103. #print data
  104. self.assertEqual(data, exp)
  105. def test_write_gitolite_pre_post_projectNone_to_existing_file(self):
  106. """ Test the write_gitolite_acls function of pagure.lib.git with
  107. a postconf set with existing output file """
  108. with open(self.outputconf, 'w') as stream:
  109. pass
  110. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  111. helper.write_gitolite_acls(
  112. self.session,
  113. self.outputconf,
  114. project=None,
  115. preconf=self.preconf,
  116. postconf=self.postconf
  117. )
  118. self.assertTrue(os.path.exists(self.outputconf))
  119. with open(self.outputconf) as stream:
  120. data = stream.read()
  121. self.assertEqual(data, '')
  122. def test_write_gitolite_pre_post_project_1(self):
  123. """ Test the write_gitolite_acls function of pagure.lib.git with
  124. a postconf set """
  125. with open(self.outputconf, 'w') as stream:
  126. pass
  127. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  128. helper.write_gitolite_acls(
  129. self.session,
  130. self.outputconf,
  131. project=-1,
  132. preconf=self.preconf,
  133. postconf=self.postconf
  134. )
  135. self.assertTrue(os.path.exists(self.outputconf))
  136. with open(self.outputconf) as stream:
  137. data = stream.read()
  138. exp = r"""# this is a header that is manually added
  139. @group1 = foo bar baz
  140. @group2 = threebean puiterwijk kevin pingou
  141. # end of header
  142. %s
  143. # end of body
  144. # end of generated configuration
  145. # \ó/
  146. # end of footer
  147. """ % CORE_CONFIG
  148. #print data
  149. self.assertEqual(data, exp)
  150. def test_write_gitolite_pre_post_project_test(self):
  151. """ Test the write_gitolite_acls function of pagure.lib.git with
  152. a postconf set """
  153. with open(self.outputconf, 'w') as stream:
  154. pass
  155. project = pagure.lib.query._get_project(self.session, 'test')
  156. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  157. helper.write_gitolite_acls(
  158. self.session,
  159. self.outputconf,
  160. project=project,
  161. preconf=self.preconf,
  162. postconf=self.postconf
  163. )
  164. self.assertTrue(os.path.exists(self.outputconf))
  165. with open(self.outputconf) as stream:
  166. data = stream.read()
  167. exp = r"""# this is a header that is manually added
  168. @group1 = foo bar baz
  169. @group2 = threebean puiterwijk kevin pingou
  170. # end of header
  171. repo test
  172. R = @all
  173. RW+ = pingou
  174. repo docs/test
  175. R = @all
  176. RW+ = pingou
  177. repo tickets/test
  178. RW+ = pingou
  179. repo requests/test
  180. RW+ = pingou
  181. # end of body
  182. # end of generated configuration
  183. # \ó/
  184. # end of footer
  185. """
  186. #print data
  187. self.assertEqual(data, exp)
  188. def test_write_gitolite_pre_post_project_test_full_file(self):
  189. """ Test the write_gitolite_acls function of pagure.lib.git with
  190. a postconf set """
  191. # Re-generate the gitolite config for all the projects
  192. self.test_write_gitolite_pre_post_project_1()
  193. self.assertTrue(os.path.exists(self.outputconf))
  194. project = pagure.lib.query._get_project(self.session, 'test')
  195. project.user_id = 2
  196. self.session.add(project)
  197. self.session.commit()
  198. project = pagure.lib.query._get_project(self.session, 'test')
  199. msg = pagure.lib.query.add_user_to_project(
  200. self.session,
  201. project=project,
  202. new_user='pingou',
  203. user='foo',
  204. access='commit'
  205. )
  206. self.assertEqual(msg, 'User added')
  207. self.session.commit()
  208. project = pagure.lib.query._get_project(self.session, 'test')
  209. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  210. helper.write_gitolite_acls(
  211. self.session,
  212. self.outputconf,
  213. project=project,
  214. preconf=self.preconf,
  215. postconf=self.postconf
  216. )
  217. self.assertTrue(os.path.exists(self.outputconf))
  218. with open(self.outputconf) as stream:
  219. data = stream.read()
  220. exp = r"""# this is a header that is manually added
  221. @group1 = foo bar baz
  222. @group2 = threebean puiterwijk kevin pingou
  223. # end of header
  224. repo test2
  225. R = @all
  226. RW+ = pingou
  227. repo docs/test2
  228. R = @all
  229. RW+ = pingou
  230. repo tickets/test2
  231. RW+ = pingou
  232. repo requests/test2
  233. RW+ = pingou
  234. repo somenamespace/test3
  235. R = @all
  236. RW+ = pingou
  237. repo docs/somenamespace/test3
  238. R = @all
  239. RW+ = pingou
  240. repo tickets/somenamespace/test3
  241. RW+ = pingou
  242. repo requests/somenamespace/test3
  243. RW+ = pingou
  244. repo test
  245. R = @all
  246. RW+ = foo
  247. RW+ = pingou
  248. repo docs/test
  249. R = @all
  250. RW+ = foo
  251. RW+ = pingou
  252. repo tickets/test
  253. RW+ = foo
  254. RW+ = pingou
  255. repo requests/test
  256. RW+ = foo
  257. RW+ = pingou
  258. # end of body
  259. # end of generated configuration
  260. # \ó/
  261. # end of footer
  262. """
  263. #print data
  264. self.assertEqual(data, exp)
  265. @patch.dict('pagure.config.config',
  266. {'ENABLE_DOCS': False, 'ENABLE_TICKETS': False})
  267. def test_write_gitolite_disabled_docs_tickets(self):
  268. """ Test the write_gitolite_acls function when docs and tickets
  269. are disabled """
  270. # Re-generate the gitolite config for all the projects
  271. project = pagure.lib.query._get_project(self.session, 'test')
  272. project.user_id = 2
  273. self.session.add(project)
  274. self.session.commit()
  275. project = pagure.lib.query._get_project(self.session, 'test')
  276. msg = pagure.lib.query.add_user_to_project(
  277. self.session,
  278. project=project,
  279. new_user='pingou',
  280. user='foo',
  281. access='commit'
  282. )
  283. self.assertEqual(msg, 'User added')
  284. self.session.commit()
  285. project = pagure.lib.query._get_project(self.session, 'test')
  286. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  287. helper.write_gitolite_acls(
  288. self.session,
  289. self.outputconf,
  290. project=project,
  291. )
  292. self.assertTrue(os.path.exists(self.outputconf))
  293. with open(self.outputconf) as stream:
  294. data = stream.read()
  295. exp = """repo test
  296. R = @all
  297. RW+ = foo
  298. RW+ = pingou
  299. repo requests/test
  300. RW+ = foo
  301. RW+ = pingou
  302. repo test2
  303. R = @all
  304. RW+ = pingou
  305. repo requests/test2
  306. RW+ = pingou
  307. repo somenamespace/test3
  308. R = @all
  309. RW+ = pingou
  310. repo requests/somenamespace/test3
  311. RW+ = pingou
  312. # end of body
  313. """
  314. self.assertEqual(data, exp)
  315. class PagureLibGitoliteGroupConfigtests(tests.Modeltests):
  316. """ Tests for generating the gitolite configuration file for a group
  317. change
  318. """
  319. maxDiff = None
  320. def setUp(self):
  321. """ Set up the environnment, ran before every tests. """
  322. super(PagureLibGitoliteGroupConfigtests, self).setUp()
  323. tests.create_projects(self.session)
  324. pagure.lib.query.add_group(
  325. self.session,
  326. group_name='grp',
  327. display_name='grp group',
  328. description=None,
  329. group_type='user',
  330. user='pingou',
  331. is_admin=False,
  332. blacklist=[],
  333. )
  334. pagure.lib.query.add_group(
  335. self.session,
  336. group_name='grp2',
  337. display_name='grp2 group',
  338. description=None,
  339. group_type='user',
  340. user='foo',
  341. is_admin=False,
  342. blacklist=[],
  343. )
  344. self.session.commit()
  345. self.outputconf = os.path.join(self.path, 'test_gitolite.conf')
  346. self.preconf = os.path.join(self.path, 'header_gitolite')
  347. with open(self.preconf, 'w') as stream:
  348. stream.write('# this is a header that is manually added\n')
  349. stream.write('\n')
  350. stream.write('@group1 = foo bar baz\n')
  351. stream.write('@group2 = threebean puiterwijk kevin pingou\n')
  352. self.postconf = os.path.join(self.path, 'footer_gitolite')
  353. with open(self.postconf, 'w') as stream:
  354. stream.write('# end of generated configuration\n')
  355. stream.write(r'# \ó/')
  356. stream.write('\n# end of footer\n')
  357. def tearDown(self):
  358. """ Tearn down the environnment, ran before every tests. """
  359. super(PagureLibGitoliteGroupConfigtests, self).tearDown()
  360. if os.path.exists(self.outputconf):
  361. os.unlink(self.outputconf)
  362. self.assertFalse(os.path.exists(self.outputconf))
  363. def test_write_gitolite_project_test_group(self):
  364. """ Test the write_gitolite_acls when updating a single group. """
  365. with open(self.outputconf, 'w') as stream:
  366. pass
  367. project = pagure.lib.query._get_project(self.session, 'test')
  368. group = pagure.lib.query.search_groups(self.session, group_name='grp')
  369. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  370. helper.write_gitolite_acls(
  371. self.session,
  372. self.outputconf,
  373. project=project,
  374. preconf=self.preconf,
  375. postconf=self.postconf,
  376. group=group,
  377. )
  378. self.assertTrue(os.path.exists(self.outputconf))
  379. with open(self.outputconf) as stream:
  380. data = stream.read()
  381. exp = r"""# this is a header that is manually added
  382. @group1 = foo bar baz
  383. @group2 = threebean puiterwijk kevin pingou
  384. # end of header
  385. @grp = pingou
  386. repo test
  387. R = @all
  388. RW+ = pingou
  389. repo docs/test
  390. R = @all
  391. RW+ = pingou
  392. repo tickets/test
  393. RW+ = pingou
  394. repo requests/test
  395. RW+ = pingou
  396. # end of body
  397. # end of generated configuration
  398. # \ó/
  399. # end of footer
  400. """
  401. #print data
  402. self.assertEqual(data, exp)
  403. def test_write_gitolite_project_test_all_groups(self):
  404. """ Test the write_gitolite_acls when updating all groups. """
  405. with open(self.outputconf, 'w') as stream:
  406. pass
  407. project = pagure.lib.query._get_project(self.session, 'test')
  408. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  409. helper.write_gitolite_acls(
  410. self.session,
  411. self.outputconf,
  412. project=project,
  413. preconf=self.preconf,
  414. postconf=self.postconf,
  415. )
  416. self.assertTrue(os.path.exists(self.outputconf))
  417. with open(self.outputconf) as stream:
  418. data = stream.read()
  419. exp = r"""# this is a header that is manually added
  420. @group1 = foo bar baz
  421. @group2 = threebean puiterwijk kevin pingou
  422. # end of header
  423. @grp = pingou
  424. @grp2 = foo
  425. # end of groups
  426. repo test
  427. R = @all
  428. RW+ = pingou
  429. repo docs/test
  430. R = @all
  431. RW+ = pingou
  432. repo tickets/test
  433. RW+ = pingou
  434. repo requests/test
  435. RW+ = pingou
  436. # end of body
  437. # end of generated configuration
  438. # \ó/
  439. # end of footer
  440. """
  441. #print data
  442. self.assertEqual(data, exp)
  443. def test_write_gitolite_project_all_projects_groups(self):
  444. """ Test the generating the entire gitolite config. """
  445. with open(self.outputconf, 'w') as stream:
  446. pass
  447. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  448. helper.write_gitolite_acls(
  449. self.session,
  450. self.outputconf,
  451. project=-1,
  452. preconf=self.preconf,
  453. postconf=self.postconf,
  454. )
  455. self.assertTrue(os.path.exists(self.outputconf))
  456. with open(self.outputconf) as stream:
  457. data = stream.read()
  458. exp = r"""# this is a header that is manually added
  459. @group1 = foo bar baz
  460. @group2 = threebean puiterwijk kevin pingou
  461. # end of header
  462. @grp = pingou
  463. @grp2 = foo
  464. # end of groups
  465. %s
  466. # end of body
  467. # end of generated configuration
  468. # \ó/
  469. # end of footer
  470. """ % CORE_CONFIG
  471. #print data
  472. self.assertEqual(data, exp)
  473. def test_write_gitolite_project_all_projects_one_group(self):
  474. """ Test the generating the entire gitolite config. """
  475. # Generate the full gitolite config that we will update
  476. self.test_write_gitolite_project_all_projects_groups()
  477. project = pagure.lib.query._get_project(self.session, 'test')
  478. group = pagure.lib.query.search_groups(self.session, group_name='grp')
  479. # Let's add `foo` to `grp` so something changes
  480. msg = pagure.lib.query.add_user_to_group(
  481. self.session,
  482. username='foo',
  483. group=group,
  484. user='pingou',
  485. is_admin=False,
  486. )
  487. self.session.commit()
  488. self.assertEqual(msg, 'User `foo` added to the group `grp`.')
  489. # Let's add `foo` to `test` so the project changes as well
  490. msg = pagure.lib.query.add_user_to_project(
  491. self.session,
  492. project=project,
  493. new_user='foo',
  494. user='pingou',
  495. access='commit'
  496. )
  497. self.assertEqual(msg, 'User added')
  498. self.session.commit()
  499. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  500. helper.write_gitolite_acls(
  501. self.session,
  502. self.outputconf,
  503. project=project,
  504. group=group,
  505. preconf=self.preconf,
  506. postconf=self.postconf,
  507. )
  508. self.assertTrue(os.path.exists(self.outputconf))
  509. with open(self.outputconf) as stream:
  510. data = stream.read()
  511. exp = r"""# this is a header that is manually added
  512. @group1 = foo bar baz
  513. @group2 = threebean puiterwijk kevin pingou
  514. # end of header
  515. @grp = foo pingou
  516. @grp2 = foo
  517. # end of groups
  518. repo test2
  519. R = @all
  520. RW+ = pingou
  521. repo docs/test2
  522. R = @all
  523. RW+ = pingou
  524. repo tickets/test2
  525. RW+ = pingou
  526. repo requests/test2
  527. RW+ = pingou
  528. repo somenamespace/test3
  529. R = @all
  530. RW+ = pingou
  531. repo docs/somenamespace/test3
  532. R = @all
  533. RW+ = pingou
  534. repo tickets/somenamespace/test3
  535. RW+ = pingou
  536. repo requests/somenamespace/test3
  537. RW+ = pingou
  538. repo test
  539. R = @all
  540. RW+ = pingou
  541. RW+ = foo
  542. repo docs/test
  543. R = @all
  544. RW+ = pingou
  545. RW+ = foo
  546. repo tickets/test
  547. RW+ = pingou
  548. RW+ = foo
  549. repo requests/test
  550. RW+ = pingou
  551. RW+ = foo
  552. # end of body
  553. # end of generated configuration
  554. # \ó/
  555. # end of footer
  556. """
  557. #print data
  558. self.assertEqual(data, exp)
  559. def test_write_gitolite_delete_group(self):
  560. """ Test the updating the gitolite config after having
  561. deleted a group.
  562. """
  563. # Generate the full gitolite config that we will update
  564. self.test_write_gitolite_project_all_projects_groups()
  565. # Delete the group `grp`
  566. self.assertEqual(len(pagure.lib.query.search_groups(self.session)), 2)
  567. group = pagure.lib.query.search_groups(self.session, group_name='grp')
  568. self.session.delete(group)
  569. self.session.commit()
  570. self.assertEqual(len(pagure.lib.query.search_groups(self.session)), 1)
  571. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  572. helper.write_gitolite_acls(
  573. self.session,
  574. self.outputconf,
  575. project=None,
  576. preconf=self.preconf,
  577. postconf=self.postconf,
  578. )
  579. self.assertTrue(os.path.exists(self.outputconf))
  580. with open(self.outputconf) as stream:
  581. data = stream.read()
  582. exp = r"""# this is a header that is manually added
  583. @group1 = foo bar baz
  584. @group2 = threebean puiterwijk kevin pingou
  585. # end of header
  586. @grp2 = foo
  587. # end of groups
  588. %s
  589. # end of body
  590. # end of generated configuration
  591. # \ó/
  592. # end of footer
  593. """ % CORE_CONFIG
  594. #print data
  595. self.assertEqual(data, exp)
  596. @patch('pagure.lib.git_auth.get_git_auth_helper')
  597. def test_task_generate_gitolite_acls_one_group(self, get_helper):
  598. """ Test the generate_gitolite_acls task to ensure if group is None
  599. then None is passed to the helper. """
  600. helper = MagicMock()
  601. get_helper.return_value = helper
  602. pagure.lib.query.SESSIONMAKER = self.session.session_factory
  603. pagure.lib.tasks.generate_gitolite_acls(
  604. namespace=None, name='test', user=None, group=None)
  605. get_helper.assert_called_with()
  606. args = helper.generate_acls.call_args
  607. self.assertIsNone(args[1].get('group'))
  608. self.assertIsNotNone(args[1].get('project'))
  609. def test_write_gitolite_project_test_private(self):
  610. """ Test the write_gitolite_acls function of pagure.lib.git with
  611. a postconf set """
  612. # Make the test project private
  613. project = pagure.lib.query._get_project(self.session, 'test')
  614. project.private = True
  615. self.session.add(project)
  616. self.session.commit()
  617. # Re-generate the gitolite config just for this project
  618. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  619. helper.write_gitolite_acls(
  620. self.session,
  621. self.outputconf,
  622. project=None,
  623. )
  624. self.assertTrue(os.path.exists(self.outputconf))
  625. with open(self.outputconf) as stream:
  626. data = stream.read()
  627. exp = """@grp = pingou
  628. @grp2 = foo
  629. # end of groups
  630. repo test
  631. RW+ = pingou
  632. repo docs/test
  633. RW+ = pingou
  634. repo tickets/test
  635. RW+ = pingou
  636. repo requests/test
  637. RW+ = pingou
  638. repo test2
  639. R = @all
  640. RW+ = pingou
  641. repo docs/test2
  642. R = @all
  643. RW+ = pingou
  644. repo tickets/test2
  645. RW+ = pingou
  646. repo requests/test2
  647. RW+ = pingou
  648. repo somenamespace/test3
  649. R = @all
  650. RW+ = pingou
  651. repo docs/somenamespace/test3
  652. R = @all
  653. RW+ = pingou
  654. repo tickets/somenamespace/test3
  655. RW+ = pingou
  656. repo requests/somenamespace/test3
  657. RW+ = pingou
  658. # end of body
  659. """
  660. #print data
  661. self.assertEqual(data, exp)
  662. def test_remove_acls(self):
  663. """ Test the remove_acls function of pagure.lib.git when deleting
  664. a project """
  665. pagure.config.config['GITOLITE_CONFIG'] = self.outputconf
  666. with open(self.outputconf, 'w') as stream:
  667. pass
  668. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  669. helper.write_gitolite_acls(
  670. self.session,
  671. self.outputconf,
  672. project=-1,
  673. )
  674. self.assertTrue(os.path.exists(self.outputconf))
  675. with open(self.outputconf) as stream:
  676. data = stream.read()
  677. exp = """@grp = pingou
  678. @grp2 = foo
  679. # end of groups
  680. %s
  681. # end of body
  682. """ % CORE_CONFIG
  683. #print data
  684. self.assertEqual(data, exp)
  685. # Test removing a project from the existing config
  686. project = pagure.lib.query.get_authorized_project(
  687. self.session, project_name='test')
  688. helper.remove_acls(self.session, project=project)
  689. with open(self.outputconf) as stream:
  690. data = stream.read()
  691. exp = """@grp = pingou
  692. @grp2 = foo
  693. # end of groups
  694. repo test2
  695. R = @all
  696. RW+ = pingou
  697. repo docs/test2
  698. R = @all
  699. RW+ = pingou
  700. repo tickets/test2
  701. RW+ = pingou
  702. repo requests/test2
  703. RW+ = pingou
  704. repo somenamespace/test3
  705. R = @all
  706. RW+ = pingou
  707. repo docs/somenamespace/test3
  708. R = @all
  709. RW+ = pingou
  710. repo tickets/somenamespace/test3
  711. RW+ = pingou
  712. repo requests/somenamespace/test3
  713. RW+ = pingou
  714. # end of body
  715. """
  716. #print data
  717. self.assertEqual(data, exp)
  718. def test_remove_acls_no_project(self):
  719. """ Test the remove_acls function of pagure.lib.git when no project
  720. is specified """
  721. pagure.config.config['GITOLITE_CONFIG'] = self.outputconf
  722. with open(self.outputconf, 'w') as stream:
  723. pass
  724. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  725. helper.write_gitolite_acls(
  726. self.session,
  727. self.outputconf,
  728. project=-1,
  729. )
  730. self.assertTrue(os.path.exists(self.outputconf))
  731. with open(self.outputconf) as stream:
  732. data = stream.read()
  733. exp = """@grp = pingou
  734. @grp2 = foo
  735. # end of groups
  736. %s
  737. # end of body
  738. """ % CORE_CONFIG
  739. #print data
  740. self.assertEqual(data, exp)
  741. # Test nothing changes if no project is specified
  742. self.assertRaises(
  743. RuntimeError,
  744. helper.remove_acls,
  745. self.session,
  746. project=None
  747. )
  748. with open(self.outputconf) as stream:
  749. data = stream.read()
  750. self.assertEqual(data, exp)
  751. if __name__ == '__main__':
  752. unittest.main(verbosity=2)