mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-03 21:43:21 +00:00 
			
		
		
		
	test flake fix: Avoid logging leak for webhook tests.
We can still improve these tests to use assertLogs context managers, but this stops the tests from having logging side effects via setUp.
This commit is contained in:
		
				
					committed by
					
						
						Steve Howell
					
				
			
			
				
	
			
			
			
						parent
						
							b840a8b3ed
						
					
				
				
					commit
					38bd66d8ae
				
			@@ -1,4 +1,3 @@
 | 
				
			|||||||
import logging
 | 
					 | 
				
			||||||
from typing import Any, Optional
 | 
					from typing import Any, Optional
 | 
				
			||||||
from unittest import mock
 | 
					from unittest import mock
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -61,7 +60,6 @@ class DoRestCallTests(ZulipTestCase):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        self.bot_user = self.example_user('outgoing_webhook_bot')
 | 
					        self.bot_user = self.example_user('outgoing_webhook_bot')
 | 
				
			||||||
        self.service_handler = GenericOutgoingWebhookService("token", self.bot_user, "service")
 | 
					        self.service_handler = GenericOutgoingWebhookService("token", self.bot_user, "service")
 | 
				
			||||||
        logging.disable(logging.WARNING)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @mock.patch('zerver.lib.outgoing_webhook.send_response_message')
 | 
					    @mock.patch('zerver.lib.outgoing_webhook.send_response_message')
 | 
				
			||||||
    def test_successful_request(self, mock_send: mock.Mock) -> None:
 | 
					    def test_successful_request(self, mock_send: mock.Mock) -> None:
 | 
				
			||||||
@@ -80,7 +78,7 @@ class DoRestCallTests(ZulipTestCase):
 | 
				
			|||||||
        response = ResponseMock(500)
 | 
					        response = ResponseMock(500)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.mock_event['failed_tries'] = 3
 | 
					        self.mock_event['failed_tries'] = 3
 | 
				
			||||||
        with mock.patch('requests.request', return_value=response):
 | 
					        with mock.patch('requests.request', return_value=response), mock.patch('logging.warning'):
 | 
				
			||||||
            do_rest_call('',  None, self.mock_event, self.service_handler)
 | 
					            do_rest_call('',  None, self.mock_event, self.service_handler)
 | 
				
			||||||
            bot_owner_notification = self.get_last_message()
 | 
					            bot_owner_notification = self.get_last_message()
 | 
				
			||||||
            self.assertEqual(bot_owner_notification.content,
 | 
					            self.assertEqual(bot_owner_notification.content,
 | 
				
			||||||
@@ -92,7 +90,7 @@ The webhook got a response with status code *500*.''')
 | 
				
			|||||||
    @mock.patch('zerver.lib.outgoing_webhook.fail_with_message')
 | 
					    @mock.patch('zerver.lib.outgoing_webhook.fail_with_message')
 | 
				
			||||||
    def test_fail_request(self, mock_fail_with_message: mock.Mock) -> None:
 | 
					    def test_fail_request(self, mock_fail_with_message: mock.Mock) -> None:
 | 
				
			||||||
        response = ResponseMock(400)
 | 
					        response = ResponseMock(400)
 | 
				
			||||||
        with mock.patch('requests.request', return_value=response):
 | 
					        with mock.patch('requests.request', return_value=response), mock.patch('logging.warning'):
 | 
				
			||||||
            do_rest_call('', None, self.mock_event, self.service_handler)
 | 
					            do_rest_call('', None, self.mock_event, self.service_handler)
 | 
				
			||||||
            bot_owner_notification = self.get_last_message()
 | 
					            bot_owner_notification = self.get_last_message()
 | 
				
			||||||
            self.assertTrue(mock_fail_with_message.called)
 | 
					            self.assertTrue(mock_fail_with_message.called)
 | 
				
			||||||
@@ -103,7 +101,7 @@ The webhook got a response with status code *400*.''')
 | 
				
			|||||||
            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)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_headers(self) -> None:
 | 
					    def test_headers(self) -> None:
 | 
				
			||||||
        with mock.patch('requests.request') as mock_request:
 | 
					        with mock.patch('requests.request') as mock_request, mock.patch('logging.warning'):
 | 
				
			||||||
            do_rest_call('', 'payload-stub', self.mock_event, self.service_handler)
 | 
					            do_rest_call('', 'payload-stub', self.mock_event, self.service_handler)
 | 
				
			||||||
            kwargs = mock_request.call_args[1]
 | 
					            kwargs = mock_request.call_args[1]
 | 
				
			||||||
            self.assertEqual(kwargs['data'], 'payload-stub')
 | 
					            self.assertEqual(kwargs['data'], 'payload-stub')
 | 
				
			||||||
@@ -117,7 +115,7 @@ The webhook got a response with status code *400*.''')
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    def test_error_handling(self) -> None:
 | 
					    def test_error_handling(self) -> None:
 | 
				
			||||||
        def helper(side_effect: Any, error_text: str) -> None:
 | 
					        def helper(side_effect: Any, error_text: str) -> None:
 | 
				
			||||||
            with mock.patch('logging.info'):
 | 
					            with mock.patch('logging.warning'), mock.patch('logging.info'):
 | 
				
			||||||
                with mock.patch('requests.request', side_effect=side_effect):
 | 
					                with mock.patch('requests.request', side_effect=side_effect):
 | 
				
			||||||
                    do_rest_call('', None, self.mock_event, self.service_handler)
 | 
					                    do_rest_call('', None, self.mock_event, self.service_handler)
 | 
				
			||||||
                    bot_owner_notification = self.get_last_message()
 | 
					                    bot_owner_notification = self.get_last_message()
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user