Debian Codegraph @

tests/test_plugin_eight00com.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 from json import loads
30 import logging
31 import os
32 from unittest import mock
33 from urllib.parse import urlparse
34
35 from helpers import AppriseURLTester
36 import pytest
37 import requests
38
39 from apprise import Apprise, AppriseAttachment, NotifyType
40 from apprise.plugins.eight00com import NotifyEight00com
41
42 logging.disable(logging.CRITICAL)
43
44 # Attachment Directory
45 TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var")
46
47 # Our Testing URLs
48 apprise_url_tests = (
49 (
50 "eight00com://",
51 {
52 # No token -> TypeError
53 "instance": TypeError,
54 },
55 ),
56 (
57 "eight00com://:@/",
58 {
59 # Empty token -> TypeError
60 "instance": TypeError,
61 },
62 ),
63 (
64 "eight00com://GOODTOKEN@badphone",
65 {
66 # Non-numeric from-phone -> TypeError
67 "instance": TypeError,
68 },
69 ),
70 (
71 "eight00com://GOODTOKEN@9876543210/12",
72 {
73 # Target too short to be a valid phone number; it is
74 # dropped, leaving no targets -> notify_response False
75 "instance": NotifyEight00com,
76 "notify_response": False,
77 },
78 ),
79 (
80 "eight00com://{}@{}".format("t" * 10, "8" * 10),
81 {
82 # Valid token + source only; defaults to texting ourselves
83 "instance": NotifyEight00com,
84 },
85 ),
86 (
87 "eight00com://{}@{}/{}".format("t" * 10, "8" * 10, "5" * 11),
88 {
89 # Valid token, source, and one target
90 "instance": NotifyEight00com,
91 "privacy_url": (
92 "eight00com://t...t@{}/{}".format("8" * 10, "5" * 11)
93 ),
94 },
95 ),
96 (
97 "eight00com://{}@{}/{}/{}".format(
98 "t" * 10, "8" * 10, "5" * 11, "6" * 11
99 ),
100 {
101 # Multiple targets
102 "instance": NotifyEight00com,
103 },
104 ),
105 (
106 "eight00com://?token={}&from={}".format("t" * 10, "8" * 10),
107 {
108 # Token and source via query params
109 "instance": NotifyEight00com,
110 },
111 ),
112 (
113 "eight00com://?token={}&from={}&to={}".format(
114 "t" * 10, "8" * 10, "5" * 11
115 ),
116 {
117 # Token, source, and target all via query params
118 "instance": NotifyEight00com,
119 },
120 ),
121 (
122 "eight00com://{}@{}".format("t" * 10, "8" * 10),
123 {
124 "instance": NotifyEight00com,
125 # HTTP 500 -> failure
126 "response": False,
127 "requests_response_code": requests.codes.internal_server_error,
128 },
129 ),
130 (
131 "eight00com://{}@{}".format("t" * 10, "8" * 10),
132 {
133 "instance": NotifyEight00com,
134 # Unknown HTTP code -> failure
135 "response": False,
136 "requests_response_code": 999,
137 },
138 ),
139 (
140 "eight00com://{}@{}".format("t" * 10, "8" * 10),
141 {
142 "instance": NotifyEight00com,
143 # Simulate network I/O exceptions
144 "test_requests_exceptions": True,
145 },
146 ),
147 )
148
149
150 def test_plugin_eight00com_urls():
151 """NotifyEight00com() Apprise URLs."""
152 AppriseURLTester(tests=apprise_url_tests).run_all()
153
154
155 @mock.patch("requests.post")
156 def test_plugin_eight00com_init(mock_post):
157 """NotifyEight00com() initialization and edge cases."""
158
159 # Prepare a generic success response
160 response = requests.Request()
161 response.status_code = requests.codes.ok
162 mock_post.return_value = response
163
164 # Missing token -> TypeError
165 with pytest.raises(TypeError):
166 NotifyEight00com(token=None, source="8005551234")
167
168 # Empty token -> TypeError
169 with pytest.raises(TypeError):
170 NotifyEight00com(token="", source="8005551234")
171
172 # Invalid source phone -> TypeError
173 with pytest.raises(TypeError):
174 NotifyEight00com(token="mytoken", source="notaphone")
175
176 # Valid with source only: defaults to texting ourselves
177 obj = NotifyEight00com(token="mytoken", source="8005551234")
178 assert obj.targets == ["8005551234"]
179 assert len(obj) == 1
180
181 # Invalid target is dropped with a warning logged
182 obj = NotifyEight00com(
183 token="mytoken",
184 source="8005551234",
185 targets=["12"],
186 )
187 assert obj.targets == []
188 # __len__ returns 1 minimum (consistent with httpsms pattern)
189 assert len(obj) == 1
190
191 # Multiple valid targets
192 obj = NotifyEight00com(
193 token="mytoken",
194 source="8005551234",
195 targets=["5551234567", "6661234567"],
196 )
197 assert len(obj.targets) == 2
198 assert len(obj) == 2
199
200
201 @mock.patch("requests.post")
202 def test_plugin_eight00com_sms(mock_post):
203 """NotifyEight00com() SMS send path."""
204
205 response = requests.Request()
206 response.status_code = requests.codes.ok
207 response.content = b"{}"
208 mock_post.return_value = response
209
210 # Single target
211 obj = NotifyEight00com(
212 token="mytoken",
213 source="8005551234",
214 targets=["5559876543"],
215 )
216 assert obj.notify(body="Test", title="Title") is True
217 assert mock_post.call_count == 1
218
219 # Verify payload
220 payload = loads(mock_post.call_args[1]["data"])
221 assert payload["sender"] == "+8005551234"
222 assert payload["recipient"] == "+5559876543"
223 assert payload["message"] == "Title\r\nTest"
224
225 # Verify Authorization header
226 headers = mock_post.call_args[1]["headers"]
227 assert headers["Authorization"] == "Bearer mytoken"
228
229 mock_post.reset_mock()
230
231 # Two targets -> two POST calls
232 obj = NotifyEight00com(
233 token="mytoken",
234 source="8005551234",
235 targets=["5559876543", "4445551234"],
236 )
237 assert obj.notify(body="Hello") is True
238 assert mock_post.call_count == 2
239
240 mock_post.reset_mock()
241
242 # HTTP 500 -> failure
243 response.status_code = requests.codes.internal_server_error
244 obj = NotifyEight00com(
245 token="mytoken",
246 source="8005551234",
247 targets=["5559876543"],
248 )
249 assert obj.notify(body="Test") is False
250
251 mock_post.reset_mock()
252
253 # Unknown status code -> failure
254 response.status_code = 999
255 assert obj.notify(body="Test") is False
256
257 mock_post.reset_mock()
258
259 # RequestException -> failure
260 mock_post.side_effect = requests.RequestException("Connection error")
261 response.status_code = requests.codes.ok
262 assert obj.notify(body="Test") is False
263
264 mock_post.side_effect = None
265
266 # No targets after bad-number drops -> failure without HTTP call
267 obj = NotifyEight00com(
268 token="mytoken",
269 source="8005551234",
270 targets=["12"],
271 )
272 mock_post.reset_mock()
273 assert obj.notify(body="Test") is False
274 assert mock_post.call_count == 0
275
276
277 @mock.patch("requests.post")
278 def test_plugin_eight00com_url_round_trip(mock_post):
279 """NotifyEight00com() URL round-trip and privacy_url."""
280
281 response = requests.Request()
282 response.status_code = requests.codes.ok
283 mock_post.return_value = response
284
285 # Round-trip with one target
286 obj1 = NotifyEight00com(
287 token="secrettoken",
288 source="8005551234",
289 targets=["5559876543"],
290 )
291 result = NotifyEight00com.parse_url(obj1.url())
292 obj2 = NotifyEight00com(**result)
293 assert obj1.url_identifier == obj2.url_identifier
294 assert len(obj1.targets) == len(obj2.targets)
295
296 # Privacy URL masks the token
297 privacy = obj1.url(privacy=True)
298 assert "secrettoken" not in privacy
299 assert "s...n" in privacy
300
301 # Round-trip with no explicit target (self-send omits path)
302 obj3 = NotifyEight00com(token="mytoken", source="8005551234")
303 result = NotifyEight00com.parse_url(obj3.url())
304 obj4 = NotifyEight00com(**result)
305 assert obj3.url_identifier == obj4.url_identifier
306 # Self-send: targets are preserved
307 assert obj3.targets == obj4.targets
308
309 # url_identifier is schema / source / token -- not targets
310 assert obj1.url_identifier == (
311 "eight00com",
312 "8005551234",
313 "secrettoken",
314 )
315
316
317 @mock.patch("requests.post")
318 def test_plugin_eight00com_parse_url(mock_post):
319 """NotifyEight00com() parse_url edge cases."""
320
321 response = requests.Request()
322 response.status_code = requests.codes.ok
323 mock_post.return_value = response
324
325 # Standard form: token@source/target
326 result = NotifyEight00com.parse_url(
327 "eight00com://mytoken@8005551234/5559876543"
328 )
329 assert result["token"] == "mytoken"
330 assert result["source"] == "8005551234"
331 assert "5559876543" in result["targets"]
332
333 # ?token= override
334 result = NotifyEight00com.parse_url(
335 "eight00com://8005551234?token=newtoken&from=8009876543&to=5551234567"
336 )
337 assert result["token"] == "newtoken"
338 assert result["source"] == "8009876543"
339
340 # ?from= causes host to become a target instead of source
341 result = NotifyEight00com.parse_url(
342 "eight00com://mytoken@9998887776?from=8005551234"
343 )
344 assert result["token"] == "mytoken"
345 assert result["source"] == "8005551234"
346 # The host (9998887776) becomes a target
347 assert "9998887776" in " ".join(result["targets"])
348
349 # ?to= adds targets
350 result = NotifyEight00com.parse_url(
351 "eight00com://mytoken@8005551234?to=5559876543"
352 )
353 assert "5559876543" in " ".join(result["targets"])
354
355 # Unparseable URL -> None
356 assert NotifyEight00com.parse_url(None) is None
357
358
359 @mock.patch("requests.post")
360 def test_plugin_eight00com_attach_success(mock_post):
361 """NotifyEight00com() attachment send -- success path."""
362
363 response = requests.Request()
364 response.status_code = requests.codes.ok
365 mock_post.return_value = response
366
367 obj = NotifyEight00com(
368 token="mytoken",
369 source="8005551234",
370 targets=["5559876543"],
371 )
372
373 attach = AppriseAttachment.instantiate(
374 os.path.join(TEST_VAR_DIR, "apprise-test.gif")
375 )
376 assert (
377 obj.notify(
378 body="Test MMS",
379 title="",
380 notify_type=NotifyType.INFO,
381 attach=attach,
382 )
383 is True
384 )
385
386 # Exactly one POST for one target
387 assert mock_post.call_count == 1
388
389 # Confirm the MMS fields were sent as multipart (files kwarg set)
390 call_kwargs = mock_post.call_args[1]
391 assert call_kwargs["files"] is not None
392 assert call_kwargs["data"]["sender"] == "+8005551234"
393 assert call_kwargs["data"]["recipient"] == "+5559876543"
394 assert call_kwargs["data"]["message"] == "Test MMS"
395
396 # Confirm authorization header is present
397 assert call_kwargs["headers"]["Authorization"] == "Bearer mytoken"
398
399 # Verify the URL host is correct
400 call_url = mock_post.call_args[0][0]
401 assert urlparse(call_url).hostname == "api.800.com"
402
403
404 @mock.patch("requests.post")
405 def test_plugin_eight00com_attach_multi(mock_post):
406 """NotifyEight00com() MMS with multiple attachments."""
407
408 response = requests.Request()
409 response.status_code = requests.codes.ok
410 mock_post.return_value = response
411
412 obj = NotifyEight00com(
413 token="mytoken",
414 source="8005551234",
415 targets=["5559876543"],
416 )
417
418 attach = AppriseAttachment()
419 attach.add(os.path.join(TEST_VAR_DIR, "apprise-test.gif"))
420 attach.add(os.path.join(TEST_VAR_DIR, "apprise-test.png"))
421
422 assert (
423 obj.notify(
424 body="Multi-attachment MMS",
425 attach=attach,
426 )
427 is True
428 )
429 assert mock_post.call_count == 1
430
431 # Two media[] tuples in the files list
432 files = mock_post.call_args[1]["files"]
433 assert len(files) == 2
434 assert all(f[0] == "media[]" for f in files)
435
436
437 @mock.patch("requests.post")
438 def test_plugin_eight00com_attach_inaccessible(mock_post):
439 """NotifyEight00com() attachment -- inaccessible file guard."""
440
441 response = requests.Request()
442 response.status_code = requests.codes.ok
443 mock_post.return_value = response
444
445 obj = NotifyEight00com(
446 token="mytoken",
447 source="8005551234",
448 targets=["5559876543"],
449 )
450
451 attach = AppriseAttachment.instantiate(
452 os.path.join(TEST_VAR_DIR, "apprise-test.gif")
453 )
454
455 # Simulate the file being inaccessible (os.path.isfile -> False)
456 with mock.patch("os.path.isfile", return_value=False):
457 assert obj.notify(body="Test", attach=attach) is False
458
459 # No HTTP call should have been made
460 assert mock_post.call_count == 0
461
462
463 @mock.patch("requests.post")
464 def test_plugin_eight00com_attach_oserror(mock_post):
465 """NotifyEight00com() attachment -- OSError on open guard."""
466
467 response = requests.Request()
468 response.status_code = requests.codes.ok
469 mock_post.return_value = response
470
471 obj = NotifyEight00com(
472 token="mytoken",
473 source="8005551234",
474 targets=["5559876543"],
475 )
476
477 attach = AppriseAttachment.instantiate(
478 os.path.join(TEST_VAR_DIR, "apprise-test.gif")
479 )
480
481 with mock.patch("builtins.open", side_effect=OSError("IO fail")):
482 assert obj.notify(body="Test", attach=attach) is False
483
484 # No HTTP call should have been made
485 assert mock_post.call_count == 0
486
487
488 @mock.patch("requests.post")
489 def test_plugin_eight00com_attach_http_error(mock_post):
490 """NotifyEight00com() attachment -- server HTTP error response."""
491
492 response = requests.Request()
493 response.status_code = requests.codes.internal_server_error
494 response.content = b"{}"
495 mock_post.return_value = response
496
497 obj = NotifyEight00com(
498 token="mytoken",
499 source="8005551234",
500 targets=["5559876543"],
501 )
502
503 attach = AppriseAttachment.instantiate(
504 os.path.join(TEST_VAR_DIR, "apprise-test.gif")
505 )
506 assert obj.notify(body="Test", attach=attach) is False
507 assert mock_post.call_count == 1
508
509
510 @mock.patch("requests.post")
511 def test_plugin_eight00com_attach_request_exception(mock_post):
512 """NotifyEight00com() attachment -- RequestException guard."""
513
514 mock_post.side_effect = requests.RequestException("Connection error")
515
516 obj = NotifyEight00com(
517 token="mytoken",
518 source="8005551234",
519 targets=["5559876543"],
520 )
521
522 attach = AppriseAttachment.instantiate(
523 os.path.join(TEST_VAR_DIR, "apprise-test.gif")
524 )
525 assert obj.notify(body="Test", attach=attach) is False
526
527
528 @mock.patch("requests.post")
529 def test_plugin_eight00com_attach_partial_failure(mock_post):
530 """NotifyEight00com() MMS partial target failure."""
531
532 def _side_effect(*args, **kwargs):
533 # First call succeeds, second fails
534 if mock_post.call_count == 1:
535 resp = requests.Request()
536 resp.status_code = requests.codes.ok
537 return resp
538
539 resp = requests.Request()
540 resp.status_code = requests.codes.internal_server_error
541 resp.content = b"{}"
542 return resp
543
544 mock_post.side_effect = _side_effect
545
546 obj = NotifyEight00com(
547 token="mytoken",
548 source="8005551234",
549 targets=["5559876543", "4441234567"],
550 )
551
552 attach = AppriseAttachment.instantiate(
553 os.path.join(TEST_VAR_DIR, "apprise-test.gif")
554 )
555 # One success and one failure -> overall False
556 assert obj.notify(body="Test", attach=attach) is False
557 assert mock_post.call_count == 2
558
559
560 @mock.patch("requests.post")
561 def test_plugin_eight00com_apprise_integration(mock_post):
562 """NotifyEight00com() integration with Apprise."""
563
564 response = requests.Request()
565 response.status_code = requests.codes.ok
566 mock_post.return_value = response
567
568 # Load via URL
569 a = Apprise()
570 assert a.add("eight00com://mytoken@8005551234/5559876543") is True
571 assert a.notify(body="Integration test") is True
572 assert mock_post.call_count == 1
573
574 mock_post.reset_mock()
575
576 # Load via URL with ?to= and ?from=
577 a2 = Apprise()
578 assert (
579 a2.add("eight00com://?token=mytoken&from=8005551234&to=5559876543")
580 is True
581 )
582 assert a2.notify(body="Integration test 2") is True
583 assert mock_post.call_count == 1