|
8 | 8 | from anymail.signals import AnymailTrackingEvent |
9 | 9 | from anymail.webhooks.mailpace import MailPaceTrackingWebhookView |
10 | 10 |
|
11 | | -from .utils_mailpace import ClientWithMailPaceSignature, make_key |
| 11 | +from .utils_mailpace import ( |
| 12 | + ClientWithMailPaceBasicAuth, |
| 13 | + ClientWithMailPaceSignature, |
| 14 | + make_key, |
| 15 | +) |
12 | 16 | from .webhook_cases import WebhookTestCase |
13 | 17 |
|
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 |
17 | 19 | try: |
18 | 20 | from nacl.signing import SigningKey |
19 | 21 |
|
|
23 | 25 |
|
24 | 26 |
|
25 | 27 | @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 | +) |
27 | 31 | class MailPaceWebhookSecurityTestCase(WebhookTestCase): |
28 | 32 | client_class = ClientWithMailPaceSignature |
29 | 33 |
|
@@ -57,6 +61,54 @@ def test_failed_signature_check(self): |
57 | 61 | self.assertEqual(response.status_code, 400) |
58 | 62 |
|
59 | 63 |
|
| 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 | + |
60 | 112 | @tag("mailpace") |
61 | 113 | @unittest.skipUnless(PYNACL_INSTALLED, "Install Pynacl to run MailPace Webhook Tests") |
62 | 114 | class MailPaceDeliveryTestCase(WebhookTestCase): |
|
0 commit comments