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