2018-03-22 21:43:28 +01:00
from django . conf import settings
2017-11-16 00:43:10 +01:00
2016-11-10 19:30:09 +01:00
from zerver . lib . test_classes import WebhookTestCase
2018-03-22 21:43:28 +01:00
from zerver . models import get_system_bot
2016-09-20 22:51:11 +02:00
2020-01-14 22:06:24 +01:00
2016-09-20 22:51:11 +02:00
class HelloWorldHookTests ( WebhookTestCase ) :
STREAM_NAME = ' test '
2018-03-13 23:36:11 +01:00
URL_TEMPLATE = " /api/v1/external/helloworld?&api_key= {api_key} &stream= {stream} "
PM_URL_TEMPLATE = " /api/v1/external/helloworld?&api_key= {api_key} "
2016-09-20 22:51:11 +02:00
FIXTURE_DIR_NAME = ' hello '
# Note: Include a test function per each distinct message condition your integration supports
2017-11-04 07:47:46 +01:00
def test_hello_message ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " Hello World "
expected_message = " Hello! I am happy to be here! :smile: \n The Wikipedia featured article for today is **[Marilyn Monroe](https://en.wikipedia.org/wiki/Marilyn_Monroe)** "
2016-09-20 22:51:11 +02:00
# use fixture named helloworld_hello
2018-11-09 20:33:58 +01:00
self . send_and_test_stream_message ( ' hello ' , expected_topic , expected_message ,
2016-09-20 22:51:11 +02:00
content_type = " application/x-www-form-urlencoded " )
2017-11-04 07:47:46 +01:00
def test_goodbye_message ( self ) - > None :
2020-04-09 21:51:58 +02:00
expected_topic = " Hello World "
expected_message = " Hello! I am happy to be here! :smile: \n The Wikipedia featured article for today is **[Goodbye](https://en.wikipedia.org/wiki/Goodbye)** "
2016-09-20 22:51:11 +02:00
# use fixture named helloworld_goodbye
2018-11-09 20:33:58 +01:00
self . send_and_test_stream_message ( ' goodbye ' , expected_topic , expected_message ,
2016-09-20 22:51:11 +02:00
content_type = " application/x-www-form-urlencoded " )
2018-03-13 23:36:11 +01:00
def test_pm_to_bot_owner ( self ) - > None :
# Note that this is really just a test for check_send_webhook_message
self . URL_TEMPLATE = self . PM_URL_TEMPLATE
self . url = self . build_webhook_url ( )
2020-04-09 21:51:58 +02:00
expected_message = " Hello! I am happy to be here! :smile: \n The Wikipedia featured article for today is **[Goodbye](https://en.wikipedia.org/wiki/Goodbye)** "
2018-03-13 23:36:11 +01:00
self . send_and_test_private_message ( ' goodbye ' , expected_message = expected_message ,
content_type = " application/x-www-form-urlencoded " )
2018-03-22 21:43:28 +01:00
def test_stream_error_pm_to_bot_owner ( self ) - > None :
2020-01-30 17:29:37 +01:00
# Note that this is really just a test for check_send_webhook_message
2018-03-22 21:43:28 +01:00
self . STREAM_NAME = ' nonexistent '
self . url = self . build_webhook_url ( )
notification_bot = get_system_bot ( settings . NOTIFICATION_BOT )
2019-07-11 01:22:32 +02:00
expected_message = " Your bot `webhook-bot@zulip.com` tried to send a message to stream #**nonexistent**, but that stream does not exist. Click [here](#streams/new) to create it. "
2018-03-22 21:43:28 +01:00
self . send_and_test_private_message ( ' goodbye ' , expected_message = expected_message ,
content_type = ' application/x-www-form-urlencoded ' ,
sender = notification_bot )
2018-03-13 23:36:11 +01:00
def test_custom_topic ( self ) - > None :
# Note that this is really just a test for check_send_webhook_message
2020-04-09 21:51:58 +02:00
expected_topic = " Custom Topic "
2018-11-09 20:33:58 +01:00
self . url = self . build_webhook_url ( topic = expected_topic )
2020-04-09 21:51:58 +02:00
expected_message = " Hello! I am happy to be here! :smile: \n The Wikipedia featured article for today is **[Goodbye](https://en.wikipedia.org/wiki/Goodbye)** "
2018-03-13 23:36:11 +01:00
2018-11-09 20:33:58 +01:00
self . send_and_test_stream_message ( ' goodbye ' , expected_topic , expected_message ,
2018-03-13 23:36:11 +01:00
content_type = " application/x-www-form-urlencoded " )
2018-05-10 19:34:01 +02:00
def get_body ( self , fixture_name : str ) - > str :
2018-04-20 03:57:21 +02:00
return self . webhook_fixture_data ( " helloworld " , fixture_name , file_type = " json " )