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:
Steve Howell 2020-08-23 17:09:27 +00:00 committed by Tim Abbott
parent 388053db6b
commit 2fdf966d9f
4 changed files with 47 additions and 15 deletions

View File

@ -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

View File

@ -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

View File

@ -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):

View File

@ -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.