Debian Codegraph @

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