mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	webhook tests: Extract assert_stream_message.
This forces us to be a bit more explicit about testing the three key values in any stream message, and it also de-clutters the code a bit. I eventually want to phase out do_test_topic and friends, since they have the pitfall that you can call them and have them do nothing, because they don't actually require values to be be passed in. I also clean up the code a bit for the tests that have two new messages arriving.
This commit is contained in:
		@@ -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,
 | 
			
		||||
 
 | 
			
		||||
@@ -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')
 | 
			
		||||
 
 | 
			
		||||
@@ -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."""
 | 
			
		||||
        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."""
 | 
			
		||||
 | 
			
		||||
        self.check_webhook("repo_push_update_multiple_branches")
 | 
			
		||||
 | 
			
		||||
        msg = self.get_last_message()
 | 
			
		||||
        self.do_test_topic(msg, TOPIC_BRANCH_EVENTS.format(branch="master"))
 | 
			
		||||
        self.do_test_message(msg, expected_message_second)
 | 
			
		||||
 | 
			
		||||
        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')
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,6 @@ from urllib.parse import quote, unquote
 | 
			
		||||
 | 
			
		||||
from zerver.lib.test_classes import WebhookTestCase
 | 
			
		||||
from zerver.lib.users import get_api_key
 | 
			
		||||
from zerver.models import get_display_recipient
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class 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"
 | 
			
		||||
 
 | 
			
		||||
@@ -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,
 | 
			
		||||
        )
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user