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.

webhook tests: Rename helper to send_webhook_payload.

Steve Howell2fdf966d9fca
diff --git a/templates/zerver/api/incoming-webhooks-walkthrough.md b/templates/zerver/api/incoming-webhooks-walkthrough.md
index 925b81e3cc7..737f793dbc5 100644
--- a/templates/zerver/api/incoming-webhooks-walkthrough.md
+++ b/templates/zerver/api/incoming-webhooks-walkthrough.md
@@ -510,7 +510,7 @@ Here is an example from the WordPress integration:
```
def test_unknown_action_no_data(self) -> None:
# 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
# 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.
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
diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.py
index 1c79c69bf82..8724a580829 100644
--- a/zerver/lib/test_classes.py
+++ b/zerver/lib/test_classes.py
@@ -845,9 +845,29 @@ class ZulipTestCase(TestCase):
for x, y in zip(subscribed_streams, streams):
self.assertEqual(x["name"], y.name)
- def send_json_payload(self, user_profile: UserProfile, url: str,
- payload: Union[bytes, str, Dict[str, Any]],
- stream_name: Optional[str]=None, **post_params: Any) -> Message:
+ def send_webhook_payload(
+ self,
+ 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:
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 = standardize_headers(headers)
kwargs.update(headers)
- msg = self.send_json_payload(self.test_user, self.url, payload,
- self.STREAM_NAME, **kwargs)
+ msg = self.send_webhook_payload(
+ self.test_user,
+ self.url,
+ payload,
+ self.STREAM_NAME,
+ **kwargs,
+ )
self.do_test_topic(msg, expected_topic)
self.do_test_message(msg, expected_message)
@@ -1068,8 +1093,13 @@ class WebhookTestCase(ZulipTestCase):
kwargs.update(headers)
# The sender profile shouldn't be passed any further in kwargs, so we pop it.
sender = kwargs.pop('sender', self.test_user)
- msg = self.send_json_payload(sender, self.url, payload,
- stream_name=None, **kwargs)
+ msg = self.send_webhook_payload(
+ sender,
+ self.url,
+ payload,
+ stream_name=None,
+ **kwargs,
+ )
self.assertEqual(msg.content, expected_message)
return msg
diff --git a/zerver/webhooks/jira/tests.py b/zerver/webhooks/jira/tests.py
index 0ccca1bcd36..31b0032691a 100644
--- a/zerver/webhooks/jira/tests.py
+++ b/zerver/webhooks/jira/tests.py
@@ -11,11 +11,13 @@ class JiraHookTests(WebhookTestCase):
def test_custom_stream(self) -> None:
api_key = get_api_key(self.test_user)
url = f"/api/v1/external/jira?api_key={api_key}&stream=jira_custom"
- msg = self.send_json_payload(self.test_user,
- url,
- self.get_body('created_v2'),
- stream_name="jira_custom",
- content_type="application/json")
+ msg = self.send_webhook_payload(
+ self.test_user,
+ url,
+ self.get_body("created_v2"),
+ stream_name="jira_custom",
+ content_type="application/json",
+ )
self.assertEqual(msg.topic_name(), "BUG-15: New bug with hook")
expected_message = """
Leo Franchi created [BUG-15: New bug with hook](http://lfranchi.com:8080/browse/BUG-15):
diff --git a/zerver/webhooks/wordpress/tests.py b/zerver/webhooks/wordpress/tests.py
index 0ad504f6c33..54cf2080d0d 100644
--- a/zerver/webhooks/wordpress/tests.py
+++ b/zerver/webhooks/wordpress/tests.py
@@ -82,7 +82,7 @@ class WordPressHookTests(WebhookTestCase):
def test_unknown_action_no_data(self) -> None:
# 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
# return if no params are sent. The fixture for this test is an empty file.