test_pagure_flask.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2017 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. __requires__ = ['SQLAlchemy >= 0.8']
  8. import pkg_resources
  9. import unittest
  10. import shutil
  11. import sys
  12. import os
  13. import mock
  14. import munch
  15. import pygit2
  16. import werkzeug
  17. sys.path.insert(0, os.path.join(os.path.dirname(
  18. os.path.abspath(__file__)), '..'))
  19. import pagure.lib
  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(os.path.join(self.path, 'repos', 'test2.git'))
  31. @mock.patch(
  32. 'pagure.lib.repo.PagureRepo.pull',
  33. mock.MagicMock(side_effect=pygit2.GitError))
  34. def test_passing(self):
  35. """ Test get_remote_repo_path in pagure. """
  36. output = pagure.utils.get_remote_repo_path(
  37. os.path.join(self.path, 'repos', 'test2.git'), 'master',
  38. ignore_non_exist=True)
  39. self.assertTrue(output.endswith('repos_test2.git_master'))
  40. def test_is_repo_committer_logged_out(self):
  41. """ Test is_repo_committer in pagure when there is no logged in user.
  42. """
  43. repo = pagure.lib._get_project(self.session, 'test')
  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._get_project(self.session, 'test')
  50. g = munch.Munch()
  51. g.fas_user = tests.FakeUser(username='pingou')
  52. g.authenticated = True
  53. with mock.patch('pagure.flask_app.flask.g', g):
  54. output = pagure.utils.is_repo_committer(repo)
  55. self.assertTrue(output)
  56. def test_is_repo_committer_logged_in_wrong_user(self):
  57. """ Test is_repo_committer in pagure with the wrong user logged in.
  58. """
  59. repo = pagure.lib._get_project(self.session, 'test')
  60. g = munch.Munch()
  61. g.fas_user = tests.FakeUser()
  62. g.authenticated = True
  63. with mock.patch('pagure.flask_app.flask.g', g):
  64. output = pagure.utils.is_repo_committer(repo)
  65. self.assertFalse(output)
  66. # Mocked config
  67. config = {
  68. 'provenpackager': {}
  69. }
  70. @mock.patch.dict('pagure.config.config', {'EXTERNAL_COMMITTER': config})
  71. def test_is_repo_committer_external_committer_generic_no_member(self):
  72. """ Test is_repo_committer in pagure with EXTERNAL_COMMITTER
  73. configured to give access to all the provenpackager, but the user
  74. is not one.
  75. """
  76. repo = pagure.lib._get_project(self.session, 'test')
  77. user = tests.FakeUser()
  78. g = munch.Munch()
  79. g.fas_user = user
  80. g.authenticated = True
  81. with mock.patch('pagure.flask_app.flask.g', g):
  82. output = pagure.utils.is_repo_committer(repo)
  83. self.assertFalse(output)
  84. @mock.patch.dict('pagure.config.config', {'EXTERNAL_COMMITTER': config})
  85. def test_is_repo_committer_external_committer_generic_member(self):
  86. """ Test is_repo_committer in pagure with EXTERNAL_COMMITTER
  87. configured to give access to all the provenpackager, and the user
  88. is one
  89. """
  90. repo = pagure.lib._get_project(self.session, 'test')
  91. g = munch.Munch()
  92. g.fas_user = tests.FakeUser()
  93. g.fas_user.groups.append('provenpackager')
  94. g.authenticated = True
  95. with mock.patch('pagure.flask_app.flask.g', g):
  96. output = pagure.utils.is_repo_committer(repo)
  97. self.assertTrue(output)
  98. config = {
  99. 'provenpackager': {
  100. 'exclude': ['test']
  101. }
  102. }
  103. @mock.patch.dict('pagure.config.config', {'EXTERNAL_COMMITTER': config})
  104. def test_is_repo_committer_external_committer_excluding_one(self):
  105. """ Test is_repo_committer in pagure with EXTERNAL_COMMITTER
  106. configured to give access to all the provenpackager but for this
  107. one repo
  108. """
  109. repo = pagure.lib._get_project(self.session, 'test')
  110. g = munch.Munch()
  111. g.fas_user = tests.FakeUser()
  112. g.fas_user.groups.append('provenpackager')
  113. g.authenticated = True
  114. with mock.patch('pagure.flask_app.flask.g', g):
  115. output = pagure.utils.is_repo_committer(repo)
  116. self.assertFalse(output)
  117. config = {
  118. 'provenpackager': {
  119. 'restrict': ['test']
  120. }
  121. }
  122. @mock.patch.dict('pagure.config.config', {'EXTERNAL_COMMITTER': config})
  123. def test_is_repo_committer_external_committer_restricted_not_member(self):
  124. """ Test is_repo_committer in pagure with EXTERNAL_COMMITTER
  125. configured to give access the provenpackager just for one repo
  126. """
  127. repo = pagure.lib._get_project(self.session, 'test')
  128. g = munch.Munch()
  129. g.fas_user = tests.FakeUser()
  130. g.authenticated = True
  131. with mock.patch('pagure.flask_app.flask.g', g):
  132. output = pagure.utils.is_repo_committer(repo)
  133. self.assertFalse(output)
  134. @mock.patch.dict('pagure.config.config', {'EXTERNAL_COMMITTER': config})
  135. def test_is_repo_committer_external_committer_restricting_to_one(self):
  136. """ Test is_repo_committer in pagure with EXTERNAL_COMMITTER
  137. configured to give access the provenpackager just for one repo
  138. """
  139. repo = pagure.lib._get_project(self.session, 'test')
  140. g = munch.Munch()
  141. g.fas_user = tests.FakeUser()
  142. g.authenticated = True
  143. g.fas_user.groups.append('provenpackager')
  144. with mock.patch('pagure.flask_app.flask.g', g):
  145. output = pagure.utils.is_repo_committer(repo)
  146. self.assertTrue(output)
  147. @mock.patch.dict('pagure.config.config', {'EXTERNAL_COMMITTER': config})
  148. def test_is_repo_committer_external_committer_restricting_another_one(self):
  149. """ Test is_repo_committer in pagure with EXTERNAL_COMMITTER
  150. configured to give access the provenpackager just for one repo not
  151. this one
  152. """
  153. repo = pagure.lib._get_project(self.session, 'test2')
  154. g = munch.Munch()
  155. g.fas_user = tests.FakeUser()
  156. g.authenticated = True
  157. g.fas_user.groups.append('provenpackager')
  158. with mock.patch('pagure.flask_app.flask.g', g):
  159. output = pagure.utils.is_repo_committer(repo)
  160. self.assertFalse(output)
  161. if __name__ == '__main__':
  162. unittest.main(verbosity=2)