|
7 | 7 |
|
8 | 8 |
|
9 | 9 | class TestSandboxMode(SimpleTestCase): |
10 | | - def test_sandbox_mode(self): |
| 10 | + def test_sendgrid_sandbox_mode_setting(self): |
| 11 | + """ |
| 12 | + Tests the SENDGRID_SANDBOX_MODE setting which enables sandbox mode |
| 13 | + unconditionally, regardless of DEBUG. |
| 14 | + """ |
| 15 | + msg = EmailMessage( |
| 16 | + subject="Hello, World!", |
| 17 | + body="Hello, World!", |
| 18 | + from_email="Sam Smith <sam.smith@example.com>", |
| 19 | + to=["John Doe <john.doe@example.com>"], |
| 20 | + ) |
| 21 | + |
| 22 | + # SENDGRID_SANDBOX_MODE=True should enable sandbox mode even when DEBUG=False |
| 23 | + with override_settings(DEBUG=False, SENDGRID_SANDBOX_MODE=True): |
| 24 | + backend = SendgridBackend(api_key="stub") |
| 25 | + self.assertTrue(backend.sandbox_mode) |
| 26 | + |
| 27 | + result = backend._build_sg_mail(msg) |
| 28 | + self.assertIn("mail_settings", result) |
| 29 | + self.assertIn("sandbox_mode", result["mail_settings"]) |
| 30 | + self.assertTrue(result["mail_settings"]["sandbox_mode"]["enable"]) |
| 31 | + |
| 32 | + # SENDGRID_SANDBOX_MODE=True should enable sandbox mode when DEBUG=True |
| 33 | + with override_settings(DEBUG=True, SENDGRID_SANDBOX_MODE=True): |
| 34 | + backend = SendgridBackend(api_key="stub") |
| 35 | + self.assertTrue(backend.sandbox_mode) |
| 36 | + |
| 37 | + result = backend._build_sg_mail(msg) |
| 38 | + self.assertTrue(result["mail_settings"]["sandbox_mode"]["enable"]) |
| 39 | + |
| 40 | + # SENDGRID_SANDBOX_MODE=True takes precedence over SENDGRID_SANDBOX_MODE_IN_DEBUG=False |
| 41 | + with override_settings( |
| 42 | + DEBUG=False, SENDGRID_SANDBOX_MODE=True, SENDGRID_SANDBOX_MODE_IN_DEBUG=False |
| 43 | + ): |
| 44 | + backend = SendgridBackend(api_key="stub") |
| 45 | + self.assertTrue(backend.sandbox_mode) |
| 46 | + |
| 47 | + result = backend._build_sg_mail(msg) |
| 48 | + self.assertTrue(result["mail_settings"]["sandbox_mode"]["enable"]) |
| 49 | + |
| 50 | + # SENDGRID_SANDBOX_MODE=False should not affect existing behavior |
| 51 | + with override_settings(DEBUG=False, SENDGRID_SANDBOX_MODE=False): |
| 52 | + backend = SendgridBackend(api_key="stub") |
| 53 | + self.assertFalse(backend.sandbox_mode) |
| 54 | + |
| 55 | + result = backend._build_sg_mail(msg) |
| 56 | + self.assertFalse(result["mail_settings"]["sandbox_mode"]["enable"]) |
| 57 | + |
| 58 | + # When SENDGRID_SANDBOX_MODE is not set, fall back to existing behavior |
| 59 | + with override_settings(DEBUG=True, SENDGRID_SANDBOX_MODE_IN_DEBUG=True): |
| 60 | + backend = SendgridBackend(api_key="stub") |
| 61 | + self.assertTrue(backend.sandbox_mode) |
| 62 | + |
| 63 | + def test_sandbox_mode_in_debug(self): |
11 | 64 | """ |
12 | 65 | Tests combinations of DEBUG, SENDGRID_SANDBOX_MODE_IN_DEBUG, and mail_settings to ensure |
13 | 66 | that the behavior is as expected. |
|
0 commit comments