Debian Codegraph @

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