1
0

test_pagure_flask.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. import werkzeug
  16. sys.path.insert(
  17. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  18. )
  19. import pagure.lib.query
  20. import pagure.lib.model
  21. import pagure.utils
  22. import tests
  23. class PagureGetRemoteRepoPath(tests.SimplePagureTest):
  24. """ Tests for pagure """
  25. def setUp(self):
  26. """ Set up the environnment, ran before every tests. """
  27. super(PagureGetRemoteRepoPath, self).setUp()
  28. tests.create_projects(self.session)
  29. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  30. tests.add_content_git_repo(
  31. os.path.join(self.path, "repos", "test2.git")
  32. )
  33. @mock.patch(
  34. "pagure.lib.repo.PagureRepo.pull",
  35. mock.MagicMock(side_effect=pygit2.GitError),
  36. )
  37. def test_passing(self):
  38. """ Test get_remote_repo_path in pagure. """
  39. output = pagure.utils.get_remote_repo_path(
  40. os.path.join(self.path, "repos", "test2.git"),
  41. "master",
  42. ignore_non_exist=True,
  43. )
  44. self.assertTrue(output.endswith("repos_test2.git_master"))
  45. def test_is_repo_committer_logged_out(self):
  46. """ Test is_repo_committer in pagure when there is no logged in user.
  47. """
  48. repo = pagure.lib.query._get_project(self.session, "test")
  49. with self.app.application.app_context():
  50. output = pagure.utils.is_repo_committer(repo)
  51. self.assertFalse(output)
  52. def test_is_repo_committer_logged_in(self):
  53. """ Test is_repo_committer in pagure with the appropriate user logged
  54. in. """
  55. repo = pagure.lib.query._get_project(self.session, "test")
  56. g = munch.Munch()
  57. g.fas_user = tests.FakeUser(username="pingou")
  58. g.authenticated = True
  59. g.session = self.session
  60. with mock.patch("flask.g", g):
  61. output = pagure.utils.is_repo_committer(repo)
  62. self.assertTrue(output)
  63. def test_is_repo_committer_logged_in_in_group(self):
  64. """ Test is_repo_committer in pagure with the appropriate user logged
  65. in. """
  66. # Create group
  67. msg = pagure.lib.query.add_group(
  68. self.session,
  69. group_name="packager",
  70. display_name="packager",
  71. description="The Fedora packager groups",
  72. group_type="user",
  73. user="pingou",
  74. is_admin=False,
  75. blacklist=[],
  76. )
  77. self.session.commit()
  78. self.assertEqual(msg, "User `pingou` added to the group `packager`.")
  79. # Add user to group
  80. group = pagure.lib.query.search_groups(
  81. self.session, group_name="packager"
  82. )
  83. msg = pagure.lib.query.add_user_to_group(
  84. self.session,
  85. username="foo",
  86. group=group,
  87. user="pingou",
  88. is_admin=True,
  89. )
  90. self.session.commit()
  91. self.assertEqual(msg, "User `foo` added to the group `packager`.")
  92. # Add group packager to project test
  93. project = pagure.lib.query._get_project(self.session, "test")
  94. msg = pagure.lib.query.add_group_to_project(
  95. self.session, project=project, new_group="packager", user="pingou"
  96. )
  97. self.session.commit()
  98. self.assertEqual(msg, "Group added")
  99. repo = pagure.lib.query._get_project(self.session, "test")
  100. g = munch.Munch()
  101. g.fas_user = tests.FakeUser(username="foo")
  102. g.authenticated = True
  103. g.session = self.session
  104. with mock.patch("flask.g", g):
  105. output = pagure.utils.is_repo_committer(repo)
  106. self.assertTrue(output)
  107. def test_is_repo_committer_logged_in_in_ticket_group(self):
  108. """ Test is_repo_committer in pagure with the appropriate user logged
  109. in. """
  110. # Create group
  111. msg = pagure.lib.query.add_group(
  112. self.session,
  113. group_name="packager",
  114. display_name="packager",
  115. description="The Fedora packager groups",
  116. group_type="user",
  117. user="pingou",
  118. is_admin=False,
  119. blacklist=[],
  120. )
  121. self.session.commit()
  122. self.assertEqual(msg, "User `pingou` added to the group `packager`.")
  123. # Add user to group
  124. group = pagure.lib.query.search_groups(
  125. self.session, group_name="packager"
  126. )
  127. msg = pagure.lib.query.add_user_to_group(
  128. self.session,
  129. username="foo",
  130. group=group,
  131. user="pingou",
  132. is_admin=True,
  133. )
  134. self.session.commit()
  135. self.assertEqual(msg, "User `foo` added to the group `packager`.")
  136. # Add group packager to project test
  137. project = pagure.lib.query._get_project(self.session, "test")
  138. msg = pagure.lib.query.add_group_to_project(
  139. self.session,
  140. project=project,
  141. new_group="packager",
  142. user="pingou",
  143. access="ticket",
  144. )
  145. self.session.commit()
  146. self.assertEqual(msg, "Group added")
  147. repo = pagure.lib.query._get_project(self.session, "test")
  148. g = munch.Munch()
  149. g.fas_user = tests.FakeUser(username="foo")
  150. g.authenticated = True
  151. g.session = self.session
  152. with mock.patch("flask.g", g):
  153. output = pagure.utils.is_repo_committer(repo)
  154. self.assertFalse(output)
  155. def test_is_repo_committer_logged_in_wrong_user(self):
  156. """ Test is_repo_committer in pagure with the wrong user logged in.
  157. """
  158. repo = pagure.lib.query._get_project(self.session, "test")
  159. g = munch.Munch()
  160. g.fas_user = tests.FakeUser()
  161. g.authenticated = True
  162. g.session = self.session
  163. with mock.patch("flask.g", g):
  164. output = pagure.utils.is_repo_committer(repo)
  165. self.assertFalse(output)
  166. # Mocked config
  167. config = {"provenpackager": {}}
  168. @mock.patch.dict("pagure.config.config", {"EXTERNAL_COMMITTER": config})
  169. def test_is_repo_committer_external_committer_generic_no_member(self):
  170. """ Test is_repo_committer in pagure with EXTERNAL_COMMITTER
  171. configured to give access to all the provenpackager, but the user
  172. is not one.
  173. """
  174. repo = pagure.lib.query._get_project(self.session, "test")
  175. user = tests.FakeUser()
  176. g = munch.Munch()
  177. g.fas_user = user
  178. g.authenticated = True
  179. g.session = self.session
  180. with mock.patch("flask.g", g):
  181. output = pagure.utils.is_repo_committer(repo)
  182. self.assertFalse(output)
  183. @mock.patch.dict("pagure.config.config", {"EXTERNAL_COMMITTER": config})
  184. def test_is_repo_committer_external_committer_generic_member(self):
  185. """ Test is_repo_committer in pagure with EXTERNAL_COMMITTER
  186. configured to give access to all the provenpackager, and the user
  187. is one
  188. """
  189. repo = pagure.lib.query._get_project(self.session, "test")
  190. g = munch.Munch()
  191. g.fas_user = tests.FakeUser(username="foo")
  192. g.fas_user.groups.append("provenpackager")
  193. g.authenticated = True
  194. g.session = self.session
  195. with mock.patch("flask.g", g):
  196. output = pagure.utils.is_repo_committer(repo)
  197. self.assertTrue(output)
  198. config = {"provenpackager": {"exclude": ["test"]}}
  199. @mock.patch.dict("pagure.config.config", {"EXTERNAL_COMMITTER": config})
  200. def test_is_repo_committer_external_committer_excluding_one(self):
  201. """ Test is_repo_committer in pagure with EXTERNAL_COMMITTER
  202. configured to give access to all the provenpackager but for this
  203. one repo
  204. """
  205. repo = pagure.lib.query._get_project(self.session, "test")
  206. g = munch.Munch()
  207. g.fas_user = tests.FakeUser()
  208. g.fas_user.groups.append("provenpackager")
  209. g.authenticated = True
  210. g.session = self.session
  211. with mock.patch("flask.g", g):
  212. output = pagure.utils.is_repo_committer(repo)
  213. self.assertFalse(output)
  214. @mock.patch.dict("pagure.config.config", {"EXTERNAL_COMMITTER": config})
  215. def test_is_repo_committer_owner_external_committer_excluding_one(self):
  216. """ Test is_repo_committer in pagure with EXTERNAL_COMMITTER
  217. configured to give access to all the provenpackager but for this
  218. one repo, but the user is still a direct committer
  219. """
  220. repo = pagure.lib.query._get_project(self.session, "test")
  221. g = munch.Munch()
  222. g.fas_user = tests.FakeUser(username="pingou")
  223. g.fas_user.groups.append("provenpackager")
  224. g.authenticated = True
  225. g.session = self.session
  226. with mock.patch("flask.g", g):
  227. output = pagure.utils.is_repo_committer(repo)
  228. self.assertTrue(output)
  229. config = {"provenpackager": {"restrict": ["test"]}}
  230. @mock.patch.dict("pagure.config.config", {"EXTERNAL_COMMITTER": config})
  231. def test_is_repo_committer_external_committer_restricted_not_member(self):
  232. """ Test is_repo_committer in pagure with EXTERNAL_COMMITTER
  233. configured to give access the provenpackager just for one repo
  234. """
  235. repo = pagure.lib.query._get_project(self.session, "test")
  236. g = munch.Munch()
  237. g.fas_user = tests.FakeUser()
  238. g.authenticated = True
  239. g.session = self.session
  240. with mock.patch("flask.g", g):
  241. output = pagure.utils.is_repo_committer(repo)
  242. self.assertFalse(output)
  243. @mock.patch.dict("pagure.config.config", {"EXTERNAL_COMMITTER": config})
  244. def test_is_repo_committer_external_committer_restricting_to_one(self):
  245. """ Test is_repo_committer in pagure with EXTERNAL_COMMITTER
  246. configured to give access the provenpackager just for one repo
  247. """
  248. repo = pagure.lib.query._get_project(self.session, "test")
  249. g = munch.Munch()
  250. g.fas_user = tests.FakeUser(username="foo")
  251. g.authenticated = True
  252. g.fas_user.groups.append("provenpackager")
  253. g.session = self.session
  254. with mock.patch("flask.g", g):
  255. output = pagure.utils.is_repo_committer(repo)
  256. self.assertTrue(output)
  257. @mock.patch.dict("pagure.config.config", {"EXTERNAL_COMMITTER": config})
  258. def test_is_repo_committer_external_committer_restricting_another_one(
  259. self
  260. ):
  261. """ Test is_repo_committer in pagure with EXTERNAL_COMMITTER
  262. configured to give access the provenpackager just for one repo not
  263. this one
  264. """
  265. repo = pagure.lib.query._get_project(self.session, "test2")
  266. g = munch.Munch()
  267. g.fas_user = tests.FakeUser(username="foo")
  268. g.authenticated = True
  269. g.fas_user.groups.append("provenpackager")
  270. g.session = self.session
  271. with mock.patch("flask.g", g):
  272. output = pagure.utils.is_repo_committer(repo)
  273. self.assertFalse(output)
  274. if __name__ == "__main__":
  275. unittest.main(verbosity=2)