Debian Codegraph @

tests/test_plugin_join.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 import NotifyType
38 from apprise.plugins.join import JoinPriority, NotifyJoin
39
40 logging.disable(logging.CRITICAL)
41
42 # Our Testing URLs
43 apprise_url_tests = (
44 (
45 "join://",
46 {
47 "instance": TypeError,
48 },
49 ),
50 # API Key + bad url
51 (
52 "join://:@/",
53 {
54 "instance": TypeError,
55 },
56 ),
57 # APIkey; no device
58 (
59 "join://%s" % ("a" * 32),
60 {
61 "instance": NotifyJoin,
62 },
63 ),
64 # API Key + device (using to=)
65 (
66 "join://{}?to={}".format("a" * 32, "d" * 32),
67 {
68 "instance": NotifyJoin,
69 # Our expected url(privacy=True) startswith() response:
70 "privacy_url": "join://a...a/",
71 },
72 ),
73 # API Key + priority setting
74 (
75 "join://%s?priority=high" % ("a" * 32),
76 {
77 "instance": NotifyJoin,
78 },
79 ),
80 # API Key + invalid priority setting
81 (
82 "join://%s?priority=invalid" % ("a" * 32),
83 {
84 "instance": NotifyJoin,
85 },
86 ),
87 # API Key + priority setting (empty)
88 (
89 "join://%s?priority=" % ("a" * 32),
90 {
91 "instance": NotifyJoin,
92 },
93 ),
94 # API Key + device
95 (
96 "join://{}@{}?image=True".format("a" * 32, "d" * 32),
97 {
98 "instance": NotifyJoin,
99 },
100 ),
101 # No image
102 (
103 "join://{}@{}?image=False".format("a" * 32, "d" * 32),
104 {
105 "instance": NotifyJoin,
106 },
107 ),
108 # API Key + Device Name
109 (
110 "join://{}/{}".format("a" * 32, "My Device"),
111 {
112 "instance": NotifyJoin,
113 },
114 ),
115 # API Key + device
116 (
117 "join://{}/{}".format("a" * 32, "d" * 32),
118 {
119 "instance": NotifyJoin,
120 # don't include an image by default
121 "include_image": False,
122 },
123 ),
124 # API Key + 2 devices
125 (
126 "join://{}/{}/{}".format("a" * 32, "d" * 32, "e" * 32),
127 {
128 "instance": NotifyJoin,
129 # don't include an image by default
130 "include_image": False,
131 },
132 ),
133 # API Key + 1 device and 1 group
134 (
135 "join://{}/{}/{}".format("a" * 32, "d" * 32, "group.chrome"),
136 {
137 "instance": NotifyJoin,
138 },
139 ),
140 (
141 "join://%s" % ("a" * 32),
142 {
143 "instance": NotifyJoin,
144 # force a failure
145 "response": False,
146 "requests_response_code": requests.codes.internal_server_error,
147 },
148 ),
149 (
150 "join://%s" % ("a" * 32),
151 {
152 "instance": NotifyJoin,
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 "join://%s" % ("a" * 32),
160 {
161 "instance": NotifyJoin,
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_join_urls():
171 """NotifyJoin() Apprise URLs."""
172
173 # Run our general tests
174 AppriseURLTester(tests=apprise_url_tests).run_all()
175
176
177 @mock.patch("requests.get")
178 @mock.patch("requests.post")
179 def test_plugin_join_edge_cases(mock_post, mock_get):
180 """NotifyJoin() Edge Cases."""
181
182 # Generate some generic message types
183 device = "A" * 32
184 group = "group.chrome"
185 apikey = "a" * 32
186
187 # Initializes the plugin with devices set to a string
188 NotifyJoin(apikey=apikey, targets=group)
189
190 # Initializes the plugin with devices set to None
191 NotifyJoin(apikey=apikey, targets=None)
192
193 # Initializes the plugin with an invalid apikey
194 with pytest.raises(TypeError):
195 NotifyJoin(apikey=None)
196
197 # Whitespace also acts as an invalid apikey
198 with pytest.raises(TypeError):
199 NotifyJoin(apikey=" ")
200
201 # Initializes the plugin with devices set to a set
202 p = NotifyJoin(apikey=apikey, targets=[group, device])
203
204 # Prepare our mock responses
205 req = requests.Request()
206 req.status_code = requests.codes.created
207 req.content = ""
208 mock_get.return_value = req
209 mock_post.return_value = req
210
211 # Test notifications without a body or a title; nothing to send
212 # so we return False
213 assert (
214 p.notify(body=None, title=None, notify_type=NotifyType.INFO) is False
215 )
216
217
218 @mock.patch("requests.post")
219 def test_plugin_join_config_files(mock_post):
220 """NotifyJoin() Config File Cases."""
221 content = """
222 urls:
223 - join://{}@{}:
224 - priority: -2
225 tag: join_int low
226 - priority: "-2"
227 tag: join_str_int low
228 - priority: low
229 tag: join_str low
230
231 # This will take on normal (default) priority
232 - priority: invalid
233 tag: join_invalid
234
235 - join://{}@{}:
236 - priority: 2
237 tag: join_int emerg
238 - priority: "2"
239 tag: join_str_int emerg
240 - priority: emergency
241 tag: join_str emerg
242 """.format("a" * 32, "b" * 32, "c" * 32, "d" * 32)
243
244 # Prepare Mock
245 mock_post.return_value = requests.Request()
246 mock_post.return_value.status_code = requests.codes.ok
247
248 # Create ourselves a config object
249 ac = apprise.AppriseConfig()
250 assert ac.add_config(content=content) is True
251
252 aobj = apprise.Apprise()
253
254 # Add our configuration
255 aobj.add(ac)
256
257 # We should be able to read our 7 servers from that
258 # 3x low
259 # 3x emerg
260 # 1x invalid (so takes on normal priority)
261 assert len(ac.servers()) == 7
262 assert len(aobj) == 7
263 assert len(list(aobj.find(tag="low"))) == 3
264 for s in aobj.find(tag="low"):
265 assert s.priority == JoinPriority.LOW
266
267 assert len(list(aobj.find(tag="emerg"))) == 3
268 for s in aobj.find(tag="emerg"):
269 assert s.priority == JoinPriority.EMERGENCY
270
271 assert len(list(aobj.find(tag="join_str"))) == 2
272 assert len(list(aobj.find(tag="join_str_int"))) == 2
273 assert len(list(aobj.find(tag="join_int"))) == 2
274
275 assert len(list(aobj.find(tag="join_invalid"))) == 1
276 assert next(aobj.find(tag="join_invalid")).priority == JoinPriority.NORMAL
277
278 # Notifications work
279 assert aobj.notify(title="title", body="body") is True