Debian Codegraph @

tests/test_plugin_prowl.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 import apprise
37 from apprise.exception import AppriseImproperlyConfigured
38 from apprise.plugins.prowl import NotifyProwl, ProwlPriority
39
40 logging.disable(logging.CRITICAL)
41
42 # Our Testing URLs
43 apprise_url_tests = (
44 (
45 "prowl://",
46 {
47 "instance": AppriseImproperlyConfigured,
48 },
49 ),
50 # bad url
51 (
52 "prowl://:@/",
53 {
54 "instance": AppriseImproperlyConfigured,
55 },
56 ),
57 # Invalid API Key
58 (
59 "prowl://%s" % ("a" * 20),
60 {
61 "instance": AppriseImproperlyConfigured,
62 },
63 ),
64 # Provider Key
65 (
66 "prowl://{}/{}".format("a" * 40, "b" * 40),
67 {
68 "instance": NotifyProwl,
69 },
70 ),
71 # Invalid Provider Key
72 (
73 "prowl://{}/{}".format("a" * 40, "b" * 20),
74 {
75 "instance": AppriseImproperlyConfigured,
76 },
77 ),
78 # APIkey; no device
79 (
80 "prowl://%s" % ("a" * 40),
81 {
82 "instance": NotifyProwl,
83 },
84 ),
85 # API Key
86 (
87 "prowl://%s" % ("a" * 40),
88 {
89 "instance": NotifyProwl,
90 # don't include an image by default
91 "include_image": False,
92 },
93 ),
94 # API Key + priority setting
95 (
96 "prowl://%s?priority=high" % ("a" * 40),
97 {
98 "instance": NotifyProwl,
99 },
100 ),
101 # API Key + invalid priority setting
102 (
103 "prowl://%s?priority=invalid" % ("a" * 40),
104 {
105 "instance": NotifyProwl,
106 },
107 ),
108 # API Key + priority setting (empty)
109 (
110 "prowl://%s?priority=" % ("a" * 40),
111 {
112 "instance": NotifyProwl,
113 },
114 ),
115 # API Key + No Provider Key (empty)
116 (
117 "prowl://%s///" % ("w" * 40),
118 {
119 "instance": NotifyProwl,
120 # Our expected url(privacy=True) startswith() response:
121 "privacy_url": "prowl://w...w/",
122 },
123 ),
124 # API Key + Provider Key
125 (
126 "prowl://{}/{}".format("a" * 40, "b" * 40),
127 {
128 "instance": NotifyProwl,
129 # Our expected url(privacy=True) startswith() response:
130 "privacy_url": "prowl://a...a/b...b",
131 },
132 ),
133 # API Key + with image
134 (
135 "prowl://%s" % ("a" * 40),
136 {
137 "instance": NotifyProwl,
138 },
139 ),
140 (
141 "prowl://%s" % ("a" * 40),
142 {
143 "instance": NotifyProwl,
144 # force a failure
145 "response": False,
146 "requests_response_code": requests.codes.internal_server_error,
147 },
148 ),
149 (
150 "prowl://%s" % ("a" * 40),
151 {
152 "instance": NotifyProwl,
153 # throw a bizarre code forcing us to fail to look it up
154 "response": False,
155 "requests_response_code": 999,
156 },
157 ),
158 (
159 "prowl://%s" % ("a" * 40),
160 {
161 "instance": NotifyProwl,
162 # Throws a series of i/o exceptions with this flag
163 # is set and tests that we gracefully handle them
164 "test_requests_exceptions": True,
165 },
166 ),
167 )
168
169
170 def test_plugin_prowl():
171 """NotifyProwl() Apprise URLs."""
172
173 # Run our general tests
174 AppriseURLTester(tests=apprise_url_tests).run_all()
175
176
177 def test_plugin_prowl_edge_cases():
178 """NotifyProwl() Edge Cases."""
179 # Initializes the plugin with an invalid apikey
180 with pytest.raises(AppriseImproperlyConfigured):
181 NotifyProwl(apikey=None)
182 # Whitespace also acts as an invalid apikey value
183 with pytest.raises(AppriseImproperlyConfigured):
184 NotifyProwl(apikey=" ")
185
186 # Whitespace also acts as an invalid provider key
187 with pytest.raises(AppriseImproperlyConfigured):
188 NotifyProwl(apikey="abcd", providerkey=object())
189 with pytest.raises(AppriseImproperlyConfigured):
190 NotifyProwl(apikey="abcd", providerkey=" ")
191
192
193 @mock.patch("requests.post")
194 def test_plugin_prowl_config_files(mock_post):
195 """NotifyProwl() Config File Cases."""
196 content = """
197 urls:
198 - prowl://{}:
199 - priority: -2
200 tag: prowl_int low
201 - priority: "-2"
202 tag: prowl_str_int low
203 - priority: low
204 tag: prowl_str low
205
206 # This will take on moderate (default) priority
207 - priority: invalid
208 tag: prowl_invalid
209
210 - prowl://{}:
211 - priority: 2
212 tag: prowl_int emerg
213 - priority: "2"
214 tag: prowl_str_int emerg
215 - priority: emergency
216 tag: prowl_str emerg
217 """.format("a" * 40, "b" * 40)
218
219 # Prepare Mock
220 mock_post.return_value = requests.Request()
221 mock_post.return_value.status_code = requests.codes.ok
222
223 # Create ourselves a config object
224 ac = apprise.AppriseConfig()
225 assert ac.add_config(content=content) is True
226
227 aobj = apprise.Apprise()
228
229 # Add our configuration
230 aobj.add(ac)
231
232 # We should be able to read our 7 services from that
233 # 3x low
234 # 3x emerg
235 # 1x invalid (so takes on normal priority)
236 assert len(ac.services()) == 7
237 assert len(aobj) == 7
238 assert len(list(aobj.find(tag="low"))) == 3
239 for s in aobj.find(tag="low"):
240 assert s.priority == ProwlPriority.LOW
241
242 assert len(list(aobj.find(tag="emerg"))) == 3
243 for s in aobj.find(tag="emerg"):
244 assert s.priority == ProwlPriority.EMERGENCY
245
246 assert len(list(aobj.find(tag="prowl_str"))) == 2
247 assert len(list(aobj.find(tag="prowl_str_int"))) == 2
248 assert len(list(aobj.find(tag="prowl_int"))) == 2
249
250 assert len(list(aobj.find(tag="prowl_invalid"))) == 1
251 assert (
252 next(aobj.find(tag="prowl_invalid")).priority == ProwlPriority.NORMAL
253 )