Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
webhook tests: Extract assert_stream_message.
Steve Howellf74aa29a1c63
diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.pyindex 3966c44a444..4d03db07793 100644--- a/zerver/lib/test_classes.py+++ b/zerver/lib/test_classes.py@@ -1075,6 +1075,17 @@ class WebhookTestCase(ZulipTestCase):return msg+ def assert_stream_message(+ self,+ message: Message,+ stream_name: str,+ topic_name: str,+ content: str,+ ) -> None:+ self.assertEqual(get_display_recipient(message.recipient), stream_name)+ self.assertEqual(message.topic_name(), topic_name)+ self.assertEqual(message.content, content)+def send_and_test_private_message(self,fixture_name: str,diff --git a/zerver/webhooks/bitbucket2/tests.py b/zerver/webhooks/bitbucket2/tests.pyindex dbc9d0e95f8..4663c47d7d0 100644--- a/zerver/webhooks/bitbucket2/tests.py+++ b/zerver/webhooks/bitbucket2/tests.py@@ -273,24 +273,44 @@ class Bitbucket2HookTests(WebhookTestCase):"HTTP_X_EVENT_KEY": 'pullrequest:push',}self.check_webhook("push_more_than_one_tag", **kwargs)- msg = self.get_last_message()- self.do_test_topic(msg, TOPIC)- self.do_test_message(msg, expected_message.format(name='b'))+msg = self.get_second_to_last_message()- self.do_test_topic(msg, TOPIC)- self.do_test_message(msg, expected_message.format(name='a'))+ self.assert_stream_message(+ message=msg,+ stream_name=self.STREAM_NAME,+ topic_name=TOPIC,+ content=expected_message.format(name="a"),+ )++ msg = self.get_last_message()+ self.assert_stream_message(+ message=msg,+ stream_name=self.STREAM_NAME,+ topic_name=TOPIC,+ content=expected_message.format(name="b"),+ )def test_bitbucket2_on_more_than_one_push_event(self) -> None:kwargs = {"HTTP_X_EVENT_KEY": 'pullrequest:push',}self.check_webhook("more_than_one_push_event", **kwargs)+msg = self.get_second_to_last_message()- self.do_test_message(msg, 'kolaszek [pushed](https://bitbucket.org/kolaszek/repository-name/branch/master) 1 commit to branch master.\n\n* first commit ([84b96ad](https://bitbucket.org/kolaszek/repository-name/commits/84b96adc644a30fd6465b3d196369d880762afed))')- self.do_test_topic(msg, TOPIC_BRANCH_EVENTS)+ self.assert_stream_message(+ message=msg,+ stream_name=self.STREAM_NAME,+ topic_name=TOPIC_BRANCH_EVENTS,+ content="kolaszek [pushed](https://bitbucket.org/kolaszek/repository-name/branch/master) 1 commit to branch master.\n\n* first commit ([84b96ad](https://bitbucket.org/kolaszek/repository-name/commits/84b96adc644a30fd6465b3d196369d880762afed))"+ )+msg = self.get_last_message()- self.do_test_message(msg, 'kolaszek pushed tag [a](https://bitbucket.org/kolaszek/repository-name/commits/tag/a).')- self.do_test_topic(msg, TOPIC)+ self.assert_stream_message(+ message=msg,+ stream_name=self.STREAM_NAME,+ topic_name=TOPIC,+ content="kolaszek pushed tag [a](https://bitbucket.org/kolaszek/repository-name/commits/tag/a).",+ )def test_bitbucket2_on_more_than_one_push_event_filtered_by_branches(self) -> None:self.url = self.build_webhook_url(branches='master,development')@@ -298,12 +318,22 @@ class Bitbucket2HookTests(WebhookTestCase):"HTTP_X_EVENT_KEY": 'pullrequest:push',}self.check_webhook("more_than_one_push_event", **kwargs)+msg = self.get_second_to_last_message()- self.do_test_message(msg, 'kolaszek [pushed](https://bitbucket.org/kolaszek/repository-name/branch/master) 1 commit to branch master.\n\n* first commit ([84b96ad](https://bitbucket.org/kolaszek/repository-name/commits/84b96adc644a30fd6465b3d196369d880762afed))')- self.do_test_topic(msg, TOPIC_BRANCH_EVENTS)+ self.assert_stream_message(+ message=msg,+ stream_name=self.STREAM_NAME,+ topic_name=TOPIC_BRANCH_EVENTS,+ content="kolaszek [pushed](https://bitbucket.org/kolaszek/repository-name/branch/master) 1 commit to branch master.\n\n* first commit ([84b96ad](https://bitbucket.org/kolaszek/repository-name/commits/84b96adc644a30fd6465b3d196369d880762afed))",+ )+msg = self.get_last_message()- self.do_test_message(msg, 'kolaszek pushed tag [a](https://bitbucket.org/kolaszek/repository-name/commits/tag/a).')- self.do_test_topic(msg, TOPIC)+ self.assert_stream_message(+ message=msg,+ stream_name=self.STREAM_NAME,+ topic_name=TOPIC,+ content="kolaszek pushed tag [a](https://bitbucket.org/kolaszek/repository-name/commits/tag/a).",+ )def test_bitbucket2_on_more_than_one_push_event_filtered_by_branches_ignore(self) -> None:self.url = self.build_webhook_url(branches='changes,development')diff --git a/zerver/webhooks/bitbucket3/tests.py b/zerver/webhooks/bitbucket3/tests.pyindex b073916b778..dbbd1a4731e 100644--- a/zerver/webhooks/bitbucket3/tests.py+++ b/zerver/webhooks/bitbucket3/tests.py@@ -65,17 +65,26 @@ class Bitbucket3HookTests(WebhookTestCase):self.check_webhook("repo_push_update_single_branch", expected_topic, expected_message)def test_push_update_multiple_branches(self) -> None:- expected_message_first = """[hypro999](http://139.59.64.214:7990/users/hypro999) pushed to branch branch1. Head is now 3980c2be32a7e23c795741d5dc1a2eecb9b85d6d."""- expected_message_second = """[hypro999](http://139.59.64.214:7990/users/hypro999) pushed to branch master. Head is now fc43d13cff1abb28631196944ba4fc4ad06a2cf2."""- self.check_webhook("repo_push_update_multiple_branches")+ branch1_content = """[hypro999](http://139.59.64.214:7990/users/hypro999) pushed to branch branch1. Head is now 3980c2be32a7e23c795741d5dc1a2eecb9b85d6d."""+ master_content = """[hypro999](http://139.59.64.214:7990/users/hypro999) pushed to branch master. Head is now fc43d13cff1abb28631196944ba4fc4ad06a2cf2."""- msg = self.get_last_message()- self.do_test_topic(msg, TOPIC_BRANCH_EVENTS.format(branch="master"))- self.do_test_message(msg, expected_message_second)+ self.check_webhook("repo_push_update_multiple_branches")msg = self.get_second_to_last_message()- self.do_test_topic(msg, TOPIC_BRANCH_EVENTS.format(branch="branch1"))- self.do_test_message(msg, expected_message_first)+ self.assert_stream_message(+ message=msg,+ stream_name=self.STREAM_NAME,+ topic_name=TOPIC_BRANCH_EVENTS.format(branch="branch1"),+ content=branch1_content,+ )++ msg = self.get_last_message()+ self.assert_stream_message(+ message=msg,+ stream_name=self.STREAM_NAME,+ topic_name=TOPIC_BRANCH_EVENTS.format(branch="master"),+ content=master_content,+ )def test_push_update_multiple_branches_with_branch_filter(self) -> None:self.url = self.build_webhook_url(branches='master')diff --git a/zerver/webhooks/jira/tests.py b/zerver/webhooks/jira/tests.pyindex 57526993723..61932673185 100644--- a/zerver/webhooks/jira/tests.py+++ b/zerver/webhooks/jira/tests.py@@ -2,7 +2,6 @@ from urllib.parse import quote, unquotefrom zerver.lib.test_classes import WebhookTestCasefrom zerver.lib.users import get_api_key-from zerver.models import get_display_recipientclass JiraHookTests(WebhookTestCase):@@ -19,15 +18,18 @@ class JiraHookTests(WebhookTestCase):self.get_body("created_v2"),content_type="application/json",)- self.assertEqual(get_display_recipient(msg.recipient), "jira_custom")- self.assertEqual(msg.topic_name(), "BUG-15: New bug with hook")- expected_message = """+ expected_content = """Leo Franchi created [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15):* **Priority**: Major* **Assignee**: no one""".strip()- self.assertEqual(msg.content, expected_message)+ self.assert_stream_message(+ message=msg,+ stream_name="jira_custom",+ topic_name="BUG-15: New bug with hook",+ content=expected_content,+ )def test_created(self) -> None:expected_topic = "BUG-15: New bug with hook"diff --git a/zerver/webhooks/updown/tests.py b/zerver/webhooks/updown/tests.pyindex c7fc31a386d..a68408ccab3 100644--- a/zerver/webhooks/updown/tests.py+++ b/zerver/webhooks/updown/tests.py@@ -22,17 +22,25 @@ class UpdownHookTests(WebhookTestCase):self.check_webhook("check_up_first_time", expected_topic, expected_message)def test_updown_check_up_multiple_events(self) -> None:- first_message_expected_topic = "https://updown.io"- first_message_expected_message = "Service is `up` again after 1 second."+ topic_name = "https://updown.io"- second_message_expected_topic = "https://updown.io"- second_message_expected_message = "Service is `down`. It returned a 500 error at 2016-02-07 13:11:43 UTC."+ down_content = "Service is `down`. It returned a 500 error at 2016-02-07 13:11:43 UTC."+ up_content = "Service is `up` again after 1 second."self.check_webhook("check_multiple_events")- last_message = self.get_last_message()- self.do_test_topic(last_message, first_message_expected_topic)- self.do_test_message(last_message, first_message_expected_message)- second_to_last_message = self.get_second_to_last_message()- self.do_test_topic(second_to_last_message, second_message_expected_topic)- self.do_test_message(second_to_last_message, second_message_expected_message)+ msg = self.get_second_to_last_message()+ self.assert_stream_message(+ message=msg,+ stream_name=self.STREAM_NAME,+ topic_name=topic_name,+ content=down_content,+ )++ msg = self.get_last_message()+ self.assert_stream_message(+ message=msg,+ stream_name=self.STREAM_NAME,+ topic_name=topic_name,+ content=up_content,+ )