Debian Codegraph @

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