2016-11-29 22:48:22 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from zerver.lib.test_classes import WebhookTestCase
|
|
|
|
|
|
|
|
class WordPressHookTests(WebhookTestCase):
|
|
|
|
STREAM_NAME = 'wordpress'
|
2018-03-16 22:53:50 +01:00
|
|
|
URL_TEMPLATE = "/api/v1/external/wordpress?api_key={api_key}&stream={stream}"
|
2016-11-29 22:48:22 +01:00
|
|
|
FIXTURE_DIR_NAME = 'wordpress'
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_publish_post(self) -> None:
|
2016-11-29 22:48:22 +01:00
|
|
|
|
|
|
|
expected_topic = u"WordPress Post"
|
2019-04-17 02:09:09 +02:00
|
|
|
expected_message = u"New post published:\n* [New Blog Post](http://example.com\n)"
|
2016-11-29 22:48:22 +01:00
|
|
|
|
|
|
|
self.send_and_test_stream_message('publish_post', expected_topic, expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded")
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_publish_post_type_not_provided(self) -> None:
|
2016-11-29 22:48:22 +01:00
|
|
|
|
|
|
|
expected_topic = u"WordPress Post"
|
2019-04-17 02:09:09 +02:00
|
|
|
expected_message = u"New post published:\n* [New Blog Post](http://example.com\n)"
|
2016-11-29 22:48:22 +01:00
|
|
|
|
|
|
|
self.send_and_test_stream_message('publish_post_type_not_provided',
|
|
|
|
expected_topic, expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded")
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_publish_post_no_data_provided(self) -> None:
|
2016-11-29 22:48:22 +01:00
|
|
|
|
|
|
|
# Note: the fixture includes 'hook=publish_post' because it's always added by HookPress
|
|
|
|
expected_topic = u"WordPress Notification"
|
2019-04-17 02:09:09 +02:00
|
|
|
expected_message = u"New post published:\n* [New WordPress Post](WordPress Post URL)"
|
2016-11-29 22:48:22 +01:00
|
|
|
|
|
|
|
self.send_and_test_stream_message('publish_post_no_data_provided',
|
|
|
|
expected_topic, expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded")
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_publish_page(self) -> None:
|
2016-11-29 22:48:22 +01:00
|
|
|
|
|
|
|
expected_topic = u"WordPress Page"
|
2019-04-17 02:09:09 +02:00
|
|
|
expected_message = u"New page published:\n* [New Blog Page](http://example.com\n)"
|
2016-11-29 22:48:22 +01:00
|
|
|
|
|
|
|
self.send_and_test_stream_message('publish_page', expected_topic, expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded")
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_user_register(self) -> None:
|
2016-11-29 22:48:22 +01:00
|
|
|
|
|
|
|
expected_topic = u"New Blog Users"
|
2019-04-17 02:09:09 +02:00
|
|
|
expected_message = u"New blog user registered:\n* **Name**: test_user\n* **Email**: test_user@example.com"
|
2016-11-29 22:48:22 +01:00
|
|
|
|
|
|
|
self.send_and_test_stream_message('user_register', expected_topic, expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded")
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_wp_login(self) -> None:
|
2016-11-29 22:48:22 +01:00
|
|
|
|
|
|
|
expected_topic = u"New Login"
|
|
|
|
expected_message = u"User testuser logged in."
|
|
|
|
|
|
|
|
self.send_and_test_stream_message('wp_login', expected_topic, expected_message,
|
|
|
|
content_type="application/x-www-form-urlencoded")
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_unknown_action_no_data(self) -> None:
|
2016-11-29 22:48:22 +01:00
|
|
|
|
|
|
|
# Mimic send_and_test_stream_message() to manually execute a negative test.
|
|
|
|
# Otherwise its call to send_json_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.
|
|
|
|
|
|
|
|
# subscribe to the target stream
|
2017-08-25 06:37:47 +02:00
|
|
|
self.subscribe(self.test_user, self.STREAM_NAME)
|
2016-11-29 22:48:22 +01:00
|
|
|
|
|
|
|
# post to the webhook url
|
|
|
|
post_params = {'stream_name': self.STREAM_NAME,
|
|
|
|
'content_type': 'application/x-www-form-urlencoded'}
|
|
|
|
result = self.client_post(self.url, 'unknown_action', **post_params)
|
|
|
|
|
|
|
|
# check that we got the expected error message
|
|
|
|
self.assert_json_error(result, "Unknown WordPress webhook action: WordPress Action")
|
|
|
|
|
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.
|
|
|
|
|
2017-08-25 06:37:47 +02:00
|
|
|
self.subscribe(self.test_user, self.STREAM_NAME)
|
2016-11-29 22:48:22 +01:00
|
|
|
post_params = {'stream_name': self.STREAM_NAME,
|
|
|
|
'content_type': 'application/x-www-form-urlencoded'}
|
|
|
|
result = self.client_post(self.url, 'unknown_action', **post_params)
|
|
|
|
|
|
|
|
self.assert_json_error(result, "Unknown WordPress webhook action: WordPress Action")
|
|
|
|
|
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")
|