Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

5.8 KB
Raw Blame
from urllib.parse import urlencode

import orjson
from typing_extensions import override

from zerver.lib.test_classes import WebhookTestCase


class TravisHookTests(WebhookTestCase):
    NON_PR_TOPIC = "travis-test / main"
    PR_TOPIC = "travis-test / PR #2 feat: Add Name.txt."

    EXPECTED_NON_PULL_REQUEST_MESSAGE = (
        ":time_ticking: Build [#1](https://app.travis-ci.com/sathwikshetty33/travis-test/builds/277712204) "
        "**is in progress** for commit [Add Projects model.](https://github.com/sathwikshetty33/travis-test/commit/193da1b72346) by sathwikshetty33"
    )

    EXPECTED_PULL_REQUEST_MESSAGE = (
        ":warning: Build [#42](https://app.travis-ci.com/sathwikshetty33/travis-test/builds/277927122) "
        "**failed** for commit [feat: Created Name.txt.](https://github.com/sathwikshetty33/travis-test/pull/2) by Sathwik Shetty."
    )

    def test_travis_message(self) -> None:
        """
        Build notifications are generated by Travis after build completes.

        The subject describes the repo and Stash "project". The
        content describes the commits pushed.
        """

        self.check_webhook(
            "non_pull_request",
            self.NON_PR_TOPIC,
            f"{self.EXPECTED_NON_PULL_REQUEST_MESSAGE}.",
            content_type="application/x-www-form-urlencoded",
        )

    def test_travis_only_pull_request_event(self) -> None:
        self.url = f'{self.build_webhook_url()}&only_events=["pull_request"]'

        self.check_webhook(
            "pull_request",
            self.PR_TOPIC,
            self.EXPECTED_PULL_REQUEST_MESSAGE,
            content_type="application/x-www-form-urlencoded",
        )

        self.check_webhook(
            "non_pull_request",
            content_type="application/x-www-form-urlencoded",
            expect_noop=True,
        )

    def test_travis_exclude_pull_request_event(self) -> None:
        self.url = f'{self.build_webhook_url()}&exclude_events=["pull_request"]'

        self.check_webhook(
            "pull_request",
            content_type="application/x-www-form-urlencoded",
            expect_noop=True,
        )

        self.check_webhook(
            "non_pull_request",
            self.NON_PR_TOPIC,
            f"{self.EXPECTED_NON_PULL_REQUEST_MESSAGE}.",
            content_type="application/x-www-form-urlencoded",
        )

    def test_travis_only_push_event(self) -> None:
        self.url = f'{self.build_webhook_url()}&only_events=["push"]'

        self.check_webhook(
            "non_pull_request",
            self.NON_PR_TOPIC,
            f"{self.EXPECTED_NON_PULL_REQUEST_MESSAGE}.",
            content_type="application/x-www-form-urlencoded",
        )

        self.check_webhook(
            "pull_request",
            content_type="application/x-www-form-urlencoded",
            expect_noop=True,
        )

    def test_travis_exclude_push_event(self) -> None:
        self.url = f'{self.build_webhook_url()}&exclude_events=["push"]'

        self.check_webhook(
            "non_pull_request",
            content_type="application/x-www-form-urlencoded",
            expect_noop=True,
        )

        self.check_webhook(
            "pull_request",
            self.PR_TOPIC,
            self.EXPECTED_PULL_REQUEST_MESSAGE,
            content_type="application/x-www-form-urlencoded",
        )

    def test_travis_include_glob_events(self) -> None:
        self.url = f'{self.build_webhook_url()}&include_events=["*"]'

        self.check_webhook(
            "pull_request",
            self.PR_TOPIC,
            self.EXPECTED_PULL_REQUEST_MESSAGE,
            content_type="application/x-www-form-urlencoded",
        )

        self.check_webhook(
            "non_pull_request",
            self.NON_PR_TOPIC,
            f"{self.EXPECTED_NON_PULL_REQUEST_MESSAGE}.",
            content_type="application/x-www-form-urlencoded",
        )

    def test_travis_exclude_glob_events(self) -> None:
        self.url = f'{self.build_webhook_url()}&exclude_events=["*"]'

        self.check_webhook(
            "pull_request",
            content_type="application/x-www-form-urlencoded",
            expect_noop=True,
        )

        self.check_webhook(
            "non_pull_request",
            content_type="application/x-www-form-urlencoded",
            expect_noop=True,
        )

    def test_travis_noop(self) -> None:
        expected_error_message = """
While no message is expected given expect_noop=True,
your test code triggered an endpoint that did write
one or more new messages.
        """.strip()

        with self.assertRaises(Exception) as exc:
            self.check_webhook(
                "non_pull_request",
                content_type="application/x-www-form-urlencoded",
                expect_noop=True,
            )
        self.assertEqual(str(exc.exception), expected_error_message)

    @override
    def get_body(self, fixture_name: str) -> str:
        if fixture_name in {"api", "cron"}:
            payload = orjson.loads(
                self.webhook_fixture_data("travis", "non_pull_request", file_type="json")
            )
            payload["type"] = fixture_name
            return urlencode({"payload": orjson.dumps(payload).decode()})

        return urlencode(
            {"payload": self.webhook_fixture_data("travis", fixture_name, file_type="json")}
        )

    def test_travis_api_event(self) -> None:
        self.check_webhook(
            "api",
            self.NON_PR_TOPIC,
            f"{self.EXPECTED_NON_PULL_REQUEST_MESSAGE} (triggered by api).",
            content_type="application/x-www-form-urlencoded",
        )

    def test_travis_cron_event(self) -> None:
        self.check_webhook(
            "cron",
            self.NON_PR_TOPIC,
            f"{self.EXPECTED_NON_PULL_REQUEST_MESSAGE} (triggered by cron).",
            content_type="application/x-www-form-urlencoded",
        )