outgoing webhooks: Warn user that PMs are not supported in Slack-format webhook.

Private messages are not supported in Slack-format webhook.
Instead of raising a NotImplementedError, we warn the user
that PM service is not supported by sending a message to the
user.

Added tests for the same.

Fixes #9239
This commit is contained in:
Rhea Parekh 2018-06-24 12:21:47 +05:30 committed by Tim Abbott
parent 2357b1e145
commit cf60b8821d
3 changed files with 35 additions and 3 deletions

View File

@ -83,7 +83,9 @@ class SlackOutgoingWebhookService(OutgoingWebhookServiceInterface):
'request_kwargs': {}}
if event['message']['type'] == 'private':
raise NotImplementedError("Private messaging service not supported.")
failure_message = "Slack outgoing webhooks don't support private messages."
fail_with_message(event, failure_message)
return None, None
request_data = [("token", self.token),
("team_id", event['message']['sender_realm_str']),

View File

@ -71,12 +71,31 @@ class TestSlackOutgoingWebhookService(ZulipTestCase):
'sender_full_name': 'Sample User',
}
}
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,
'subject': 'test',
'content': 'test content',
}
}
self.handler = SlackOutgoingWebhookService(base_url='http://example.domain.com',
token="abcdef",
user_profile=None,
service_name='test-service')
def test_process_event_stream_message(self, mock_get_service_profile: mock.Mock) -> None:
def test_process_event_stream_message(self) -> None:
rest_operation, request_data = self.handler.process_event(self.stream_message_event)
self.assertEqual(rest_operation['base_url'], 'http://example.domain.com')
@ -93,6 +112,16 @@ class TestSlackOutgoingWebhookService(ZulipTestCase):
self.assertEqual(request_data[9][1], "mention") # trigger_word
self.assertEqual(request_data[10][1], 12) # user_profile_id
@mock.patch('zerver.lib.outgoing_webhook.get_service_profile', return_value=mock_service)
@mock.patch('zerver.lib.outgoing_webhook.fail_with_message')
def test_process_event_private_message(self, mock_fail_with_message: mock.Mock,
mock_get_service_profile: mock.Mock) -> None:
rest_operation, request_data = self.handler.process_event(self.private_message_event)
self.assertIsNone(request_data)
self.assertIsNone(rest_operation)
self.assertTrue(mock_fail_with_message.called)
def test_process_success(self) -> None:
response = mock.Mock(spec=Response)
response.text = json.dumps({"response_not_required": True})

View File

@ -479,7 +479,8 @@ class OutgoingWebhookWorker(QueueProcessingWorker):
dup_event['service_name'] = str(service.name)
service_handler = get_outgoing_webhook_service_handler(service)
rest_operation, request_data = service_handler.process_event(dup_event)
do_rest_call(rest_operation, request_data, dup_event, service_handler)
if rest_operation:
do_rest_call(rest_operation, request_data, dup_event, service_handler)
@assign_queue('embedded_bots')
class EmbeddedBotWorker(QueueProcessingWorker):