From a37c1837286de391648973ab4b393de5df1cffe5 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Sat, 28 Mar 2020 12:22:19 +0000 Subject: [PATCH] bot tests: Add test_multiple_services(). This test would have failed before we fixed the bug in #14384. --- zerver/tests/test_outgoing_webhook_system.py | 63 ++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/zerver/tests/test_outgoing_webhook_system.py b/zerver/tests/test_outgoing_webhook_system.py index 95659f92c1..493f879f42 100644 --- a/zerver/tests/test_outgoing_webhook_system.py +++ b/zerver/tests/test_outgoing_webhook_system.py @@ -7,6 +7,10 @@ import requests from typing import Any, Optional +from zerver.lib.actions import ( + do_create_user, +) + from zerver.lib.outgoing_webhook import ( do_rest_call, GenericOutgoingWebhookService, @@ -15,11 +19,13 @@ from zerver.lib.outgoing_webhook import ( from zerver.lib.test_classes import ZulipTestCase from zerver.lib.topic import TOPIC_NAME +from zerver.lib.users import add_service from zerver.models import ( get_display_recipient, get_realm, get_user, Recipient, + Service, UserProfile, ) @@ -154,6 +160,63 @@ class TestOutgoingWebhookMessaging(ZulipTestCase): service_name='foo-service' ) + def test_multiple_services(self) -> None: + bot_owner = self.example_user("othello") + + bot = do_create_user( + bot_owner=bot_owner, + bot_type=UserProfile.OUTGOING_WEBHOOK_BOT, + full_name='Outgoing Webhook Bot', + email='whatever', + realm=bot_owner.realm, + short_name='', + password=None, + ) + + add_service( + 'weather', + user_profile=bot, + interface=Service.GENERIC, + base_url='weather_url', + token='weather_token', + ) + + add_service( + 'qotd', + user_profile=bot, + interface=Service.GENERIC, + base_url='qotd_url', + token='qotd_token', + ) + + sender = self.example_user("hamlet") + + with mock.patch('zerver.worker.queue_processors.do_rest_call') as m: + self.send_personal_message( + sender, + bot, + content="some content" + ) + + url_token_tups = set() + for item in m.call_args_list: + args = item[0] + base_url = args[0] + request_data = ujson.loads(args[1]) + tup = (base_url, request_data['token']) + url_token_tups.add(tup) + message_data = request_data['message'] + self.assertEqual(message_data['content'], 'some content') + self.assertEqual(message_data['sender_id'], sender.id) + + self.assertEqual( + url_token_tups, + { + ('weather_url', 'weather_token'), + ('qotd_url', 'qotd_token'), + } + ) + @mock.patch('requests.request', return_value=ResponseMock(200, {"response_string": "Hidley ho, I'm a webhook responding!"})) def test_pm_to_outgoing_webhook_bot(self, mock_requests_request: mock.Mock) -> None: bot_owner = self.example_user("othello")