Debian Codegraph @

tests/test_plugin_aprs.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 socket
31 from unittest import mock
32
33 import apprise
34 from apprise.plugins.aprs import NotifyAprs
35
36 logging.disable(logging.CRITICAL)
37
38
39 @mock.patch("socket.create_connection")
40 def test_plugin_aprs_urls(mock_create_connection):
41 """NotifyAprs() Apprise URLs."""
42 # A socket object
43 sobj = mock.Mock()
44 sobj.return_value = 1
45 sobj.getpeername.return_value = ("localhost", 1234)
46 sobj.socket_close.return_value = None
47 sobj.setblocking.return_value = True
48 sobj.recv.return_value = "ping\npong pong DF1JSL-15 verified pong".encode(
49 "latin-1"
50 )
51 sobj.sendall.return_value = True
52 sobj.settimeout.return_value = True
53
54 # Prepare Mock
55 mock_create_connection.return_value = sobj
56
57 # Test invalid URLs
58 assert apprise.Apprise.instantiate("aprs://") is None
59 assert apprise.Apprise.instantiate("aprs://:@/") is None
60
61 # No call-sign specified
62 assert apprise.Apprise.instantiate("aprs://DF1JSL-15:12345") is None
63
64 # Garbage
65 assert NotifyAprs.parse_url(None) is None
66
67 # Valid call-sign but no password
68 assert apprise.Apprise.instantiate("aprs://DF1JSL-15:@DF1ABC") is None
69 assert apprise.Apprise.instantiate("aprs://DF1JSL-15@DF1ABC") is None
70 # Password of -1 not supported
71 assert apprise.Apprise.instantiate("aprs://DF1JSL-15:-1@DF1ABC") is None
72 # Alpha Password not supported
73 assert apprise.Apprise.instantiate("aprs://DF1JSL-15:abcd@DF1ABC") is None
74
75 # Valid instances
76 instance = apprise.Apprise.instantiate("aprs://DF1JSL-15:12345@DF1ABC")
77 assert isinstance(instance, NotifyAprs)
78 assert instance.url(privacy=True).startswith(
79 "aprs://DF1JSL-15:****@D...C?"
80 )
81 assert bool(instance.notify("test")) is True
82
83 # 1N3 callsigns
84 instance = apprise.Apprise.instantiate("aprs://D1JSL-15:12345@D1ABC")
85 assert isinstance(instance, NotifyAprs)
86
87 instance = apprise.Apprise.instantiate(
88 "aprs://DF1JSL-15:12345@DF1ABC?delay=3.0"
89 )
90 assert isinstance(instance, NotifyAprs)
91 instance = apprise.Apprise.instantiate(
92 "aprs://DF1JSL-15:12345@DF1ABC?delay=2"
93 )
94 assert isinstance(instance, NotifyAprs)
95 instance = apprise.Apprise.instantiate(
96 "aprs://DF1JSL-15:12345@DF1ABC?delay=-3.0"
97 )
98 assert instance is None
99 instance = apprise.Apprise.instantiate(
100 "aprs://DF1JSL-15:12345@DF1ABC?delay=40.0"
101 )
102 assert instance is None
103 instance = apprise.Apprise.instantiate(
104 "aprs://DF1JSL-15:12345@DF1ABC?delay=invalid"
105 )
106 assert instance is None
107
108 instance = apprise.Apprise.instantiate(
109 "aprs://DF1JSL-15:12345@DF1ABC/DF1DEF"
110 )
111 assert isinstance(instance, NotifyAprs)
112 assert instance.url(privacy=True).startswith(
113 "aprs://DF1JSL-15:****@D...C/D...F?"
114 )
115 assert bool(instance.notify("test")) is True
116
117 instance = apprise.Apprise.instantiate(
118 "aprs://DF1JSL-15:12345@DF1ABC-1/DF1ABC/DF1ABC-15"
119 )
120 assert isinstance(instance, NotifyAprs)
121 assert instance.url(privacy=True).startswith(
122 "aprs://DF1JSL-15:****@D...1/D...C/D...5?"
123 )
124 assert bool(instance.notify("test")) is True
125
126 instance = apprise.Apprise.instantiate(
127 "aprs://DF1JSL-15:12345@?to=DF1ABC,DF1DEF"
128 )
129 assert isinstance(instance, NotifyAprs)
130 assert instance.url(privacy=True).startswith(
131 "aprs://DF1JSL-15:****@D...C/D...F?"
132 )
133 assert bool(instance.notify("test")) is True
134
135 # Test Locale settings
136 instance = apprise.Apprise.instantiate(
137 "aprs://DF1JSL-15:12345@DF1ABC?locale=EURO"
138 )
139 assert isinstance(instance, NotifyAprs)
140 assert instance.url(privacy=True).startswith(
141 "aprs://DF1JSL-15:****@D...C?"
142 )
143 # we used the default locale, so no setting
144 assert "locale=" not in instance.url(privacy=True)
145 assert bool(instance.notify("test")) is True
146
147 instance = apprise.Apprise.instantiate(
148 "aprs://DF1JSL-15:12345@DF1ABC?locale=NOAM"
149 )
150 assert isinstance(instance, NotifyAprs)
151 assert instance.url(privacy=True).startswith(
152 "aprs://DF1JSL-15:****@D...C?"
153 )
154 # locale is set in URL
155 assert "locale=NOAM" in instance.url(privacy=True)
156 assert bool(instance.notify("test")) is True
157
158 # Invalid locale
159 assert (
160 apprise.Apprise.instantiate(
161 "aprs://DF1JSL-15:12345@DF1ABC?locale=invalid"
162 )
163 is None
164 )
165
166 # Invalid call signs
167 instance = apprise.Apprise.instantiate(
168 "aprs://DF1JSL-15:12345@abcdefghi/a"
169 )
170
171 # We still instantiate
172 assert isinstance(instance, NotifyAprs)
173
174 # We still load our bad entries
175 assert instance.url(privacy=True).startswith(
176 "aprs://DF1JSL-15:****@A...I/A...A?"
177 )
178
179 # But with only bad entries, we have nothing to notify
180 assert bool(instance.notify("test")) is False
181
182 # Enforces a close
183 del instance
184
185
186 @mock.patch("socket.create_connection")
187 def test_plugin_aprs_edge_cases(mock_create_connection):
188 """NotifyAprs() Edge Cases."""
189
190 # A socket object
191 sobj = mock.Mock()
192 sobj.return_value = 1
193 sobj.getpeername.return_value = ("localhost", 1234)
194 sobj.socket_close.return_value = None
195 sobj.setblocking.return_value = True
196 sobj.recv.return_value = "ping\npong pong DF1JSL-15 verified pong".encode(
197 "latin-1"
198 )
199 sobj.sendall.return_value = True
200 sobj.settimeout.return_value = True
201
202 # Prepare Mock
203 mock_create_connection.return_value = sobj
204
205 # Valid instances
206 instance = apprise.Apprise.instantiate(
207 "aprs://DF1JSL-15:12345@DF1ABC/DF1DEF"
208 )
209 assert isinstance(instance, NotifyAprs)
210
211 # our URL Identifier
212 assert isinstance(instance.url_id(), str)
213
214 # Objects read
215 assert len(instance) == 2
216
217 # Bad data
218 sobj.recv.return_value = "one line".encode("latin-1")
219 assert bool(instance.notify(body="body", title="title")) is False
220 sobj.recv.return_value = "\n\n\n".encode("latin-1")
221 assert bool(instance.notify(body="body", title="title")) is False
222 sobj.recv.return_value = "".encode("latin-1")
223 assert bool(instance.notify(body="body", title="title")) is False
224 sobj.recv.return_value = "\ndata".encode("latin-1")
225 assert bool(instance.notify(body="body", title="title")) is False
226 # Different Call-Sign then what we logged in as
227 sobj.recv.return_value = "ping\npong pong DF1JSL-14 verified, pong".encode(
228 "latin-1"
229 )
230 assert bool(instance.notify(body="body", title="title")) is False
231 # Unverified
232 sobj.recv.return_value = (
233 "ping\npong pong DF1JSL-15 unverified, pong".encode("latin-1")
234 )
235 assert bool(instance.notify(body="body", title="title")) is False
236
237 #
238 # Test Login edge cases
239 #
240 sobj.return_value = False
241 assert instance.aprsis_login() is False
242 sobj.return_value = 1
243 sobj.recv.return_value = "".encode("latin-1")
244 assert instance.aprsis_login() is False
245 sobj.recv.return_value = "ping\npong pong DF1JSL-15 verified pong".encode(
246 "latin-1"
247 )
248
249 #
250 # Test Socket Send Exceptions
251 #
252 sobj.sendall.return_value = None
253 sobj.sendall.side_effect = socket.gaierror("gaierror")
254 # No connection
255 assert instance.socket_send("data") is False
256 # Ensure we have a connection before calling socket_send()
257 assert instance.socket_open() is True
258 assert instance.socket_send("data") is False
259 sobj.sendall.side_effect = socket.timeout("timeout")
260 assert instance.socket_open() is True
261 assert instance.socket_send("data") is False
262 assert instance.socket_open() is True
263 sobj.sendall.side_effect = OSError("error")
264 assert instance.socket_send("data") is False
265
266 # Login is impacted by socket_send
267 sobj.return_value = 1
268 assert instance.socket_open() is True
269 assert instance.aprsis_login() is False
270
271 # Return some of our
272 sobj.sendall.side_effect = None
273 sobj.sendall.return_value = True
274
275 assert instance.socket_open() is True
276 sobj.close.return_value = None
277 sobj.close.side_effect = socket.gaierror("gaierror")
278 instance.socket_close()
279 sobj.close.side_effect = socket.timeout("timeout")
280 instance.socket_close()
281 sobj.close.side_effect = OSError("error")
282 instance.socket_close()
283 sobj.return_value = None
284 instance.socket_close()
285 # Socket isn't open; so we can't get content
286 assert instance.socket_receive(100) is False
287 sobj.close.side_effect = None
288 sobj.close.return_value = None
289 # Double close test
290 instance.socket_close()
291
292 sobj.return_value = 1
293 mock_create_connection.return_value = None
294 mock_create_connection.side_effect = socket.gaierror("gaierror")
295 assert instance.socket_open() is False
296 assert bool(instance.notify("test")) is False
297 mock_create_connection.side_effect = socket.timeout("timeout")
298 assert instance.socket_open() is False
299 assert bool(instance.notify("test")) is False
300 mock_create_connection.side_effect = OSError("error")
301 assert instance.socket_open() is False
302 assert bool(instance.notify("test")) is False
303 mock_create_connection.side_effect = ConnectionError("ConnectionError")
304 assert instance.socket_open() is False
305 assert bool(instance.notify("test")) is False
306
307 # Restore our good connection
308 mock_create_connection.return_value = sobj
309 mock_create_connection.side_effect = None
310
311 # Functionality has been restored
312 assert instance.socket_open() is True
313
314 # Now play with getpeername
315 sobj.getpeername.return_value = None
316 sobj.getpeername.side_effect = ValueError("getpeername ValueError")
317 assert instance.socket_open() is True
318
319 sobj.getpeername.return_value = ("localhost", 1234)
320 assert instance.socket_open() is True
321 # Test different receive settings
322 assert instance.socket_receive(0)
323 assert instance.socket_receive(-1)
324 assert instance.socket_receive(100)
325
326 sobj.recv.side_effect = socket.gaierror("gaierror")
327 assert instance.socket_open() is True
328 assert instance.socket_receive(100) is False
329 sobj.recv.side_effect = socket.timeout("timeout")
330 assert instance.socket_open() is True
331 assert instance.socket_receive(100) is False
332 sobj.recv.side_effect = OSError("error")
333 assert instance.socket_open() is True
334 assert instance.socket_receive(100) is False
335
336 # Restore
337 sobj.recv.side_effect = None
338 sobj.recv.return_value = "ping\npong pong DF1JSL-15 verified pong".encode(
339 "latin-1"
340 )
341
342 # Simulate a successful connection, but a failed notification
343 # To do this we need to have a login succeed, but the second call to send
344 # to fail
345 sobj.sendall.return_value = True
346 assert bool(instance.notify("test")) is True
347
348 sobj.sendall.return_value = None
349 sobj.sendall.side_effect = (True, socket.gaierror("gaierror"))
350 assert bool(instance.notify("test")) is False
351
352 sobj.sendall.return_value = True
353 sobj.sendall.side_effect = None
354 del sobj
355
356
357 def test_plugin_aprs_config_files():
358 """NotifyAprs() Config File Cases."""
359 content = """
360 urls:
361 - aprs://DF1JSL-15:12345@DF1ABC":
362 - locale: NOAM
363
364 - aprs://DF1JSL-15:12345@DF1ABC:
365 - locale: SOAM
366
367 - aprs://DF1JSL-15:12345@DF1ABC:
368 - locale: EURO
369
370 - aprs://DF1JSL-15:12345@DF1ABC:
371 - locale: ASIA
372
373 - aprs://DF1JSL-15:12345@DF1ABC:
374 - locale: AUNZ
375
376 - aprs://DF1JSL-15:12345@DF1ABC:
377 - locale: ROTA
378
379 # This will fail to load because the locale is bad
380 - aprs://DF1JSL-15:12345@DF1ABC:
381 - locale: aprs_invalid
382 """
383
384 # Create ourselves a config object
385 ac = apprise.AppriseConfig()
386 assert ac.add_config(content=content) is True
387
388 aobj = apprise.Apprise()
389
390 # Add our configuration
391 aobj.add(ac)
392
393 assert len(ac.services()) == 6
394 assert len(aobj) == 6