Debian Codegraph @

tests/test_plugin_brevo.py

raw
1 # BSD 2-Clause License
2 #
3 # Apprise - Push Notification Library.
4 # Copyright (c) 2026, Chris Caron <[email protected]>
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright notice,
10 # this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright notice,
13 # this list of conditions and the following disclaimer in the documentation
14 # and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 # POSSIBILITY OF SUCH DAMAGE.
27
28 # Disable logging for a cleaner testing output
29 import logging
30 import os
31 from unittest import mock
32
33 from helpers import AppriseURLTester
34 import pytest
35 import requests
36
37 from apprise import Apprise, AppriseAttachment, NotifyType
38 from apprise.exception import AppriseImproperlyConfigured
39 from apprise.plugins.brevo import NotifyBrevo
40
41 logging.disable(logging.CRITICAL)
42
43 # Attachment Directory
44 TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var")
45
46 # a test UUID we can use
47 UUID4 = "8b799edf-6f98-4d3a-9be7-2862fb4e5752"
48
49 # Our Testing URLs
50 apprise_url_tests = (
51 (
52 "brevo://",
53 {
54 "instance": None,
55 },
56 ),
57 (
58 "brevo://:@/",
59 {
60 "instance": None,
61 },
62 ),
63 (
64 "brevo://abcd",
65 {
66 # Just an broken email (no api key or email)
67 "instance": None,
68 },
69 ),
70 (
71 "brevo://abcd@host",
72 {
73 # Just an Email specified, no API Key
74 "instance": None,
75 },
76 ),
77 (
78 "brevo://invalid-api-key+*-d:[email protected]",
79 {
80 # An invalid API Key
81 "instance": AppriseImproperlyConfigured,
82 },
83 ),
84 (
85 ("brevo://abcd:[email protected]/[email protected]?reply=%20!"),
86 {
87 # An invalid Reply-To address
88 "instance": AppriseImproperlyConfigured,
89 },
90 ),
91 (
92 "brevo://abcd:[email protected]?format=text",
93 {
94 # No To/Target Address(es) specified; so we sub in the same From
95 # address
96 "instance": NotifyBrevo,
97 },
98 ),
99 (
100 (
101 "brevo://abcd:[email protected]/[email protected]"
102 "[email protected]"
103 ),
104 {
105 # A good email
106 "instance": NotifyBrevo,
107 "force_debug": True,
108 },
109 ),
110 (
111 "brevo://abcd:[email protected]/bademailaddress",
112 {
113 # won't be able to send email
114 "instance": NotifyBrevo,
115 "notify_response": False,
116 },
117 ),
118 (
119 (
120 "brevo://abcd:[email protected]/[email protected]"
121 "[email protected]"
122 ),
123 {
124 # A good email with Blind Carbon Copy
125 "instance": NotifyBrevo,
126 },
127 ),
128 (
129 (
130 "brevo://abcd:[email protected]/[email protected]"
131 "[email protected]"
132 ),
133 {
134 # A good email with Carbon Copy
135 "instance": NotifyBrevo,
136 },
137 ),
138 (
139 (
140 "brevo://abcd:[email protected]/[email protected]"
141 "[email protected]"
142 ),
143 {
144 # A good email with Carbon Copy
145 "instance": NotifyBrevo,
146 },
147 ),
148 (
149 "brevo://abcd:[email protected]/[email protected]",
150 {
151 "instance": NotifyBrevo,
152 # force a failure
153 "response": False,
154 "requests_response_code": requests.codes.internal_server_error,
155 },
156 ),
157 (
158 "brevo://abcd:[email protected]/[email protected]",
159 {
160 "instance": NotifyBrevo,
161 # throw a bizzare code forcing us to fail to look it up
162 "response": False,
163 "requests_response_code": 999,
164 },
165 ),
166 (
167 "brevo://abcd:[email protected]/[email protected]",
168 {
169 "instance": NotifyBrevo,
170 # Throws a series of i/o exceptions with this flag
171 # is set and tests that we gracfully handle them
172 "test_requests_exceptions": True,
173 },
174 ),
175 )
176
177
178 def test_plugin_brevo_urls():
179 """NotifyBrevo() Apprise URLs."""
180
181 # Run our general tests
182 AppriseURLTester(tests=apprise_url_tests).run_all()
183
184
185 @mock.patch("requests.get")
186 @mock.patch("requests.post")
187 def test_plugin_brevo_edge_cases(mock_post, mock_get):
188 """NotifyBrevo() Edge Cases."""
189
190 # no apikey
191 with pytest.raises(AppriseImproperlyConfigured):
192 NotifyBrevo(apikey=None, from_email="[email protected]")
193
194 # invalid from email
195 with pytest.raises(AppriseImproperlyConfigured):
196 NotifyBrevo(apikey="abcd", from_email="!invalid")
197
198 # no email
199 with pytest.raises(AppriseImproperlyConfigured):
200 NotifyBrevo(apikey="abcd", from_email=None)
201
202 # Invalid To email address
203 NotifyBrevo(
204 apikey="abcd", from_email="[email protected]", targets="!invalid"
205 )
206
207 # Test invalid bcc/cc entries mixed with good ones
208 assert isinstance(
209 NotifyBrevo(
210 apikey="abcd",
211 from_email="[email protected]",
212 bcc=("[email protected]", "!invalid"),
213 cc=("[email protected]", "!invalid"),
214 ),
215 NotifyBrevo,
216 )
217
218
219 @mock.patch("requests.post")
220 def test_plugin_brevo_attachments(mock_post):
221 """NotifyBrevo() Attachments."""
222
223 request = mock.Mock()
224 request.status_code = requests.codes.ok
225
226 # Prepare Mock
227 mock_post.return_value = request
228
229 path = os.path.join(TEST_VAR_DIR, "apprise-test.gif")
230 attach = AppriseAttachment(path)
231 obj = Apprise.instantiate("brevo://abcd:[email protected]")
232 assert isinstance(obj, NotifyBrevo)
233 assert (
234 bool(
235 obj.notify(
236 body="body",
237 title="title",
238 notify_type=NotifyType.INFO,
239 attach=attach,
240 )
241 )
242 is True
243 )
244
245 mock_post.reset_mock()
246
247 # Try again in a use case where we can't access the file
248 with mock.patch("os.path.isfile", return_value=False):
249 assert (
250 bool(
251 obj.notify(
252 body="body",
253 title="title",
254 notify_type=NotifyType.INFO,
255 attach=attach,
256 )
257 )
258 is False
259 )
260
261 # Try again in a use case where we can't access the file
262 with mock.patch("builtins.open", side_effect=OSError):
263 assert (
264 bool(
265 obj.notify(
266 body="body",
267 title="title",
268 notify_type=NotifyType.INFO,
269 attach=attach,
270 )
271 )
272 is False
273 )