1
0

test_stream_server.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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
  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 # pylint: disable=wrong-import-position
  25. from pagure.exceptions import 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._get_project(self.session, 'test')
  43. self.repo2 = pagure.lib._get_project(self.session, 'test2')
  44. # Disable repo 2's issue tracker and PR tracker
  45. pagure.lib.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.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.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.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 = pss._parse_path('/pagure/issue/1')
  90. self.assertEqual(result, (None, None, 'pagure', 'issue', '1'))
  91. # Pull request for namespaced repo.
  92. result = pss._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 = pss._parse_path('/fork/adamwill/pagure/issue/3')
  96. self.assertEqual(result, ('adamwill', None, 'pagure', 'issue', '3'))
  97. # Issue for forked, namespaced repo.
  98. result = pss._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 = pss._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. PagureEvException,
  107. r"No known object",
  108. pss._parse_path, '/pagure/unexpected/1'
  109. )
  110. # No object ID.
  111. six.assertRaisesRegex(
  112. self,
  113. PagureEvException,
  114. r"No project or object ID",
  115. pss._parse_path, '/pagure/issue'
  116. )
  117. # No repo name. Note: we cannot catch 'namespace but no repo name',
  118. # but that should fail later in pagure.lib.get_project
  119. six.assertRaisesRegex(
  120. self,
  121. PagureEvException,
  122. r"No project or object ID",
  123. pss._parse_path, '/issue/1'
  124. )
  125. # /fork but no user name.
  126. six.assertRaisesRegex(
  127. self,
  128. PagureEvException,
  129. r"no user found!",
  130. pss._parse_path, '/fork/pagure/issue/1'
  131. )
  132. # Too many path components before object type.
  133. six.assertRaisesRegex(
  134. self,
  135. PagureEvException,
  136. r"More path components",
  137. pss._parse_path, '/fork/adamwill/fedora-qa/fedfind/unexpected/issue/1'
  138. )
  139. six.assertRaisesRegex(
  140. self,
  141. PagureEvException,
  142. r"More path components",
  143. pss._parse_path, '/fedora-qa/fedfind/unexpected/issue/1'
  144. )
  145. def test_get_issue(self):
  146. """Tests for _get_issue."""
  147. # Simple case: get the existing issue from the existing repo.
  148. result = pss._get_issue(self.repo, '1')
  149. self.assertEqual(result.id, 1)
  150. # Issue that doesn't exist.
  151. six.assertRaisesRegex(
  152. self,
  153. PagureEvException,
  154. r"Issue '3' not found",
  155. pss._get_issue, self.repo, '3'
  156. )
  157. # Private issue (for now we don't handle auth).
  158. six.assertRaisesRegex(
  159. self,
  160. PagureEvException,
  161. r"issue is private",
  162. pss._get_issue, self.repo, '2'
  163. )
  164. # Issue from a project with no issue tracker.
  165. six.assertRaisesRegex(
  166. self,
  167. PagureEvException,
  168. r"No issue tracker found",
  169. pss._get_issue, self.repo2, '1'
  170. )
  171. def test_get_pull_request(self):
  172. """Tests for _get_pull_request."""
  173. # Simple case: get the existing PR from the existing repo.
  174. result = pss._get_pull_request(self.repo, '3')
  175. self.assertEqual(result.id, 3)
  176. # PR that doesn't exist.
  177. six.assertRaisesRegex(
  178. self,
  179. PagureEvException,
  180. r"Pull-Request '2' not found",
  181. pss._get_pull_request, self.repo, '2'
  182. )
  183. # PR from a project with no PR tracker.
  184. six.assertRaisesRegex(
  185. self,
  186. PagureEvException,
  187. r"No pull-request tracker found",
  188. pss._get_pull_request, self.repo2, '1'
  189. )
  190. def test_get_obj_from_path(self):
  191. """Tests for get_obj_from_path."""
  192. # Simple issue case.
  193. result = pss.get_obj_from_path('/test/issue/1')
  194. self.assertEqual(result.id, 1)
  195. # Simple PR case.
  196. result = pss.get_obj_from_path('/test/pull-request/3')
  197. self.assertEqual(result.id, 3)
  198. # Non-existent repo.
  199. six.assertRaisesRegex(
  200. self,
  201. PagureEvException,
  202. r"Project 'foo' not found",
  203. pss.get_obj_from_path, '/foo/issue/1'
  204. )
  205. # NOTE: we cannot test the 'Invalid object provided' exception
  206. # as it's a backup (current code will never hit it)
  207. if __name__ == '__main__':
  208. unittest.main(verbosity=2)