2023-10-12 19:43:45 +02:00
|
|
|
from typing_extensions import override
|
|
|
|
|
2016-11-29 22:48:22 +01:00
|
|
|
from zerver.lib.test_classes import WebhookTestCase
|
|
|
|
|
2020-01-14 22:06:24 +01:00
|
|
|
|
2016-11-29 22:48:22 +01:00
|
|
|
class WordPressHookTests(WebhookTestCase):
|
2024-05-04 22:02:50 +02:00
|
|
|
CHANNEL_NAME = "wordpress"
|
2018-03-16 22:53:50 +01:00
|
|
|
URL_TEMPLATE = "/api/v1/external/wordpress?api_key={api_key}&stream={stream}"
|
2021-06-26 09:18:33 +02:00
|
|
|
WEBHOOK_DIR_NAME = "wordpress"
|
2016-11-29 22:48:22 +01:00
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_publish_post(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "WordPress Post"
|
2020-04-09 21:51:58 +02:00
|
|
|
expected_message = "New post published:\n* [New Blog Post](http://example.com\n)"
|
2016-11-29 22:48:22 +01:00
|
|
|
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"publish_post",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2020-08-23 15:49:24 +02:00
|
|
|
expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
2016-11-29 22:48:22 +01:00
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_publish_post_type_not_provided(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "WordPress Post"
|
2020-04-09 21:51:58 +02:00
|
|
|
expected_message = "New post published:\n* [New Blog Post](http://example.com\n)"
|
2016-11-29 22:48:22 +01:00
|
|
|
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"publish_post_type_not_provided",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2020-08-23 15:49:24 +02:00
|
|
|
expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
2016-11-29 22:48:22 +01:00
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_publish_post_no_data_provided(self) -> None:
|
2024-07-16 18:57:42 +02:00
|
|
|
# If the user forgot to configure the fields for the action in WordPress.
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "WordPress notification"
|
2021-05-10 07:02:14 +02:00
|
|
|
expected_message = "New post published:\n* [New WordPress post](WordPress post URL)"
|
2016-11-29 22:48:22 +01:00
|
|
|
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"publish_post_no_data_provided",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2020-08-23 15:49:24 +02:00
|
|
|
expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
2016-11-29 22:48:22 +01:00
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_publish_page(self) -> None:
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "WordPress Page"
|
2020-04-09 21:51:58 +02:00
|
|
|
expected_message = "New page published:\n* [New Blog Page](http://example.com\n)"
|
2016-11-29 22:48:22 +01:00
|
|
|
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"publish_page",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2020-08-23 15:49:24 +02:00
|
|
|
expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
2016-11-29 22:48:22 +01:00
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_unknown_action_no_data(self) -> None:
|
2020-08-23 15:49:24 +02:00
|
|
|
# Mimic check_webhook() to manually execute a negative test.
|
2020-08-23 19:09:27 +02:00
|
|
|
# Otherwise its call to send_webhook_payload() would assert on the non-success
|
2016-11-29 22:48:22 +01:00
|
|
|
# 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.
|
|
|
|
|
2024-05-04 22:39:36 +02:00
|
|
|
# subscribe to the target channel
|
2024-05-04 22:02:50 +02:00
|
|
|
self.subscribe(self.test_user, self.CHANNEL_NAME)
|
2016-11-29 22:48:22 +01:00
|
|
|
|
|
|
|
# post to the webhook url
|
2022-06-23 02:21:40 +02:00
|
|
|
result = self.client_post(
|
|
|
|
self.url,
|
|
|
|
self.get_body("unknown_action_no_data"),
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
2016-11-29 22:48:22 +01:00
|
|
|
|
|
|
|
# check that we got the expected error message
|
2021-05-10 07:02:14 +02:00
|
|
|
self.assert_json_error(result, "Unknown WordPress webhook action: WordPress action")
|
2016-11-29 22:48:22 +01:00
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_unknown_action_no_hook_provided(self) -> None:
|
2016-11-29 22:48:22 +01:00
|
|
|
# Similar to unknown_action_no_data, except the fixture contains valid blog post
|
|
|
|
# params but without the hook parameter. This should also return an error.
|
|
|
|
|
2024-05-04 22:02:50 +02:00
|
|
|
self.subscribe(self.test_user, self.CHANNEL_NAME)
|
2022-06-23 02:21:40 +02:00
|
|
|
result = self.client_post(
|
|
|
|
self.url,
|
|
|
|
self.get_body("unknown_action_no_hook_provided"),
|
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
2016-11-29 22:48:22 +01:00
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
self.assert_json_error(result, "Unknown WordPress webhook action: WordPress action")
|
2016-11-29 22:48:22 +01:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2017-11-09 08:59:34 +01:00
|
|
|
def get_body(self, fixture_name: str) -> str:
|
2018-04-20 03:57:21 +02:00
|
|
|
return self.webhook_fixture_data("wordpress", fixture_name, file_type="txt")
|