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 ) :
2021-02-12 08:20:45 +01:00
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} "
2020-08-20 20:29:32 +02:00
FIXTURE_DIR_NAME = " helloworld "
2016-09-20 22:51:11 +02:00
# 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
2020-08-23 15:49:24 +02:00
self . check_webhook (
" hello " ,
expected_topic ,
expected_message ,
content_type = " application/x-www-form-urlencoded " ,
)
2016-09-20 22:51:11 +02:00
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
2020-08-23 15:49:24 +02:00
self . check_webhook (
" goodbye " ,
expected_topic ,
expected_message ,
content_type = " application/x-www-form-urlencoded " ,
)
2016-09-20 22:51:11 +02:00
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
2021-02-12 08:19:30 +01:00
self . send_and_test_private_message (
2021-02-12 08:20:45 +01:00
" goodbye " ,
2021-02-12 08:19:30 +01:00
expected_message = expected_message ,
content_type = " application/x-www-form-urlencoded " ,
)
2018-03-13 23:36:11 +01:00
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
2021-02-12 08:20:45 +01:00
self . STREAM_NAME = " nonexistent "
2018-03-22 21:43:28 +01:00
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. "
2021-02-12 08:19:30 +01:00
self . send_and_test_private_message (
2021-02-12 08:20:45 +01:00
" goodbye " ,
2021-02-12 08:19:30 +01:00
expected_message = expected_message ,
2021-02-12 08:20:45 +01:00
content_type = " application/x-www-form-urlencoded " ,
2021-02-12 08:19:30 +01:00
sender = notification_bot ,
)
2018-03-22 21:43:28 +01:00
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
2020-08-23 15:49:24 +02:00
self . check_webhook (
" goodbye " ,
expected_topic ,
expected_message ,
content_type = " application/x-www-form-urlencoded " ,
)