| 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 |
import json |
| 29 |
|
| 30 |
|
| 31 |
import logging |
| 32 |
import os |
| 33 |
from unittest import mock |
| 34 |
|
| 35 |
from helpers import AppriseURLTester |
| 36 |
import requests |
| 37 |
|
| 38 |
from apprise import Apprise, AppriseAttachment, NotifyType |
| 39 |
from apprise.plugins.custom_json import NotifyJSON |
| 40 |
|
| 41 |
logging.disable(logging.CRITICAL) |
| 42 |
|
| 43 |
|
| 44 |
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var") |
| 45 |
|
| 46 |
|
| 47 |
apprise_url_tests = ( |
| 48 |
( |
| 49 |
"json://:@/", |
| 50 |
{ |
| 51 |
"instance": None, |
| 52 |
}, |
| 53 |
), |
| 54 |
( |
| 55 |
"json://", |
| 56 |
{ |
| 57 |
"instance": None, |
| 58 |
}, |
| 59 |
), |
| 60 |
( |
| 61 |
"jsons://", |
| 62 |
{ |
| 63 |
"instance": None, |
| 64 |
}, |
| 65 |
), |
| 66 |
( |
| 67 |
"json://localhost", |
| 68 |
{ |
| 69 |
"instance": NotifyJSON, |
| 70 |
}, |
| 71 |
), |
| 72 |
( |
| 73 |
"json://user@localhost?method=invalid", |
| 74 |
{ |
| 75 |
"instance": TypeError, |
| 76 |
}, |
| 77 |
), |
| 78 |
( |
| 79 |
"json://user:pass@localhost", |
| 80 |
{ |
| 81 |
"instance": NotifyJSON, |
| 82 |
|
| 83 |
"privacy_url": "json://user:****@localhost", |
| 84 |
}, |
| 85 |
), |
| 86 |
( |
| 87 |
"json://user@localhost", |
| 88 |
{ |
| 89 |
"instance": NotifyJSON, |
| 90 |
}, |
| 91 |
), |
| 92 |
|
| 93 |
( |
| 94 |
"json://user@localhost?method=put", |
| 95 |
{ |
| 96 |
"instance": NotifyJSON, |
| 97 |
}, |
| 98 |
), |
| 99 |
( |
| 100 |
"json://user@localhost?method=get", |
| 101 |
{ |
| 102 |
"instance": NotifyJSON, |
| 103 |
}, |
| 104 |
), |
| 105 |
( |
| 106 |
"json://user@localhost?method=post", |
| 107 |
{ |
| 108 |
"instance": NotifyJSON, |
| 109 |
"force_debug": True, |
| 110 |
}, |
| 111 |
), |
| 112 |
( |
| 113 |
"json://user@localhost?method=head", |
| 114 |
{ |
| 115 |
"instance": NotifyJSON, |
| 116 |
}, |
| 117 |
), |
| 118 |
( |
| 119 |
"json://user@localhost?method=delete", |
| 120 |
{ |
| 121 |
"instance": NotifyJSON, |
| 122 |
}, |
| 123 |
), |
| 124 |
( |
| 125 |
"json://user@localhost?method=patch", |
| 126 |
{ |
| 127 |
"instance": NotifyJSON, |
| 128 |
}, |
| 129 |
), |
| 130 |
( |
| 131 |
"json://user@localhost?method=update", |
| 132 |
{ |
| 133 |
"instance": NotifyJSON, |
| 134 |
}, |
| 135 |
), |
| 136 |
( |
| 137 |
"json://user@localhost?method=options", |
| 138 |
{ |
| 139 |
"instance": NotifyJSON, |
| 140 |
}, |
| 141 |
), |
| 142 |
|
| 143 |
( |
| 144 |
"json://localhost:8080", |
| 145 |
{ |
| 146 |
"instance": NotifyJSON, |
| 147 |
}, |
| 148 |
), |
| 149 |
( |
| 150 |
"json://user:pass@localhost:8080", |
| 151 |
{ |
| 152 |
"instance": NotifyJSON, |
| 153 |
}, |
| 154 |
), |
| 155 |
( |
| 156 |
"jsons://localhost", |
| 157 |
{ |
| 158 |
"instance": NotifyJSON, |
| 159 |
}, |
| 160 |
), |
| 161 |
( |
| 162 |
"jsons://user:pass@localhost", |
| 163 |
{ |
| 164 |
"instance": NotifyJSON, |
| 165 |
}, |
| 166 |
), |
| 167 |
( |
| 168 |
"jsons://localhost:8080/path/", |
| 169 |
{ |
| 170 |
"instance": NotifyJSON, |
| 171 |
|
| 172 |
"privacy_url": "jsons://localhost:8080/path/", |
| 173 |
}, |
| 174 |
), |
| 175 |
|
| 176 |
( |
| 177 |
"json://localhost:8080/path?-ParamA=Value", |
| 178 |
{ |
| 179 |
"instance": NotifyJSON, |
| 180 |
}, |
| 181 |
), |
| 182 |
( |
| 183 |
"jsons://user:password@localhost:8080", |
| 184 |
{ |
| 185 |
"instance": NotifyJSON, |
| 186 |
|
| 187 |
"privacy_url": "jsons://user:****@localhost:8080", |
| 188 |
}, |
| 189 |
), |
| 190 |
|
| 191 |
( |
| 192 |
"json://localhost:8080/path?+HeaderKey=HeaderValue", |
| 193 |
{ |
| 194 |
"instance": NotifyJSON, |
| 195 |
}, |
| 196 |
), |
| 197 |
( |
| 198 |
"json://user:pass@localhost:8081", |
| 199 |
{ |
| 200 |
"instance": NotifyJSON, |
| 201 |
|
| 202 |
"response": False, |
| 203 |
"requests_response_code": requests.codes.internal_server_error, |
| 204 |
}, |
| 205 |
), |
| 206 |
( |
| 207 |
"json://user:pass@localhost:8082", |
| 208 |
{ |
| 209 |
"instance": NotifyJSON, |
| 210 |
|
| 211 |
"response": False, |
| 212 |
"requests_response_code": 999, |
| 213 |
}, |
| 214 |
), |
| 215 |
( |
| 216 |
"json://user:pass@localhost:8083", |
| 217 |
{ |
| 218 |
"instance": NotifyJSON, |
| 219 |
|
| 220 |
|
| 221 |
"test_requests_exceptions": True, |
| 222 |
}, |
| 223 |
), |
| 224 |
) |
| 225 |
|
| 226 |
|
| 227 |
def test_plugin_custom_json_urls(): |
| 228 |
"""NotifyJSON() Apprise URLs.""" |
| 229 |
|
| 230 |
|
| 231 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 232 |
|
| 233 |
|
| 234 |
@mock.patch("requests.request") |
| 235 |
def test_plugin_custom_json_edge_cases(mock_request): |
| 236 |
"""NotifyJSON() Edge Cases.""" |
| 237 |
|
| 238 |
|
| 239 |
response = requests.Request() |
| 240 |
response.status_code = requests.codes.ok |
| 241 |
|
| 242 |
|
| 243 |
mock_request.return_value = response |
| 244 |
|
| 245 |
|
| 246 |
results = NotifyJSON.parse_url( |
| 247 |
"json://localhost:8080/command?" |
| 248 |
":message=msg&:test=value&method=GET" |
| 249 |
"&:type=" |
| 250 |
) |
| 251 |
|
| 252 |
assert isinstance(results, dict) |
| 253 |
assert results["user"] is None |
| 254 |
assert results["password"] is None |
| 255 |
assert results["port"] == 8080 |
| 256 |
assert results["host"] == "localhost" |
| 257 |
assert results["fullpath"] == "/command" |
| 258 |
assert results["path"] == "/" |
| 259 |
assert results["query"] == "command" |
| 260 |
assert results["schema"] == "json" |
| 261 |
assert results["url"] == "json://localhost:8080/command" |
| 262 |
assert isinstance(results["qsd:"], dict) |
| 263 |
assert results["qsd:"]["message"] == "msg" |
| 264 |
|
| 265 |
assert results["qsd:"]["type"] == "" |
| 266 |
|
| 267 |
instance = NotifyJSON(**results) |
| 268 |
assert isinstance(instance, NotifyJSON) |
| 269 |
|
| 270 |
response = instance.send(title="title", body="body") |
| 271 |
assert response is True |
| 272 |
assert mock_request.call_count == 1 |
| 273 |
|
| 274 |
details = mock_request.call_args_list[0] |
| 275 |
assert details[0][0] == "GET" |
| 276 |
assert details[0][1] == "http://localhost:8080/command" |
| 277 |
assert "title" in details[1]["data"] |
| 278 |
dataset = json.loads(details[1]["data"]) |
| 279 |
assert dataset["title"] == "title" |
| 280 |
assert "message" not in dataset |
| 281 |
|
| 282 |
assert "NotifyType." not in details[1]["data"] |
| 283 |
assert "msg" in dataset |
| 284 |
|
| 285 |
assert "type" not in dataset |
| 286 |
|
| 287 |
|
| 288 |
assert dataset["msg"] == "body" |
| 289 |
|
| 290 |
assert "test" in dataset |
| 291 |
assert dataset["test"] == "value" |
| 292 |
|
| 293 |
assert instance.url(privacy=False).startswith( |
| 294 |
"json://localhost:8080/command?" |
| 295 |
) |
| 296 |
|
| 297 |
|
| 298 |
new_results = NotifyJSON.parse_url(instance.url(safe=False)) |
| 299 |
for k in ( |
| 300 |
"user", |
| 301 |
"password", |
| 302 |
"port", |
| 303 |
"host", |
| 304 |
"fullpath", |
| 305 |
"path", |
| 306 |
"query", |
| 307 |
"schema", |
| 308 |
"url", |
| 309 |
"method", |
| 310 |
): |
| 311 |
assert new_results[k] == results[k] |
| 312 |
|
| 313 |
mock_request.reset_mock() |
| 314 |
|
| 315 |
results = NotifyJSON.parse_url( |
| 316 |
"json://localhost:8080/command?:message=msg&method=GET" |
| 317 |
) |
| 318 |
instance = NotifyJSON(**results) |
| 319 |
assert instance.send(title="title", body="body") is True |
| 320 |
details = mock_request.call_args_list[0] |
| 321 |
assert details[0][0] == "GET" |
| 322 |
dataset = json.loads(details[1]["data"]) |
| 323 |
|
| 324 |
assert dataset["type"] == NotifyType.INFO.value |
| 325 |
assert "NotifyType." not in details[1]["data"] |
| 326 |
|
| 327 |
mock_request.reset_mock() |
| 328 |
|
| 329 |
|
| 330 |
results = NotifyJSON.parse_url("json://localhost?method=UPDATE") |
| 331 |
instance = NotifyJSON(**results) |
| 332 |
assert instance.send(title="title", body="body") is True |
| 333 |
assert mock_request.call_count == 1 |
| 334 |
details = mock_request.call_args_list[0] |
| 335 |
assert details[0][0] == "UPDATE" |
| 336 |
assert details[0][1] == "http://localhost" |
| 337 |
|
| 338 |
mock_request.reset_mock() |
| 339 |
|
| 340 |
|
| 341 |
results = NotifyJSON.parse_url("json://localhost?method=OPTIONS") |
| 342 |
instance = NotifyJSON(**results) |
| 343 |
assert instance.send(title="title", body="body") is True |
| 344 |
assert mock_request.call_count == 1 |
| 345 |
details = mock_request.call_args_list[0] |
| 346 |
assert details[0][0] == "OPTIONS" |
| 347 |
assert details[0][1] == "http://localhost" |
| 348 |
|
| 349 |
|
| 350 |
@mock.patch("requests.request") |
| 351 |
def test_notify_json_plugin_attachments(mock_request): |
| 352 |
"""NotifyJSON() Attachments.""" |
| 353 |
|
| 354 |
okay_response = requests.Request() |
| 355 |
okay_response.status_code = requests.codes.ok |
| 356 |
okay_response.content = "" |
| 357 |
|
| 358 |
|
| 359 |
mock_request.return_value = okay_response |
| 360 |
|
| 361 |
obj = Apprise.instantiate("json://localhost.localdomain/") |
| 362 |
assert isinstance(obj, NotifyJSON) |
| 363 |
|
| 364 |
|
| 365 |
path = os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 366 |
attach = AppriseAttachment(path) |
| 367 |
assert ( |
| 368 |
obj.notify( |
| 369 |
body="body", |
| 370 |
title="title", |
| 371 |
notify_type=NotifyType.INFO, |
| 372 |
attach=attach, |
| 373 |
) |
| 374 |
is True |
| 375 |
) |
| 376 |
|
| 377 |
|
| 378 |
path = os.path.join(TEST_VAR_DIR, "/invalid/path/to/an/invalid/file.jpg") |
| 379 |
assert ( |
| 380 |
obj.notify( |
| 381 |
body="body", |
| 382 |
title="title", |
| 383 |
notify_type=NotifyType.INFO, |
| 384 |
attach=path, |
| 385 |
) |
| 386 |
is False |
| 387 |
) |
| 388 |
|
| 389 |
|
| 390 |
path = ( |
| 391 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif"), |
| 392 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif"), |
| 393 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif"), |
| 394 |
) |
| 395 |
attach = AppriseAttachment(path) |
| 396 |
|
| 397 |
|
| 398 |
mock_request.side_effect = None |
| 399 |
mock_request.return_value = okay_response |
| 400 |
with mock.patch("builtins.open", side_effect=OSError()): |
| 401 |
|
| 402 |
assert ( |
| 403 |
obj.notify( |
| 404 |
body="body", |
| 405 |
title="title", |
| 406 |
notify_type=NotifyType.INFO, |
| 407 |
attach=attach, |
| 408 |
) |
| 409 |
is False |
| 410 |
) |
| 411 |
|
| 412 |
|
| 413 |
obj = Apprise.instantiate("json://[email protected]/") |
| 414 |
assert isinstance(obj, NotifyJSON) |
| 415 |
|
| 416 |
|
| 417 |
mock_request.reset_mock() |
| 418 |
assert ( |
| 419 |
obj.notify( |
| 420 |
body="body", |
| 421 |
title="title", |
| 422 |
notify_type=NotifyType.INFO, |
| 423 |
attach=attach, |
| 424 |
) |
| 425 |
is True |
| 426 |
) |
| 427 |
assert mock_request.call_count == 1 |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
@mock.patch("requests.request") |
| 433 |
def test_plugin_custom_form_for_synology(mock_request): |
| 434 |
"""NotifyJSON() Synology Chat Test Case.""" |
| 435 |
|
| 436 |
|
| 437 |
response = requests.Request() |
| 438 |
response.status_code = requests.codes.ok |
| 439 |
|
| 440 |
|
| 441 |
mock_request.return_value = response |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
results = NotifyJSON.parse_url( |
| 448 |
"jsons://localhost:8081/webapi/entry.cgi?" |
| 449 |
"-api=SYNO.Chat.External&-method=incoming&-version=2&-token=abc123" |
| 450 |
"&:message=text&:version=&:type=&:title=&:attachments" |
| 451 |
"&:file_url=https://i.redd.it/my2t4d2fx0u31.jpg" |
| 452 |
) |
| 453 |
|
| 454 |
assert isinstance(results, dict) |
| 455 |
assert results["user"] is None |
| 456 |
assert results["password"] is None |
| 457 |
assert results["port"] == 8081 |
| 458 |
assert results["host"] == "localhost" |
| 459 |
assert results["fullpath"] == "/webapi/entry.cgi" |
| 460 |
assert results["path"] == "/webapi/" |
| 461 |
assert results["query"] == "entry.cgi" |
| 462 |
assert results["schema"] == "jsons" |
| 463 |
assert results["url"] == "jsons://localhost:8081/webapi/entry.cgi" |
| 464 |
assert isinstance(results["qsd:"], dict) |
| 465 |
|
| 466 |
assert results["qsd-"]["api"] == "SYNO.Chat.External" |
| 467 |
assert results["qsd-"]["method"] == "incoming" |
| 468 |
assert results["qsd-"]["version"] == "2" |
| 469 |
assert results["qsd-"]["token"] == "abc123" |
| 470 |
|
| 471 |
instance = NotifyJSON(**results) |
| 472 |
assert isinstance(instance, NotifyJSON) |
| 473 |
|
| 474 |
response = instance.send(title="title", body="body") |
| 475 |
assert response is True |
| 476 |
assert mock_request.call_count == 1 |
| 477 |
|
| 478 |
details = mock_request.call_args_list[0] |
| 479 |
assert details[0][0] == "POST" |
| 480 |
assert details[0][1] == "https://localhost:8081/webapi/entry.cgi" |
| 481 |
|
| 482 |
params = details[1]["params"] |
| 483 |
assert params.get("api") == "SYNO.Chat.External" |
| 484 |
assert params.get("method") == "incoming" |
| 485 |
assert params.get("version") == "2" |
| 486 |
assert params.get("token") == "abc123" |
| 487 |
|
| 488 |
payload = json.loads(details[1]["data"]) |
| 489 |
assert "version" not in payload |
| 490 |
assert "title" not in payload |
| 491 |
assert "message" not in payload |
| 492 |
assert "type" not in payload |
| 493 |
assert payload.get("text") == "body" |
| 494 |
assert payload.get("file_url") == "https://i.redd.it/my2t4d2fx0u31.jpg" |