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