Debian Codegraph @

tests/test_plugin_wxpusher.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 from json import dumps, loads
29
30 # Disable logging for a cleaner testing output
31 import logging
32 import os
33 from unittest import mock
34
35 from helpers import AppriseURLTester
36 import requests
37
38 from apprise import Apprise
39 from apprise.plugins.wxpusher import NotifyWxPusher
40
41 logging.disable(logging.CRITICAL)
42
43 WXPUSHER_GOOD_RESPONSE = dumps({"code": 1000})
44 WXPUSHER_BAD_RESPONSE = dumps({"code": 99})
45
46
47 # Attachment Directory
48 TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var")
49
50 # Our Testing URLs
51 apprise_url_tests = (
52 (
53 "wxpusher://",
54 {
55 # No token specified
56 "instance": TypeError,
57 },
58 ),
59 (
60 "wxpusher://:@/",
61 {
62 # invalid url
63 "instance": TypeError,
64 },
65 ),
66 (
67 "wxpusher://invalid",
68 {
69 # invalid app token
70 "instance": TypeError,
71 },
72 ),
73 (
74 "wxpusher://AT_appid/123/",
75 {
76 # invalid 'to' phone number
77 "instance": NotifyWxPusher,
78 # Notify will fail because it couldn't send to anyone
79 "response": False,
80 # Our expected url(privacy=True) startswith() response:
81 "privacy_url": "wxpusher://****/123/",
82 # Our response expected server response
83 "requests_response_text": WXPUSHER_GOOD_RESPONSE,
84 },
85 ),
86 (
87 "wxpusher://AT_appid/%20/%20/",
88 {
89 # invalid 'to' phone number
90 "instance": NotifyWxPusher,
91 # Notify will fail because it couldn't send to anyone
92 "response": False,
93 # Our expected url(privacy=True) startswith() response:
94 "privacy_url": "wxpusher://****/",
95 # Our response expected server response
96 "requests_response_text": WXPUSHER_GOOD_RESPONSE,
97 },
98 ),
99 (
100 "wxpusher://AT_appid/123/",
101 {
102 # one phone number will notify ourselves
103 "instance": NotifyWxPusher,
104 # Our response expected server response
105 "requests_response_text": WXPUSHER_GOOD_RESPONSE,
106 },
107 ),
108 (
109 "wxpusher://123?token=AT_abc1234",
110 {
111 # pass our token in as an argument and our host actually becomes a
112 # target
113 "instance": NotifyWxPusher,
114 # Our response expected server response
115 "requests_response_text": WXPUSHER_GOOD_RESPONSE,
116 },
117 ),
118 (
119 "wxpusher://?token=AT_abc1234",
120 {
121 # slightly different then test above; a token is defined, but
122 # there are no targets
123 "instance": NotifyWxPusher,
124 # Notify will fail because it couldn't send to anyone
125 "response": False,
126 # Our response expected server response
127 "requests_response_text": WXPUSHER_GOOD_RESPONSE,
128 },
129 ),
130 (
131 "wxpusher://?token=AT_abc1234&to=UID_abc",
132 {
133 # all kwargs to load url with
134 "instance": NotifyWxPusher,
135 # Our response expected server response
136 "requests_response_text": WXPUSHER_GOOD_RESPONSE,
137 },
138 ),
139 (
140 "wxpusher://AT_appid/UID_abcd/",
141 {
142 # a valid contact
143 "instance": NotifyWxPusher,
144 # Our expected url(privacy=True) startswith() response:
145 "privacy_url": "wxpusher://****/UID_abcd",
146 # Our response expected server response
147 "requests_response_text": WXPUSHER_GOOD_RESPONSE,
148 },
149 ),
150 (
151 "wxpusher://AT_appid/@/#/,/",
152 {
153 # Test case where we provide bad data
154 "instance": NotifyWxPusher,
155 # Our failed response
156 "requests_response_text": WXPUSHER_GOOD_RESPONSE,
157 # as a result, we expect a failed notification
158 "response": False,
159 },
160 ),
161 (
162 "wxpusher://AT_appid/123/",
163 {
164 # Test case where we get a bad response
165 "instance": NotifyWxPusher,
166 # Our expected url(privacy=True) startswith() response:
167 "privacy_url": "wxpusher://****/123",
168 # Our failed response
169 "requests_response_text": WXPUSHER_BAD_RESPONSE,
170 # as a result, we expect a failed notification
171 "response": False,
172 },
173 ),
174 (
175 "wxpusher://AT_appid/UID_345/",
176 {
177 # Test case where we get a bad response
178 "instance": NotifyWxPusher,
179 # Our expected url(privacy=True) startswith() response:
180 "privacy_url": "wxpusher://****/UID_345",
181 # Our failed response
182 "requests_response_text": None,
183 # as a result, we expect a failed notification
184 "response": False,
185 },
186 ),
187 (
188 "wxpusher://AT_appid/UID_345/",
189 {
190 # Test case where we get a bad response
191 "instance": NotifyWxPusher,
192 # Our expected url(privacy=True) startswith() response:
193 "privacy_url": "wxpusher://****/UID_345",
194 # Our failed response (bad json)
195 "requests_response_text": "{",
196 # as a result, we expect a failed notification
197 "response": False,
198 },
199 ),
200 (
201 "wxpusher://AT_appid/?to={},{}".format("2" * 11, "3" * 11),
202 {
203 # use get args to acomplish the same thing
204 "instance": NotifyWxPusher,
205 # Our response expected server response
206 "requests_response_text": WXPUSHER_GOOD_RESPONSE,
207 },
208 ),
209 (
210 "wxpusher://AT_appid/?to={},{},{}".format("2" * 11, "3" * 11, "5" * 3),
211 {
212 # 2 good targets and one invalid one
213 "instance": NotifyWxPusher,
214 # Our response expected server response
215 "requests_response_text": WXPUSHER_GOOD_RESPONSE,
216 },
217 ),
218 (
219 "wxpusher://AT_appid/{}/{}/".format("2" * 11, "3" * 11),
220 {
221 # If we have from= specified, then all elements take on the
222 # to= value
223 "instance": NotifyWxPusher,
224 # Our response expected server response
225 "requests_response_text": WXPUSHER_GOOD_RESPONSE,
226 },
227 ),
228 (
229 "wxpusher://AT_appid/{}".format("3" * 11),
230 {
231 # use get args to acomplish the same thing (use source instead
232 # of from)
233 "instance": NotifyWxPusher,
234 # Our response expected server response
235 "requests_response_text": WXPUSHER_GOOD_RESPONSE,
236 },
237 ),
238 (
239 "wxpusher://AT_appid/{}".format("4" * 11),
240 {
241 "instance": NotifyWxPusher,
242 # throw a bizarre code forcing us to fail to look it up
243 "response": False,
244 "requests_response_code": 999,
245 # Our response expected server response
246 "requests_response_text": WXPUSHER_GOOD_RESPONSE,
247 },
248 ),
249 (
250 "wxpusher://AT_appid/{}".format("4" * 11),
251 {
252 "instance": NotifyWxPusher,
253 # Throws a series of i/o exceptions with this flag
254 # is set and tests that we gracefully handle them
255 "test_requests_exceptions": True,
256 },
257 ),
258 )
259
260
261 def test_plugin_wxpusher_urls():
262 """NotifyWxPusher() Apprise URLs."""
263
264 # Run our general tests
265 AppriseURLTester(tests=apprise_url_tests).run_all()
266
267
268 @mock.patch("requests.post")
269 def test_plugin_wxpusher_edge_cases(mock_post):
270 """NotifyWxPusher() Edge Cases."""
271
272 # Prepare our response
273 response = requests.Request()
274 response.status_code = requests.codes.ok
275 response.content = WXPUSHER_GOOD_RESPONSE
276
277 # Prepare Mock
278 mock_post.return_value = response
279
280 # Initialize some generic (but valid) tokens
281 target = "UID_abcd"
282 body = "test body"
283 title = "My Title"
284
285 aobj = Apprise()
286 assert aobj.add(f"wxpusher://AT_appid/{target}")
287 assert len(aobj) == 1
288 assert aobj.notify(title=title, body=body)
289 assert mock_post.call_count == 1
290
291 details = mock_post.call_args_list[0]
292 assert details[0][0] == "https://wxpusher.zjiecode.com/api/send/message"
293 payload = loads(details[1]["data"])
294 assert payload == {
295 "appToken": "AT_appid",
296 "content": "test body",
297 "summary": "My Title",
298 "contentType": 1,
299 "topicIds": [],
300 "uids": ["UID_abcd"],
301 "url": None,
302 }
303
304 # Reset our mock object
305 mock_post.reset_mock()
306
307
308 @mock.patch("requests.post")
309 def test_plugin_wxpusher_result_set(mock_post):
310 """NotifyWxPusher() Result Sets."""
311
312 # Prepare our response
313 response = requests.Request()
314 response.status_code = requests.codes.ok
315 response.content = WXPUSHER_GOOD_RESPONSE
316
317 # Prepare Mock
318 mock_post.return_value = response
319
320 body = "test body"
321 title = "My Title"
322
323 aobj = Apprise()
324 aobj.add("wxpusher://AT_appid/123/abc/UID_456")
325 # One bad entry and 2 good
326 assert len(aobj[0]) == 1
327
328 assert aobj.notify(title=title, body=body)
329
330 # 2 posts made
331 assert mock_post.call_count == 1
332
333 details = mock_post.call_args_list[0]
334 assert details[0][0] == "https://wxpusher.zjiecode.com/api/send/message"
335 payload = loads(details[1]["data"])
336 assert payload == {
337 "appToken": "AT_appid",
338 "content": "test body",
339 "summary": "My Title",
340 "contentType": 1,
341 "topicIds": [123],
342 "uids": ["UID_456"],
343 "url": None,
344 }
345
346 # Validate our information is also placed back into the assembled URL
347 assert "/123" in aobj[0].url()
348 assert "/UID_456" in aobj[0].url()
349 assert "/abc" in aobj[0].url()
350
351 mock_post.reset_mock()
352
353 aobj = Apprise()
354 aobj.add("wxpusher://AT_appid//UID_123/UID_abc/123456789")
355 assert len(aobj[0]) == 1
356
357 assert aobj.notify(title=title, body=body)
358
359 # If batch is off then there is a post per entry
360 assert mock_post.call_count == 1
361
362 details = mock_post.call_args_list[0]
363 assert details[0][0] == "https://wxpusher.zjiecode.com/api/send/message"
364 payload = loads(details[1]["data"])
365
366 assert payload == {
367 "appToken": "AT_appid",
368 "content": "test body",
369 "summary": "My Title",
370 "contentType": 1,
371 "topicIds": [123456789],
372 "uids": ["UID_123", "UID_abc"],
373 "url": None,
374 }
375
376 assert "/123456789" in aobj[0].url()
377 assert "/UID_123" in aobj[0].url()
378 assert "/UID_abc" in aobj[0].url()
379
380
381 @mock.patch("requests.post")
382 def test_notify_wxpusher_plugin_result_list(mock_post):
383 """NotifyWxPusher() Result List Response."""
384
385 okay_response = requests.Request()
386 okay_response.status_code = requests.codes.ok
387 # We want to test the case where the `result` set returned is a list
388
389 # Invalid JSON response
390 okay_response.content = "{"
391
392 # Assign our mock object our return value
393 mock_post.return_value = okay_response
394
395 obj = Apprise.instantiate("wxpusher://AT_apptoken/UID_abcd/")
396 assert isinstance(obj, NotifyWxPusher)
397
398 # We should now fail
399 assert obj.notify("test") is False