test_pagure_lib_gitolite_config.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2017 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. __requires__ = ['SQLAlchemy >= 0.8']
  8. import pkg_resources
  9. import datetime
  10. import os
  11. import shutil
  12. import sys
  13. import tempfile
  14. import time
  15. import unittest
  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
  21. import pagure.lib.git
  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') as stream:
  70. stream.write('# end of generated configuration\n')
  71. stream.write('# \ó/\n')
  72. stream.write('# 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) as stream:
  92. data = stream.read().decode('utf-8')
  93. exp = u"""# 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().decode('utf-8')
  138. exp = u"""# 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._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().decode('utf-8')
  167. exp = u"""# 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._get_project(self.session, 'test')
  195. project.user_id = 2
  196. self.session.add(project)
  197. self.session.commit()
  198. project = pagure.lib._get_project(self.session, 'test')
  199. msg = pagure.lib.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._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().decode('utf-8')
  220. exp = u"""# 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._get_project(self.session, 'test')
  272. project.user_id = 2
  273. self.session.add(project)
  274. self.session.commit()
  275. project = pagure.lib._get_project(self.session, 'test')
  276. msg = pagure.lib.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._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().decode('utf-8')
  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. pagure.lib.git.SESSION = self.session
  324. tests.create_projects(self.session)
  325. pagure.lib.add_group(
  326. self.session,
  327. group_name='grp',
  328. display_name='grp group',
  329. description=None,
  330. group_type='user',
  331. user='pingou',
  332. is_admin=False,
  333. blacklist=[],
  334. )
  335. pagure.lib.add_group(
  336. self.session,
  337. group_name='grp2',
  338. display_name='grp2 group',
  339. description=None,
  340. group_type='user',
  341. user='foo',
  342. is_admin=False,
  343. blacklist=[],
  344. )
  345. self.session.commit()
  346. self.outputconf = os.path.join(self.path, 'test_gitolite.conf')
  347. self.preconf = os.path.join(self.path, 'header_gitolite')
  348. with open(self.preconf, 'w') as stream:
  349. stream.write('# this is a header that is manually added\n')
  350. stream.write('\n')
  351. stream.write('@group1 = foo bar baz\n')
  352. stream.write('@group2 = threebean puiterwijk kevin pingou\n')
  353. self.postconf = os.path.join(self.path, 'footer_gitolite')
  354. with open(self.postconf, 'w') as stream:
  355. stream.write('# end of generated configuration\n')
  356. stream.write('# \ó/\n')
  357. stream.write('# end of footer\n')
  358. def tearDown(self):
  359. """ Tearn down the environnment, ran before every tests. """
  360. super(PagureLibGitoliteGroupConfigtests, self).tearDown()
  361. if os.path.exists(self.outputconf):
  362. os.unlink(self.outputconf)
  363. self.assertFalse(os.path.exists(self.outputconf))
  364. def test_write_gitolite_project_test_group(self):
  365. """ Test the write_gitolite_acls when updating a single group. """
  366. with open(self.outputconf, 'w') as stream:
  367. pass
  368. project = pagure.lib._get_project(self.session, 'test')
  369. group = pagure.lib.search_groups(self.session, group_name='grp')
  370. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  371. helper.write_gitolite_acls(
  372. self.session,
  373. self.outputconf,
  374. project=project,
  375. preconf=self.preconf,
  376. postconf=self.postconf,
  377. group=group,
  378. )
  379. self.assertTrue(os.path.exists(self.outputconf))
  380. with open(self.outputconf) as stream:
  381. data = stream.read().decode('utf-8')
  382. exp = u"""# this is a header that is manually added
  383. @group1 = foo bar baz
  384. @group2 = threebean puiterwijk kevin pingou
  385. # end of header
  386. @grp = pingou
  387. repo test
  388. R = @all
  389. RW+ = pingou
  390. repo docs/test
  391. R = @all
  392. RW+ = pingou
  393. repo tickets/test
  394. RW+ = pingou
  395. repo requests/test
  396. RW+ = pingou
  397. # end of body
  398. # end of generated configuration
  399. # \ó/
  400. # end of footer
  401. """
  402. #print data
  403. self.assertEqual(data, exp)
  404. def test_write_gitolite_project_test_all_groups(self):
  405. """ Test the write_gitolite_acls when updating all groups. """
  406. with open(self.outputconf, 'w') as stream:
  407. pass
  408. project = pagure.lib._get_project(self.session, 'test')
  409. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  410. helper.write_gitolite_acls(
  411. self.session,
  412. self.outputconf,
  413. project=project,
  414. preconf=self.preconf,
  415. postconf=self.postconf,
  416. )
  417. self.assertTrue(os.path.exists(self.outputconf))
  418. with open(self.outputconf) as stream:
  419. data = stream.read().decode('utf-8')
  420. exp = u"""# this is a header that is manually added
  421. @group1 = foo bar baz
  422. @group2 = threebean puiterwijk kevin pingou
  423. # end of header
  424. @grp = pingou
  425. @grp2 = foo
  426. # end of groups
  427. repo test
  428. R = @all
  429. RW+ = pingou
  430. repo docs/test
  431. R = @all
  432. RW+ = pingou
  433. repo tickets/test
  434. RW+ = pingou
  435. repo requests/test
  436. RW+ = pingou
  437. # end of body
  438. # end of generated configuration
  439. # \ó/
  440. # end of footer
  441. """
  442. #print data
  443. self.assertEqual(data, exp)
  444. def test_write_gitolite_project_all_projects_groups(self):
  445. """ Test the generating the entire gitolite config. """
  446. with open(self.outputconf, 'w') as stream:
  447. pass
  448. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  449. helper.write_gitolite_acls(
  450. self.session,
  451. self.outputconf,
  452. project=-1,
  453. preconf=self.preconf,
  454. postconf=self.postconf,
  455. )
  456. self.assertTrue(os.path.exists(self.outputconf))
  457. with open(self.outputconf) as stream:
  458. data = stream.read().decode('utf-8')
  459. exp = u"""# this is a header that is manually added
  460. @group1 = foo bar baz
  461. @group2 = threebean puiterwijk kevin pingou
  462. # end of header
  463. @grp = pingou
  464. @grp2 = foo
  465. # end of groups
  466. %s
  467. # end of body
  468. # end of generated configuration
  469. # \ó/
  470. # end of footer
  471. """ % CORE_CONFIG
  472. #print data
  473. self.assertEqual(data, exp)
  474. def test_write_gitolite_project_all_projects_one_group(self):
  475. """ Test the generating the entire gitolite config. """
  476. # Generate the full gitolite config that we will update
  477. self.test_write_gitolite_project_all_projects_groups()
  478. project = pagure.lib._get_project(self.session, 'test')
  479. group = pagure.lib.search_groups(self.session, group_name='grp')
  480. # Let's add `foo` to `grp` so something changes
  481. msg = pagure.lib.add_user_to_group(
  482. self.session,
  483. username='foo',
  484. group=group,
  485. user='pingou',
  486. is_admin=False,
  487. )
  488. self.session.commit()
  489. self.assertEqual(msg, 'User `foo` added to the group `grp`.')
  490. # Let's add `foo` to `test` so the project changes as well
  491. msg = pagure.lib.add_user_to_project(
  492. self.session,
  493. project=project,
  494. new_user='foo',
  495. user='pingou',
  496. access='commit'
  497. )
  498. self.assertEqual(msg, 'User added')
  499. self.session.commit()
  500. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  501. helper.write_gitolite_acls(
  502. self.session,
  503. self.outputconf,
  504. project=project,
  505. group=group,
  506. preconf=self.preconf,
  507. postconf=self.postconf,
  508. )
  509. self.assertTrue(os.path.exists(self.outputconf))
  510. with open(self.outputconf) as stream:
  511. data = stream.read().decode('utf-8')
  512. exp = u"""# this is a header that is manually added
  513. @group1 = foo bar baz
  514. @group2 = threebean puiterwijk kevin pingou
  515. # end of header
  516. @grp = foo pingou
  517. @grp2 = foo
  518. # end of groups
  519. repo test2
  520. R = @all
  521. RW+ = pingou
  522. repo docs/test2
  523. R = @all
  524. RW+ = pingou
  525. repo tickets/test2
  526. RW+ = pingou
  527. repo requests/test2
  528. RW+ = pingou
  529. repo somenamespace/test3
  530. R = @all
  531. RW+ = pingou
  532. repo docs/somenamespace/test3
  533. R = @all
  534. RW+ = pingou
  535. repo tickets/somenamespace/test3
  536. RW+ = pingou
  537. repo requests/somenamespace/test3
  538. RW+ = pingou
  539. repo test
  540. R = @all
  541. RW+ = pingou
  542. RW+ = foo
  543. repo docs/test
  544. R = @all
  545. RW+ = pingou
  546. RW+ = foo
  547. repo tickets/test
  548. RW+ = pingou
  549. RW+ = foo
  550. repo requests/test
  551. RW+ = pingou
  552. RW+ = foo
  553. # end of body
  554. # end of generated configuration
  555. # \ó/
  556. # end of footer
  557. """
  558. #print data
  559. self.assertEqual(data, exp)
  560. def test_write_gitolite_delete_group(self):
  561. """ Test the updating the gitolite config after having
  562. deleted a group.
  563. """
  564. # Generate the full gitolite config that we will update
  565. self.test_write_gitolite_project_all_projects_groups()
  566. # Delete the group `grp`
  567. self.assertEqual(len(pagure.lib.search_groups(self.session)), 2)
  568. group = pagure.lib.search_groups(self.session, group_name='grp')
  569. self.session.delete(group)
  570. self.session.commit()
  571. self.assertEqual(len(pagure.lib.search_groups(self.session)), 1)
  572. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  573. helper.write_gitolite_acls(
  574. self.session,
  575. self.outputconf,
  576. project=None,
  577. preconf=self.preconf,
  578. postconf=self.postconf,
  579. )
  580. self.assertTrue(os.path.exists(self.outputconf))
  581. with open(self.outputconf) as stream:
  582. data = stream.read().decode('utf-8')
  583. exp = u"""# this is a header that is manually added
  584. @group1 = foo bar baz
  585. @group2 = threebean puiterwijk kevin pingou
  586. # end of header
  587. @grp2 = foo
  588. # end of groups
  589. %s
  590. # end of body
  591. # end of generated configuration
  592. # \ó/
  593. # end of footer
  594. """ % CORE_CONFIG
  595. #print data
  596. self.assertEqual(data, exp)
  597. @patch('pagure.lib.git_auth.get_git_auth_helper')
  598. def test_task_generate_gitolite_acls_one_group(self, get_helper):
  599. """ Test the generate_gitolite_acls task to ensure if group is None
  600. then None is passed to the helper. """
  601. helper = MagicMock()
  602. get_helper.return_value = helper
  603. pagure.lib.SESSIONMAKER = self.session.session_factory
  604. pagure.lib.tasks.generate_gitolite_acls(
  605. namespace=None, name='test', user=None, group=None)
  606. get_helper.assert_called_with('gitolite3')
  607. args = helper.generate_acls.call_args
  608. self.assertIsNone(args[1].get('group'))
  609. self.assertIsNotNone(args[1].get('project'))
  610. def test_write_gitolite_project_test_private(self):
  611. """ Test the write_gitolite_acls function of pagure.lib.git with
  612. a postconf set """
  613. # Make the test project private
  614. project = pagure.lib._get_project(self.session, 'test')
  615. project.private = True
  616. self.session.add(project)
  617. self.session.commit()
  618. # Re-generate the gitolite config just for this project
  619. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  620. helper.write_gitolite_acls(
  621. self.session,
  622. self.outputconf,
  623. project=None,
  624. )
  625. self.assertTrue(os.path.exists(self.outputconf))
  626. with open(self.outputconf) as stream:
  627. data = stream.read().decode('utf-8')
  628. exp = u"""@grp = pingou
  629. @grp2 = foo
  630. # end of groups
  631. repo test
  632. RW+ = pingou
  633. repo docs/test
  634. RW+ = pingou
  635. repo tickets/test
  636. RW+ = pingou
  637. repo requests/test
  638. RW+ = pingou
  639. repo test2
  640. R = @all
  641. RW+ = pingou
  642. repo docs/test2
  643. R = @all
  644. RW+ = pingou
  645. repo tickets/test2
  646. RW+ = pingou
  647. repo requests/test2
  648. RW+ = pingou
  649. repo somenamespace/test3
  650. R = @all
  651. RW+ = pingou
  652. repo docs/somenamespace/test3
  653. R = @all
  654. RW+ = pingou
  655. repo tickets/somenamespace/test3
  656. RW+ = pingou
  657. repo requests/somenamespace/test3
  658. RW+ = pingou
  659. # end of body
  660. """
  661. #print data
  662. self.assertEqual(data, exp)
  663. def test_remove_acls(self):
  664. """ Test the remove_acls function of pagure.lib.git when deleting
  665. a project """
  666. pagure.config.config['GITOLITE_CONFIG'] = self.outputconf
  667. with open(self.outputconf, 'w') as stream:
  668. pass
  669. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  670. helper.write_gitolite_acls(
  671. self.session,
  672. self.outputconf,
  673. project=-1,
  674. )
  675. self.assertTrue(os.path.exists(self.outputconf))
  676. with open(self.outputconf) as stream:
  677. data = stream.read().decode('utf-8')
  678. exp = u"""@grp = pingou
  679. @grp2 = foo
  680. # end of groups
  681. %s
  682. # end of body
  683. """ % CORE_CONFIG
  684. #print data
  685. self.assertEqual(data, exp)
  686. # Test removing a project from the existing config
  687. project = pagure.lib.get_authorized_project(
  688. self.session, project_name='test')
  689. helper.remove_acls(self.session, project=project)
  690. with open(self.outputconf) as stream:
  691. data = stream.read().decode('utf-8')
  692. exp = u"""@grp = pingou
  693. @grp2 = foo
  694. # end of groups
  695. repo test2
  696. R = @all
  697. RW+ = pingou
  698. repo docs/test2
  699. R = @all
  700. RW+ = pingou
  701. repo tickets/test2
  702. RW+ = pingou
  703. repo requests/test2
  704. RW+ = pingou
  705. repo somenamespace/test3
  706. R = @all
  707. RW+ = pingou
  708. repo docs/somenamespace/test3
  709. R = @all
  710. RW+ = pingou
  711. repo tickets/somenamespace/test3
  712. RW+ = pingou
  713. repo requests/somenamespace/test3
  714. RW+ = pingou
  715. # end of body
  716. """
  717. #print data
  718. self.assertEqual(data, exp)
  719. def test_remove_acls_no_project(self):
  720. """ Test the remove_acls function of pagure.lib.git when no project
  721. is specified """
  722. pagure.config.config['GITOLITE_CONFIG'] = self.outputconf
  723. with open(self.outputconf, 'w') as stream:
  724. pass
  725. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  726. helper.write_gitolite_acls(
  727. self.session,
  728. self.outputconf,
  729. project=-1,
  730. )
  731. self.assertTrue(os.path.exists(self.outputconf))
  732. with open(self.outputconf) as stream:
  733. data = stream.read().decode('utf-8')
  734. exp = u"""@grp = pingou
  735. @grp2 = foo
  736. # end of groups
  737. %s
  738. # end of body
  739. """ % CORE_CONFIG
  740. #print data
  741. self.assertEqual(data, exp)
  742. # Test nothing changes if no project is specified
  743. self.assertRaises(
  744. RuntimeError,
  745. helper.remove_acls,
  746. self.session,
  747. project=None
  748. )
  749. with open(self.outputconf) as stream:
  750. data = stream.read().decode('utf-8')
  751. self.assertEqual(data, exp)
  752. if __name__ == '__main__':
  753. unittest.main(verbosity=2)