mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	webhook tests: Rename helper to send_webhook_payload.
Not all webhook payloads are json, so send_json_payload was a bit misleading. In passing I also remove "bytes" from the Union type for "payload" parameter.
This commit is contained in:
		@@ -510,7 +510,7 @@ Here is an example from the WordPress integration:
 | 
				
			|||||||
```
 | 
					```
 | 
				
			||||||
def test_unknown_action_no_data(self) -> None:
 | 
					def test_unknown_action_no_data(self) -> None:
 | 
				
			||||||
    # Mimic check_webhook() to manually execute a negative test.
 | 
					    # Mimic check_webhook() to manually execute a negative test.
 | 
				
			||||||
    # Otherwise its call to send_json_payload() would assert on the non-success
 | 
					    # Otherwise its call to send_webhook_payload() would assert on the non-success
 | 
				
			||||||
    # we are testing. The value of result is the error message the webhook should
 | 
					    # we are testing. The value of result is the error message the webhook should
 | 
				
			||||||
    # return if no params are sent. The fixture for this test is an empty file.
 | 
					    # return if no params are sent. The fixture for this test is an empty file.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -541,7 +541,7 @@ URL yourself. (In most cases, it is.)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
`assert_json_error` then checks if the result matches the expected error.
 | 
					`assert_json_error` then checks if the result matches the expected error.
 | 
				
			||||||
If you had used `check_webhook`, it would have called
 | 
					If you had used `check_webhook`, it would have called
 | 
				
			||||||
`send_json_payload`, which checks the result with `assert_json_success`.
 | 
					`send_webhook_payload`, which checks the result with `assert_json_success`.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### Custom query parameters
 | 
					### Custom query parameters
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -845,9 +845,29 @@ class ZulipTestCase(TestCase):
 | 
				
			|||||||
        for x, y in zip(subscribed_streams, streams):
 | 
					        for x, y in zip(subscribed_streams, streams):
 | 
				
			||||||
            self.assertEqual(x["name"], y.name)
 | 
					            self.assertEqual(x["name"], y.name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def send_json_payload(self, user_profile: UserProfile, url: str,
 | 
					    def send_webhook_payload(
 | 
				
			||||||
                          payload: Union[bytes, str, Dict[str, Any]],
 | 
					        self,
 | 
				
			||||||
                          stream_name: Optional[str]=None, **post_params: Any) -> Message:
 | 
					        user_profile: UserProfile,
 | 
				
			||||||
 | 
					        url: str,
 | 
				
			||||||
 | 
					        payload: Union[str, Dict[str, Any]],
 | 
				
			||||||
 | 
					        stream_name: Optional[str]=None,
 | 
				
			||||||
 | 
					        **post_params: Any,
 | 
				
			||||||
 | 
					    ) -> Message:
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        Send a webhook payload to the server, and verify that the
 | 
				
			||||||
 | 
					        post is successful.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        This is a pretty low-level function.  For most use cases
 | 
				
			||||||
 | 
					        see the helpers that call this function, which do additional
 | 
				
			||||||
 | 
					        checks.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Occasionally tests will call this directly, for unique
 | 
				
			||||||
 | 
					        situations like having multiple messages go to a stream,
 | 
				
			||||||
 | 
					        where the other helper functions are a bit too rigid,
 | 
				
			||||||
 | 
					        and you'll want the test itself do various assertions.
 | 
				
			||||||
 | 
					        Even in those cases, you're often better to simply
 | 
				
			||||||
 | 
					        call client_post and assert_json_success.
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
        if stream_name is not None:
 | 
					        if stream_name is not None:
 | 
				
			||||||
            self.subscribe(user_profile, stream_name)
 | 
					            self.subscribe(user_profile, stream_name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -1038,8 +1058,13 @@ class WebhookTestCase(ZulipTestCase):
 | 
				
			|||||||
            headers = get_fixture_http_headers(self.FIXTURE_DIR_NAME, fixture_name)
 | 
					            headers = get_fixture_http_headers(self.FIXTURE_DIR_NAME, fixture_name)
 | 
				
			||||||
            headers = standardize_headers(headers)
 | 
					            headers = standardize_headers(headers)
 | 
				
			||||||
            kwargs.update(headers)
 | 
					            kwargs.update(headers)
 | 
				
			||||||
        msg = self.send_json_payload(self.test_user, self.url, payload,
 | 
					        msg = self.send_webhook_payload(
 | 
				
			||||||
                                     self.STREAM_NAME, **kwargs)
 | 
					            self.test_user,
 | 
				
			||||||
 | 
					            self.url,
 | 
				
			||||||
 | 
					            payload,
 | 
				
			||||||
 | 
					            self.STREAM_NAME,
 | 
				
			||||||
 | 
					            **kwargs,
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
        self.do_test_topic(msg, expected_topic)
 | 
					        self.do_test_topic(msg, expected_topic)
 | 
				
			||||||
        self.do_test_message(msg, expected_message)
 | 
					        self.do_test_message(msg, expected_message)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -1068,8 +1093,13 @@ class WebhookTestCase(ZulipTestCase):
 | 
				
			|||||||
            kwargs.update(headers)
 | 
					            kwargs.update(headers)
 | 
				
			||||||
        # The sender profile shouldn't be passed any further in kwargs, so we pop it.
 | 
					        # The sender profile shouldn't be passed any further in kwargs, so we pop it.
 | 
				
			||||||
        sender = kwargs.pop('sender', self.test_user)
 | 
					        sender = kwargs.pop('sender', self.test_user)
 | 
				
			||||||
        msg = self.send_json_payload(sender, self.url, payload,
 | 
					        msg = self.send_webhook_payload(
 | 
				
			||||||
                                     stream_name=None, **kwargs)
 | 
					            sender,
 | 
				
			||||||
 | 
					            self.url,
 | 
				
			||||||
 | 
					            payload,
 | 
				
			||||||
 | 
					            stream_name=None,
 | 
				
			||||||
 | 
					            **kwargs,
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
        self.assertEqual(msg.content, expected_message)
 | 
					        self.assertEqual(msg.content, expected_message)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return msg
 | 
					        return msg
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -11,11 +11,13 @@ class JiraHookTests(WebhookTestCase):
 | 
				
			|||||||
    def test_custom_stream(self) -> None:
 | 
					    def test_custom_stream(self) -> None:
 | 
				
			||||||
        api_key = get_api_key(self.test_user)
 | 
					        api_key = get_api_key(self.test_user)
 | 
				
			||||||
        url = f"/api/v1/external/jira?api_key={api_key}&stream=jira_custom"
 | 
					        url = f"/api/v1/external/jira?api_key={api_key}&stream=jira_custom"
 | 
				
			||||||
        msg = self.send_json_payload(self.test_user,
 | 
					        msg = self.send_webhook_payload(
 | 
				
			||||||
                                     url,
 | 
					            self.test_user,
 | 
				
			||||||
                                     self.get_body('created_v2'),
 | 
					            url,
 | 
				
			||||||
                                     stream_name="jira_custom",
 | 
					            self.get_body("created_v2"),
 | 
				
			||||||
                                     content_type="application/json")
 | 
					            stream_name="jira_custom",
 | 
				
			||||||
 | 
					            content_type="application/json",
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
        self.assertEqual(msg.topic_name(), "BUG-15: New bug with hook")
 | 
					        self.assertEqual(msg.topic_name(), "BUG-15: New bug with hook")
 | 
				
			||||||
        expected_message = """
 | 
					        expected_message = """
 | 
				
			||||||
Leo Franchi created [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15):
 | 
					Leo Franchi created [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15):
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -82,7 +82,7 @@ class WordPressHookTests(WebhookTestCase):
 | 
				
			|||||||
    def test_unknown_action_no_data(self) -> None:
 | 
					    def test_unknown_action_no_data(self) -> None:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # Mimic check_webhook() to manually execute a negative test.
 | 
					        # Mimic check_webhook() to manually execute a negative test.
 | 
				
			||||||
        # Otherwise its call to send_json_payload() would assert on the non-success
 | 
					        # Otherwise its call to send_webhook_payload() would assert on the non-success
 | 
				
			||||||
        # we are testing. The value of result is the error message the webhook should
 | 
					        # we are testing. The value of result is the error message the webhook should
 | 
				
			||||||
        # return if no params are sent. The fixture for this test is an empty file.
 | 
					        # return if no params are sent. The fixture for this test is an empty file.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user