test_pagure_flask.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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 unittest
  9. import shutil
  10. import sys
  11. import os
  12. import mock
  13. import munch
  14. import pygit2
  15. sys.path.insert(
  16. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  17. )
  18. import pagure.lib.query
  19. import pagure.lib.model
  20. import pagure.utils
  21. import tests
  22. class PagureGetRemoteRepoPath(tests.SimplePagureTest):
  23. """ Tests for pagure """
  24. def setUp(self):
  25. """ Set up the environnment, ran before every tests. """
  26. super(PagureGetRemoteRepoPath, self).setUp()
  27. tests.create_projects(self.session)
  28. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  29. tests.add_content_git_repo(
  30. os.path.join(self.path, "repos", "test2.git")
  31. )
  32. @mock.patch(
  33. "pagure.lib.repo.PagureRepo.pull",
  34. mock.MagicMock(side_effect=pygit2.GitError),
  35. )
  36. def test_passing(self):
  37. """ Test get_remote_repo_path in pagure. """
  38. output = pagure.utils.get_remote_repo_path(
  39. os.path.join(self.path, "repos", "test2.git"),
  40. "master",
  41. ignore_non_exist=True,
  42. )
  43. self.assertTrue(output.endswith("repos_test2.git_master"))
  44. def test_is_repo_committer_logged_out(self):
  45. """ Test is_repo_committer in pagure when there is no logged in user.
  46. """
  47. repo = pagure.lib.query._get_project(self.session, "test")
  48. with self.app.application.app_context():
  49. output = pagure.utils.is_repo_committer(repo)
  50. self.assertFalse(output)
  51. def test_is_repo_committer_logged_in(self):
  52. """ Test is_repo_committer in pagure with the appropriate user logged
  53. in. """
  54. repo = pagure.lib.query._get_project(self.session, "test")
  55. g = munch.Munch()
  56. g.fas_user = tests.FakeUser(username="pingou")
  57. g.authenticated = True
  58. g.session = self.session
  59. with mock.patch("flask.g", g):
  60. output = pagure.utils.is_repo_committer(repo)
  61. self.assertTrue(output)
  62. def test_is_repo_committer_logged_in_in_group(self):
  63. """ Test is_repo_committer in pagure with the appropriate user logged
  64. in. """
  65. # Create group
  66. msg = pagure.lib.query.add_group(
  67. self.session,
  68. group_name="packager",
  69. display_name="packager",
  70. description="The Fedora packager groups",
  71. group_type="user",
  72. user="pingou",
  73. is_admin=False,
  74. blacklist=[],
  75. )
  76. self.session.commit()
  77. self.assertEqual(msg, "User `pingou` added to the group `packager`.")
  78. # Add user to group
  79. group = pagure.lib.query.search_groups(
  80. self.session, group_name="packager"
  81. )
  82. msg = pagure.lib.query.add_user_to_group(
  83. self.session,
  84. username="foo",
  85. group=group,
  86. user="pingou",
  87. is_admin=True,
  88. )
  89. self.session.commit()
  90. self.assertEqual(msg, "User `foo` added to the group `packager`.")
  91. # Add group packager to project test
  92. project = pagure.lib.query._get_project(self.session, "test")
  93. msg = pagure.lib.query.add_group_to_project(
  94. self.session, project=project, new_group="packager", user="pingou"
  95. )
  96. self.session.commit()
  97. self.assertEqual(msg, "Group added")
  98. repo = pagure.lib.query._get_project(self.session, "test")
  99. g = munch.Munch()
  100. g.fas_user = tests.FakeUser(username="foo")
  101. g.authenticated = True
  102. g.session = self.session
  103. with mock.patch("flask.g", g):
  104. output = pagure.utils.is_repo_committer(repo)
  105. self.assertTrue(output)
  106. def test_is_repo_committer_logged_in_in_ticket_group(self):
  107. """ Test is_repo_committer in pagure with the appropriate user logged
  108. in. """
  109. # Create group
  110. msg = pagure.lib.query.add_group(
  111. self.session,
  112. group_name="packager",
  113. display_name="packager",
  114. description="The Fedora packager groups",
  115. group_type="user",
  116. user="pingou",
  117. is_admin=False,
  118. blacklist=[],
  119. )
  120. self.session.commit()
  121. self.assertEqual(msg, "User `pingou` added to the group `packager`.")
  122. # Add user to group
  123. group = pagure.lib.query.search_groups(
  124. self.session, group_name="packager"
  125. )
  126. msg = pagure.lib.query.add_user_to_group(
  127. self.session,
  128. username="foo",
  129. group=group,
  130. user="pingou",
  131. is_admin=True,
  132. )
  133. self.session.commit()
  134. self.assertEqual(msg, "User `foo` added to the group `packager`.")
  135. # Add group packager to project test
  136. project = pagure.lib.query._get_project(self.session, "test")
  137. msg = pagure.lib.query.add_group_to_project(
  138. self.session,
  139. project=project,
  140. new_group="packager",
  141. user="pingou",
  142. access="ticket",
  143. )
  144. self.session.commit()
  145. self.assertEqual(msg, "Group added")
  146. repo = pagure.lib.query._get_project(self.session, "test")
  147. g = munch.Munch()
  148. g.fas_user = tests.FakeUser(username="foo")
  149. g.authenticated = True
  150. g.session = self.session
  151. with mock.patch("flask.g", g):
  152. output = pagure.utils.is_repo_committer(repo)
  153. self.assertFalse(output)
  154. def test_is_repo_committer_logged_in_wrong_user(self):
  155. """ Test is_repo_committer in pagure with the wrong user logged in.
  156. """
  157. repo = pagure.lib.query._get_project(self.session, "test")
  158. g = munch.Munch()
  159. g.fas_user = tests.FakeUser()
  160. g.authenticated = True
  161. g.session = self.session
  162. with mock.patch("flask.g", g):
  163. output = pagure.utils.is_repo_committer(repo)
  164. self.assertFalse(output)
  165. # Mocked config
  166. config = {"provenpackager": {}}
  167. @mock.patch.dict("pagure.config.config", {"EXTERNAL_COMMITTER": config})
  168. def test_is_repo_committer_external_committer_generic_no_member(self):
  169. """ Test is_repo_committer in pagure with EXTERNAL_COMMITTER
  170. configured to give access to all the provenpackager, but the user
  171. is not one.
  172. """
  173. repo = pagure.lib.query._get_project(self.session, "test")
  174. user = tests.FakeUser()
  175. g = munch.Munch()
  176. g.fas_user = user
  177. g.authenticated = True
  178. g.session = self.session
  179. with mock.patch("flask.g", g):
  180. output = pagure.utils.is_repo_committer(repo)
  181. self.assertFalse(output)
  182. @mock.patch.dict("pagure.config.config", {"EXTERNAL_COMMITTER": config})
  183. def test_is_repo_committer_external_committer_generic_member(self):
  184. """ Test is_repo_committer in pagure with EXTERNAL_COMMITTER
  185. configured to give access to all the provenpackager, and the user
  186. is one
  187. """
  188. repo = pagure.lib.query._get_project(self.session, "test")
  189. g = munch.Munch()
  190. g.fas_user = tests.FakeUser(username="foo")
  191. g.fas_user.groups.append("provenpackager")
  192. g.authenticated = True
  193. g.session = self.session
  194. with mock.patch("flask.g", g):
  195. output = pagure.utils.is_repo_committer(repo)
  196. self.assertTrue(output)
  197. config = {"provenpackager": {"exclude": ["test"]}}
  198. @mock.patch.dict("pagure.config.config", {"EXTERNAL_COMMITTER": config})
  199. def test_is_repo_committer_external_committer_excluding_one(self):
  200. """ Test is_repo_committer in pagure with EXTERNAL_COMMITTER
  201. configured to give access to all the provenpackager but for this
  202. one repo
  203. """
  204. repo = pagure.lib.query._get_project(self.session, "test")
  205. g = munch.Munch()
  206. g.fas_user = tests.FakeUser()
  207. g.fas_user.groups.append("provenpackager")
  208. g.authenticated = True
  209. g.session = self.session
  210. with mock.patch("flask.g", g):
  211. output = pagure.utils.is_repo_committer(repo)
  212. self.assertFalse(output)
  213. @mock.patch.dict("pagure.config.config", {"EXTERNAL_COMMITTER": config})
  214. def test_is_repo_committer_owner_external_committer_excluding_one(self):
  215. """ Test is_repo_committer in pagure with EXTERNAL_COMMITTER
  216. configured to give access to all the provenpackager but for this
  217. one repo, but the user is still a direct committer
  218. """
  219. repo = pagure.lib.query._get_project(self.session, "test")
  220. g = munch.Munch()
  221. g.fas_user = tests.FakeUser(username="pingou")
  222. g.fas_user.groups.append("provenpackager")
  223. g.authenticated = True
  224. g.session = self.session
  225. with mock.patch("flask.g", g):
  226. output = pagure.utils.is_repo_committer(repo)
  227. self.assertTrue(output)
  228. config = {"provenpackager": {"restrict": ["test"]}}
  229. @mock.patch.dict("pagure.config.config", {"EXTERNAL_COMMITTER": config})
  230. def test_is_repo_committer_external_committer_restricted_not_member(self):
  231. """ Test is_repo_committer in pagure with EXTERNAL_COMMITTER
  232. configured to give access the provenpackager just for one repo
  233. """
  234. repo = pagure.lib.query._get_project(self.session, "test")
  235. g = munch.Munch()
  236. g.fas_user = tests.FakeUser()
  237. g.authenticated = True
  238. g.session = self.session
  239. with mock.patch("flask.g", g):
  240. output = pagure.utils.is_repo_committer(repo)
  241. self.assertFalse(output)
  242. @mock.patch.dict("pagure.config.config", {"EXTERNAL_COMMITTER": config})
  243. def test_is_repo_committer_external_committer_restricting_to_one(self):
  244. """ Test is_repo_committer in pagure with EXTERNAL_COMMITTER
  245. configured to give access the provenpackager just for one repo
  246. """
  247. repo = pagure.lib.query._get_project(self.session, "test")
  248. g = munch.Munch()
  249. g.fas_user = tests.FakeUser(username="foo")
  250. g.authenticated = True
  251. g.fas_user.groups.append("provenpackager")
  252. g.session = self.session
  253. with mock.patch("flask.g", g):
  254. output = pagure.utils.is_repo_committer(repo)
  255. self.assertTrue(output)
  256. @mock.patch.dict("pagure.config.config", {"EXTERNAL_COMMITTER": config})
  257. def test_is_repo_committer_external_committer_restricting_another_one(
  258. self,
  259. ):
  260. """ Test is_repo_committer in pagure with EXTERNAL_COMMITTER
  261. configured to give access the provenpackager just for one repo not
  262. this one
  263. """
  264. repo = pagure.lib.query._get_project(self.session, "test2")
  265. g = munch.Munch()
  266. g.fas_user = tests.FakeUser(username="foo")
  267. g.authenticated = True
  268. g.fas_user.groups.append("provenpackager")
  269. g.session = self.session
  270. with mock.patch("flask.g", g):
  271. output = pagure.utils.is_repo_committer(repo)
  272. self.assertFalse(output)
  273. def test_is_repo_collaborator_logged_out(self):
  274. """ Test is_repo_committer in pagure when there is no logged in user.
  275. """
  276. repo = pagure.lib.query._get_project(self.session, "test")
  277. with self.app.application.app_context():
  278. output = pagure.utils.is_repo_collaborator(repo, "master")
  279. self.assertFalse(output)
  280. def test_is_repo_collaborator_logged_in(self):
  281. """ Test is_repo_collaborator in pagure with the appropriate user logged
  282. in. """
  283. repo = pagure.lib.query._get_project(self.session, "test")
  284. g = munch.Munch()
  285. g.fas_user = tests.FakeUser(username="pingou")
  286. g.authenticated = True
  287. g.session = self.session
  288. with mock.patch("flask.g", g):
  289. output = pagure.utils.is_repo_collaborator(
  290. repo, "refs/heads/master"
  291. )
  292. self.assertTrue(output)
  293. def test_is_repo_collaborator_invalid_username(self):
  294. """ Test is_repo_collaborator in pagure with the appropriate user logged
  295. in. """
  296. repo = pagure.lib.query._get_project(self.session, "test")
  297. g = munch.Munch()
  298. g.fas_user = tests.FakeUser(username="invalid")
  299. g.authenticated = True
  300. g.session = self.session
  301. with mock.patch("flask.g", g):
  302. output = pagure.utils.is_repo_collaborator(
  303. repo, "refs/heads/master"
  304. )
  305. self.assertFalse(output)
  306. @mock.patch.dict("pagure.config.config", {"PAGURE_ADMIN_USERS": ["foo"]})
  307. def test_is_repo_collaborator_admin_user(self):
  308. """ Test is_repo_collaborator in pagure with the appropriate user logged
  309. in. """
  310. repo = pagure.lib.query._get_project(self.session, "test")
  311. g = munch.Munch()
  312. g.fas_user = tests.FakeUser(username="foo")
  313. g.authenticated = True
  314. g.session = self.session
  315. with mock.patch("flask.g", g):
  316. output = pagure.utils.is_repo_collaborator(
  317. repo, "refs/heads/master"
  318. )
  319. self.assertTrue(output)
  320. def test_is_repo_collaborator_not_in_project(self):
  321. """ Test is_repo_collaborator in pagure with the appropriate user logged
  322. in. """
  323. repo = pagure.lib.query._get_project(self.session, "test")
  324. g = munch.Munch()
  325. g.fas_user = tests.FakeUser(username="foo")
  326. g.authenticated = True
  327. g.session = self.session
  328. with mock.patch("flask.g", g):
  329. output = pagure.utils.is_repo_collaborator(
  330. repo, "refs/heads/master"
  331. )
  332. self.assertFalse(output)
  333. def test_is_repo_collaborator_in_project(self):
  334. """ Test is_repo_collaborator in pagure with the appropriate user logged
  335. in. """
  336. repo = pagure.lib.query._get_project(self.session, "test")
  337. # Add user foo to project test
  338. msg = pagure.lib.query.add_user_to_project(
  339. self.session,
  340. project=repo,
  341. new_user="foo",
  342. user="pingou",
  343. access="collaborator",
  344. branches="epel*",
  345. )
  346. self.session.commit()
  347. g = munch.Munch()
  348. g.fas_user = tests.FakeUser(username="foo")
  349. g.authenticated = True
  350. g.session = self.session
  351. with mock.patch("flask.g", g):
  352. # Collaborator trying to read the project
  353. output = pagure.utils.is_repo_collaborator(repo, None)
  354. self.assertTrue(output)
  355. # Collaborator trying to write to the project
  356. output = pagure.utils.is_repo_collaborator(
  357. repo, "refs/heads/master"
  358. )
  359. self.assertFalse(output)
  360. output = pagure.utils.is_repo_collaborator(repo, "refs/heads/epel")
  361. self.assertTrue(output)
  362. output = pagure.utils.is_repo_collaborator(
  363. repo, "refs/heads/epel8"
  364. )
  365. self.assertTrue(output)
  366. output = pagure.utils.is_repo_collaborator(
  367. repo, "refs/heads/epel8-sig-foobar"
  368. )
  369. self.assertTrue(output)
  370. def test_is_repo_collaborator_logged_in_in_group(self):
  371. """ Test is_repo_committer in pagure with the appropriate user logged
  372. in. """
  373. # Create group
  374. msg = pagure.lib.query.add_group(
  375. self.session,
  376. group_name="packager",
  377. display_name="packager",
  378. description="The Fedora packager groups",
  379. group_type="user",
  380. user="pingou",
  381. is_admin=False,
  382. blacklist=[],
  383. )
  384. self.session.commit()
  385. self.assertEqual(msg, "User `pingou` added to the group `packager`.")
  386. # Add user to group
  387. group = pagure.lib.query.search_groups(
  388. self.session, group_name="packager"
  389. )
  390. msg = pagure.lib.query.add_user_to_group(
  391. self.session,
  392. username="foo",
  393. group=group,
  394. user="pingou",
  395. is_admin=True,
  396. )
  397. self.session.commit()
  398. self.assertEqual(msg, "User `foo` added to the group `packager`.")
  399. # Add group packager to project test
  400. project = pagure.lib.query._get_project(self.session, "test")
  401. msg = pagure.lib.query.add_group_to_project(
  402. self.session,
  403. project=project,
  404. new_group="packager",
  405. user="pingou",
  406. access="collaborator",
  407. branches="epel*",
  408. )
  409. self.session.commit()
  410. self.assertEqual(msg, "Group added")
  411. repo = pagure.lib.query._get_project(self.session, "test")
  412. g = munch.Munch()
  413. g.fas_user = tests.FakeUser(username="foo")
  414. g.authenticated = True
  415. g.session = self.session
  416. with mock.patch("flask.g", g):
  417. # Collaborator in the group trying to read the project
  418. output = pagure.utils.is_repo_collaborator(repo, None)
  419. self.assertTrue(output)
  420. # Collaborator in the group trying to write to the project
  421. output = pagure.utils.is_repo_collaborator(
  422. repo, "refs/heads/master"
  423. )
  424. self.assertFalse(output)
  425. output = pagure.utils.is_repo_collaborator(repo, "refs/heads/epel")
  426. self.assertTrue(output)
  427. output = pagure.utils.is_repo_collaborator(
  428. repo, "refs/heads/epel8"
  429. )
  430. self.assertTrue(output)
  431. output = pagure.utils.is_repo_collaborator(
  432. repo, "refs/heads/epel8-sig-foobar"
  433. )
  434. self.assertTrue(output)
  435. if __name__ == "__main__":
  436. unittest.main(verbosity=2)