Blame tests/test_stream_server.py

Adam Williamson 01c1a4
#!/usr/bin/env python
Adam Williamson 01c1a4
# -*- coding: utf-8 -*-
Adam Williamson 01c1a4
Adam Williamson 01c1a4
"""
Adam Williamson 01c1a4
 (c) 2016 - Copyright Red Hat Inc
Adam Williamson 01c1a4
Adam Williamson 01c1a4
 Authors:
Adam Williamson 01c1a4
   Adam Williamson <awilliam@redhat.com></awilliam@redhat.com>
Adam Williamson 01c1a4
Adam Williamson 01c1a4
Tests for the Pagure streaming server.
Adam Williamson 01c1a4
Adam Williamson 01c1a4
"""
Adam Williamson 01c1a4
Adam Williamson 01c1a4
# obviously this is fine for testing.
Adam Williamson 01c1a4
# pylint: disable=locally-disabled, protected-access
Adam Williamson 01c1a4
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard dcf6f6
Adam Williamson 01c1a4
import logging
Adam Williamson 01c1a4
import os
Adam Williamson 01c1a4
import sys
Adam Williamson 01c1a4
import unittest
Adam Williamson 01c1a4
Adam Williamson 01c1a4
import mock
Aurélien Bompard 626417
import six
Adam Williamson 01c1a4
Pierre-Yves Chibon 73d120
sys.path.insert(
Pierre-Yves Chibon 73d120
    0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
Pierre-Yves Chibon 73d120
)
Pierre-Yves Chibon 73d120
sys.path.insert(
Pierre-Yves Chibon 73d120
    0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "../pagure-ev")
Pierre-Yves Chibon 73d120
)
Adam Williamson 01c1a4
Pierre-Yves Chibon 76b0c9
if six.PY3:
Pierre-Yves Chibon 73d120
    raise unittest.case.SkipTest("Skipping on python3")
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
import pagure.lib.query  # pylint: disable=wrong-import-position
Pierre-Yves Chibon 73d120
from pagure.exceptions import (
Pierre-Yves Chibon 73d120
    PagureException,
Pierre-Yves Chibon 73d120
    PagureEvException,
Pierre-Yves Chibon 73d120
)  # pylint: disable=wrong-import-position
Pierre-Yves Chibon 73d120
import tests  # pylint: disable=wrong-import-position
Pierre-Yves Chibon 76b0c9
Adam Williamson 01c1a4
# comes from ev-server/
Pierre-Yves Chibon 73d120
import pagure_stream_server as pss  # pylint: disable=wrong-import-position, import-error
Adam Williamson 01c1a4
Adam Williamson 01c1a4
logging.basicConfig(stream=sys.stderr)
Adam Williamson 01c1a4
Adam Williamson 01c1a4
Adam Williamson 01c1a4
class StreamingServerTests(tests.Modeltests):
Adam Williamson 01c1a4
    """Tests for the streaming server."""
Adam Williamson 01c1a4
Adam Williamson 01c1a4
    def setUp(self):
Adam Williamson 01c1a4
        """Set up the environnment, run before every test."""
Adam Williamson 01c1a4
        super(StreamingServerTests, self).setUp()
Pierre-Yves Chibon b130e5
Pierre-Yves Chibon b130e5
        # Make sure the server uses the existing session
Pierre-Yves Chibon b130e5
        pss.SESSION = self.session
Adam Williamson 01c1a4
Adam Williamson 01c1a4
        # Mock send_email, we never want to send or see emails here.
Pierre-Yves Chibon 73d120
        self.mailpatcher = mock.patch("pagure.lib.notify.send_email")
Adam Williamson 01c1a4
        self.mailpatcher.start()
Adam Williamson 01c1a4
Adam Williamson 01c1a4
        # Setup projects
Adam Williamson 01c1a4
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        self.repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 73d120
        self.repo2 = pagure.lib.query._get_project(self.session, "test2")
Adam Williamson 01c1a4
Adam Williamson 01c1a4
        # Disable repo 2's issue tracker and PR tracker
Pierre-Yves Chibon 930073
        pagure.lib.query.update_project_settings(
Adam Williamson 01c1a4
            session=self.session,
Adam Williamson 01c1a4
            repo=self.repo2,
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            settings={"issue_tracker": False, "pull_requests": False},
Adam Williamson 01c1a4
        )
Adam Williamson 01c1a4
Adam Williamson 01c1a4
        # Create a public issue
Pierre-Yves Chibon 930073
        pagure.lib.query.new_issue(
Adam Williamson 01c1a4
            session=self.session,
Adam Williamson 01c1a4
            repo=self.repo,
Pierre-Yves Chibon 73d120
            title="Test issue",
Pierre-Yves Chibon 73d120
            content="We should work on this",
Pierre-Yves Chibon 73d120
            user="pingou",
Adam Williamson 01c1a4
        )
Adam Williamson 01c1a4
Adam Williamson 01c1a4
        # Create a private issue
Pierre-Yves Chibon 930073
        pagure.lib.query.new_issue(
Adam Williamson 01c1a4
            session=self.session,
Adam Williamson 01c1a4
            repo=self.repo,
Pierre-Yves Chibon 73d120
            title="Private issue #2",
Pierre-Yves Chibon 73d120
            content="The world can see my porn folder",
Pierre-Yves Chibon 73d120
            user="pingou",
Adam Williamson 01c1a4
            private=True,
Adam Williamson 01c1a4
        )
Adam Williamson 01c1a4
Adam Williamson 01c1a4
        # Create a PR
Pierre-Yves Chibon 930073
        pagure.lib.query.new_pull_request(
Adam Williamson 01c1a4
            session=self.session,
Adam Williamson 01c1a4
            repo_from=self.repo,
Adam Williamson 01c1a4
            repo_to=self.repo,
Pierre-Yves Chibon 73d120
            branch_from="feature",
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="Test PR",
Pierre-Yves Chibon 73d120
            user="pingou",
Adam Williamson 01c1a4
        )
Adam Williamson 01c1a4
Adam Williamson 01c1a4
    def tearDown(self):
Pierre-Yves Chibon 73d120
        "Stop the patchers, as well as calling super." ""
Adam Williamson 01c1a4
        super(StreamingServerTests, self).tearDown()
Adam Williamson 01c1a4
        self.mailpatcher.stop()
Adam Williamson 01c1a4
Adam Williamson 01c1a4
    def test_parse_path(self):
Adam Williamson 01c1a4
        """Tests for _parse_path."""
Adam Williamson 01c1a4
        # Result format is: (username, namespace, repo, objtype, objid)
Adam Williamson 01c1a4
        # Simple case: issue for non-namespaced, non-forked repo.
Pierre-Yves Chibon 73d120
        result = pagure.utils.parse_path("/pagure/issue/1")
Pierre-Yves Chibon 73d120
        self.assertEqual(result, (None, None, "pagure", "issue", "1"))
Adam Williamson 01c1a4
Adam Williamson 01c1a4
        # Pull request for namespaced repo.
Pierre-Yves Chibon 73d120
        result = pagure.utils.parse_path("/fedora-qa/fedfind/pull-request/2")
Pierre-Yves Chibon 73d120
        self.assertEqual(
Pierre-Yves Chibon 73d120
            result, (None, "fedora-qa", "fedfind", "pull-request", "2")
Pierre-Yves Chibon 73d120
        )
Adam Williamson 01c1a4
Adam Williamson 01c1a4
        # Issue for forked repo.
Pierre-Yves Chibon 73d120
        result = pagure.utils.parse_path("/fork/adamwill/pagure/issue/3")
Pierre-Yves Chibon 73d120
        self.assertEqual(result, ("adamwill", None, "pagure", "issue", "3"))
Adam Williamson 01c1a4
Adam Williamson 01c1a4
        # Issue for forked, namespaced repo.
Pierre-Yves Chibon 73d120
        result = pagure.utils.parse_path(
Pierre-Yves Chibon 73d120
            "/fork/pingou/fedora-qa/fedfind/issue/4"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(
Pierre-Yves Chibon 73d120
            result, ("pingou", "fedora-qa", "fedfind", "issue", "4")
Pierre-Yves Chibon 73d120
        )
Adam Williamson 01c1a4
Adam Williamson 01c1a4
        # Issue for repo named 'pull-request' (yeah, now we're getting tricksy).
Pierre-Yves Chibon 73d120
        result = pagure.utils.parse_path("/pull-request/issue/5")
Pierre-Yves Chibon 73d120
        self.assertEqual(result, (None, None, "pull-request", "issue", "5"))
Adam Williamson 01c1a4
Adam Williamson 01c1a4
        # Unknown object type.
Aurélien Bompard 626417
        six.assertRaisesRegex(
Aurélien Bompard 626417
            self,
Pierre-Yves Chibon c13fca
            PagureException,
Adam Williamson 01c1a4
            r"No known object",
Pierre-Yves Chibon c13fca
            pagure.utils.parse_path,
Pierre-Yves Chibon 73d120
            "/pagure/unexpected/1",
Adam Williamson 01c1a4
        )
Adam Williamson 01c1a4
Adam Williamson 01c1a4
        # No object ID.
Aurélien Bompard 626417
        six.assertRaisesRegex(
Aurélien Bompard 626417
            self,
Pierre-Yves Chibon c13fca
            PagureException,
Adam Williamson 01c1a4
            r"No project or object ID",
Pierre-Yves Chibon c13fca
            pagure.utils.parse_path,
Pierre-Yves Chibon 73d120
            "/pagure/issue",
Adam Williamson 01c1a4
        )
Adam Williamson 01c1a4
Adam Williamson 01c1a4
        # No repo name. Note: we cannot catch 'namespace but no repo name',
Pierre-Yves Chibon 930073
        # but that should fail later in pagure.lib.query.get_project
Aurélien Bompard 626417
        six.assertRaisesRegex(
Aurélien Bompard 626417
            self,
Pierre-Yves Chibon c13fca
            PagureException,
Adam Williamson 01c1a4
            r"No project or object ID",
Pierre-Yves Chibon c13fca
            pagure.utils.parse_path,
Pierre-Yves Chibon 73d120
            "/issue/1",
Adam Williamson 01c1a4
        )
Adam Williamson 01c1a4
Adam Williamson 01c1a4
        # /fork but no user name.
Aurélien Bompard 626417
        six.assertRaisesRegex(
Aurélien Bompard 626417
            self,
Pierre-Yves Chibon c13fca
            PagureException,
Adam Williamson 01c1a4
            r"no user found!",
Pierre-Yves Chibon c13fca
            pagure.utils.parse_path,
Pierre-Yves Chibon 73d120
            "/fork/pagure/issue/1",
Adam Williamson 01c1a4
        )
Adam Williamson 01c1a4
Adam Williamson 01c1a4
        # Too many path components before object type.
Aurélien Bompard 626417
        six.assertRaisesRegex(
Aurélien Bompard 626417
            self,
Pierre-Yves Chibon c13fca
            PagureException,
Adam Williamson 01c1a4
            r"More path components",
Pierre-Yves Chibon c13fca
            pagure.utils.parse_path,
Pierre-Yves Chibon 73d120
            "/fork/adamwill/fedora-qa/fedfind/unexpected/issue/1",
Adam Williamson 01c1a4
        )
Aurélien Bompard 626417
        six.assertRaisesRegex(
Aurélien Bompard 626417
            self,
Pierre-Yves Chibon c13fca
            PagureException,
Adam Williamson 01c1a4
            r"More path components",
Pierre-Yves Chibon c13fca
            pagure.utils.parse_path,
Pierre-Yves Chibon 73d120
            "/fedora-qa/fedfind/unexpected/issue/1",
Adam Williamson 01c1a4
        )
Adam Williamson 01c1a4
Adam Williamson 01c1a4
    def test_get_issue(self):
Adam Williamson 01c1a4
        """Tests for _get_issue."""
Adam Williamson 01c1a4
        # Simple case: get the existing issue from the existing repo.
Pierre-Yves Chibon 73d120
        result = pss._get_issue(self.repo, "1")
Adam Williamson 01c1a4
        self.assertEqual(result.id, 1)
Adam Williamson 01c1a4
Adam Williamson 01c1a4
        # Issue that doesn't exist.
Aurélien Bompard 626417
        six.assertRaisesRegex(
Aurélien Bompard 626417
            self,
Adam Williamson 01c1a4
            PagureEvException,
Adam Williamson 01c1a4
            r"Issue '3' not found",
Pierre-Yves Chibon 73d120
            pss._get_issue,
Pierre-Yves Chibon 73d120
            self.repo,
Pierre-Yves Chibon 73d120
            "3",
Adam Williamson 01c1a4
        )
Adam Williamson 01c1a4
Adam Williamson 01c1a4
        # Private issue (for now we don't handle auth).
Aurélien Bompard 626417
        six.assertRaisesRegex(
Aurélien Bompard 626417
            self,
Adam Williamson 01c1a4
            PagureEvException,
Adam Williamson 01c1a4
            r"issue is private",
Pierre-Yves Chibon 73d120
            pss._get_issue,
Pierre-Yves Chibon 73d120
            self.repo,
Pierre-Yves Chibon 73d120
            "2",
Adam Williamson 01c1a4
        )
Adam Williamson 01c1a4
Adam Williamson 01c1a4
        # Issue from a project with no issue tracker.
Aurélien Bompard 626417
        six.assertRaisesRegex(
Aurélien Bompard 626417
            self,
Adam Williamson 01c1a4
            PagureEvException,
Adam Williamson 01c1a4
            r"No issue tracker found",
Pierre-Yves Chibon 73d120
            pss._get_issue,
Pierre-Yves Chibon 73d120
            self.repo2,
Pierre-Yves Chibon 73d120
            "1",
Adam Williamson 01c1a4
        )
Adam Williamson 01c1a4
Adam Williamson 01c1a4
    def test_get_pull_request(self):
Adam Williamson 01c1a4
        """Tests for _get_pull_request."""
Adam Williamson 01c1a4
        # Simple case: get the existing PR from the existing repo.
Pierre-Yves Chibon 73d120
        result = pss._get_pull_request(self.repo, "3")
Adam Williamson 01c1a4
        self.assertEqual(result.id, 3)
Adam Williamson 01c1a4
Adam Williamson 01c1a4
        # PR that doesn't exist.
Aurélien Bompard 626417
        six.assertRaisesRegex(
Aurélien Bompard 626417
            self,
Adam Williamson 01c1a4
            PagureEvException,
Adam Williamson 01c1a4
            r"Pull-Request '2' not found",
Pierre-Yves Chibon 73d120
            pss._get_pull_request,
Pierre-Yves Chibon 73d120
            self.repo,
Pierre-Yves Chibon 73d120
            "2",
Adam Williamson 01c1a4
        )
Adam Williamson 01c1a4
Adam Williamson 01c1a4
        # PR from a project with no PR tracker.
Aurélien Bompard 626417
        six.assertRaisesRegex(
Aurélien Bompard 626417
            self,
Adam Williamson 01c1a4
            PagureEvException,
Adam Williamson 01c1a4
            r"No pull-request tracker found",
Pierre-Yves Chibon 73d120
            pss._get_pull_request,
Pierre-Yves Chibon 73d120
            self.repo2,
Pierre-Yves Chibon 73d120
            "1",
Adam Williamson 01c1a4
        )
Adam Williamson 01c1a4
Adam Williamson 01c1a4
    def test_get_obj_from_path(self):
Adam Williamson 01c1a4
        """Tests for get_obj_from_path."""
Adam Williamson 01c1a4
        # Simple issue case.
Pierre-Yves Chibon 73d120
        result = pss.get_obj_from_path("/test/issue/1")
Adam Williamson 01c1a4
        self.assertEqual(result.id, 1)
Adam Williamson 01c1a4
Adam Williamson 01c1a4
        # Simple PR case.
Pierre-Yves Chibon 73d120
        result = pss.get_obj_from_path("/test/pull-request/3")
Adam Williamson 01c1a4
        self.assertEqual(result.id, 3)
Adam Williamson 01c1a4
Adam Williamson 01c1a4
        # Non-existent repo.
Aurélien Bompard 626417
        six.assertRaisesRegex(
Aurélien Bompard 626417
            self,
Adam Williamson 01c1a4
            PagureEvException,
Adam Williamson 01c1a4
            r"Project 'foo' not found",
Pierre-Yves Chibon 73d120
            pss.get_obj_from_path,
Pierre-Yves Chibon 73d120
            "/foo/issue/1",
Adam Williamson 01c1a4
        )
Adam Williamson 01c1a4
Adam Williamson 01c1a4
        # NOTE: we cannot test the 'Invalid object provided' exception
Adam Williamson 01c1a4
        # as it's a backup (current code will never hit it)
Adam Williamson 01c1a4
Pierre-Yves Chibon 393f31
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)