test_pagure_flask.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. if __name__ == "__main__":
  274. unittest.main(verbosity=2)