mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	Previously, this command would reliably fail: ``` tools/test-backend --skip-provision-check --parallel=3 zerver.tests.test_email_log.EmailLogTest.test_forward_address_details zerver.tests.test_email_log.EmailLogTest.test_generate_and_clear_email_log zerver.tests.test_example.TestDevelopmentEmailsLog ``` and now it reliably succeeds. :-) After hours of fiddling/googling/hair-tearing, I found that mocking-away Django Connection.send_messages() was the best: - We're testing Zulip and not Django. - Mocking at this lower level exercises more of our code. - EmailLogBackEnd._do_send_messages() helper method added to simplify mocking. Fixes #21925.
		
			
				
	
	
		
			47 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			47 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
import os
 | 
						|
from unittest import mock
 | 
						|
 | 
						|
from django.conf import settings
 | 
						|
 | 
						|
from zerver.lib.test_classes import ZulipTestCase
 | 
						|
from zproject.email_backends import get_forward_address
 | 
						|
 | 
						|
 | 
						|
class EmailLogTest(ZulipTestCase):
 | 
						|
    def test_generate_and_clear_email_log(self) -> None:
 | 
						|
        with self.settings(EMAIL_BACKEND="zproject.email_backends.EmailLogBackEnd"), mock.patch(
 | 
						|
            "zproject.email_backends.EmailLogBackEnd._do_send_messages", lambda *args: 1
 | 
						|
        ), self.assertLogs(level="INFO") as m, self.settings(DEVELOPMENT_LOG_EMAILS=True):
 | 
						|
            result = self.client_get("/emails/generate/")
 | 
						|
            self.assertEqual(result.status_code, 302)
 | 
						|
            self.assertIn("emails", result["Location"])
 | 
						|
 | 
						|
            result = self.client_get("/emails/")
 | 
						|
            self.assert_in_success_response(["All the emails sent in the Zulip"], result)
 | 
						|
 | 
						|
            result = self.client_get("/emails/clear/")
 | 
						|
            self.assertEqual(result.status_code, 302)
 | 
						|
            result = self.client_get(result["Location"])
 | 
						|
            self.assertIn("manually generate most of the emails by clicking", str(result.content))
 | 
						|
            output_log = (
 | 
						|
                "INFO:root:Emails sent in development are available at http://testserver/emails"
 | 
						|
            )
 | 
						|
            self.assertEqual(m.output, [output_log for i in range(15)])
 | 
						|
 | 
						|
    def test_forward_address_details(self) -> None:
 | 
						|
        forward_address = "forward-to@example.com"
 | 
						|
        result = self.client_post("/emails/", {"forward_address": forward_address})
 | 
						|
        self.assert_json_success(result)
 | 
						|
 | 
						|
        self.assertEqual(get_forward_address(), forward_address)
 | 
						|
 | 
						|
        with self.settings(EMAIL_BACKEND="zproject.email_backends.EmailLogBackEnd"), mock.patch(
 | 
						|
            "zproject.email_backends.EmailLogBackEnd._do_send_messages", lambda *args: 1
 | 
						|
        ):
 | 
						|
            result = self.client_get("/emails/generate/")
 | 
						|
            self.assertEqual(result.status_code, 302)
 | 
						|
            self.assertIn("emails", result["Location"])
 | 
						|
            result = self.client_get(result["Location"])
 | 
						|
            self.assert_in_success_response([forward_address], result)
 | 
						|
        os.remove(settings.FORWARD_ADDRESS_CONFIG_FILE)
 |