Skip to content

Commit c87f358

Browse files
committed
Fix failing tests, due to unset webhook key, correct warning message
1 parent 30749b7 commit c87f358

File tree

3 files changed

+33
-26
lines changed

3 files changed

+33
-26
lines changed

anymail/webhooks/mailpace.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class MailPaceTrackingWebhookView(MailPaceBaseWebhookView):
5252

5353
def __init__(self, **kwargs):
5454
try:
55-
get_anymail_setting(
55+
self.webhook_key = get_anymail_setting(
5656
"webhook_key", esp_name=self.esp_name, kwargs=kwargs, allow_bare=True
5757
)
5858
except AnymailConfigurationError:
@@ -75,26 +75,30 @@ def __init__(self, **kwargs):
7575
# MailPace doesn't send a signature for inbound webhooks, yet
7676
# When/if MailPace does this, move this to the parent class
7777
def validate_request(self, request):
78-
try:
79-
signature_base64 = request.headers["X-MailPace-Signature"]
80-
signature = base64.b64decode(signature_base64)
81-
except (KeyError, binascii.Error):
82-
raise AnymailWebhookValidationFailure(
83-
"MailPace webhook called with invalid or missing signature"
84-
)
85-
86-
verify_key_base64 = self.webhook_key
87-
88-
verify_key = VerifyKey(base64.b64decode(verify_key_base64))
89-
90-
message = request.body
91-
92-
try:
93-
verify_key.verify(message, signature)
94-
except (CryptoError, ValueError):
95-
raise AnymailWebhookValidationFailure(
96-
"MailPace webhook called with incorrect signature"
97-
)
78+
if self.webhook_key:
79+
try:
80+
signature_base64 = request.headers["X-MailPace-Signature"]
81+
signature = base64.b64decode(signature_base64)
82+
except (KeyError, binascii.Error):
83+
raise AnymailWebhookValidationFailure(
84+
"MailPace webhook called with invalid or missing signature"
85+
)
86+
87+
verify_key_base64 = self.webhook_key
88+
89+
verify_key = VerifyKey(base64.b64decode(verify_key_base64))
90+
91+
message = request.body
92+
93+
try:
94+
verify_key.verify(message, signature)
95+
except (CryptoError, ValueError):
96+
raise AnymailWebhookValidationFailure(
97+
"MailPace webhook called with incorrect signature"
98+
)
99+
else:
100+
return True
101+
# No webhook key set
98102

99103
def esp_to_anymail_event(self, esp_event):
100104
event_type = self.event_record_types.get(esp_event["event"], EventType.UNKNOWN)

tests/test_mailpace_webhooks.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@
2323

2424

2525
@tag("mailpace")
26-
@unittest.skipUnless(PYNACL_INSTALLED, "pynacl is not installed")
26+
@unittest.skipUnless(PYNACL_INSTALLED, "Install Pynacl to run MailPace Webhook Tests")
2727
class MailPaceWebhookSecurityTestCase(WebhookTestCase):
2828
client_class = ClientWithMailPaceSignature
2929

3030
def setUp(self):
3131
super().setUp()
32-
self.clear_basic_auth()
3332
self.client.set_private_key(make_key())
3433

3534
def test_failed_signature_check(self):
@@ -59,13 +58,12 @@ def test_failed_signature_check(self):
5958

6059

6160
@tag("mailpace")
62-
@unittest.skipUnless(PYNACL_INSTALLED, "pynacl is not installed")
61+
@unittest.skipUnless(PYNACL_INSTALLED, "Install Pynacl to run MailPace Webhook Tests")
6362
class MailPaceDeliveryTestCase(WebhookTestCase):
6463
client_class = ClientWithMailPaceSignature
6564

6665
def setUp(self):
6766
super().setUp()
68-
self.clear_basic_auth()
6967
self.client.set_private_key(make_key())
7068

7169
def test_queued_event(self):

tests/utils_mailpace.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ def post(self, *args, **kwargs):
4949
headers["X-MailPace-Signature"] = signature
5050

5151
webhook_key = derive_public_webhook_key(self.private_key)
52-
with override_settings(ANYMAIL={"MAILPACE_WEBHOOK_KEY": webhook_key}):
52+
with override_settings(
53+
ANYMAIL={
54+
"MAILPACE_WEBHOOK_KEY": webhook_key,
55+
"WEBHOOK_SECRET": "username:password",
56+
}
57+
):
5358
# Django 4.2+ test Client allows headers=headers;
5459
# before that, must convert to HTTP_ args:
5560
return super().post(

0 commit comments

Comments
 (0)