Debian Codegraph @

tests/test_plugin_brevo.py

raw symbols SCIP
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.plugins.brevo import NotifyBrevo
39
40 logging.disable(logging.CRITICAL)
41
42 # Attachment Directory
43 TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var")
44
45 # a test UUID we can use
46 UUID4 = "8b799edf-6f98-4d3a-9be7-2862fb4e5752"
47
48 # Our Testing URLs
49 apprise_url_tests = (
50 (
51 "brevo://",
52 {
53 "instance": None,
54 },
55 ),
56 (
57 "brevo://:@/",
58 {
59 "instance": None,
60 },
61 ),
62 (
63 "brevo://abcd",
64 {
65 # Just an broken email (no api key or email)
66 "instance": None,
67 },
68 ),
69 (
70 "brevo://abcd@host",
71 {
72 # Just an Email specified, no API Key
73 "instance": None,
74 },
75 ),
76 (
77 "brevo://invalid-api-key+*-d:[email protected]",
78 {
79 # An invalid API Key
80 "instance": TypeError,
81 },
82 ),
83 (
84 ("brevo://abcd:[email protected]/[email protected]?reply=%20!"),
85 {
86 # An invalid Reply-To address
87 "instance": TypeError,
88 },
89 ),
90 (
91 "brevo://abcd:[email protected]?format=text",
92 {
93 # No To/Target Address(es) specified; so we sub in the same From
94 # address
95 "instance": NotifyBrevo,
96 },
97 ),
98 (
99 (
100 "brevo://abcd:[email protected]/[email protected]"
101 "[email protected]"
102 ),
103 {
104 # A good email
105 "instance": NotifyBrevo,
106 "force_debug": True,
107 },
108 ),
109 (
110 "brevo://abcd:[email protected]/bademailaddress",
111 {
112 # won't be able to send email
113 "instance": NotifyBrevo,
114 "notify_response": False,
115 },
116 ),
117 (
118 (
119 "brevo://abcd:[email protected]/[email protected]"
120 "[email protected]"
121 ),
122 {
123 # A good email with Blind Carbon Copy
124 "instance": NotifyBrevo,
125 },
126 ),
127 (
128 (
129 "brevo://abcd:[email protected]/[email protected]"
130 "[email protected]"
131 ),
132 {
133 # A good email with Carbon Copy
134 "instance": NotifyBrevo,
135 },
136 ),
137 (
138 (
139 "brevo://abcd:[email protected]/[email protected]"
140 "[email protected]"
141 ),
142 {
143 # A good email with Carbon Copy
144 "instance": NotifyBrevo,
145 },
146 ),
147 (
148 "brevo://abcd:[email protected]/[email protected]",
149 {
150 "instance": NotifyBrevo,
151 # force a failure
152 "response": False,
153 "requests_response_code": requests.codes.internal_server_error,
154 },
155 ),
156 (
157 "brevo://abcd:[email protected]/[email protected]",
158 {
159 "instance": NotifyBrevo,
160 # throw a bizzare code forcing us to fail to look it up
161 "response": False,
162 "requests_response_code": 999,
163 },
164 ),
165 (
166 "brevo://abcd:[email protected]/[email protected]",
167 {
168 "instance": NotifyBrevo,
169 # Throws a series of i/o exceptions with this flag
170 # is set and tests that we gracfully handle them
171 "test_requests_exceptions": True,
172 },
173 ),
174 )
175
176
177 def test_plugin_brevo_urls():
178 """NotifyBrevo() Apprise URLs."""
179
180 # Run our general tests
181 AppriseURLTester(tests=apprise_url_tests).run_all()
182
183
184 @mock.patch("requests.get")
185 @mock.patch("requests.post")
186 def test_plugin_brevo_edge_cases(mock_post, mock_get):
187 """NotifyBrevo() Edge Cases."""
188
189 # no apikey
190 with pytest.raises(TypeError):
191 NotifyBrevo(apikey=None, from_email="[email protected]")
192
193 # invalid from email
194 with pytest.raises(TypeError):
195 NotifyBrevo(apikey="abcd", from_email="!invalid")
196
197 # no email
198 with pytest.raises(TypeError):
199 NotifyBrevo(apikey="abcd", from_email=None)
200
201 # Invalid To email address
202 NotifyBrevo(
203 apikey="abcd", from_email="[email protected]", targets="!invalid"
204 )
205
206 # Test invalid bcc/cc entries mixed with good ones
207 assert isinstance(
208 NotifyBrevo(
209 apikey="abcd",
210 from_email="[email protected]",
211 bcc=("[email protected]", "!invalid"),
212 cc=("[email protected]", "!invalid"),
213 ),
214 NotifyBrevo,
215 )
216
217
218 @mock.patch("requests.post")
219 def test_plugin_brevo_attachments(mock_post):
220 """NotifyBrevo() Attachments."""
221
222 request = mock.Mock()
223 request.status_code = requests.codes.ok
224
225 # Prepare Mock
226 mock_post.return_value = request
227
228 path = os.path.join(TEST_VAR_DIR, "apprise-test.gif")
229 attach = AppriseAttachment(path)
230 obj = Apprise.instantiate("brevo://abcd:[email protected]")
231 assert isinstance(obj, NotifyBrevo)
232 assert (
233 obj.notify(
234 body="body",
235 title="title",
236 notify_type=NotifyType.INFO,
237 attach=attach,
238 )
239 is True
240 )
241
242 mock_post.reset_mock()
243
244 # Try again in a use case where we can't access the file
245 with mock.patch("os.path.isfile", return_value=False):
246 assert (
247 obj.notify(
248 body="body",
249 title="title",
250 notify_type=NotifyType.INFO,
251 attach=attach,
252 )
253 is False
254 )
255
256 # Try again in a use case where we can't access the file
257 with mock.patch("builtins.open", side_effect=OSError):
258 assert (
259 obj.notify(
260 body="body",
261 title="title",
262 notify_type=NotifyType.INFO,
263 attach=attach,
264 )
265 is False
266 )