Skip to content

Commit 8309f8b

Browse files
committed
add global sandbox mode
1 parent fd862bb commit 8309f8b

2 files changed

Lines changed: 60 additions & 2 deletions

File tree

sendgrid_backend/mail.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,15 @@ def __init__(self, *args, **kwargs):
9393
self.sg = SendGridAPIClient(**sg_args)
9494

9595
# Configure sandbox mode based on settings
96+
# SENDGRID_SANDBOX_MODE takes precedence and enables sandbox mode unconditionally
97+
# SENDGRID_SANDBOX_MODE_IN_DEBUG only enables sandbox mode when DEBUG=True
98+
sandbox_mode = get_django_setting("SENDGRID_SANDBOX_MODE", False)
9699
sandbox_mode_in_debug = get_django_setting(
97100
"SENDGRID_SANDBOX_MODE_IN_DEBUG", True
98101
)
99-
self.sandbox_mode = bool(settings.DEBUG) and bool(sandbox_mode_in_debug)
102+
self.sandbox_mode = bool(sandbox_mode) or (
103+
bool(settings.DEBUG) and bool(sandbox_mode_in_debug)
104+
)
100105

101106
if self.sandbox_mode:
102107
warnings.warn(

test/test_sandbox_mode.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,60 @@
77

88

99
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):
1164
"""
1265
Tests combinations of DEBUG, SENDGRID_SANDBOX_MODE_IN_DEBUG, and mail_settings to ensure
1366
that the behavior is as expected.

0 commit comments

Comments
 (0)