File: test_sendinblue_deprecations.py

package info (click to toggle)
django-anymail 13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 2,480 kB
  • sloc: python: 27,832; makefile: 132; javascript: 33; sh: 9
file content (117 lines) | stat: -rw-r--r-- 4,643 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from unittest.mock import ANY

from django.core.mail import EmailMessage, send_mail
from django.test import ignore_warnings, override_settings, tag

from anymail.exceptions import AnymailConfigurationError, AnymailDeprecationWarning
from anymail.webhooks.sendinblue import (
    SendinBlueInboundWebhookView,
    SendinBlueTrackingWebhookView,
)

from .mock_requests_backend import RequestsBackendMockAPITestCase
from .webhook_cases import WebhookTestCase


@tag("brevo", "sendinblue")
@override_settings(
    EMAIL_BACKEND="anymail.backends.sendinblue.EmailBackend",
    ANYMAIL={"SENDINBLUE_API_KEY": "test_api_key"},
)
@ignore_warnings(category=AnymailDeprecationWarning)
class SendinBlueBackendDeprecationTests(RequestsBackendMockAPITestCase):
    DEFAULT_RAW_RESPONSE = (
        b'{"messageId":"<201801020304.1234567890@smtp-relay.mailin.fr>"}'
    )
    DEFAULT_STATUS_CODE = 201  # Brevo v3 uses '201 Created' for success (in most cases)

    def test_deprecation_warning(self):
        message = EmailMessage(
            "Subject", "Body", "from@example.com", ["to@example.com"]
        )
        with self.assertWarnsMessage(
            AnymailDeprecationWarning,
            "`anymail.backends.sendinblue.EmailBackend` has been renamed"
            " `anymail.backends.brevo.EmailBackend`.",
        ):
            message.send()
        self.assert_esp_called("https://api.brevo.com/v3/smtp/email")

    @override_settings(ANYMAIL={"BREVO_API_KEY": "test_api_key"})
    def test_missing_api_key_error_uses_correct_setting_name(self):
        # The sendinblue.EmailBackend requires SENDINBLUE_ settings names
        with self.assertRaisesMessage(AnymailConfigurationError, "SENDINBLUE_API_KEY"):
            send_mail("Subject", "Body", "from@example.com", ["to@example.com"])


@tag("brevo", "sendinblue")
@ignore_warnings(category=AnymailDeprecationWarning)
class SendinBlueTrackingWebhookDeprecationTests(WebhookTestCase):
    def test_deprecation_warning(self):
        with self.assertWarnsMessage(
            AnymailDeprecationWarning,
            "Anymail's SendinBlue webhook URLs are deprecated.",
        ):
            response = self.client.post(
                "/anymail/sendinblue/tracking/",
                content_type="application/json",
                data="{}",
            )
        self.assertEqual(response.status_code, 200)
        # Old url uses old names to preserve compatibility:
        self.assert_handler_called_once_with(
            self.tracking_handler,
            sender=SendinBlueTrackingWebhookView,  # *not* BrevoTrackingWebhookView
            event=ANY,
            esp_name="SendinBlue",  # *not* "Brevo"
        )

    def test_misconfigured_inbound(self):
        # Uses old esp_name when called on old URL
        errmsg = (
            "You seem to have set Brevo's *inbound* webhook URL"
            " to Anymail's SendinBlue *tracking* webhook URL."
        )
        with self.assertRaisesMessage(AnymailConfigurationError, errmsg):
            self.client.post(
                "/anymail/sendinblue/tracking/",
                content_type="application/json",
                data={"items": []},
            )


@tag("brevo", "sendinblue")
@override_settings(ANYMAIL_SENDINBLUE_API_KEY="test-api-key")
@ignore_warnings(category=AnymailDeprecationWarning)
class SendinBlueInboundWebhookDeprecationTests(WebhookTestCase):
    def test_deprecation_warning(self):
        with self.assertWarnsMessage(
            AnymailDeprecationWarning,
            "Anymail's SendinBlue webhook URLs are deprecated.",
        ):
            response = self.client.post(
                "/anymail/sendinblue/inbound/",
                content_type="application/json",
                data='{"items":[{}]}',
            )
        self.assertEqual(response.status_code, 200)
        # Old url uses old names to preserve compatibility:
        self.assert_handler_called_once_with(
            self.inbound_handler,
            sender=SendinBlueInboundWebhookView,  # *not* BrevoInboundWebhookView
            event=ANY,
            esp_name="SendinBlue",  # *not* "Brevo"
        )

    def test_misconfigured_tracking(self):
        # Uses old esp_name when called on old URL
        errmsg = (
            "You seem to have set Brevo's *tracking* webhook URL"
            " to Anymail's SendinBlue *inbound* webhook URL."
        )
        with self.assertRaisesMessage(AnymailConfigurationError, errmsg):
            self.client.post(
                "/anymail/sendinblue/inbound/",
                content_type="application/json",
                data={"event": "delivered"},
            )