2017-07-22 12:49:20 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-10-10 15:11:50 +02:00
|
|
|
from typing import cast, Any, Dict
|
2017-07-22 12:49:20 +02:00
|
|
|
|
|
|
|
import mock
|
|
|
|
import json
|
2018-10-10 15:11:50 +02:00
|
|
|
import requests
|
2017-07-22 12:49:20 +02:00
|
|
|
|
2019-11-05 20:47:50 +01:00
|
|
|
from zerver.lib.message import MessageDict
|
2018-10-11 19:07:09 +02:00
|
|
|
from zerver.lib.outgoing_webhook import (
|
|
|
|
get_service_interface_class,
|
|
|
|
process_success_response,
|
|
|
|
)
|
2017-07-22 12:49:20 +02:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2018-11-10 16:43:59 +01:00
|
|
|
from zerver.lib.topic import TOPIC_NAME
|
2019-11-05 20:47:50 +01:00
|
|
|
from zerver.models import get_realm, get_user, SLACK_INTERFACE, Message
|
2017-07-22 12:49:20 +02:00
|
|
|
|
2017-07-24 08:42:22 +02:00
|
|
|
class TestGenericOutgoingWebhookService(ZulipTestCase):
|
2017-07-22 12:49:20 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def setUp(self) -> None:
|
2019-10-19 20:47:00 +02:00
|
|
|
super().setUp()
|
2019-11-05 20:47:50 +01:00
|
|
|
|
|
|
|
# TODO: Ideally, this test would use the full flow, rather
|
|
|
|
# than making a mock message like this.
|
2020-03-07 11:43:05 +01:00
|
|
|
message_id = self.send_stream_message(self.example_user('othello'),
|
2019-11-05 20:47:50 +01:00
|
|
|
"Denmark", content="@**test**")
|
|
|
|
message = Message.objects.get(id=message_id)
|
|
|
|
wide_message_dict = MessageDict.wide_dict(message)
|
|
|
|
|
2017-07-22 12:49:20 +02:00
|
|
|
self.event = {
|
|
|
|
u'command': '@**test**',
|
2019-11-05 20:47:50 +01:00
|
|
|
u'message': wide_message_dict,
|
2018-05-23 16:14:22 +02:00
|
|
|
u'trigger': 'mention',
|
2017-07-22 12:49:20 +02:00
|
|
|
}
|
2018-05-23 16:14:22 +02:00
|
|
|
self.bot_user = get_user("outgoing-webhook@zulip.com", get_realm("zulip"))
|
2018-10-11 19:07:09 +02:00
|
|
|
service_class = get_service_interface_class('whatever') # GenericOutgoingWebhookService
|
|
|
|
self.handler = service_class(service_name='test-service',
|
|
|
|
token='abcdef',
|
|
|
|
user_profile=self.bot_user)
|
2017-07-22 12:49:20 +02:00
|
|
|
|
2018-10-10 15:11:50 +02:00
|
|
|
def test_process_success_response(self) -> None:
|
|
|
|
class Stub:
|
|
|
|
def __init__(self, text: str) -> None:
|
2019-08-10 00:30:34 +02:00
|
|
|
self.text = text
|
2018-10-10 15:11:50 +02:00
|
|
|
|
|
|
|
def make_response(text: str) -> requests.Response:
|
|
|
|
return cast(requests.Response, Stub(text=text))
|
|
|
|
|
|
|
|
event = dict(
|
|
|
|
user_profile_id=99,
|
|
|
|
message=dict(type='private')
|
|
|
|
)
|
|
|
|
service_handler = self.handler
|
|
|
|
|
|
|
|
response = make_response(text=json.dumps(dict(content='whatever')))
|
|
|
|
|
|
|
|
with mock.patch('zerver.lib.outgoing_webhook.send_response_message') as m:
|
|
|
|
process_success_response(
|
|
|
|
event=event,
|
|
|
|
service_handler=service_handler,
|
|
|
|
response=response,
|
|
|
|
)
|
|
|
|
self.assertTrue(m.called)
|
|
|
|
|
|
|
|
response = make_response(text='unparsable text')
|
|
|
|
|
|
|
|
with mock.patch('zerver.lib.outgoing_webhook.fail_with_message') as m:
|
|
|
|
process_success_response(
|
|
|
|
event=event,
|
|
|
|
service_handler=service_handler,
|
|
|
|
response=response
|
|
|
|
)
|
|
|
|
self.assertTrue(m.called)
|
|
|
|
|
2018-10-11 00:45:19 +02:00
|
|
|
def test_build_bot_request(self) -> None:
|
|
|
|
request_data = self.handler.build_bot_request(self.event)
|
2017-07-22 12:49:20 +02:00
|
|
|
request_data = json.loads(request_data)
|
|
|
|
self.assertEqual(request_data['data'], "@**test**")
|
|
|
|
self.assertEqual(request_data['token'], "abcdef")
|
|
|
|
self.assertEqual(request_data['message'], self.event['message'])
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_process_success(self) -> None:
|
2018-10-10 13:16:42 +02:00
|
|
|
response = dict(response_not_required=True) # type: Dict[str, Any]
|
2018-10-09 17:12:57 +02:00
|
|
|
success_response = self.handler.process_success(response, self.event)
|
2017-07-22 12:49:20 +02:00
|
|
|
self.assertEqual(success_response, None)
|
|
|
|
|
2018-10-10 13:16:42 +02:00
|
|
|
response = dict(response_string='test_content')
|
2018-10-09 17:12:57 +02:00
|
|
|
success_response = self.handler.process_success(response, self.event)
|
2018-10-10 01:36:31 +02:00
|
|
|
self.assertEqual(success_response, dict(content='test_content'))
|
2017-07-22 12:49:20 +02:00
|
|
|
|
2018-10-26 18:08:08 +02:00
|
|
|
response = dict(
|
|
|
|
content='test_content',
|
|
|
|
widget_content='test_widget_content',
|
|
|
|
red_herring='whatever',
|
|
|
|
)
|
|
|
|
success_response = self.handler.process_success(response, self.event)
|
|
|
|
expected_response = dict(
|
|
|
|
content='test_content',
|
|
|
|
widget_content='test_widget_content',
|
|
|
|
)
|
|
|
|
self.assertEqual(success_response, expected_response)
|
|
|
|
|
2018-10-10 13:16:42 +02:00
|
|
|
response = dict()
|
2018-10-09 17:12:57 +02:00
|
|
|
success_response = self.handler.process_success(response, self.event)
|
2017-07-24 09:02:29 +02:00
|
|
|
self.assertEqual(success_response, None)
|
2017-07-22 12:49:20 +02:00
|
|
|
|
2017-07-24 08:42:22 +02:00
|
|
|
class TestSlackOutgoingWebhookService(ZulipTestCase):
|
2017-07-01 14:42:34 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def setUp(self) -> None:
|
2019-10-19 20:47:00 +02:00
|
|
|
super().setUp()
|
2018-06-25 17:27:06 +02:00
|
|
|
self.stream_message_event = {
|
2017-07-01 14:42:34 +02:00
|
|
|
u'command': '@**test**',
|
|
|
|
u'user_profile_id': 12,
|
|
|
|
u'service_name': 'test-service',
|
|
|
|
u'trigger': 'mention',
|
|
|
|
u'message': {
|
|
|
|
'content': 'test_content',
|
|
|
|
'type': 'stream',
|
|
|
|
'sender_realm_str': 'zulip',
|
|
|
|
'sender_email': 'sampleuser@zulip.com',
|
|
|
|
'stream_id': '123',
|
|
|
|
'display_recipient': 'integrations',
|
|
|
|
'timestamp': 123456,
|
|
|
|
'sender_id': 21,
|
|
|
|
'sender_full_name': 'Sample User',
|
|
|
|
}
|
|
|
|
}
|
2018-06-24 08:51:47 +02:00
|
|
|
|
|
|
|
self.private_message_event = {
|
|
|
|
u'user_profile_id': 24,
|
|
|
|
u'service_name': 'test-service',
|
|
|
|
u'command': 'test content',
|
|
|
|
u'trigger': 'private_message',
|
|
|
|
u'message': {
|
|
|
|
'sender_id': 3,
|
|
|
|
'sender_realm_str': 'zulip',
|
|
|
|
'timestamp': 1529821610,
|
|
|
|
'sender_email': 'cordelia@zulip.com',
|
|
|
|
'type': 'private',
|
|
|
|
'sender_realm_id': 1,
|
|
|
|
'id': 219,
|
2018-11-10 16:43:59 +01:00
|
|
|
TOPIC_NAME: 'test',
|
2018-06-24 08:51:47 +02:00
|
|
|
'content': 'test content',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-11 19:07:09 +02:00
|
|
|
service_class = get_service_interface_class(SLACK_INTERFACE)
|
|
|
|
self.handler = service_class(token="abcdef",
|
|
|
|
user_profile=None,
|
|
|
|
service_name='test-service')
|
2017-07-01 14:42:34 +02:00
|
|
|
|
2018-10-11 00:45:19 +02:00
|
|
|
def test_build_bot_request_stream_message(self) -> None:
|
|
|
|
request_data = self.handler.build_bot_request(self.stream_message_event)
|
2017-07-01 14:42:34 +02:00
|
|
|
|
|
|
|
self.assertEqual(request_data[0][1], "abcdef") # token
|
|
|
|
self.assertEqual(request_data[1][1], "zulip") # team_id
|
|
|
|
self.assertEqual(request_data[2][1], "zulip.com") # team_domain
|
|
|
|
self.assertEqual(request_data[3][1], "123") # channel_id
|
|
|
|
self.assertEqual(request_data[4][1], "integrations") # channel_name
|
|
|
|
self.assertEqual(request_data[5][1], 123456) # timestamp
|
|
|
|
self.assertEqual(request_data[6][1], 21) # user_id
|
|
|
|
self.assertEqual(request_data[7][1], "Sample User") # user_name
|
|
|
|
self.assertEqual(request_data[8][1], "@**test**") # text
|
|
|
|
self.assertEqual(request_data[9][1], "mention") # trigger_word
|
2018-07-23 17:12:08 +02:00
|
|
|
self.assertEqual(request_data[10][1], 12) # user_profile_id
|
2017-07-01 14:42:34 +02:00
|
|
|
|
2018-06-24 08:51:47 +02:00
|
|
|
@mock.patch('zerver.lib.outgoing_webhook.fail_with_message')
|
2019-02-03 00:24:01 +01:00
|
|
|
def test_build_bot_request_private_message(self, mock_fail_with_message: mock.Mock) -> None:
|
2018-06-24 08:51:47 +02:00
|
|
|
|
2018-10-11 00:45:19 +02:00
|
|
|
request_data = self.handler.build_bot_request(self.private_message_event)
|
2018-06-24 08:51:47 +02:00
|
|
|
self.assertIsNone(request_data)
|
|
|
|
self.assertTrue(mock_fail_with_message.called)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_process_success(self) -> None:
|
2018-10-10 13:16:42 +02:00
|
|
|
response = dict(response_not_required=True) # type: Dict[str, Any]
|
2018-10-09 17:12:57 +02:00
|
|
|
success_response = self.handler.process_success(response, self.stream_message_event)
|
2017-07-24 09:02:29 +02:00
|
|
|
self.assertEqual(success_response, None)
|
2017-07-01 14:42:34 +02:00
|
|
|
|
2018-10-10 13:16:42 +02:00
|
|
|
response = dict(text='test_content')
|
2018-10-09 17:12:57 +02:00
|
|
|
success_response = self.handler.process_success(response, self.stream_message_event)
|
2018-10-10 01:36:31 +02:00
|
|
|
self.assertEqual(success_response, dict(content='test_content'))
|