test_stream_server.py 7.5 KB

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