Debian Codegraph @

tests/test_plugin_dapnet.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 from unittest import mock
31
32 from helpers import AppriseURLTester
33 import requests
34
35 import apprise
36 from apprise import NotifyType
37 from apprise.exception import AppriseImproperlyConfigured
38 from apprise.plugins.dapnet import DapnetPriority, NotifyDapnet
39
40 logging.disable(logging.CRITICAL)
41
42 # Our Testing URLs
43 apprise_url_tests = (
44 (
45 "dapnet://",
46 {
47 # We failed to identify any valid authentication
48 "instance": AppriseImproperlyConfigured,
49 },
50 ),
51 (
52 "dapnet://:@/",
53 {
54 # We failed to identify any valid authentication
55 "instance": AppriseImproperlyConfigured,
56 },
57 ),
58 (
59 "dapnet://user:pass",
60 {
61 # No call-sign specified
62 "instance": AppriseImproperlyConfigured,
63 },
64 ),
65 (
66 "dapnet://user@host",
67 {
68 # No password specified
69 "instance": AppriseImproperlyConfigured,
70 },
71 ),
72 (
73 "dapnet://user:pass@{}".format("DF1ABC"),
74 {
75 # valid call sign
76 "instance": NotifyDapnet,
77 "requests_response_code": requests.codes.created,
78 },
79 ),
80 (
81 "dapnet://user:pass@{}/{}".format("DF1ABC", "DF1DEF"),
82 {
83 # valid call signs
84 "instance": NotifyDapnet,
85 "requests_response_code": requests.codes.created,
86 },
87 ),
88 (
89 "dapnet://user:pass@DF1ABC-1/DF1ABC/DF1ABC-15",
90 {
91 # valid call signs; but a few are duplicates;
92 # at the end there will only be 1 entry
93 "instance": NotifyDapnet,
94 "requests_response_code": requests.codes.created,
95 # Our expected url(privacy=True) startswith() response:
96 # Note that only 1 entry is saved (as other 2 are duplicates)
97 "privacy_url": "dapnet://user:****@D...C?",
98 },
99 ),
100 (
101 "dapnet://user:pass@?to={},{}".format("DF1ABC", "DF1DEF"),
102 {
103 # support the to= argument
104 "instance": NotifyDapnet,
105 "requests_response_code": requests.codes.created,
106 },
107 ),
108 (
109 "dapnet://user:pass@{}?priority=normal".format("DF1ABC"),
110 {
111 # valid call sign with priority
112 "instance": NotifyDapnet,
113 "requests_response_code": requests.codes.created,
114 },
115 ),
116 (
117 "dapnet://user:pass@{}?priority=em&batch=false".format(
118 "/".join(["DF1ABC", "0A1DEF"])
119 ),
120 {
121 # valid call sign with priority (emergency) + no batch
122 # transmissions
123 "instance": NotifyDapnet,
124 "requests_response_code": requests.codes.created,
125 },
126 ),
127 (
128 "dapnet://user:pass@{}?priority=invalid".format("DF1ABC"),
129 {
130 # invalid priority
131 "instance": NotifyDapnet,
132 "requests_response_code": requests.codes.created,
133 },
134 ),
135 (
136 "dapnet://user:pass@{}?txgroups=dl-all,all".format("DF1ABC"),
137 {
138 # valid call sign with two transmitter groups
139 "instance": NotifyDapnet,
140 "requests_response_code": requests.codes.created,
141 },
142 ),
143 (
144 "dapnet://user:pass@{}?txgroups=invalid".format("DF1ABC"),
145 {
146 # valid call sign with invalid transmitter group
147 "instance": NotifyDapnet,
148 "requests_response_code": requests.codes.created,
149 },
150 ),
151 (
152 "dapnet://user:pass@{}/{}".format("abcdefghi", "a"),
153 {
154 # invalid call signs
155 "instance": NotifyDapnet,
156 "notify_response": False,
157 },
158 ),
159 # Edge cases
160 (
161 "dapnet://user:pass@{}".format("DF1ABC"),
162 {
163 "instance": NotifyDapnet,
164 # throw a bizarre code forcing us to fail to look it up
165 "response": False,
166 "requests_response_code": 999,
167 },
168 ),
169 (
170 "dapnet://user:pass@{}".format("DF1ABC"),
171 {
172 "instance": NotifyDapnet,
173 # Throws a series of i/o exceptions with this flag
174 # is set and tests that we gracefully handle them
175 "test_requests_exceptions": True,
176 },
177 ),
178 )
179
180
181 def test_plugin_dapnet_urls():
182 """NotifyDapnet() Apprise URLs."""
183
184 # Run our general tests
185 AppriseURLTester(tests=apprise_url_tests).run_all()
186
187
188 @mock.patch("requests.post")
189 def test_plugin_dapnet_edge_cases(mock_post):
190 """NotifyDapnet() Edge Cases."""
191 # Prepare Mock
192 mock_post.return_value = requests.Request()
193 mock_post.return_value.status_code = requests.codes.created
194
195 # test the handling of our batch modes
196 obj = apprise.Apprise.instantiate(
197 "dapnet://user:pass@{}?batch=yes".format(
198 "/".join(["DF1ABC", "DF1DEF"])
199 )
200 )
201 assert isinstance(obj, NotifyDapnet)
202
203 # objects will be combined into a single post in batch mode
204 assert len(obj) == 1
205
206 # Force our batch to break into separate messages
207 obj.default_batch_size = 1
208
209 # We'll send 2 messages now
210 assert len(obj) == 2
211
212 assert (
213 bool(
214 obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
215 )
216 is True
217 )
218 assert mock_post.call_count == 2
219
220
221 @mock.patch("requests.post")
222 def test_plugin_dapnet_config_files(mock_post):
223 """NotifyDapnet() Config File Cases."""
224 content = """
225 urls:
226 - dapnet://user:pass@DF1ABC:
227 - priority: 0
228 tag: dapnet_int normal
229 - priority: "0"
230 tag: dapnet_str_int normal
231 - priority: normal
232 tag: dapnet_str normal
233
234 # This will take on normal (default) priority
235 - priority: invalid
236 tag: dapnet_invalid
237
238 - dapnet://user1:pass2@DF1ABC:
239 - priority: 1
240 tag: dapnet_int emerg
241 - priority: "1"
242 tag: dapnet_str_int emerg
243 - priority: emergency
244 tag: dapnet_str emerg
245 """
246
247 # Prepare Mock
248 mock_post.return_value = requests.Request()
249 mock_post.return_value.status_code = requests.codes.created
250
251 # Create ourselves a config object
252 ac = apprise.AppriseConfig()
253 assert ac.add_config(content=content) is True
254
255 aobj = apprise.Apprise()
256
257 # Add our configuration
258 aobj.add(ac)
259
260 # We should be able to read our 7 services from that
261 # 4x normal (invalid + 3 exclusivly specified to be so)
262 # 3x emerg
263 assert len(ac.services()) == 7
264 assert len(aobj) == 7
265 assert len(list(aobj.find(tag="normal"))) == 3
266 for s in aobj.find(tag="normal"):
267 assert s.priority == DapnetPriority.NORMAL
268
269 assert len(list(aobj.find(tag="emerg"))) == 3
270 for s in aobj.find(tag="emerg"):
271 assert s.priority == DapnetPriority.EMERGENCY
272
273 assert len(list(aobj.find(tag="dapnet_str"))) == 2
274 assert len(list(aobj.find(tag="dapnet_str_int"))) == 2
275 assert len(list(aobj.find(tag="dapnet_int"))) == 2
276
277 assert len(list(aobj.find(tag="dapnet_invalid"))) == 1
278 assert (
279 next(aobj.find(tag="dapnet_invalid")).priority == DapnetPriority.NORMAL
280 )
281
282 # Notifications work
283 assert bool(aobj.notify(title="title", body="body")) is True