Debian Codegraph @

tests/test_plugin_flowtriq.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 json
30 import logging
31 from unittest import mock
32 from urllib.parse import urlparse
33
34 from helpers import AppriseURLTester
35 import pytest
36 import requests
37
38 from apprise import Apprise, NotifyType
39 from apprise.exception import AppriseImproperlyConfigured
40 from apprise.plugins.flowtriq import NotifyFlowtriq
41
42 logging.disable(logging.CRITICAL)
43
44 # Our Testing URLs
45 apprise_url_tests = (
46 (
47 "flowtriq://",
48 {
49 "instance": None,
50 },
51 ),
52 # No webhook path specified
53 (
54 "flowtriq://apikey@hostname",
55 {
56 "instance": AppriseImproperlyConfigured,
57 },
58 ),
59 # No API key specified
60 (
61 "flowtriq://hostname/hooks/abc123",
62 {
63 "instance": AppriseImproperlyConfigured,
64 },
65 ),
66 # Provide a hostname, apikey, and webhook path (insecure)
67 (
68 "flowtriq://myapikey@hostname/hooks/abc123/",
69 {
70 "instance": NotifyFlowtriq,
71 # Our expected url(privacy=True) startswith() response:
72 "privacy_url": "flowtriq://m...y@hostname/hooks/abc123/",
73 },
74 ),
75 # Secure variant (flowtriqs://)
76 (
77 "flowtriqs://myapikey@hostname/hooks/abc123/",
78 {
79 "instance": NotifyFlowtriq,
80 "privacy_url": "flowtriqs://m...y@hostname/hooks/abc123/",
81 },
82 ),
83 # Provide a hostname with port (insecure)
84 (
85 "flowtriq://myapikey@hostname:8080/hooks/abc123/",
86 {
87 "instance": NotifyFlowtriq,
88 "privacy_url": "flowtriq://m...y@hostname:8080/hooks/abc123/",
89 },
90 ),
91 # Secure with non-default port
92 (
93 "flowtriqs://myapikey@hostname:8443/hooks/abc123/",
94 {
95 "instance": NotifyFlowtriq,
96 "privacy_url": "flowtriqs://m...y@hostname:8443/hooks/abc123/",
97 },
98 ),
99 # Multi-segment webhook path
100 (
101 "flowtriqs://[email protected]/api/v1/webhook/xyz/",
102 {
103 "instance": NotifyFlowtriq,
104 },
105 ),
106 # An invalid url
107 (
108 "flowtriq://:@/",
109 {
110 "instance": None,
111 },
112 ),
113 # Test failure cases
114 (
115 "flowtriq://myapikey@hostname/hooks/abc123/",
116 {
117 "instance": NotifyFlowtriq,
118 # force a failure
119 "response": False,
120 "requests_response_code": requests.codes.internal_server_error,
121 },
122 ),
123 (
124 "flowtriq://myapikey@hostname/hooks/abc123/",
125 {
126 "instance": NotifyFlowtriq,
127 # throw a bizarre code forcing us to fail to look it up
128 "response": False,
129 "requests_response_code": 999,
130 },
131 ),
132 (
133 "flowtriq://myapikey@hostname/hooks/abc123/",
134 {
135 "instance": NotifyFlowtriq,
136 # Throws a series of i/o exceptions with this flag
137 # is set and tests that we gracefully handle them
138 "test_requests_exceptions": True,
139 },
140 ),
141 )
142
143
144 def test_plugin_flowtriq_urls():
145 """NotifyFlowtriq() Apprise URLs."""
146
147 # Run our general tests
148 AppriseURLTester(tests=apprise_url_tests).run_all()
149
150
151 def test_plugin_flowtriq_edge_cases():
152 """NotifyFlowtriq() Edge Cases."""
153 # Initializes the plugin with an invalid API key
154 with pytest.raises(AppriseImproperlyConfigured):
155 NotifyFlowtriq(apikey=None, webhook_path="hooks/abc123")
156 # Whitespace also acts as an invalid API key
157 with pytest.raises(AppriseImproperlyConfigured):
158 NotifyFlowtriq(apikey=" ", webhook_path="hooks/abc123")
159
160 # Missing webhook path
161 with pytest.raises(AppriseImproperlyConfigured):
162 NotifyFlowtriq(apikey="validkey", webhook_path=None)
163 with pytest.raises(AppriseImproperlyConfigured):
164 NotifyFlowtriq(apikey="validkey", webhook_path=" ")
165
166 # A webhook path consisting only of slashes strips to empty
167 with pytest.raises(AppriseImproperlyConfigured):
168 NotifyFlowtriq(apikey="validkey", webhook_path="///")
169
170 # Missing host is caught after super().__init__()
171 with pytest.raises(AppriseImproperlyConfigured):
172 NotifyFlowtriq(
173 apikey="validkey", webhook_path="hooks/abc123", host=None
174 )
175
176
177 @mock.patch("requests.post")
178 def test_plugin_flowtriq_send(mock_post):
179 """NotifyFlowtriq() send path coverage."""
180
181 def _mk_resp(code=requests.codes.ok):
182 r = mock.Mock()
183 r.status_code = code
184 r.content = b"{}"
185 return r
186
187 # ------------------------------------------------------------------
188 # Successful send — 200
189 # ------------------------------------------------------------------
190 mock_post.return_value = _mk_resp(requests.codes.ok)
191 obj = NotifyFlowtriq(
192 apikey="ft_key_xxxx",
193 webhook_path="hooks/abc123",
194 host="flowtriq.com",
195 )
196 assert obj.send(body="Test body", title="Test title") is True
197 assert mock_post.call_count == 1
198
199 # Verify the POST landed at the right hostname
200 call_url = mock_post.call_args[0][0]
201 assert urlparse(call_url).hostname == "flowtriq.com"
202
203 # Verify payload structure and severity mapping for INFO
204 payload = json.loads(mock_post.call_args[1]["data"])
205 assert payload["body"] == "Test body"
206 assert payload["title"] == "Test title"
207 assert payload["severity"] == "info"
208 assert payload["source"] == "apprise"
209
210 # Verify the API key header
211 headers = mock_post.call_args[1]["headers"]
212 assert headers["X-API-Key"] == "ft_key_xxxx"
213 assert headers["Content-Type"] == "application/json"
214
215 mock_post.reset_mock()
216
217 # ------------------------------------------------------------------
218 # 201, 202, 204 are also accepted
219 # ------------------------------------------------------------------
220 for code in (
221 requests.codes.created,
222 requests.codes.accepted,
223 requests.codes.no_content,
224 ):
225 mock_post.return_value = _mk_resp(code)
226 assert obj.send(body="body") is True
227
228 mock_post.reset_mock()
229
230 # ------------------------------------------------------------------
231 # Severity mapping for all notify types
232 # ------------------------------------------------------------------
233 severity_cases = [
234 (NotifyType.INFO, "info"),
235 (NotifyType.SUCCESS, "success"),
236 (NotifyType.WARNING, "warning"),
237 (NotifyType.FAILURE, "critical"),
238 ]
239 mock_post.return_value = _mk_resp()
240 for notify_type, expected_severity in severity_cases:
241 obj.send(body="body", notify_type=notify_type)
242 payload = json.loads(mock_post.call_args[1]["data"])
243 assert payload["severity"] == expected_severity
244
245 mock_post.reset_mock()
246
247 # ------------------------------------------------------------------
248 # Port is included in the request URL when set
249 # ------------------------------------------------------------------
250 obj_port = NotifyFlowtriq(
251 apikey="key",
252 webhook_path="hooks/test",
253 host="myhost.example.com",
254 port=8443,
255 )
256 mock_post.return_value = _mk_resp()
257 assert obj_port.send(body="body") is True
258 call_url = mock_post.call_args[0][0]
259 parsed = urlparse(call_url)
260 assert parsed.hostname == "myhost.example.com"
261 assert parsed.port == 8443
262
263 mock_post.reset_mock()
264
265 # ------------------------------------------------------------------
266 # HTTP error response returns False
267 # ------------------------------------------------------------------
268 mock_post.return_value = _mk_resp(requests.codes.internal_server_error)
269 assert obj.send(body="body") is False
270
271 mock_post.reset_mock()
272
273 # ------------------------------------------------------------------
274 # Unknown HTTP error code (no entry in lookup table) returns False
275 # ------------------------------------------------------------------
276 mock_post.return_value = _mk_resp(999)
277 assert obj.send(body="body") is False
278
279 mock_post.reset_mock()
280
281 # ------------------------------------------------------------------
282 # requests.RequestException returns False
283 # ------------------------------------------------------------------
284 mock_post.side_effect = requests.RequestException("boom")
285 assert obj.send(body="body") is False
286
287
288 @mock.patch("requests.post")
289 def test_plugin_flowtriq_url_parsing(mock_post):
290 """NotifyFlowtriq() URL round-trip and parse_url edge cases."""
291
292 # Round-trip: parse_url(url()) must recover the same object
293 obj = NotifyFlowtriq(
294 apikey="mykey",
295 webhook_path="hooks/abc123",
296 host="flowtriq.com",
297 )
298 result = NotifyFlowtriq.parse_url(obj.url())
299 assert result is not None
300 obj2 = NotifyFlowtriq(**result)
301 assert obj.url_identifier == obj2.url_identifier
302
303 # Round-trip with non-default port
304 obj_port = NotifyFlowtriq(
305 apikey="mykey",
306 webhook_path="api/v1/wh",
307 host="myhost",
308 port=9000,
309 )
310 result = NotifyFlowtriq.parse_url(obj_port.url())
311 assert result is not None
312 obj_port2 = NotifyFlowtriq(**result)
313 assert obj_port.url_identifier == obj_port2.url_identifier
314
315 # url_identifier via URL parse: flowtriq:// is insecure → port fallback 80
316 obj_insecure = NotifyFlowtriq.parse_url("flowtriq://key@h/hooks/x")
317 assert obj_insecure is not None
318 uid_ins = NotifyFlowtriq(**obj_insecure).url_identifier
319 assert uid_ins[0] == "flowtriq"
320 assert uid_ins[1] == "h"
321 assert uid_ins[2] == 80
322
323 # flowtriqs:// is secure → port fallback 443
324 obj_secure = NotifyFlowtriq.parse_url("flowtriqs://key@h/hooks/x")
325 assert obj_secure is not None
326 uid_sec = NotifyFlowtriq(**obj_secure).url_identifier
327 assert uid_sec[0] == "flowtriqs"
328 assert uid_sec[1] == "h"
329 assert uid_sec[2] == 443
330
331 # parse_url with no user field → apikey is None
332 result = NotifyFlowtriq.parse_url("flowtriq://hostname/hooks/abc123")
333 assert result is not None
334 assert result["apikey"] is None
335
336 # parse_url with empty path → webhook_path is None
337 result = NotifyFlowtriq.parse_url("flowtriq://key@hostname")
338 assert result is not None
339 assert not result.get("webhook_path")
340
341 # parse_url returns None for an unparseable URL
342 result = NotifyFlowtriq.parse_url("flowtriq://:@/")
343 assert result is None
344
345 # privacy_url hides the API key
346 obj = NotifyFlowtriq(
347 apikey="myapikey",
348 webhook_path="hooks/abc123",
349 host="hostname",
350 )
351 priv = obj.url(privacy=True)
352 assert "myapikey" not in priv
353 assert "m...y" in priv
354
355
356 @mock.patch("requests.post")
357 def test_plugin_flowtriq_apprise_integration(mock_post):
358 """NotifyFlowtriq() Apprise integration."""
359
360 mock_post.return_value = mock.Mock()
361 mock_post.return_value.status_code = requests.codes.ok
362 mock_post.return_value.content = b"{}"
363
364 app = Apprise()
365 assert app.add("flowtriq://[email protected]/hooks/abc123") is True
366 assert bool(app.notify(title="Title", body="Body")) is True
367 assert mock_post.call_count == 1