2017-07-22 12:49:20 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
import mock
|
|
|
|
import json
|
|
|
|
|
|
|
|
from requests.models import Response
|
2017-07-24 07:51:18 +02:00
|
|
|
from zerver.lib.outgoing_webhook import GenericOutgoingWebhookService, \
|
|
|
|
SlackOutgoingWebhookService
|
2017-07-22 12:49:20 +02:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2018-05-23 16:14:22 +02:00
|
|
|
from zerver.models import Service, get_realm, get_user
|
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:
|
2017-07-22 12:49:20 +02:00
|
|
|
self.event = {
|
|
|
|
u'command': '@**test**',
|
|
|
|
u'message': {
|
2018-05-23 16:14:22 +02:00
|
|
|
'content': '@**test**',
|
|
|
|
},
|
|
|
|
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"))
|
2017-07-22 12:49:20 +02:00
|
|
|
self.handler = GenericOutgoingWebhookService(service_name='test-service',
|
|
|
|
base_url='http://example.domain.com',
|
|
|
|
token='abcdef',
|
2018-05-23 16:14:22 +02:00
|
|
|
user_profile=self.bot_user)
|
2017-07-22 12:49:20 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_process_event(self) -> None:
|
2017-07-22 12:49:20 +02:00
|
|
|
rest_operation, request_data = self.handler.process_event(self.event)
|
|
|
|
request_data = json.loads(request_data)
|
|
|
|
self.assertEqual(request_data['data'], "@**test**")
|
|
|
|
self.assertEqual(request_data['token'], "abcdef")
|
|
|
|
self.assertEqual(rest_operation['base_url'], "http://example.domain.com")
|
|
|
|
self.assertEqual(rest_operation['method'], "POST")
|
|
|
|
self.assertEqual(request_data['message'], self.event['message'])
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_process_success(self) -> None:
|
2017-07-22 12:49:20 +02:00
|
|
|
response = mock.Mock(spec=Response)
|
|
|
|
response.text = json.dumps({"response_not_required": True})
|
2018-05-01 12:20:29 +02:00
|
|
|
success_response, _ = self.handler.process_success(response, self.event)
|
2017-07-22 12:49:20 +02:00
|
|
|
self.assertEqual(success_response, None)
|
|
|
|
|
|
|
|
response.text = json.dumps({"response_string": 'test_content'})
|
2018-05-01 12:20:29 +02:00
|
|
|
success_response, _ = self.handler.process_success(response, self.event)
|
2017-07-22 12:49:20 +02:00
|
|
|
self.assertEqual(success_response, 'test_content')
|
|
|
|
|
|
|
|
response.text = json.dumps({})
|
2018-05-01 12:20:29 +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-01 14:42:34 +02:00
|
|
|
mock_service = Service()
|
|
|
|
|
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:
|
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',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.handler = SlackOutgoingWebhookService(base_url='http://example.domain.com',
|
|
|
|
token="abcdef",
|
|
|
|
user_profile=None,
|
|
|
|
service_name='test-service')
|
|
|
|
|
2018-06-25 17:27:06 +02:00
|
|
|
def test_process_event_stream_message(self, mock_get_service_profile: mock.Mock) -> None:
|
|
|
|
rest_operation, request_data = self.handler.process_event(self.stream_message_event)
|
2017-07-01 14:42:34 +02:00
|
|
|
|
|
|
|
self.assertEqual(rest_operation['base_url'], 'http://example.domain.com')
|
|
|
|
self.assertEqual(rest_operation['method'], 'POST')
|
|
|
|
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
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_process_success(self) -> None:
|
2017-07-01 14:42:34 +02:00
|
|
|
response = mock.Mock(spec=Response)
|
|
|
|
response.text = json.dumps({"response_not_required": True})
|
2018-06-25 17:27:06 +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
|
|
|
|
|
|
|
response.text = json.dumps({"text": 'test_content'})
|
2018-06-25 17:27:06 +02:00
|
|
|
success_response, _ = self.handler.process_success(response, self.stream_message_event)
|
2017-07-01 14:42:34 +02:00
|
|
|
self.assertEqual(success_response, 'test_content')
|