File: test_stream_server.py

package info (click to toggle)
pagure 5.11.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 15,640 kB
  • sloc: python: 113,281; javascript: 23,100; makefile: 194; sh: 66
file content (280 lines) | stat: -rw-r--r-- 8,237 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
 (c) 2016 - Copyright Red Hat Inc

 Authors:
   Adam Williamson <awilliam@redhat.com>

Tests for the Pagure streaming server.

"""

# obviously this is fine for testing.
# pylint: disable=locally-disabled, protected-access

from __future__ import unicode_literals, absolute_import

import logging
import os
import sys
import unittest

import mock
import six

sys.path.insert(
    0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
)
sys.path.insert(
    0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "../pagure-ev")
)

if six.PY3:
    raise unittest.case.SkipTest("Skipping on python3")

import pagure.lib.query  # pylint: disable=wrong-import-position
from pagure.exceptions import (
    PagureException,
    PagureEvException,
)  # pylint: disable=wrong-import-position
import tests  # pylint: disable=wrong-import-position

# comes from ev-server/
import pagure_stream_server as pss  # pylint: disable=wrong-import-position, import-error

logging.basicConfig(stream=sys.stderr)


class StreamingServerTests(tests.Modeltests):
    """Tests for the streaming server."""

    def setUp(self):
        """Set up the environnment, run before every test."""
        super(StreamingServerTests, self).setUp()

        # Make sure the server uses the existing session
        pss.SESSION = self.session

        # Mock send_email, we never want to send or see emails here.
        self.mailpatcher = mock.patch("pagure.lib.notify.send_email")
        self.mailpatcher.start()

        # Setup projects
        tests.create_projects(self.session)
        self.repo = pagure.lib.query._get_project(self.session, "test")
        self.repo2 = pagure.lib.query._get_project(self.session, "test2")

        # Disable repo 2's issue tracker and PR tracker
        pagure.lib.query.update_project_settings(
            session=self.session,
            repo=self.repo2,
            user="pingou",
            settings={"issue_tracker": False, "pull_requests": False},
        )

        # Create a public issue
        pagure.lib.query.new_issue(
            session=self.session,
            repo=self.repo,
            title="Test issue",
            content="We should work on this",
            user="pingou",
        )

        # Create a private issue
        pagure.lib.query.new_issue(
            session=self.session,
            repo=self.repo,
            title="Private issue #2",
            content="The world can see my porn folder",
            user="pingou",
            private=True,
        )

        # Create a PR
        pagure.lib.query.new_pull_request(
            session=self.session,
            repo_from=self.repo,
            repo_to=self.repo,
            branch_from="feature",
            branch_to="master",
            title="Test PR",
            user="pingou",
        )

    def tearDown(self):
        "Stop the patchers, as well as calling super." ""
        super(StreamingServerTests, self).tearDown()
        self.mailpatcher.stop()

    def test_parse_path(self):
        """Tests for _parse_path."""
        # Result format is: (username, namespace, repo, objtype, objid)
        # Simple case: issue for non-namespaced, non-forked repo.
        result = pagure.utils.parse_path("/pagure/issue/1")
        self.assertEqual(result, (None, None, "pagure", "issue", "1"))

        # Pull request for namespaced repo.
        result = pagure.utils.parse_path("/fedora-qa/fedfind/pull-request/2")
        self.assertEqual(
            result, (None, "fedora-qa", "fedfind", "pull-request", "2")
        )

        # Issue for forked repo.
        result = pagure.utils.parse_path("/fork/adamwill/pagure/issue/3")
        self.assertEqual(result, ("adamwill", None, "pagure", "issue", "3"))

        # Issue for forked, namespaced repo.
        result = pagure.utils.parse_path(
            "/fork/pingou/fedora-qa/fedfind/issue/4"
        )
        self.assertEqual(
            result, ("pingou", "fedora-qa", "fedfind", "issue", "4")
        )

        # Issue for repo named 'pull-request' (yeah, now we're getting tricksy).
        result = pagure.utils.parse_path("/pull-request/issue/5")
        self.assertEqual(result, (None, None, "pull-request", "issue", "5"))

        # Unknown object type.
        six.assertRaisesRegex(
            self,
            PagureException,
            r"No known object",
            pagure.utils.parse_path,
            "/pagure/unexpected/1",
        )

        # No object ID.
        six.assertRaisesRegex(
            self,
            PagureException,
            r"No project or object ID",
            pagure.utils.parse_path,
            "/pagure/issue",
        )

        # No repo name. Note: we cannot catch 'namespace but no repo name',
        # but that should fail later in pagure.lib.query.get_project
        six.assertRaisesRegex(
            self,
            PagureException,
            r"No project or object ID",
            pagure.utils.parse_path,
            "/issue/1",
        )

        # /fork but no user name.
        six.assertRaisesRegex(
            self,
            PagureException,
            r"no user found!",
            pagure.utils.parse_path,
            "/fork/pagure/issue/1",
        )

        # Too many path components before object type.
        six.assertRaisesRegex(
            self,
            PagureException,
            r"More path components",
            pagure.utils.parse_path,
            "/fork/adamwill/fedora-qa/fedfind/unexpected/issue/1",
        )
        six.assertRaisesRegex(
            self,
            PagureException,
            r"More path components",
            pagure.utils.parse_path,
            "/fedora-qa/fedfind/unexpected/issue/1",
        )

    def test_get_issue(self):
        """Tests for _get_issue."""
        # Simple case: get the existing issue from the existing repo.
        result = pss._get_issue(self.repo, "1")
        self.assertEqual(result.id, 1)

        # Issue that doesn't exist.
        six.assertRaisesRegex(
            self,
            PagureEvException,
            r"Issue '3' not found",
            pss._get_issue,
            self.repo,
            "3",
        )

        # Private issue (for now we don't handle auth).
        six.assertRaisesRegex(
            self,
            PagureEvException,
            r"issue is private",
            pss._get_issue,
            self.repo,
            "2",
        )

        # Issue from a project with no issue tracker.
        six.assertRaisesRegex(
            self,
            PagureEvException,
            r"No issue tracker found",
            pss._get_issue,
            self.repo2,
            "1",
        )

    def test_get_pull_request(self):
        """Tests for _get_pull_request."""
        # Simple case: get the existing PR from the existing repo.
        result = pss._get_pull_request(self.repo, "3")
        self.assertEqual(result.id, 3)

        # PR that doesn't exist.
        six.assertRaisesRegex(
            self,
            PagureEvException,
            r"Pull-Request '2' not found",
            pss._get_pull_request,
            self.repo,
            "2",
        )

        # PR from a project with no PR tracker.
        six.assertRaisesRegex(
            self,
            PagureEvException,
            r"No pull-request tracker found",
            pss._get_pull_request,
            self.repo2,
            "1",
        )

    def test_get_obj_from_path(self):
        """Tests for get_obj_from_path."""
        # Simple issue case.
        result = pss.get_obj_from_path("/test/issue/1")
        self.assertEqual(result.id, 1)

        # Simple PR case.
        result = pss.get_obj_from_path("/test/pull-request/3")
        self.assertEqual(result.id, 3)

        # Non-existent repo.
        six.assertRaisesRegex(
            self,
            PagureEvException,
            r"Project 'foo' not found",
            pss.get_obj_from_path,
            "/foo/issue/1",
        )

        # NOTE: we cannot test the 'Invalid object provided' exception
        # as it's a backup (current code will never hit it)


if __name__ == "__main__":
    unittest.main(verbosity=2)