Skip to content

Commit c889807

Browse files
committed
Warn if basic auth is not on, and signature validation is not setup
1 parent c87f358 commit c889807

File tree

3 files changed

+66
-5
lines changed

3 files changed

+66
-5
lines changed

anymail/webhooks/mailpace.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ def __init__(self, **kwargs):
5555
self.webhook_key = get_anymail_setting(
5656
"webhook_key", esp_name=self.esp_name, kwargs=kwargs, allow_bare=True
5757
)
58+
self.warn_if_no_basic_auth = False
5859
except AnymailConfigurationError:
5960
self.webhook_key = None
61+
self.warn_if_no_basic_auth = True
6062

6163
super().__init__(**kwargs)
6264

tests/test_mailpace_webhooks.py

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
from anymail.signals import AnymailTrackingEvent
99
from anymail.webhooks.mailpace import MailPaceTrackingWebhookView
1010

11-
from .utils_mailpace import ClientWithMailPaceSignature, make_key
11+
from .utils_mailpace import (
12+
ClientWithMailPaceBasicAuth,
13+
ClientWithMailPaceSignature,
14+
make_key,
15+
)
1216
from .webhook_cases import WebhookTestCase
1317

14-
# These tests are triggered both with and without 'pynacl' installed,
15-
# without the ability to generate a signing key, there is no way to test
16-
# the webhook signature validation.
18+
# These tests are triggered both with and without 'pynacl' installed
1719
try:
1820
from nacl.signing import SigningKey
1921

@@ -23,7 +25,9 @@
2325

2426

2527
@tag("mailpace")
26-
@unittest.skipUnless(PYNACL_INSTALLED, "Install Pynacl to run MailPace Webhook Tests")
28+
@unittest.skipUnless(
29+
PYNACL_INSTALLED, "Install Pynacl to run MailPace Webhook Signature Tests"
30+
)
2731
class MailPaceWebhookSecurityTestCase(WebhookTestCase):
2832
client_class = ClientWithMailPaceSignature
2933

@@ -57,6 +61,54 @@ def test_failed_signature_check(self):
5761
self.assertEqual(response.status_code, 400)
5862

5963

64+
@unittest.skipIf(PYNACL_INSTALLED, "Pynacl is not available, fallback to basic auth")
65+
class MailPaceWebhookBasicAuthTestCase(WebhookTestCase):
66+
client_class = ClientWithMailPaceBasicAuth
67+
68+
def setUp(self):
69+
super().setUp()
70+
71+
def test_queued_event(self):
72+
raw_event = {
73+
"event": "email.queued",
74+
"payload": {
75+
"status": "queued",
76+
"id": 1,
77+
"domain_id": 1,
78+
"created_at": "2021-11-16T14:50:15.445Z",
79+
"updated_at": "2021-11-16T14:50:15.445Z",
80+
"from": "sender@example.com",
81+
"to": "queued@example.com",
82+
"htmlbody": "string",
83+
"textbody": "string",
84+
"cc": "string",
85+
"bcc": "string",
86+
"subject": "string",
87+
"replyto": "string",
88+
"message_id": "string",
89+
"list_unsubscribe": "string",
90+
"tags": ["string", "string"],
91+
},
92+
}
93+
response = self.client.post(
94+
"/anymail/mailpace/tracking/",
95+
content_type="application/json",
96+
data=json.dumps(raw_event),
97+
)
98+
self.assertEqual(response.status_code, 200)
99+
kwargs = self.assert_handler_called_once_with(
100+
self.tracking_handler,
101+
sender=MailPaceTrackingWebhookView,
102+
event=ANY,
103+
esp_name="MailPace",
104+
)
105+
event = kwargs["event"]
106+
self.assertIsInstance(event, AnymailTrackingEvent)
107+
self.assertEqual(event.event_type, "queued")
108+
self.assertEqual(event.message_id, "string")
109+
self.assertEqual(event.recipient, "queued@example.com")
110+
111+
60112
@tag("mailpace")
61113
@unittest.skipUnless(PYNACL_INSTALLED, "Install Pynacl to run MailPace Webhook Tests")
62114
class MailPaceDeliveryTestCase(WebhookTestCase):

tests/utils_mailpace.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,11 @@ def post(self, *args, **kwargs):
6767
)
6868

6969

70+
class _ClientWithMailPaceBasicAuth(ClientWithCsrfChecks):
71+
def post(self, *args, **kwargs):
72+
with override_settings(ANYMAIL={"WEBHOOK_SECRET": "username:password"}):
73+
return super().post(*args, **kwargs)
74+
75+
7076
ClientWithMailPaceSignature = _ClientWithMailPaceSignature
77+
ClientWithMailPaceBasicAuth = _ClientWithMailPaceBasicAuth

0 commit comments

Comments
 (0)