Debian Codegraph @

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