Debian Codegraph @

tests/test_plugin_pushed.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 pytest
34 import requests
35
36 from apprise.exception import AppriseImproperlyConfigured
37 from apprise.plugins.pushed import NotifyPushed
38
39 logging.disable(logging.CRITICAL)
40
41 # Our Testing URLs
42 apprise_url_tests = (
43 (
44 "pushed://",
45 {
46 "instance": AppriseImproperlyConfigured,
47 },
48 ),
49 # Application Key Only
50 (
51 "pushed://%s" % ("a" * 32),
52 {
53 "instance": AppriseImproperlyConfigured,
54 },
55 ),
56 # Invalid URL
57 (
58 "pushed://:@/",
59 {
60 "instance": AppriseImproperlyConfigured,
61 },
62 ),
63 # Application Key+Secret
64 (
65 "pushed://{}/{}".format("a" * 32, "a" * 64),
66 {
67 "instance": NotifyPushed,
68 },
69 ),
70 # Application Key+Secret + channel
71 (
72 "pushed://{}/{}/#channel/".format("a" * 32, "a" * 64),
73 {
74 "instance": NotifyPushed,
75 },
76 ),
77 # Application Key+Secret + channel (via to=)
78 (
79 "pushed://{}/{}?to=channel".format("a" * 32, "a" * 64),
80 {
81 "instance": NotifyPushed,
82 # Our expected url(privacy=True) startswith() response:
83 "privacy_url": "pushed://a...a/****/",
84 },
85 ),
86 # Application Key+Secret + dropped entry
87 (
88 "pushed://{}/{}/dropped_value/".format("a" * 32, "a" * 64),
89 {
90 # No entries validated is a fail
91 "instance": AppriseImproperlyConfigured,
92 },
93 ),
94 # Application Key+Secret + 2 channels
95 (
96 "pushed://{}/{}/#channel1/#channel2".format("a" * 32, "a" * 64),
97 {
98 "instance": NotifyPushed,
99 },
100 ),
101 # Application Key+Secret + User Pushed ID
102 (
103 "pushed://{}/{}/@ABCD/".format("a" * 32, "a" * 64),
104 {
105 "instance": NotifyPushed,
106 },
107 ),
108 # Application Key+Secret + 2 devices
109 (
110 "pushed://{}/{}/@ABCD/@DEFG/".format("a" * 32, "a" * 64),
111 {
112 "instance": NotifyPushed,
113 },
114 ),
115 # Application Key+Secret + Combo
116 (
117 "pushed://{}/{}/@ABCD/#channel".format("a" * 32, "a" * 64),
118 {
119 "instance": NotifyPushed,
120 },
121 ),
122 # ,
123 (
124 "pushed://{}/{}".format("a" * 32, "a" * 64),
125 {
126 "instance": NotifyPushed,
127 # force a failure
128 "response": False,
129 "requests_response_code": requests.codes.internal_server_error,
130 },
131 ),
132 (
133 "pushed://{}/{}".format("a" * 32, "a" * 64),
134 {
135 "instance": NotifyPushed,
136 # throw a bizarre code forcing us to fail to look it up
137 "response": False,
138 "requests_response_code": 999,
139 },
140 ),
141 (
142 "pushed://{}/{}".format("a" * 32, "a" * 64),
143 {
144 "instance": NotifyPushed,
145 # Throws a series of i/o exceptions with this flag
146 # is set and tests that we gracefully handle them
147 "test_requests_exceptions": True,
148 },
149 ),
150 (
151 "pushed://{}/{}".format("a" * 32, "a" * 64),
152 {
153 "instance": NotifyPushed,
154 # force a failure
155 "response": False,
156 "requests_response_code": requests.codes.internal_server_error,
157 },
158 ),
159 (
160 "pushed://{}/{}".format("a" * 32, "a" * 64),
161 {
162 "instance": NotifyPushed,
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 "pushed://{}/{}/#channel".format("a" * 32, "a" * 64),
170 {
171 "instance": NotifyPushed,
172 # throw a bizarre code forcing us to fail to look it up
173 "response": False,
174 "requests_response_code": 999,
175 },
176 ),
177 (
178 "pushed://{}/{}/@user".format("a" * 32, "a" * 64),
179 {
180 "instance": NotifyPushed,
181 # throw a bizarre code forcing us to fail to look it up
182 "response": False,
183 "requests_response_code": 999,
184 },
185 ),
186 (
187 "pushed://{}/{}".format("a" * 32, "a" * 64),
188 {
189 "instance": NotifyPushed,
190 # Throws a series of i/o exceptions with this flag
191 # is set and tests that we gracefully handle them
192 "test_requests_exceptions": True,
193 },
194 ),
195 )
196
197
198 def test_plugin_pushed_urls():
199 """NotifyPushed() Apprise URLs."""
200
201 # Run our general tests
202 AppriseURLTester(tests=apprise_url_tests).run_all()
203
204
205 @mock.patch("requests.get")
206 @mock.patch("requests.post")
207 def test_plugin_pushed_edge_cases(mock_post, mock_get):
208 """NotifyPushed() Edge Cases."""
209
210 # Chat ID
211 recipients = "@ABCDEFG, @DEFGHIJ, #channel, #channel2"
212
213 # Some required input
214 app_key = "ABCDEFG"
215 app_secret = "ABCDEFG"
216
217 # Prepare Mock
218 mock_get.return_value = requests.Request()
219 mock_post.return_value = requests.Request()
220 mock_post.return_value.status_code = requests.codes.ok
221 mock_get.return_value.status_code = requests.codes.ok
222
223 # No application Key specified
224 with pytest.raises(AppriseImproperlyConfigured):
225 NotifyPushed(
226 app_key=None,
227 app_secret=app_secret,
228 recipients=None,
229 )
230
231 with pytest.raises(AppriseImproperlyConfigured):
232 NotifyPushed(
233 app_key=" ",
234 app_secret=app_secret,
235 recipients=None,
236 )
237 # No application Secret specified
238 with pytest.raises(AppriseImproperlyConfigured):
239 NotifyPushed(
240 app_key=app_key,
241 app_secret=None,
242 recipients=None,
243 )
244
245 with pytest.raises(AppriseImproperlyConfigured):
246 NotifyPushed(
247 app_key=app_key,
248 app_secret=" ",
249 )
250
251 # recipients list set to (None) is perfectly fine; in this case it will
252 # notify the App
253 obj = NotifyPushed(
254 app_key=app_key,
255 app_secret=app_secret,
256 recipients=None,
257 )
258 assert isinstance(obj, NotifyPushed) is True
259 assert len(obj.channels) == 0
260 assert len(obj.users) == 0
261
262 obj = NotifyPushed(
263 app_key=app_key,
264 app_secret=app_secret,
265 targets=recipients,
266 )
267 assert isinstance(obj, NotifyPushed) is True
268 assert len(obj.channels) == 2
269 assert len(obj.users) == 2
270
271 # Prepare Mock to fail
272 mock_post.return_value.status_code = requests.codes.internal_server_error
273 mock_get.return_value.status_code = requests.codes.internal_server_error