1
0

test_stream_server.py 7.5 KB

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