bot tests: Add test_multiple_services().

This test would have failed before we fixed the
bug in #14384.
This commit is contained in:
Steve Howell 2020-03-28 12:22:19 +00:00 committed by Tim Abbott
parent 123604e4ba
commit a37c183728
1 changed files with 63 additions and 0 deletions

View File

@ -7,6 +7,10 @@ import requests
from typing import Any, Optional from typing import Any, Optional
from zerver.lib.actions import (
do_create_user,
)
from zerver.lib.outgoing_webhook import ( from zerver.lib.outgoing_webhook import (
do_rest_call, do_rest_call,
GenericOutgoingWebhookService, GenericOutgoingWebhookService,
@ -15,11 +19,13 @@ from zerver.lib.outgoing_webhook import (
from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.topic import TOPIC_NAME from zerver.lib.topic import TOPIC_NAME
from zerver.lib.users import add_service
from zerver.models import ( from zerver.models import (
get_display_recipient, get_display_recipient,
get_realm, get_realm,
get_user, get_user,
Recipient, Recipient,
Service,
UserProfile, UserProfile,
) )
@ -154,6 +160,63 @@ class TestOutgoingWebhookMessaging(ZulipTestCase):
service_name='foo-service' 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!"})) @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: def test_pm_to_outgoing_webhook_bot(self, mock_requests_request: mock.Mock) -> None:
bot_owner = self.example_user("othello") bot_owner = self.example_user("othello")