Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

outgoing webhook tests: Move code out of with blocks.

Steve Howell924292ba65bf
diff --git a/zerver/tests/test_outgoing_webhook_system.py b/zerver/tests/test_outgoing_webhook_system.py
index c518f3ded36..7905f996135 100644
--- a/zerver/tests/test_outgoing_webhook_system.py
+++ b/zerver/tests/test_outgoing_webhook_system.py
@@ -66,13 +66,13 @@ class DoRestCallTests(ZulipTestCase):
response = ResponseMock(200, dict(content='whatever'))
with mock.patch('requests.request', return_value=response):
do_rest_call('', None, self.mock_event, self.service_handler)
- self.assertTrue(mock_send.called)
+ self.assertTrue(mock_send.called)
for service_class in [GenericOutgoingWebhookService, SlackOutgoingWebhookService]:
handler = service_class("token", self.bot_user, "service")
with mock.patch('requests.request', return_value=response):
do_rest_call('', None, self.mock_event, handler)
- self.assertTrue(mock_send.called)
+ self.assertTrue(mock_send.called)
def test_retry_request(self: mock.Mock) -> None:
response = ResponseMock(500)
@@ -80,11 +80,12 @@ class DoRestCallTests(ZulipTestCase):
self.mock_event['failed_tries'] = 3
with mock.patch('requests.request', return_value=response), mock.patch('logging.warning'):
do_rest_call('', None, self.mock_event, self.service_handler)
- bot_owner_notification = self.get_last_message()
- self.assertEqual(bot_owner_notification.content,
- '''[A message](http://zulip.testserver/#narrow/stream/999-Verona/topic/Foo/near/) triggered an outgoing webhook.
+
+ bot_owner_notification = self.get_last_message()
+ self.assertEqual(bot_owner_notification.content,
+ '''[A message](http://zulip.testserver/#narrow/stream/999-Verona/topic/Foo/near/) triggered an outgoing webhook.
The webhook got a response with status code *500*.''')
- self.assertEqual(bot_owner_notification.recipient_id, self.bot_user.bot_owner.id)
+ self.assertEqual(bot_owner_notification.recipient_id, self.bot_user.bot_owner.id)
self.mock_event['failed_tries'] = 0
@mock.patch('zerver.lib.outgoing_webhook.fail_with_message')
@@ -92,37 +93,40 @@ The webhook got a response with status code *500*.''')
response = ResponseMock(400)
with mock.patch('requests.request', return_value=response), mock.patch('logging.warning'):
do_rest_call('', None, self.mock_event, self.service_handler)
- bot_owner_notification = self.get_last_message()
- self.assertTrue(mock_fail_with_message.called)
- self.assertEqual(bot_owner_notification.content,
- '''[A message](http://zulip.testserver/#narrow/stream/999-Verona/topic/Foo/near/) triggered an outgoing webhook.
+
+ bot_owner_notification = self.get_last_message()
+ self.assertTrue(mock_fail_with_message.called)
+ self.assertEqual(bot_owner_notification.content,
+ '''[A message](http://zulip.testserver/#narrow/stream/999-Verona/topic/Foo/near/) triggered an outgoing webhook.
The webhook got a response with status code *400*.''')
- assert self.bot_user.bot_owner is not None
- self.assertEqual(bot_owner_notification.recipient_id, self.bot_user.bot_owner.id)
+ assert self.bot_user.bot_owner is not None
+ self.assertEqual(bot_owner_notification.recipient_id, self.bot_user.bot_owner.id)
def test_headers(self) -> None:
with mock.patch('requests.request') as mock_request, mock.patch('logging.warning'):
do_rest_call('', 'payload-stub', self.mock_event, self.service_handler)
- kwargs = mock_request.call_args[1]
- self.assertEqual(kwargs['data'], 'payload-stub')
- user_agent = 'ZulipOutgoingWebhook/' + ZULIP_VERSION
- headers = {
- 'content-type': 'application/json',
- 'User-Agent': user_agent,
- }
- self.assertEqual(kwargs['headers'], headers)
+ kwargs = mock_request.call_args[1]
+ self.assertEqual(kwargs['data'], 'payload-stub')
+
+ user_agent = 'ZulipOutgoingWebhook/' + ZULIP_VERSION
+ headers = {
+ 'content-type': 'application/json',
+ 'User-Agent': user_agent,
+ }
+ self.assertEqual(kwargs['headers'], headers)
def test_error_handling(self) -> None:
def helper(side_effect: Any, error_text: str) -> None:
with mock.patch('logging.warning'), mock.patch('logging.info'):
with mock.patch('requests.request', side_effect=side_effect):
do_rest_call('', None, self.mock_event, self.service_handler)
- bot_owner_notification = self.get_last_message()
- self.assertIn(error_text, bot_owner_notification.content)
- self.assertIn('triggered', bot_owner_notification.content)
- assert self.bot_user.bot_owner is not None
- self.assertEqual(bot_owner_notification.recipient_id, self.bot_user.bot_owner.id)
+
+ bot_owner_notification = self.get_last_message()
+ self.assertIn(error_text, bot_owner_notification.content)
+ self.assertIn('triggered', bot_owner_notification.content)
+ assert self.bot_user.bot_owner is not None
+ self.assertEqual(bot_owner_notification.recipient_id, self.bot_user.bot_owner.id)
helper(side_effect=timeout_error, error_text='A timeout occurred.')
helper(side_effect=connection_error, error_text='A connection error occurred.')