Debian Codegraph @

tests/test_plugin_resend.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.resend import NotifyResend
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 resend api key
47 UUID4 = "re_FmABCDEF_987654321zqbabc123abc8fw"
48
49 # Our Testing URLs
50 apprise_url_tests = (
51 (
52 "resend://",
53 {
54 "instance": AppriseImproperlyConfigured,
55 },
56 ),
57 (
58 "resend://:@/",
59 {
60 "instance": AppriseImproperlyConfigured,
61 },
62 ),
63 (
64 "resend://abcd",
65 {
66 # Just an broken email (no api key or email)
67 "instance": AppriseImproperlyConfigured,
68 },
69 ),
70 (
71 "resend://abcd@host",
72 {
73 # Just an Email specified, no API Key
74 "instance": AppriseImproperlyConfigured,
75 },
76 ),
77 (
78 "resend://invalid-api-key+*-d:[email protected]",
79 {
80 # An invalid API Key
81 "instance": AppriseImproperlyConfigured,
82 },
83 ),
84 (
85 "resend://abcd:[email protected]",
86 {
87 # No To/Target Address(es) specified; so we sub in the same From
88 # address
89 "instance": NotifyResend,
90 },
91 ),
92 (
93 "resend://abcd:[email protected]/[email protected]",
94 {
95 # A good email
96 "instance": NotifyResend,
97 },
98 ),
99 (
100 "resend://abcd:[email protected]/[email protected]?name=Jessica",
101 {
102 # A good email
103 "instance": NotifyResend,
104 "privacy_url": "resend://a...d:[email protected]/[email protected]",
105 "url_matches": r"name=Jessica",
106 },
107 ),
108 (
109 (
110 "resend://abcd@newuser4%40example.com?name=Ralph"
111 "&[email protected]"
112 ),
113 {
114 # A good email
115 "instance": NotifyResend,
116 "privacy_url": "resend://a...d:[email protected]/",
117 "url_matches": r"name=Ralph",
118 "force_debug": True,
119 },
120 ),
121 (
122 (
123 "resend://?apikey=abcd&from=Joe<[email protected]>"
124 "&[email protected]"
125 ),
126 {
127 # A good email
128 "instance": NotifyResend,
129 "privacy_url": "resend://a...d:[email protected]/[email protected]",
130 "url_matches": r"name=Joe",
131 },
132 ),
133 (
134 (
135 "resend://?apikey=abcd&from=Joe<[email protected]>"
136 "&reply=John<[email protected]>"
137 ),
138 {
139 # A good email
140 "instance": NotifyResend,
141 "privacy_url": "resend://a...d:[email protected]",
142 "url_matches": r"reply=John",
143 },
144 ),
145 (
146 ("resend://?apikey=abcd&from=Joe<[email protected]>&reply=garbage%"),
147 {
148 # A good email but has a garbage reply-to value
149 "instance": NotifyResend,
150 },
151 ),
152 (
153 (
154 "resend://abcd:[email protected]/[email protected]"
155 "[email protected]"
156 ),
157 {
158 # A good email with Blind Carbon Copy
159 "instance": NotifyResend,
160 },
161 ),
162 (
163 "resend://abcd:[email protected]/[email protected][email protected]",
164 {
165 # A good email with Carbon Copy
166 "instance": NotifyResend,
167 },
168 ),
169 (
170 (
171 "resend://abcd:[email protected]/[email protected]?"
172 "cc=Chris<[email protected]>"
173 ),
174 {
175 # A good email with Carbon Copy + Name
176 "instance": NotifyResend,
177 },
178 ),
179 (
180 "resend://abcd:[email protected]/[email protected][email protected]",
181 {
182 # A good email with Carbon Copy
183 "instance": NotifyResend,
184 },
185 ),
186 (
187 "resend://abcd:[email protected]/[email protected]",
188 {
189 "instance": NotifyResend,
190 # force a failure
191 "response": False,
192 "requests_response_code": requests.codes.internal_server_error,
193 },
194 ),
195 (
196 "resend://abcd:[email protected]/[email protected]",
197 {
198 "instance": NotifyResend,
199 # throw a bizarre code forcing us to fail to look it up
200 "response": False,
201 "requests_response_code": 999,
202 },
203 ),
204 (
205 "resend://abcd:[email protected]/[email protected]",
206 {
207 "instance": NotifyResend,
208 # Throws a series of i/o exceptions with this flag
209 # is set and tests that we gracefully handle them
210 "test_requests_exceptions": True,
211 },
212 ),
213 )
214
215
216 def test_plugin_resend_urls():
217 """NotifyResend() Apprise URLs."""
218
219 # Run our general tests
220 AppriseURLTester(tests=apprise_url_tests).run_all()
221
222
223 @mock.patch("requests.get")
224 @mock.patch("requests.post")
225 def test_plugin_resend_edge_cases(mock_post, mock_get):
226 """NotifyResend() Edge Cases."""
227
228 # no apikey
229 with pytest.raises(AppriseImproperlyConfigured):
230 NotifyResend(apikey=None, from_addr="[email protected]")
231
232 # invalid from email
233 with pytest.raises(AppriseImproperlyConfigured):
234 NotifyResend(apikey="abcd", from_addr="!invalid")
235
236 # no email
237 with pytest.raises(AppriseImproperlyConfigured):
238 NotifyResend(apikey="abcd", from_addr=None)
239
240 # Invalid To email address
241 NotifyResend(
242 apikey="abcd", from_addr="[email protected]", targets="!invalid"
243 )
244
245 # Test invalid bcc/cc entries mixed with good ones
246 assert isinstance(
247 NotifyResend(
248 apikey="abcd",
249 from_addr="[email protected]",
250 bcc=("[email protected]", "!invalid"),
251 cc=("[email protected]", "!invalid"),
252 ),
253 NotifyResend,
254 )
255
256
257 @mock.patch("requests.get")
258 @mock.patch("requests.post")
259 def test_plugin_resend_attachments(mock_post, mock_get):
260 """NotifyResend() Attachments."""
261
262 request = mock.Mock()
263 request.status_code = requests.codes.ok
264
265 # Prepare Mock
266 mock_post.return_value = request
267 mock_get.return_value = request
268
269 path = os.path.join(TEST_VAR_DIR, "apprise-test.gif")
270 attach = AppriseAttachment(path)
271 obj = Apprise.instantiate("resend://abcd:[email protected]")
272 assert isinstance(obj, NotifyResend)
273 assert (
274 bool(
275 obj.notify(
276 body="body",
277 title="title",
278 notify_type=NotifyType.INFO,
279 attach=attach,
280 )
281 )
282 is True
283 )
284
285 mock_post.reset_mock()
286 mock_get.reset_mock()
287
288 # Try again in a use case where we can't access the file
289 with mock.patch("os.path.isfile", return_value=False):
290 assert (
291 bool(
292 obj.notify(
293 body="body",
294 title="title",
295 notify_type=NotifyType.INFO,
296 attach=attach,
297 )
298 )
299 is False
300 )
301
302 # Try again in a use case where we can't access the file
303 with mock.patch("builtins.open", side_effect=OSError):
304 assert (
305 bool(
306 obj.notify(
307 body="body",
308 title="title",
309 notify_type=NotifyType.INFO,
310 attach=attach,
311 )
312 )
313 is False
314 )