1
0

test_pagure_flask.py 11 KB

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