Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
webhook: Add expect_noop option for check_webhook.
PIG20883ea6c37c57f
diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.pyindex 4844dbfebde..036b3f6601d 100644--- a/zerver/lib/test_classes.py+++ b/zerver/lib/test_classes.py@@ -105,6 +105,10 @@ if settings.ZILENCER_ENABLED:from zilencer.models import get_remote_server_by_uuid+class EmptyResponseError(Exception):+ pass++class UploadSerializeMixin(SerializeMixin):"""We cannot use override_settings to change upload directory because@@ -1068,7 +1072,7 @@ Output:msg = self.get_last_message()if msg.id == prior_msg.id:- raise Exception(+ raise EmptyResponseError("""Your test code called an endpoint that didnot write any new messages. It is probably@@ -1454,9 +1458,10 @@ You can fix this by adding "{complete_event_type}" to ALL_EVENT_TYPES for this wdef check_webhook(self,fixture_name: str,- expected_topic: str,- expected_message: str,+ expected_topic: Optional[str] = None,+ expected_message: Optional[str] = None,content_type: Optional[str] = "application/json",+ expect_noop: Optional[bool] = False,**kwargs: Any,) -> None:"""@@ -1476,6 +1481,8 @@ You can fix this by adding "{complete_event_type}" to ALL_EVENT_TYPES for this wFor the rare cases of webhooks actually sending private messages,see send_and_test_private_message.++ When no message is expected to be sent, set `expect_noop` to True."""assert self.STREAM_NAME is not Noneself.subscribe(self.test_user, self.STREAM_NAME)@@ -1487,13 +1494,30 @@ You can fix this by adding "{complete_event_type}" to ALL_EVENT_TYPES for this wheaders = get_fixture_http_headers(self.WEBHOOK_DIR_NAME, fixture_name)headers = standardize_headers(headers)kwargs.update(headers)+ try:+ msg = self.send_webhook_payload(+ self.test_user,+ self.url,+ payload,+ **kwargs,+ )+ except EmptyResponseError:+ if expect_noop:+ return+ else:+ raise AssertionError(+ "No message was sent. Pass expect_noop=True if this is intentional."+ )- msg = self.send_webhook_payload(- self.test_user,- self.url,- payload,- **kwargs,- )+ if expect_noop:+ raise Exception(+ """+While no message is expected given expect_noop=True,+your test code triggered an endpoint that did write+one or more new messages.+""".strip()+ )+ assert expected_message is not None and expected_topic is not Noneself.assert_stream_message(message=msg,diff --git a/zerver/webhooks/bitbucket2/tests.py b/zerver/webhooks/bitbucket2/tests.pyindex d677c399123..269a37e51b1 100644--- a/zerver/webhooks/bitbucket2/tests.py+++ b/zerver/webhooks/bitbucket2/tests.py@@ -1,3 +1,4 @@+from typing import Any, Dictfrom unittest.mock import MagicMock, patchfrom zerver.lib.test_classes import WebhookTestCase@@ -116,7 +117,7 @@ class Bitbucket2HookTests(WebhookTestCase):def test_bitbucket2_on_pull_request_created_event(self) -> None:expected_message = "Tomasz created [PR #1](https://bitbucket.org/kolaszek/repository-name/pull-requests/1) (assigned to Tomasz Kolek) from `new-branch` to `master`:\n\n~~~ quote\ndescription\n~~~"- kwargs = {+ kwargs: Dict[str, Any] = {"HTTP_X_EVENT_KEY": "pullrequest:created",}self.check_webhook(@@ -125,7 +126,7 @@ class Bitbucket2HookTests(WebhookTestCase):def test_bitbucket2_on_pull_request_created_without_reviewer_username_event(self) -> None:expected_message = "Tomasz created [PR #1](https://bitbucket.org/kolaszek/repository-name/pull-requests/1) (assigned to Tomasz Kolek) from `new-branch` to `master`:\n\n~~~ quote\ndescription\n~~~"- kwargs = {+ kwargs: Dict[str, Any] = {"HTTP_X_EVENT_KEY": "pullrequest:created",}self.check_webhook(@@ -139,7 +140,7 @@ class Bitbucket2HookTests(WebhookTestCase):self.url = self.build_webhook_url(topic="notifications")expected_topic = "notifications"expected_message = "Tomasz created [PR #1 new commit](https://bitbucket.org/kolaszek/repository-name/pull-requests/1) (assigned to Tomasz Kolek) from `new-branch` to `master`:\n\n~~~ quote\ndescription\n~~~"- kwargs = {+ kwargs: Dict[str, Any] = {"HTTP_X_EVENT_KEY": "pullrequest:created",}self.check_webhook(@@ -148,7 +149,7 @@ class Bitbucket2HookTests(WebhookTestCase):def test_bitbucket2_on_pull_request_updated_event(self) -> None:expected_message = "Tomasz updated [PR #1](https://bitbucket.org/kolaszek/repository-name/pull-requests/1) (assigned to Tomasz Kolek) from `new-branch` to `master`:\n\n~~~ quote\ndescription\n~~~"- kwargs = {+ kwargs: Dict[str, Any] = {"HTTP_X_EVENT_KEY": "pullrequest:updated",}self.check_webhook(@@ -157,7 +158,7 @@ class Bitbucket2HookTests(WebhookTestCase):def test_bitbucket2_on_pull_request_approved_event(self) -> None:expected_message = "Tomasz approved [PR #1](https://bitbucket.org/kolaszek/repository-name/pull-requests/1)."- kwargs = {+ kwargs: Dict[str, Any] = {"HTTP_X_EVENT_KEY": "pullrequest:approved",}self.check_webhook(@@ -168,7 +169,7 @@ class Bitbucket2HookTests(WebhookTestCase):self.url = self.build_webhook_url(topic="notifications")expected_topic = "notifications"expected_message = "Tomasz approved [PR #1 new commit](https://bitbucket.org/kolaszek/repository-name/pull-requests/1)."- kwargs = {+ kwargs: Dict[str, Any] = {"HTTP_X_EVENT_KEY": "pullrequest:approved",}self.check_webhook(@@ -177,7 +178,7 @@ class Bitbucket2HookTests(WebhookTestCase):def test_bitbucket2_on_pull_request_unapproved_event(self) -> None:expected_message = "Tomasz unapproved [PR #1](https://bitbucket.org/kolaszek/repository-name/pull-requests/1)."- kwargs = {+ kwargs: Dict[str, Any] = {"HTTP_X_EVENT_KEY": "pullrequest:unapproved",}self.check_webhook(@@ -186,7 +187,7 @@ class Bitbucket2HookTests(WebhookTestCase):def test_bitbucket2_on_pull_request_declined_event(self) -> None:expected_message = "Tomasz rejected [PR #1](https://bitbucket.org/kolaszek/repository-name/pull-requests/1)."- kwargs = {+ kwargs: Dict[str, Any] = {"HTTP_X_EVENT_KEY": "pullrequest:rejected",}self.check_webhook(@@ -197,7 +198,7 @@ class Bitbucket2HookTests(WebhookTestCase):expected_message = ("Tomasz merged [PR #1](https://bitbucket.org/kolaszek/repository-name/pull-requests/1).")- kwargs = {+ kwargs: Dict[str, Any] = {"HTTP_X_EVENT_KEY": "pullrequest:fulfilled",}self.check_webhook(@@ -206,7 +207,7 @@ class Bitbucket2HookTests(WebhookTestCase):def test_bitbucket2_on_pull_request_comment_created_event(self) -> None:expected_message = "Tomasz [commented](https://bitbucket.org/kolaszek/repository-name/pull-requests/3/_/diff#comment-20576503) on [PR #1](https://bitbucket.org/kolaszek/repository-name/pull-requests/3):\n\n~~~ quote\nComment1\n~~~"- kwargs = {+ kwargs: Dict[str, Any] = {"HTTP_X_EVENT_KEY": "pullrequest:comment_created",}self.check_webhook(@@ -217,7 +218,7 @@ class Bitbucket2HookTests(WebhookTestCase):self.url = self.build_webhook_url(topic="notifications")expected_topic = "notifications"expected_message = "Tomasz [commented](https://bitbucket.org/kolaszek/repository-name/pull-requests/3/_/diff#comment-20576503) on [PR #1 new commit](https://bitbucket.org/kolaszek/repository-name/pull-requests/3):\n\n~~~ quote\nComment1\n~~~"- kwargs = {+ kwargs: Dict[str, Any] = {"HTTP_X_EVENT_KEY": "pullrequest:comment_created",}self.check_webhook(@@ -226,7 +227,7 @@ class Bitbucket2HookTests(WebhookTestCase):def test_bitbucket2_on_pull_request_comment_updated_event(self) -> None:expected_message = "Tomasz updated a [comment](https://bitbucket.org/kolaszek/repository-name/pull-requests/3/_/diff#comment-20576503) on [PR #1](https://bitbucket.org/kolaszek/repository-name/pull-requests/3):\n\n~~~ quote\nComment1\n~~~"- kwargs = {+ kwargs: Dict[str, Any] = {"HTTP_X_EVENT_KEY": "pullrequest:comment_updated",}self.check_webhook(@@ -237,7 +238,7 @@ class Bitbucket2HookTests(WebhookTestCase):self.url = self.build_webhook_url(topic="notifications")expected_topic = "notifications"expected_message = "Tomasz updated a [comment](https://bitbucket.org/kolaszek/repository-name/pull-requests/3/_/diff#comment-20576503) on [PR #1 new commit](https://bitbucket.org/kolaszek/repository-name/pull-requests/3):\n\n~~~ quote\nComment1\n~~~"- kwargs = {+ kwargs: Dict[str, Any] = {"HTTP_X_EVENT_KEY": "pullrequest:comment_updated",}self.check_webhook(@@ -246,7 +247,7 @@ class Bitbucket2HookTests(WebhookTestCase):def test_bitbucket2_on_pull_request_comment_deleted_event(self) -> None:expected_message = "Tomasz deleted a [comment](https://bitbucket.org/kolaszek/repository-name/pull-requests/3/_/diff#comment-20576503) on [PR #1](https://bitbucket.org/kolaszek/repository-name/pull-requests/3):\n\n~~~ quote\nComment1\n~~~"- kwargs = {+ kwargs: Dict[str, Any] = {"HTTP_X_EVENT_KEY": "pullrequest:comment_deleted",}self.check_webhook(@@ -256,14 +257,14 @@ class Bitbucket2HookTests(WebhookTestCase):def test_bitbucket2_on_repo_updated_event(self) -> None:expected_message = "eeshangarg changed the website of the **new-name** repo to **http://zulipchat.com**.\neeshangarg changed the name of the **new-name** repo from **test-repo** to **new-name**.\neeshangarg changed the language of the **new-name** repo to **python**.\neeshangarg changed the full name of the **new-name** repo from **webhooktest/test-repo** to **webhooktest/new-name**.\neeshangarg changed the description of the **new-name** repo to **Random description.**"expected_topic = "new-name"- kwargs = {"HTTP_X_EVENT_KEY": "repo:updated"}+ kwargs: Dict[str, Any] = {"HTTP_X_EVENT_KEY": "repo:updated"}self.check_webhook("repo_updated", expected_topic, expected_message, **kwargs)def test_bitbucket2_on_push_one_tag_event(self) -> None:expected_message = ("Tomasz pushed tag [a](https://bitbucket.org/kolaszek/repository-name/commits/tag/a).")- kwargs = {+ kwargs: Dict[str, Any] = {"HTTP_X_EVENT_KEY": "pullrequest:push",}self.check_webhook("push_one_tag", TOPIC, expected_message, **kwargs)@@ -272,14 +273,14 @@ class Bitbucket2HookTests(WebhookTestCase):expected_message = ("Tomasz removed tag [a](https://bitbucket.org/kolaszek/repository-name/commits/tag/a).")- kwargs = {+ kwargs: Dict[str, Any] = {"HTTP_X_EVENT_KEY": "pullrequest:push",}self.check_webhook("push_remove_tag", TOPIC, expected_message, **kwargs)def test_bitbucket2_on_push_more_than_one_tag_event(self) -> None:expected_message = "Tomasz pushed tag [{name}](https://bitbucket.org/kolaszek/repository-name/commits/tag/{name})."- kwargs = {+ kwargs: Dict[str, Any] = {"HTTP_X_EVENT_KEY": "pullrequest:push",}@@ -311,7 +312,7 @@ class Bitbucket2HookTests(WebhookTestCase):)def test_bitbucket2_on_more_than_one_push_event(self) -> None:- kwargs = {+ kwargs: Dict[str, Any] = {"HTTP_X_EVENT_KEY": "pullrequest:push",}@@ -344,7 +345,7 @@ class Bitbucket2HookTests(WebhookTestCase):def test_bitbucket2_on_more_than_one_push_event_filtered_by_branches(self) -> None:self.url = self.build_webhook_url(branches="master,development")- kwargs = {+ kwargs: Dict[str, Any] = {"HTTP_X_EVENT_KEY": "pullrequest:push",}@@ -377,7 +378,7 @@ class Bitbucket2HookTests(WebhookTestCase):def test_bitbucket2_on_more_than_one_push_event_filtered_by_branches_ignore(self) -> None:self.url = self.build_webhook_url(branches="changes,development")- kwargs = {+ kwargs: Dict[str, Any] = {"HTTP_X_EVENT_KEY": "pullrequest:push",}expected_message = (diff --git a/zerver/webhooks/travis/tests.py b/zerver/webhooks/travis/tests.pyindex 0031da01276..7ff58f9a6c1 100644--- a/zerver/webhooks/travis/tests.py+++ b/zerver/webhooks/travis/tests.py@@ -31,15 +31,9 @@ Details: [changes](https://github.com/hl7-fhir/fhir-svn/compare/6dccb98bcfd9...6)def test_ignore_travis_pull_request_by_default(self) -> None:- self.subscribe(self.test_user, self.STREAM_NAME)- result = self.client_post(- self.url,- self.get_body("pull_request"),- content_type="application/x-www-form-urlencoded",+ self.check_webhook(+ "pull_request", content_type="application/x-www-form-urlencoded", expect_noop=True)- self.assert_json_success(result)- msg = self.get_last_message()- self.assertNotEqual(msg.topic_name(), self.TOPIC)def test_travis_pull_requests_are_not_ignored_when_applicable(self) -> None:self.url = f"{self.build_webhook_url()}&ignore_pull_requests=false"@@ -52,7 +46,6 @@ Details: [changes](https://github.com/hl7-fhir/fhir-svn/compare/6dccb98bcfd9...6)def test_travis_only_push_event(self) -> None:- self.subscribe(self.test_user, self.STREAM_NAME)self.url = f'{self.build_webhook_url()}&only_events=["push"]'self.check_webhook(@@ -63,33 +56,24 @@ Details: [changes](https://github.com/hl7-fhir/fhir-svn/compare/6dccb98bcfd9...6)def test_travis_only_push_event_not_sent(self) -> None:- self.subscribe(self.test_user, self.STREAM_NAME)self.url = f'{self.build_webhook_url()}&only_events=["push"]&ignore_pull_requests=false'- result = self.client_post(- self.url,- self.get_body("pull_request"),+ self.check_webhook(+ "pull_request",content_type="application/x-www-form-urlencoded",+ expect_noop=True,)- self.assert_json_success(result)- msg = self.get_last_message()- self.assertNotEqual(msg.topic_name(), self.TOPIC)def test_travis_exclude_push_event(self) -> None:- self.subscribe(self.test_user, self.STREAM_NAME)self.url = f'{self.build_webhook_url()}&exclude_events=["push"]'- result = self.client_post(- self.url,- self.get_body("build"),+ self.check_webhook(+ "build",content_type="application/x-www-form-urlencoded",+ expect_noop=True,)- self.assert_json_success(result)- msg = self.get_last_message()- self.assertNotEqual(msg.topic_name(), self.TOPIC)def test_travis_exlude_push_event_sent(self) -> None:- self.subscribe(self.test_user, self.STREAM_NAME)self.url = f'{self.build_webhook_url()}&exclude_events=["push"]&ignore_pull_requests=false'self.check_webhook(@@ -100,7 +84,6 @@ Details: [changes](https://github.com/hl7-fhir/fhir-svn/compare/6dccb98bcfd9...6)def test_travis_include_glob_events(self) -> None:- self.subscribe(self.test_user, self.STREAM_NAME)self.url = f'{self.build_webhook_url()}&include_events=["*"]&ignore_pull_requests=false'self.check_webhook(@@ -118,25 +101,19 @@ Details: [changes](https://github.com/hl7-fhir/fhir-svn/compare/6dccb98bcfd9...6)def test_travis_exclude_glob_events(self) -> None:- self.subscribe(self.test_user, self.STREAM_NAME)self.url = f'{self.build_webhook_url()}&exclude_events=["*"]&ignore_pull_requests=false'- result = self.client_post(- self.url,- self.get_body("pull_request"),+ self.check_webhook(+ "pull_request",content_type="application/x-www-form-urlencoded",+ expect_noop=True,)- self.assert_json_success(result)- result = self.client_post(- self.url,- self.get_body("build"),+ self.check_webhook(+ "build",content_type="application/x-www-form-urlencoded",+ expect_noop=True,)- self.assert_json_success(result)-- msg = self.get_last_message()- self.assertNotEqual(msg.topic_name(), self.TOPIC)def test_travis_invalid_event(self) -> None:payload = self.get_body("build")@@ -157,6 +134,19 @@ You can fix this by adding "invalid_event" to ALL_EVENT_TYPES for this webhook.)self.assertIn(expected_error_messsage, m.output[0])+ 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(+ "build", content_type="application/x-www-form-urlencoded", expect_noop=True+ )+ self.assertEqual(str(exc.exception), expected_error_message)+def get_body(self, fixture_name: str) -> str:return urllib.parse.urlencode({"payload": self.webhook_fixture_data("travis", fixture_name, file_type="json")}