Debian Codegraph @

tests/test_delivery_tracking_keys.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
29 """Ensure similar values remain distinct delivery targets.
30
31 The same value may identify different target types, such as SMS and
32 WhatsApp. Each type needs its own key so neither hides the other.
33 """
34
35 # Disable logging for a cleaner testing output
36 import contextlib
37 import logging
38 from unittest import mock
39
40 from helpers import ATTACHMENT, ok_response
41 import pytest
42
43 from apprise import Apprise, AppriseAttachment, NotifyType
44 from apprise.plugins.base import NotifyBase, _delivery_tracker
45
46 logging.disable(logging.CRITICAL)
47
48 # Each URL below names the same value twice, once under each kind of
49 # target the service understands.
50 LOOK_ALIKE = {
51 "kook": ("kook://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/1234567890/@1234567890"),
52 "mattermost": (
53 "mmost://user@localhost/3ccdd113474722377935511fc85d3dd4"
54 "?mode=bot&channels=%23abc,%2Babc"
55 ),
56 "pushover": (
57 "pover://abcdefghijklmnopqrstuvwxyz1234@"
58 "token1234567890abcdefghijklmno/device1/"
59 "#abcdefghijklmnopqrstuvwxyz1234"
60 ),
61 "pushy": "pushy://_/#abc/@abc?key=apikey",
62 "twilio": (
63 "twilio://ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:"
64 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb@12125550000/"
65 "12125550001/whatsapp:12125550001"
66 ),
67 }
68
69
70 @contextlib.contextmanager
71 def _watched():
72 """Run with the network replaced and every key asked about recorded."""
73
74 seen = []
75
76 def answer(key, per_message=False):
77 seen.append(key)
78 return False
79
80 with (
81 mock.patch("requests.post", side_effect=ok_response),
82 mock.patch("requests.get", side_effect=ok_response),
83 mock.patch("requests.put", side_effect=ok_response),
84 mock.patch("requests.patch", side_effect=ok_response),
85 mock.patch("apprise.plugins.base.time.sleep"),
86 mock.patch.object(NotifyBase, "is_delivered", side_effect=answer),
87 ):
88 yield seen
89
90
91 @pytest.mark.parametrize("name", sorted(LOOK_ALIKE))
92 def test_target_kinds_stay_distinct(name):
93 """The same value under two kinds produces two different keys."""
94
95 obj = Apprise.instantiate(LOOK_ALIKE[name], suppress_exceptions=True)
96 assert obj is not None, f"{name}: URL no longer loads"
97
98 with _watched() as seen:
99 obj.send(body="test", title="test", notify_type=NotifyType.INFO)
100
101 # Both destinations were walked
102 assert len(seen) == 2, f"{name}: expected two targets, saw {seen}"
103
104 # ... and neither one hides the other
105 distinct = {(type(key), repr(key)) for key in seen}
106 assert len(distinct) == 2, (
107 f"{name}: both targets share the delivery key {seen[0]!r}; one"
108 " delivery would hide the other"
109 )
110
111
112 def test_serwersms_mms_tracking():
113 """An MMS that arrives is recorded just like a plain message."""
114
115 obj = Apprise.instantiate(
116 "serwersms://user:pass@SenderA/+48123456789",
117 suppress_exceptions=True,
118 )
119 assert obj is not None
120
121 token = _delivery_tracker.set(set())
122 try:
123 with (
124 mock.patch("requests.post", side_effect=ok_response) as post,
125 mock.patch("apprise.plugins.base.time.sleep"),
126 ):
127 attach = AppriseAttachment(ATTACHMENT)
128 assert obj.send(body="test", title="test", attach=attach) is True
129 assert post.call_count == 1
130
131 # A retry has nothing left to send
132 post.reset_mock()
133 assert obj.send(body="test", title="test", attach=attach) is True
134 assert post.call_count == 0
135
136 finally:
137 _delivery_tracker.reset(token)
138
139
140 def test_home_assistant_batch_tracking():
141 """Each batch of devices is its own delivery."""
142
143 # The ":" form gives one service several devices, and those are what
144 # get split into batches
145 obj = Apprise.instantiate(
146 "hassio://localhost/long.lived.token/notify.mobile:dev1,dev2",
147 suppress_exceptions=True,
148 )
149 assert obj is not None
150
151 token = _delivery_tracker.set(set())
152 try:
153 with (
154 mock.patch("requests.post", side_effect=ok_response) as post,
155 mock.patch("apprise.plugins.base.time.sleep"),
156 ):
157 assert obj.send(body="test", title="test") is True
158
159 # One call per device; neither batch hides the other
160 assert post.call_count == 2
161
162 finally:
163 _delivery_tracker.reset(token)
164
165
166 def test_smpp_segment_tracking():
167 """Each part of a long message is its own delivery."""
168
169 smpplib = pytest.importorskip("smpplib")
170 obj = Apprise.instantiate(
171 "smpp://user:pass@localhost:2775/12125550000/12125550001",
172 suppress_exceptions=True,
173 )
174 assert obj is not None
175
176 token = _delivery_tracker.set(set())
177 try:
178 with (
179 mock.patch("smpplib.client.Client.connect"),
180 mock.patch("smpplib.client.Client.bind_transmitter"),
181 mock.patch("smpplib.client.Client.unbind"),
182 mock.patch("smpplib.client.Client.disconnect"),
183 mock.patch("smpplib.client.Client.send_message") as sent,
184 mock.patch("apprise.plugins.base.time.sleep"),
185 ):
186 assert smpplib is not None
187 assert obj.send(body="y" * 400, title="test") is True
188
189 # A long message is split; every part has to go out
190 assert sent.call_count > 1
191
192 # A retry has nothing left to send
193 sent.reset_mock()
194 assert obj.send(body="y" * 400, title="test") is True
195 assert sent.call_count == 0
196
197 finally:
198 _delivery_tracker.reset(token)