mirror of
https://github.com/pacnpal/django-anymail.git
synced 2025-12-20 11:51:05 -05:00
Added support for signed webhooks
See http://help.mandrill.com/entries/23704122-Authenticating-webhook-request s
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
from base64 import b64encode
|
||||
import hashlib
|
||||
import hmac
|
||||
import json
|
||||
|
||||
from django.test import TestCase
|
||||
@@ -39,6 +42,37 @@ class DjrillWebhookSecretMixinTests(TestCase):
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
|
||||
class DjrillWebhookSignatureMixinTests(TestCase):
|
||||
"""
|
||||
Test mixin used in optional Mandrill webhook signature support
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
settings.DJRILL_WEBHOOK_SECRET = 'abc123'
|
||||
settings.DJRILL_WEBHOOK_SIGNATURE_KEY = "signature"
|
||||
settings.DJRILL_WEBHOOK_URL = "/webhook/?secret=abc123"
|
||||
|
||||
def test_incorrect_settings(self):
|
||||
del settings.DJRILL_WEBHOOK_URL
|
||||
with self.assertRaises(ImproperlyConfigured):
|
||||
self.client.post('/webhook/?secret=abc123')
|
||||
settings.DJRILL_WEBHOOK_URL = "/webhook/?secret=abc123"
|
||||
|
||||
def test_unauthorized(self):
|
||||
settings.DJRILL_WEBHOOK_SIGNATURE_KEY = "anothersignature"
|
||||
response = self.client.post(settings.DJRILL_WEBHOOK_URL)
|
||||
self.assertEqual(response.status_code, 403)
|
||||
|
||||
def test_signature(self):
|
||||
signature = hmac.new(key=settings.DJRILL_WEBHOOK_SIGNATURE_KEY, msg = settings.DJRILL_WEBHOOK_URL+"mandrill_events[]", digestmod=hashlib.sha1)
|
||||
hash_string = b64encode(signature.digest())
|
||||
response = self.client.post('/webhook/?secret=abc123', data={"mandrill_events":"[]"}, **{"X-Mandrill-Signature" : hash_string})
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def tearDown(self):
|
||||
del settings.DJRILL_WEBHOOK_SIGNATURE_KEY
|
||||
del settings.DJRILL_WEBHOOK_URL
|
||||
|
||||
class DjrillWebhookViewTests(TestCase):
|
||||
"""
|
||||
Test optional Mandrill webhook view
|
||||
|
||||
Reference in New Issue
Block a user