1
0

test_stream_server.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. (c) 2016 - Copyright Red Hat Inc
  5. Authors:
  6. Adam Williamson <awilliam@redhat.com>
  7. Tests for the Pagure streaming server.
  8. """
  9. # obviously this is fine for testing.
  10. # pylint: disable=locally-disabled, protected-access
  11. from __future__ import unicode_literals, absolute_import
  12. import logging
  13. import os
  14. import sys
  15. import unittest
  16. import mock
  17. import six
  18. sys.path.insert(0, os.path.join(os.path.dirname(
  19. os.path.abspath(__file__)), '..'))
  20. sys.path.insert(0, os.path.join(os.path.dirname(
  21. os.path.abspath(__file__)), '../pagure-ev'))
  22. if six.PY3:
  23. raise unittest.case.SkipTest('Skipping on python3')
  24. import pagure.lib.query # pylint: disable=wrong-import-position
  25. from pagure.exceptions import PagureException, PagureEvException # pylint: disable=wrong-import-position
  26. import tests # pylint: disable=wrong-import-position
  27. # comes from ev-server/
  28. import pagure_stream_server as pss # pylint: disable=wrong-import-position, import-error
  29. logging.basicConfig(stream=sys.stderr)
  30. class StreamingServerTests(tests.Modeltests):
  31. """Tests for the streaming server."""
  32. def setUp(self):
  33. """Set up the environnment, run before every test."""
  34. super(StreamingServerTests, self).setUp()
  35. # Make sure the server uses the existing session
  36. pss.SESSION = self.session
  37. # Mock send_email, we never want to send or see emails here.
  38. self.mailpatcher = mock.patch('pagure.lib.notify.send_email')
  39. self.mailpatcher.start()
  40. # Setup projects
  41. tests.create_projects(self.session)
  42. self.repo = pagure.lib.query._get_project(self.session, 'test')
  43. self.repo2 = pagure.lib.query._get_project(self.session, 'test2')
  44. # Disable repo 2's issue tracker and PR tracker
  45. pagure.lib.query.update_project_settings(
  46. session=self.session,
  47. repo=self.repo2,
  48. user='pingou',
  49. settings={
  50. 'issue_tracker': False,
  51. 'pull_requests': False,
  52. }
  53. )
  54. # Create a public issue
  55. pagure.lib.query.new_issue(
  56. session=self.session,
  57. repo=self.repo,
  58. title='Test issue',
  59. content='We should work on this',
  60. user='pingou',
  61. )
  62. # Create a private issue
  63. pagure.lib.query.new_issue(
  64. session=self.session,
  65. repo=self.repo,
  66. title='Private issue #2',
  67. content='The world can see my porn folder',
  68. user='pingou',
  69. private=True,
  70. )
  71. # Create a PR
  72. pagure.lib.query.new_pull_request(
  73. session=self.session,
  74. repo_from=self.repo,
  75. repo_to=self.repo,
  76. branch_from='feature',
  77. branch_to='master',
  78. title='Test PR',
  79. user='pingou',
  80. )
  81. def tearDown(self):
  82. "Stop the patchers, as well as calling super."""
  83. super(StreamingServerTests, self).tearDown()
  84. self.mailpatcher.stop()
  85. def test_parse_path(self):
  86. """Tests for _parse_path."""
  87. # Result format is: (username, namespace, repo, objtype, objid)
  88. # Simple case: issue for non-namespaced, non-forked repo.
  89. result = pagure.utils.parse_path('/pagure/issue/1')
  90. self.assertEqual(result, (None, None, 'pagure', 'issue', '1'))
  91. # Pull request for namespaced repo.
  92. result = pagure.utils.parse_path('/fedora-qa/fedfind/pull-request/2')
  93. self.assertEqual(result, (None, 'fedora-qa', 'fedfind', 'pull-request', '2'))
  94. # Issue for forked repo.
  95. result = pagure.utils.parse_path('/fork/adamwill/pagure/issue/3')
  96. self.assertEqual(result, ('adamwill', None, 'pagure', 'issue', '3'))
  97. # Issue for forked, namespaced repo.
  98. result = pagure.utils.parse_path('/fork/pingou/fedora-qa/fedfind/issue/4')
  99. self.assertEqual(result, ('pingou', 'fedora-qa', 'fedfind', 'issue', '4'))
  100. # Issue for repo named 'pull-request' (yeah, now we're getting tricksy).
  101. result = pagure.utils.parse_path('/pull-request/issue/5')
  102. self.assertEqual(result, (None, None, 'pull-request', 'issue', '5'))
  103. # Unknown object type.
  104. six.assertRaisesRegex(
  105. self,
  106. PagureException,
  107. r"No known object",
  108. pagure.utils.parse_path,
  109. '/pagure/unexpected/1'
  110. )
  111. # No object ID.
  112. six.assertRaisesRegex(
  113. self,
  114. PagureException,
  115. r"No project or object ID",
  116. pagure.utils.parse_path,
  117. '/pagure/issue'
  118. )
  119. # No repo name. Note: we cannot catch 'namespace but no repo name',
  120. # but that should fail later in pagure.lib.query.get_project
  121. six.assertRaisesRegex(
  122. self,
  123. PagureException,
  124. r"No project or object ID",
  125. pagure.utils.parse_path,
  126. '/issue/1'
  127. )
  128. # /fork but no user name.
  129. six.assertRaisesRegex(
  130. self,
  131. PagureException,
  132. r"no user found!",
  133. pagure.utils.parse_path,
  134. '/fork/pagure/issue/1'
  135. )
  136. # Too many path components before object type.
  137. six.assertRaisesRegex(
  138. self,
  139. PagureException,
  140. r"More path components",
  141. pagure.utils.parse_path,
  142. '/fork/adamwill/fedora-qa/fedfind/unexpected/issue/1'
  143. )
  144. six.assertRaisesRegex(
  145. self,
  146. PagureException,
  147. r"More path components",
  148. pagure.utils.parse_path,
  149. '/fedora-qa/fedfind/unexpected/issue/1'
  150. )
  151. def test_get_issue(self):
  152. """Tests for _get_issue."""
  153. # Simple case: get the existing issue from the existing repo.
  154. result = pss._get_issue(self.repo, '1')
  155. self.assertEqual(result.id, 1)
  156. # Issue that doesn't exist.
  157. six.assertRaisesRegex(
  158. self,
  159. PagureEvException,
  160. r"Issue '3' not found",
  161. pss._get_issue, self.repo, '3'
  162. )
  163. # Private issue (for now we don't handle auth).
  164. six.assertRaisesRegex(
  165. self,
  166. PagureEvException,
  167. r"issue is private",
  168. pss._get_issue, self.repo, '2'
  169. )
  170. # Issue from a project with no issue tracker.
  171. six.assertRaisesRegex(
  172. self,
  173. PagureEvException,
  174. r"No issue tracker found",
  175. pss._get_issue, self.repo2, '1'
  176. )
  177. def test_get_pull_request(self):
  178. """Tests for _get_pull_request."""
  179. # Simple case: get the existing PR from the existing repo.
  180. result = pss._get_pull_request(self.repo, '3')
  181. self.assertEqual(result.id, 3)
  182. # PR that doesn't exist.
  183. six.assertRaisesRegex(
  184. self,
  185. PagureEvException,
  186. r"Pull-Request '2' not found",
  187. pss._get_pull_request, self.repo, '2'
  188. )
  189. # PR from a project with no PR tracker.
  190. six.assertRaisesRegex(
  191. self,
  192. PagureEvException,
  193. r"No pull-request tracker found",
  194. pss._get_pull_request, self.repo2, '1'
  195. )
  196. def test_get_obj_from_path(self):
  197. """Tests for get_obj_from_path."""
  198. # Simple issue case.
  199. result = pss.get_obj_from_path('/test/issue/1')
  200. self.assertEqual(result.id, 1)
  201. # Simple PR case.
  202. result = pss.get_obj_from_path('/test/pull-request/3')
  203. self.assertEqual(result.id, 3)
  204. # Non-existent repo.
  205. six.assertRaisesRegex(
  206. self,
  207. PagureEvException,
  208. r"Project 'foo' not found",
  209. pss.get_obj_from_path, '/foo/issue/1'
  210. )
  211. # NOTE: we cannot test the 'Invalid object provided' exception
  212. # as it's a backup (current code will never hit it)
  213. if __name__ == '__main__':
  214. unittest.main(verbosity=2)