Debian Codegraph @

tests/test_plugin_notifiarr.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 from inspect import cleandoc
29 from json import loads
30
31 # Disable logging for a cleaner testing output
32 import logging
33 from unittest import mock
34
35 from helpers import AppriseURLTester
36 import requests
37
38 from apprise import NotifyType
39 from apprise.plugins.notifiarr import NotifyNotifiarr
40
41 logging.disable(logging.CRITICAL)
42
43 # Our Testing URLs
44 apprise_url_tests = (
45 (
46 "notifiarr://:@/",
47 {
48 "instance": TypeError,
49 },
50 ),
51 (
52 "notifiarr://",
53 {
54 "instance": TypeError,
55 },
56 ),
57 (
58 "notifiarr://apikey",
59 {
60 "instance": NotifyNotifiarr,
61 # Response will fail due to no targets defined
62 "notify_response": False,
63 # Our expected url(privacy=True) startswith() response:
64 "privacy_url": "notifiarr://a...y",
65 },
66 ),
67 (
68 "notifiarr://apikey/1234/?event=invalid",
69 {
70 "instance": TypeError,
71 },
72 ),
73 (
74 "notifiarr://apikey/%%invalid%%",
75 {
76 "instance": NotifyNotifiarr,
77 # Response will fail due to no targets defined
78 "notify_response": False,
79 # Our expected url(privacy=True) startswith() response:
80 "privacy_url": "notifiarr://a...y",
81 },
82 ),
83 (
84 "notifiarr://apikey/#123",
85 {
86 "instance": NotifyNotifiarr,
87 # Our expected url(privacy=True) startswith() response:
88 "privacy_url": "notifiarr://a...y/#123",
89 },
90 ),
91 (
92 "notifiarr://apikey/123?image=No",
93 {
94 "instance": NotifyNotifiarr,
95 },
96 ),
97 (
98 "notifiarr://apikey/123?image=yes",
99 {
100 "instance": NotifyNotifiarr,
101 # Our expected url(privacy=True) startswith() response:
102 "privacy_url": "notifiarr://a...y/#123",
103 },
104 ),
105 (
106 "notifiarr://apikey/?to=123,432",
107 {
108 "instance": NotifyNotifiarr,
109 # Our expected url(privacy=True) startswith() response:
110 "privacy_url": "notifiarr://a...y/#123/#432",
111 },
112 ),
113 (
114 "notifiarr://apikey/?to=123,432&event=1234",
115 {
116 # Test event
117 "instance": NotifyNotifiarr,
118 # Our expected url(privacy=True) startswith() response:
119 "privacy_url": "notifiarr://a...y/#123/#432",
120 },
121 ),
122 (
123 "notifiarr://123/?apikey=myapikey",
124 {
125 "instance": NotifyNotifiarr,
126 # Our expected url(privacy=True) startswith() response:
127 "privacy_url": "notifiarr://m...y/#123",
128 },
129 ),
130 (
131 "notifiarr://123/?key=myapikey",
132 {
133 # Support key=
134 "instance": NotifyNotifiarr,
135 # Our expected url(privacy=True) startswith() response:
136 "privacy_url": "notifiarr://m...y/#123",
137 },
138 ),
139 (
140 "notifiarr://123/?apikey=myapikey&image=yes",
141 {
142 "instance": NotifyNotifiarr,
143 },
144 ),
145 (
146 "notifiarr://123/?apikey=myapikey&image=no",
147 {
148 "instance": NotifyNotifiarr,
149 },
150 ),
151 (
152 "notifiarr://123/?apikey=myapikey&source=My%20System",
153 {
154 "instance": NotifyNotifiarr,
155 # Our expected url(privacy=True) startswith() response:
156 "privacy_url": "notifiarr://m...y/#123",
157 },
158 ),
159 (
160 "notifiarr://123/?apikey=myapikey&from=My%20System",
161 {
162 "instance": NotifyNotifiarr,
163 # Our expected url(privacy=True) startswith() response:
164 "privacy_url": "notifiarr://m...y/#123",
165 },
166 ),
167 (
168 "notifiarr://?apikey=myapikey",
169 {
170 # No Channel or host
171 "instance": NotifyNotifiarr,
172 # Response will fail due to no targets defined
173 "notify_response": False,
174 # Our expected url(privacy=True) startswith() response:
175 "privacy_url": "notifiarr://m...y/",
176 },
177 ),
178 (
179 "notifiarr://invalid?apikey=myapikey",
180 {
181 # No Channel or host
182 "instance": NotifyNotifiarr,
183 # invalid channel
184 "notify_response": False,
185 # Our expected url(privacy=True) startswith() response:
186 "privacy_url": "notifiarr://m...y/",
187 },
188 ),
189 (
190 "notifiarr://123/325/?apikey=myapikey",
191 {
192 "instance": NotifyNotifiarr,
193 # Our expected url(privacy=True) startswith() response:
194 "privacy_url": "notifiarr://m...y/#123/#325",
195 },
196 ),
197 (
198 "notifiarr://apikey/123/",
199 {
200 "instance": NotifyNotifiarr,
201 },
202 ),
203 (
204 "notifiarr://apikey/123",
205 {
206 "instance": NotifyNotifiarr,
207 # force a failure
208 "response": False,
209 "requests_response_code": requests.codes.internal_server_error,
210 },
211 ),
212 (
213 "notifiarr://apikey/123",
214 {
215 "instance": NotifyNotifiarr,
216 # throw a bizarre code forcing us to fail to look it up
217 "response": False,
218 "requests_response_code": 999,
219 },
220 ),
221 (
222 "notifiarr://apikey/123",
223 {
224 "instance": NotifyNotifiarr,
225 # Throws a series of i/o exceptions with this flag
226 # is set and tests that we gracefully handle them
227 "test_requests_exceptions": True,
228 },
229 ),
230 )
231
232
233 def test_plugin_notifiarr_urls():
234 """NotifyNotifiarr() Apprise URLs."""
235
236 # Run our general tests
237 AppriseURLTester(tests=apprise_url_tests).run_all()
238
239
240 @mock.patch("requests.post")
241 def test_plugin_notifiarr_notifications(mock_post):
242 """NotifyNotifiarr() Notifications/Ping Support."""
243
244 # Test our header parsing when not lead with a header
245 body = cleandoc("""
246 # Heading
247 @everyone and @admin, wake and meet our new user <@123> and <@987>;
248 Attention Roles: <@&456> and <@&765>
249 """)
250
251 # Prepare a good response
252 response = mock.Mock()
253 response.status_code = requests.codes.ok
254
255 # Prepare Mock return object
256 mock_post.return_value = response
257
258 results = NotifyNotifiarr.parse_url("notifiarr://apikey/12345")
259
260 instance = NotifyNotifiarr(**results)
261 assert isinstance(instance, NotifyNotifiarr)
262
263 response = instance.send(body=body)
264 assert response is True
265 assert mock_post.call_count == 1
266
267 details = mock_post.call_args_list[0]
268 assert "NotifyType." not in details[1]["data"]
269 assert details[0][0] == "https://notifiarr.com/api/v1/notification/apprise"
270
271 payload = loads(details[1]["data"])
272
273 # First role and first user stored
274 assert payload == {
275 "source": "Apprise",
276 "type": NotifyType.INFO.value,
277 "notification": {"update": False, "name": "Apprise", "event": ""},
278 "discord": {
279 "color": "#3AA3E3",
280 "ping": {
281 # Only supports 1 entry each; so first one is parsed
282 "pingUser": "123",
283 "pingRole": "456",
284 },
285 "text": {
286 "title": "",
287 "content": "👉 @everyone @admin <@123> <@987> <@&456> <@&765>",
288 "description": (
289 "# Heading\n@everyone and @admin, wake and meet our new "
290 "user <@123> and <@987>;\nAttention Roles: <@&456> and "
291 "<@&765>\n "
292 ),
293 "footer": "Apprise Notifications",
294 },
295 "ids": {"channel": 12345},
296 },
297 }