Debian Codegraph @

tests/test_plugin_smtp2go.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 requests
35
36 from apprise import Apprise, AppriseAttachment, NotifyType
37 from apprise.plugins.smtp2go import NotifySMTP2Go
38
39 logging.disable(logging.CRITICAL)
40
41 # Attachment Directory
42 TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var")
43
44 # Our Testing URLs
45 apprise_url_tests = (
46 (
47 "smtp2go://",
48 {
49 "instance": TypeError,
50 },
51 ),
52 (
53 "smtp2go://:@/",
54 {
55 "instance": TypeError,
56 },
57 ),
58 # No Token specified
59 (
60 "smtp2go://[email protected]",
61 {
62 "instance": TypeError,
63 },
64 ),
65 # Token is valid, but no user name specified
66 (
67 "smtp2go://localhost.localdomain/{}-{}-{}".format(
68 "a" * 32, "b" * 8, "c" * 8
69 ),
70 {
71 "instance": TypeError,
72 },
73 ),
74 # Invalid from email address
75 (
76 'smtp2go://"@localhost.localdomain/{}-{}-{}'.format(
77 "a" * 32, "b" * 8, "c" * 8
78 ),
79 {
80 "instance": TypeError,
81 },
82 ),
83 # No To email address, but everything else is valid
84 (
85 "smtp2go://[email protected]/{}-{}-{}".format(
86 "a" * 32, "b" * 8, "c" * 8
87 ),
88 {
89 "instance": NotifySMTP2Go,
90 },
91 ),
92 (
93 "smtp2go://[email protected]/{}-{}-{}?format=markdown".format(
94 "a" * 32, "b" * 8, "c" * 8
95 ),
96 {
97 "instance": NotifySMTP2Go,
98 },
99 ),
100 (
101 "smtp2go://[email protected]/{}-{}-{}?format=html".format(
102 "a" * 32, "b" * 8, "c" * 8
103 ),
104 {
105 "instance": NotifySMTP2Go,
106 },
107 ),
108 (
109 "smtp2go://[email protected]/{}-{}-{}?format=text".format(
110 "a" * 32, "b" * 8, "c" * 8
111 ),
112 {
113 "instance": NotifySMTP2Go,
114 },
115 ),
116 # headers
117 (
118 "smtp2go://[email protected]/{}-{}-{}"
119 "?+X-Customer-Campaign-ID=Apprise".format("a" * 32, "b" * 8, "c" * 8),
120 {
121 "instance": NotifySMTP2Go,
122 "force_debug": True,
123 },
124 ),
125 # bcc and cc
126 (
127 "smtp2go://[email protected]/{}-{}-{}"
128 "[email protected]&[email protected]".format(
129 "a" * 32, "b" * 8, "c" * 8
130 ),
131 {
132 "instance": NotifySMTP2Go,
133 },
134 ),
135 # One To Email address
136 (
137 "smtp2go://[email protected]/{}-{}-{}/[email protected]".format(
138 "a" * 32, "b" * 8, "c" * 8
139 ),
140 {
141 "instance": NotifySMTP2Go,
142 },
143 ),
144 (
145 "smtp2go://[email protected]/"
146 "{}-{}-{}[email protected]".format("a" * 32, "b" * 8, "c" * 8),
147 {"instance": NotifySMTP2Go},
148 ),
149 # One To Email address, a from name specified too
150 (
151 "smtp2go://[email protected]/{}-{}-{}/"
152 '[email protected]?name="Frodo"'.format("a" * 32, "b" * 8, "c" * 8),
153 {"instance": NotifySMTP2Go},
154 ),
155 # Invalid 'To' Email address
156 (
157 "smtp2go://[email protected]/{}-{}-{}/invalid".format(
158 "a" * 32, "b" * 8, "c" * 8
159 ),
160 {
161 "instance": NotifySMTP2Go,
162 # Expected notify() response
163 "notify_response": False,
164 },
165 ),
166 # Multiple 'To', 'Cc', and 'Bcc' addresses (with invalid ones)
167 (
168 "smtp2go://[email protected]/{}-{}-{}/{}?bcc={}&cc={}".format(
169 "a" * 32,
170 "b" * 8,
171 "c" * 8,
172 "/".join(
173 ("[email protected]", "invalid", "User2:[email protected]")
174 ),
175 ",".join(("[email protected]", "i@v", "User1:[email protected]")),
176 ",".join(("[email protected]", "g@r@b", "Da:[email protected]")),
177 ),
178 {
179 "instance": NotifySMTP2Go,
180 },
181 ),
182 (
183 "smtp2go://[email protected]/{}-{}-{}".format(
184 "a" * 32, "b" * 8, "c" * 8
185 ),
186 {
187 "instance": NotifySMTP2Go,
188 # force a failure
189 "response": False,
190 "requests_response_code": requests.codes.internal_server_error,
191 },
192 ),
193 (
194 "smtp2go://[email protected]/{}-{}-{}".format(
195 "a" * 32, "b" * 8, "c" * 8
196 ),
197 {
198 "instance": NotifySMTP2Go,
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 "smtp2go://[email protected]/{}-{}-{}".format(
206 "a" * 32, "b" * 8, "c" * 8
207 ),
208 {
209 "instance": NotifySMTP2Go,
210 # Throws a series of i/o exceptions with this flag
211 # is set and tests that we gracefully handle them
212 "test_requests_exceptions": True,
213 },
214 ),
215 )
216
217
218 def test_plugin_smtp2go_urls():
219 """NotifySMTP2Go() Apprise URLs."""
220
221 # Run our general tests
222 AppriseURLTester(tests=apprise_url_tests).run_all()
223
224
225 @mock.patch("requests.post")
226 def test_plugin_smtp2go_attachments(mock_post):
227 """NotifySMTP2Go() Attachments."""
228
229 okay_response = requests.Request()
230 okay_response.status_code = requests.codes.ok
231 okay_response.content = ""
232
233 # Assign our mock object our return value
234 mock_post.return_value = okay_response
235
236 # API Key
237 apikey = "abc123"
238
239 obj = Apprise.instantiate(f"smtp2go://[email protected]/{apikey}")
240 assert isinstance(obj, NotifySMTP2Go)
241
242 # Test Valid Attachment
243 path = os.path.join(TEST_VAR_DIR, "apprise-test.gif")
244 attach = AppriseAttachment(path)
245 assert (
246 obj.notify(
247 body="body",
248 title="title",
249 notify_type=NotifyType.INFO,
250 attach=attach,
251 )
252 is True
253 )
254
255 # Test invalid attachment
256 path = os.path.join(TEST_VAR_DIR, "/invalid/path/to/an/invalid/file.jpg")
257 assert (
258 obj.notify(
259 body="body",
260 title="title",
261 notify_type=NotifyType.INFO,
262 attach=path,
263 )
264 is False
265 )
266
267 mock_post.return_value = None
268 mock_post.side_effect = OSError()
269 # We can't send the message if we can't read the attachment
270 assert (
271 obj.notify(
272 body="body",
273 title="title",
274 notify_type=NotifyType.INFO,
275 attach=attach,
276 )
277 is False
278 )
279
280 # Test Valid Attachment (load 3)
281 path = (
282 os.path.join(TEST_VAR_DIR, "apprise-test.gif"),
283 os.path.join(TEST_VAR_DIR, "apprise-test.gif"),
284 os.path.join(TEST_VAR_DIR, "apprise-test.gif"),
285 )
286 attach = AppriseAttachment(path)
287
288 # Return our good configuration
289 mock_post.side_effect = None
290 mock_post.return_value = okay_response
291 with mock.patch("builtins.open", side_effect=OSError()):
292 # We can't send the message we can't open the attachment for reading
293 assert (
294 obj.notify(
295 body="body",
296 title="title",
297 notify_type=NotifyType.INFO,
298 attach=attach,
299 )
300 is False
301 )
302
303 # test the handling of our batch modes
304 obj = Apprise.instantiate(
305 f"smtp2go://[email protected]/{apikey}/"
306 "[email protected]/[email protected]?batch=yes"
307 )
308 assert isinstance(obj, NotifySMTP2Go)
309
310 # objects will be combined into a single post in batch mode
311 assert len(obj) == 1
312
313 # Force our batch to break into separate messages
314 obj.default_batch_size = 1
315
316 # We'll send 2 messages now
317 assert len(obj) == 2
318
319 mock_post.reset_mock()
320
321 assert (
322 obj.notify(
323 body="body",
324 title="title",
325 notify_type=NotifyType.INFO,
326 attach=attach,
327 )
328 is True
329 )
330 assert mock_post.call_count == 2
331
332 # single batch
333 mock_post.reset_mock()
334 # We'll send 1 message
335 obj.default_batch_size = 2
336
337 assert (
338 obj.notify(
339 body="body",
340 title="title",
341 notify_type=NotifyType.INFO,
342 attach=attach,
343 )
344 is True
345 )
346 assert mock_post.call_count == 1
347
348
349 def test_plugin_smtp2go_cc_bcc_invalid_branch():
350 """NotifySMTP2Go() CC/BCC validation: invalid entries are dropped."""
351
352 # Build a NotifySMTP2Go with:
353 # cc = one valid address + one that parse_emails passes through but
354 # is_email rejects (hits the FALSE branch in the CC loop).
355 # A list must be used -- parse_emails filters invalid tokens out
356 # of comma-separated strings before is_email is ever called.
357 # bcc = same, for the FALSE branch in the BCC loop
358 obj = NotifySMTP2Go(
359 apikey="abc123",
360 targets=["[email protected]"],
361 cc=["[email protected]", "notanemail"],
362 bcc=["[email protected]", "alsonotanemail"],
363 user="user",
364 host="example.com",
365 )
366
367 # Valid CC entry was added; invalid one was silently dropped
368 assert "[email protected]" in obj.cc
369 assert len(obj.cc) == 1
370
371 # Valid BCC entry was added; invalid one was silently dropped
372 assert "[email protected]" in obj.bcc
373 assert len(obj.bcc) == 1