Skip to content

Commit

Permalink
outgoing webhooks: Warn user that PMs are not supported in Slack-form…
Browse files Browse the repository at this point in the history
…at 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 zulip#9239
  • Loading branch information
rheaparekh authored and timabbott committed Aug 10, 2018
1 parent 2357b1e commit cf60b88
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
4 changes: 3 additions & 1 deletion zerver/lib/outgoing_webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ def process_event(self, event: Dict[str, Any]) -> Tuple[Dict[str, Any], Any]:
'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']),
Expand Down
31 changes: 30 additions & 1 deletion zerver/tests/test_outgoing_webhook_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,31 @@ def setUp(self) -> None:
'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': '[email protected]',
'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')
Expand All @@ -93,6 +112,16 @@ def test_process_event_stream_message(self, mock_get_service_profile: mock.Mock)
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})
Expand Down
3 changes: 2 additions & 1 deletion zerver/worker/queue_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,8 @@ def consume(self, event: Mapping[str, Any]) -> None:
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):
Expand Down

0 comments on commit cf60b88

Please sign in to comment.