| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 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 |
|
| 44 |
apprise_url_tests = ( |
| 45 |
( |
| 46 |
"flowtriq://", |
| 47 |
{ |
| 48 |
"instance": None, |
| 49 |
}, |
| 50 |
), |
| 51 |
|
| 52 |
( |
| 53 |
"flowtriq://apikey@hostname", |
| 54 |
{ |
| 55 |
"instance": TypeError, |
| 56 |
}, |
| 57 |
), |
| 58 |
|
| 59 |
( |
| 60 |
"flowtriq://hostname/hooks/abc123", |
| 61 |
{ |
| 62 |
"instance": TypeError, |
| 63 |
}, |
| 64 |
), |
| 65 |
|
| 66 |
( |
| 67 |
"flowtriq://myapikey@hostname/hooks/abc123/", |
| 68 |
{ |
| 69 |
"instance": NotifyFlowtriq, |
| 70 |
|
| 71 |
"privacy_url": "flowtriq://m...y@hostname/hooks/abc123/", |
| 72 |
}, |
| 73 |
), |
| 74 |
|
| 75 |
( |
| 76 |
"flowtriqs://myapikey@hostname/hooks/abc123/", |
| 77 |
{ |
| 78 |
"instance": NotifyFlowtriq, |
| 79 |
"privacy_url": "flowtriqs://m...y@hostname/hooks/abc123/", |
| 80 |
}, |
| 81 |
), |
| 82 |
|
| 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 |
|
| 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 |
|
| 99 |
( |
| 100 |
"flowtriqs://[email protected]/api/v1/webhook/xyz/", |
| 101 |
{ |
| 102 |
"instance": NotifyFlowtriq, |
| 103 |
}, |
| 104 |
), |
| 105 |
|
| 106 |
( |
| 107 |
"flowtriq://:@/", |
| 108 |
{ |
| 109 |
"instance": None, |
| 110 |
}, |
| 111 |
), |
| 112 |
|
| 113 |
( |
| 114 |
"flowtriq://myapikey@hostname/hooks/abc123/", |
| 115 |
{ |
| 116 |
"instance": NotifyFlowtriq, |
| 117 |
|
| 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 |
|
| 127 |
"response": False, |
| 128 |
"requests_response_code": 999, |
| 129 |
}, |
| 130 |
), |
| 131 |
( |
| 132 |
"flowtriq://myapikey@hostname/hooks/abc123/", |
| 133 |
{ |
| 134 |
"instance": NotifyFlowtriq, |
| 135 |
|
| 136 |
|
| 137 |
"test_requests_exceptions": True, |
| 138 |
}, |
| 139 |
), |
| 140 |
) |
| 141 |
|
| 142 |
|
| 143 |
def test_plugin_flowtriq_urls(): |
| 144 |
"""NotifyFlowtriq() Apprise URLs.""" |
| 145 |
|
| 146 |
|
| 147 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 148 |
|
| 149 |
|
| 150 |
def test_plugin_flowtriq_edge_cases(): |
| 151 |
"""NotifyFlowtriq() Edge Cases.""" |
| 152 |
|
| 153 |
with pytest.raises(TypeError): |
| 154 |
NotifyFlowtriq(apikey=None, webhook_path="hooks/abc123") |
| 155 |
|
| 156 |
with pytest.raises(TypeError): |
| 157 |
NotifyFlowtriq(apikey=" ", webhook_path="hooks/abc123") |
| 158 |
|
| 159 |
|
| 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 |
|
| 166 |
with pytest.raises(TypeError): |
| 167 |
NotifyFlowtriq(apikey="validkey", webhook_path="///") |
| 168 |
|
| 169 |
|
| 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 |
00 |
| 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 |
|
| 199 |
call_url = mock_post.call_args[0][0] |
| 200 |
assert urlparse(call_url).hostname == "flowtriq.com" |
| 201 |
|
| 202 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
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 |
43 |
| 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 |
ne |
| 331 |
result = NotifyFlowtriq.parse_url("flowtriq://hostname/hooks/abc123") |
| 332 |
assert result is not None |
| 333 |
assert result["apikey"] is None |
| 334 |
|
| 335 |
ne |
| 336 |
result = NotifyFlowtriq.parse_url("flowtriq://key@hostname") |
| 337 |
assert result is not None |
| 338 |
assert not result.get("webhook_path") |
| 339 |
|
| 340 |
|
| 341 |
result = NotifyFlowtriq.parse_url("flowtriq://:@/") |
| 342 |
assert result is None |
| 343 |
|
| 344 |
|
| 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 |