Debian Codegraph @

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