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
2023-12-15 02:14:24 +01:00
from zerver . models . realms import get_realm
2023-12-15 01:16:00 +01:00
from zerver . models . users 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} "
2023-06-19 16:34:52 +02:00
DIRECT_MESSAGE_URL_TEMPLATE = " /api/v1/external/helloworld?&api_key= {api_key} "
2021-06-26 09:18:33 +02:00
WEBHOOK_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 :
2024-01-17 15:53:30 +01:00
expected_topic_name = " Hello World "
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 **[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 " ,
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-09-20 22:51:11 +02:00
2017-11-04 07:47:46 +01:00
def test_goodbye_message ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " Hello World "
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)** "
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 " ,
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-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
2023-06-19 16:34:52 +02:00
self . URL_TEMPLATE = self . DIRECT_MESSAGE_URL_TEMPLATE
2018-03-13 23:36:11 +01:00
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 ( )
2021-03-08 11:54:39 +01:00
realm = get_realm ( " zulip " )
notification_bot = get_system_bot ( settings . NOTIFICATION_BOT , realm . id )
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
2024-01-17 15:53:30 +01:00
expected_topic_name = " Custom Topic "
self . url = self . build_webhook_url ( topic = expected_topic_name )
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 " ,
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 " ,
)