Debian Codegraph @

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