1
0

test_pagure_lib_gitolite_config.py 23 KB

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