Debian Codegraph @

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