From 0362a1c8f41608eae918522a919824d75d6a388f Mon Sep 17 00:00:00 2001 From: Paul Oms Date: Fri, 2 Feb 2024 10:54:58 +0000 Subject: [PATCH] Fix Lazy Error in test utils, make the Webhook tests use the CLient without a secret if pynacl is unavailable --- tests/test_mailpace_webhooks.py | 19 ++++++++++++++----- tests/utils_mailpace.py | 2 +- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/tests/test_mailpace_webhooks.py b/tests/test_mailpace_webhooks.py index 990e490f..b0fdcd2b 100644 --- a/tests/test_mailpace_webhooks.py +++ b/tests/test_mailpace_webhooks.py @@ -1,5 +1,4 @@ import json -import unittest from base64 import b64encode from unittest.mock import ANY @@ -7,15 +6,22 @@ from anymail.signals import AnymailTrackingEvent from anymail.webhooks.mailpace import MailPaceTrackingWebhookView +from tests.utils import ClientWithCsrfChecks from .utils_mailpace import ClientWithMailPaceSignature, make_key from .webhook_cases import WebhookTestCase +# These tests are triggered both with and without 'pynacl' installed, +# if pynacl is unavailable, we use the ClientWithCsrfChecks class +try: + from nacl.signing import SigningKey + + PYNACL_INSTALLED = bool(SigningKey) +except ImportError: + PYNACL_INSTALLED = False + @tag("mailpace") -@unittest.skipUnless( - ClientWithMailPaceSignature, "Install 'pynacl' to run mailpace webhook tests" -) class MailPaceWebhookSecurityTestCase(WebhookTestCase): client_class = ClientWithMailPaceSignature @@ -53,7 +59,10 @@ def test_failed_signature_check(self): @tag("mailpace") class MailPaceDeliveryTestCase(WebhookTestCase): - client_class = ClientWithMailPaceSignature + if PYNACL_INSTALLED: + client_class = ClientWithMailPaceSignature + else: + client_class = ClientWithCsrfChecks def setUp(self): super().setUp() diff --git a/tests/utils_mailpace.py b/tests/utils_mailpace.py index acdf8d78..8e480e25 100644 --- a/tests/utils_mailpace.py +++ b/tests/utils_mailpace.py @@ -8,7 +8,7 @@ from nacl.signing import SigningKey except ImportError: # This will be raised if signing is attempted (and pynacl wasn't found) - VerifyKey = _LazyError( + SigningKey = _LazyError( AnymailImproperlyInstalled(missing_package="pynacl", install_extra="mailpace") )