| 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 |
from json import loads |
| 30 |
import logging |
| 31 |
import os |
| 32 |
from unittest import mock |
| 33 |
from urllib.parse import urlparse |
| 34 |
|
| 35 |
from helpers import AppriseURLTester |
| 36 |
import pytest |
| 37 |
import requests |
| 38 |
|
| 39 |
from apprise import Apprise, AppriseAttachment, NotifyType |
| 40 |
from apprise.plugins.eight00com import NotifyEight00com |
| 41 |
|
| 42 |
logging.disable(logging.CRITICAL) |
| 43 |
|
| 44 |
|
| 45 |
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var") |
| 46 |
|
| 47 |
|
| 48 |
apprise_url_tests = ( |
| 49 |
( |
| 50 |
"eight00com://", |
| 51 |
{ |
| 52 |
|
| 53 |
"instance": TypeError, |
| 54 |
}, |
| 55 |
), |
| 56 |
( |
| 57 |
"eight00com://:@/", |
| 58 |
{ |
| 59 |
|
| 60 |
"instance": TypeError, |
| 61 |
}, |
| 62 |
), |
| 63 |
( |
| 64 |
"eight00com://GOODTOKEN@badphone", |
| 65 |
{ |
| 66 |
|
| 67 |
"instance": TypeError, |
| 68 |
}, |
| 69 |
), |
| 70 |
( |
| 71 |
"eight00com://GOODTOKEN@9876543210/12", |
| 72 |
{ |
| 73 |
|
| 74 |
|
| 75 |
"instance": NotifyEight00com, |
| 76 |
"notify_response": False, |
| 77 |
}, |
| 78 |
), |
| 79 |
( |
| 80 |
"eight00com://{}@{}".format("t" * 10, "8" * 10), |
| 81 |
{ |
| 82 |
|
| 83 |
"instance": NotifyEight00com, |
| 84 |
}, |
| 85 |
), |
| 86 |
( |
| 87 |
"eight00com://{}@{}/{}".format("t" * 10, "8" * 10, "5" * 11), |
| 88 |
{ |
| 89 |
|
| 90 |
"instance": NotifyEight00com, |
| 91 |
"privacy_url": ( |
| 92 |
"eight00com://t...t@{}/{}".format("8" * 10, "5" * 11) |
| 93 |
), |
| 94 |
}, |
| 95 |
), |
| 96 |
( |
| 97 |
"eight00com://{}@{}/{}/{}".format( |
| 98 |
"t" * 10, "8" * 10, "5" * 11, "6" * 11 |
| 99 |
), |
| 100 |
{ |
| 101 |
|
| 102 |
"instance": NotifyEight00com, |
| 103 |
}, |
| 104 |
), |
| 105 |
( |
| 106 |
"eight00com://?token={}&from={}".format("t" * 10, "8" * 10), |
| 107 |
{ |
| 108 |
|
| 109 |
"instance": NotifyEight00com, |
| 110 |
}, |
| 111 |
), |
| 112 |
( |
| 113 |
"eight00com://?token={}&from={}&to={}".format( |
| 114 |
"t" * 10, "8" * 10, "5" * 11 |
| 115 |
), |
| 116 |
{ |
| 117 |
|
| 118 |
"instance": NotifyEight00com, |
| 119 |
}, |
| 120 |
), |
| 121 |
( |
| 122 |
"eight00com://{}@{}".format("t" * 10, "8" * 10), |
| 123 |
{ |
| 124 |
"instance": NotifyEight00com, |
| 125 |
|
| 126 |
"response": False, |
| 127 |
"requests_response_code": requests.codes.internal_server_error, |
| 128 |
}, |
| 129 |
), |
| 130 |
( |
| 131 |
"eight00com://{}@{}".format("t" * 10, "8" * 10), |
| 132 |
{ |
| 133 |
"instance": NotifyEight00com, |
| 134 |
|
| 135 |
"response": False, |
| 136 |
"requests_response_code": 999, |
| 137 |
}, |
| 138 |
), |
| 139 |
( |
| 140 |
"eight00com://{}@{}".format("t" * 10, "8" * 10), |
| 141 |
{ |
| 142 |
"instance": NotifyEight00com, |
| 143 |
|
| 144 |
"test_requests_exceptions": True, |
| 145 |
}, |
| 146 |
), |
| 147 |
) |
| 148 |
|
| 149 |
|
| 150 |
def test_plugin_eight00com_urls(): |
| 151 |
"""NotifyEight00com() Apprise URLs.""" |
| 152 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 153 |
|
| 154 |
|
| 155 |
@mock.patch("requests.post") |
| 156 |
def test_plugin_eight00com_init(mock_post): |
| 157 |
"""NotifyEight00com() initialization and edge cases.""" |
| 158 |
|
| 159 |
|
| 160 |
response = requests.Request() |
| 161 |
response.status_code = requests.codes.ok |
| 162 |
mock_post.return_value = response |
| 163 |
|
| 164 |
|
| 165 |
with pytest.raises(TypeError): |
| 166 |
NotifyEight00com(token=None, source="8005551234") |
| 167 |
|
| 168 |
|
| 169 |
with pytest.raises(TypeError): |
| 170 |
NotifyEight00com(token="", source="8005551234") |
| 171 |
|
| 172 |
|
| 173 |
with pytest.raises(TypeError): |
| 174 |
NotifyEight00com(token="mytoken", source="notaphone") |
| 175 |
|
| 176 |
|
| 177 |
obj = NotifyEight00com(token="mytoken", source="8005551234") |
| 178 |
assert obj.targets == ["8005551234"] |
| 179 |
assert len(obj) == 1 |
| 180 |
|
| 181 |
|
| 182 |
obj = NotifyEight00com( |
| 183 |
token="mytoken", |
| 184 |
source="8005551234", |
| 185 |
targets=["12"], |
| 186 |
) |
| 187 |
assert obj.targets == [] |
| 188 |
|
| 189 |
assert len(obj) == 1 |
| 190 |
|
| 191 |
|
| 192 |
obj = NotifyEight00com( |
| 193 |
token="mytoken", |
| 194 |
source="8005551234", |
| 195 |
targets=["5551234567", "6661234567"], |
| 196 |
) |
| 197 |
assert len(obj.targets) == 2 |
| 198 |
assert len(obj) == 2 |
| 199 |
|
| 200 |
|
| 201 |
@mock.patch("requests.post") |
| 202 |
def test_plugin_eight00com_sms(mock_post): |
| 203 |
"""NotifyEight00com() SMS send path.""" |
| 204 |
|
| 205 |
response = requests.Request() |
| 206 |
response.status_code = requests.codes.ok |
| 207 |
response.content = b"{}" |
| 208 |
mock_post.return_value = response |
| 209 |
|
| 210 |
|
| 211 |
obj = NotifyEight00com( |
| 212 |
token="mytoken", |
| 213 |
source="8005551234", |
| 214 |
targets=["5559876543"], |
| 215 |
) |
| 216 |
assert obj.notify(body="Test", title="Title") is True |
| 217 |
assert mock_post.call_count == 1 |
| 218 |
|
| 219 |
|
| 220 |
payload = loads(mock_post.call_args[1]["data"]) |
| 221 |
assert payload["sender"] == "+8005551234" |
| 222 |
assert payload["recipient"] == "+5559876543" |
| 223 |
assert payload["message"] == "Title\r\nTest" |
| 224 |
|
| 225 |
|
| 226 |
headers = mock_post.call_args[1]["headers"] |
| 227 |
assert headers["Authorization"] == "Bearer mytoken" |
| 228 |
|
| 229 |
mock_post.reset_mock() |
| 230 |
|
| 231 |
|
| 232 |
obj = NotifyEight00com( |
| 233 |
token="mytoken", |
| 234 |
source="8005551234", |
| 235 |
targets=["5559876543", "4445551234"], |
| 236 |
) |
| 237 |
assert obj.notify(body="Hello") is True |
| 238 |
assert mock_post.call_count == 2 |
| 239 |
|
| 240 |
mock_post.reset_mock() |
| 241 |
|
| 242 |
|
| 243 |
response.status_code = requests.codes.internal_server_error |
| 244 |
obj = NotifyEight00com( |
| 245 |
token="mytoken", |
| 246 |
source="8005551234", |
| 247 |
targets=["5559876543"], |
| 248 |
) |
| 249 |
assert obj.notify(body="Test") is False |
| 250 |
|
| 251 |
mock_post.reset_mock() |
| 252 |
|
| 253 |
|
| 254 |
response.status_code = 999 |
| 255 |
assert obj.notify(body="Test") is False |
| 256 |
|
| 257 |
mock_post.reset_mock() |
| 258 |
|
| 259 |
|
| 260 |
mock_post.side_effect = requests.RequestException("Connection error") |
| 261 |
response.status_code = requests.codes.ok |
| 262 |
assert obj.notify(body="Test") is False |
| 263 |
|
| 264 |
mock_post.side_effect = None |
| 265 |
|
| 266 |
|
| 267 |
obj = NotifyEight00com( |
| 268 |
token="mytoken", |
| 269 |
source="8005551234", |
| 270 |
targets=["12"], |
| 271 |
) |
| 272 |
mock_post.reset_mock() |
| 273 |
assert obj.notify(body="Test") is False |
| 274 |
assert mock_post.call_count == 0 |
| 275 |
|
| 276 |
|
| 277 |
@mock.patch("requests.post") |
| 278 |
def test_plugin_eight00com_url_round_trip(mock_post): |
| 279 |
"""NotifyEight00com() URL round-trip and privacy_url.""" |
| 280 |
|
| 281 |
response = requests.Request() |
| 282 |
response.status_code = requests.codes.ok |
| 283 |
mock_post.return_value = response |
| 284 |
|
| 285 |
|
| 286 |
obj1 = NotifyEight00com( |
| 287 |
token="secrettoken", |
| 288 |
source="8005551234", |
| 289 |
targets=["5559876543"], |
| 290 |
) |
| 291 |
result = NotifyEight00com.parse_url(obj1.url()) |
| 292 |
obj2 = NotifyEight00com(**result) |
| 293 |
assert obj1.url_identifier == obj2.url_identifier |
| 294 |
assert len(obj1.targets) == len(obj2.targets) |
| 295 |
|
| 296 |
|
| 297 |
privacy = obj1.url(privacy=True) |
| 298 |
assert "secrettoken" not in privacy |
| 299 |
assert "s...n" in privacy |
| 300 |
|
| 301 |
|
| 302 |
obj3 = NotifyEight00com(token="mytoken", source="8005551234") |
| 303 |
result = NotifyEight00com.parse_url(obj3.url()) |
| 304 |
obj4 = NotifyEight00com(**result) |
| 305 |
assert obj3.url_identifier == obj4.url_identifier |
| 306 |
|
| 307 |
assert obj3.targets == obj4.targets |
| 308 |
|
| 309 |
|
| 310 |
assert obj1.url_identifier == ( |
| 311 |
"eight00com", |
| 312 |
"8005551234", |
| 313 |
"secrettoken", |
| 314 |
) |
| 315 |
|
| 316 |
|
| 317 |
@mock.patch("requests.post") |
| 318 |
def test_plugin_eight00com_parse_url(mock_post): |
| 319 |
"""NotifyEight00com() parse_url edge cases.""" |
| 320 |
|
| 321 |
response = requests.Request() |
| 322 |
response.status_code = requests.codes.ok |
| 323 |
mock_post.return_value = response |
| 324 |
|
| 325 |
|
| 326 |
result = NotifyEight00com.parse_url( |
| 327 |
"eight00com://mytoken@8005551234/5559876543" |
| 328 |
) |
| 329 |
assert result["token"] == "mytoken" |
| 330 |
assert result["source"] == "8005551234" |
| 331 |
assert "5559876543" in result["targets"] |
| 332 |
|
| 333 |
|
| 334 |
result = NotifyEight00com.parse_url( |
| 335 |
"eight00com://8005551234?token=newtoken&from=8009876543&to=5551234567" |
| 336 |
) |
| 337 |
assert result["token"] == "newtoken" |
| 338 |
assert result["source"] == "8009876543" |
| 339 |
|
| 340 |
|
| 341 |
result = NotifyEight00com.parse_url( |
| 342 |
"eight00com://mytoken@9998887776?from=8005551234" |
| 343 |
) |
| 344 |
assert result["token"] == "mytoken" |
| 345 |
assert result["source"] == "8005551234" |
| 346 |
|
| 347 |
assert "9998887776" in " ".join(result["targets"]) |
| 348 |
|
| 349 |
|
| 350 |
result = NotifyEight00com.parse_url( |
| 351 |
"eight00com://mytoken@8005551234?to=5559876543" |
| 352 |
) |
| 353 |
assert "5559876543" in " ".join(result["targets"]) |
| 354 |
|
| 355 |
|
| 356 |
assert NotifyEight00com.parse_url(None) is None |
| 357 |
|
| 358 |
|
| 359 |
@mock.patch("requests.post") |
| 360 |
def test_plugin_eight00com_attach_success(mock_post): |
| 361 |
"""NotifyEight00com() attachment send -- success path.""" |
| 362 |
|
| 363 |
response = requests.Request() |
| 364 |
response.status_code = requests.codes.ok |
| 365 |
mock_post.return_value = response |
| 366 |
|
| 367 |
obj = NotifyEight00com( |
| 368 |
token="mytoken", |
| 369 |
source="8005551234", |
| 370 |
targets=["5559876543"], |
| 371 |
) |
| 372 |
|
| 373 |
attach = AppriseAttachment.instantiate( |
| 374 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 375 |
) |
| 376 |
assert ( |
| 377 |
obj.notify( |
| 378 |
body="Test MMS", |
| 379 |
title="", |
| 380 |
notify_type=NotifyType.INFO, |
| 381 |
attach=attach, |
| 382 |
) |
| 383 |
is True |
| 384 |
) |
| 385 |
|
| 386 |
|
| 387 |
assert mock_post.call_count == 1 |
| 388 |
|
| 389 |
|
| 390 |
call_kwargs = mock_post.call_args[1] |
| 391 |
assert call_kwargs["files"] is not None |
| 392 |
assert call_kwargs["data"]["sender"] == "+8005551234" |
| 393 |
assert call_kwargs["data"]["recipient"] == "+5559876543" |
| 394 |
assert call_kwargs["data"]["message"] == "Test MMS" |
| 395 |
|
| 396 |
|
| 397 |
assert call_kwargs["headers"]["Authorization"] == "Bearer mytoken" |
| 398 |
|
| 399 |
|
| 400 |
call_url = mock_post.call_args[0][0] |
| 401 |
assert urlparse(call_url).hostname == "api.800.com" |
| 402 |
|
| 403 |
|
| 404 |
@mock.patch("requests.post") |
| 405 |
def test_plugin_eight00com_attach_multi(mock_post): |
| 406 |
"""NotifyEight00com() MMS with multiple attachments.""" |
| 407 |
|
| 408 |
response = requests.Request() |
| 409 |
response.status_code = requests.codes.ok |
| 410 |
mock_post.return_value = response |
| 411 |
|
| 412 |
obj = NotifyEight00com( |
| 413 |
token="mytoken", |
| 414 |
source="8005551234", |
| 415 |
targets=["5559876543"], |
| 416 |
) |
| 417 |
|
| 418 |
attach = AppriseAttachment() |
| 419 |
attach.add(os.path.join(TEST_VAR_DIR, "apprise-test.gif")) |
| 420 |
attach.add(os.path.join(TEST_VAR_DIR, "apprise-test.png")) |
| 421 |
|
| 422 |
assert ( |
| 423 |
obj.notify( |
| 424 |
body="Multi-attachment MMS", |
| 425 |
attach=attach, |
| 426 |
) |
| 427 |
is True |
| 428 |
) |
| 429 |
assert mock_post.call_count == 1 |
| 430 |
|
| 431 |
|
| 432 |
files = mock_post.call_args[1]["files"] |
| 433 |
assert len(files) == 2 |
| 434 |
assert all(f[0] == "media[]" for f in files) |
| 435 |
|
| 436 |
|
| 437 |
@mock.patch("requests.post") |
| 438 |
def test_plugin_eight00com_attach_inaccessible(mock_post): |
| 439 |
"""NotifyEight00com() attachment -- inaccessible file guard.""" |
| 440 |
|
| 441 |
response = requests.Request() |
| 442 |
response.status_code = requests.codes.ok |
| 443 |
mock_post.return_value = response |
| 444 |
|
| 445 |
obj = NotifyEight00com( |
| 446 |
token="mytoken", |
| 447 |
source="8005551234", |
| 448 |
targets=["5559876543"], |
| 449 |
) |
| 450 |
|
| 451 |
attach = AppriseAttachment.instantiate( |
| 452 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 453 |
) |
| 454 |
|
| 455 |
|
| 456 |
with mock.patch("os.path.isfile", return_value=False): |
| 457 |
assert obj.notify(body="Test", attach=attach) is False |
| 458 |
|
| 459 |
|
| 460 |
assert mock_post.call_count == 0 |
| 461 |
|
| 462 |
|
| 463 |
@mock.patch("requests.post") |
| 464 |
def test_plugin_eight00com_attach_oserror(mock_post): |
| 465 |
"""NotifyEight00com() attachment -- OSError on open guard.""" |
| 466 |
|
| 467 |
response = requests.Request() |
| 468 |
response.status_code = requests.codes.ok |
| 469 |
mock_post.return_value = response |
| 470 |
|
| 471 |
obj = NotifyEight00com( |
| 472 |
token="mytoken", |
| 473 |
source="8005551234", |
| 474 |
targets=["5559876543"], |
| 475 |
) |
| 476 |
|
| 477 |
attach = AppriseAttachment.instantiate( |
| 478 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 479 |
) |
| 480 |
|
| 481 |
with mock.patch("builtins.open", side_effect=OSError("IO fail")): |
| 482 |
assert obj.notify(body="Test", attach=attach) is False |
| 483 |
|
| 484 |
|
| 485 |
assert mock_post.call_count == 0 |
| 486 |
|
| 487 |
|
| 488 |
@mock.patch("requests.post") |
| 489 |
def test_plugin_eight00com_attach_http_error(mock_post): |
| 490 |
"""NotifyEight00com() attachment -- server HTTP error response.""" |
| 491 |
|
| 492 |
response = requests.Request() |
| 493 |
response.status_code = requests.codes.internal_server_error |
| 494 |
response.content = b"{}" |
| 495 |
mock_post.return_value = response |
| 496 |
|
| 497 |
obj = NotifyEight00com( |
| 498 |
token="mytoken", |
| 499 |
source="8005551234", |
| 500 |
targets=["5559876543"], |
| 501 |
) |
| 502 |
|
| 503 |
attach = AppriseAttachment.instantiate( |
| 504 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 505 |
) |
| 506 |
assert obj.notify(body="Test", attach=attach) is False |
| 507 |
assert mock_post.call_count == 1 |
| 508 |
|
| 509 |
|
| 510 |
@mock.patch("requests.post") |
| 511 |
def test_plugin_eight00com_attach_request_exception(mock_post): |
| 512 |
"""NotifyEight00com() attachment -- RequestException guard.""" |
| 513 |
|
| 514 |
mock_post.side_effect = requests.RequestException("Connection error") |
| 515 |
|
| 516 |
obj = NotifyEight00com( |
| 517 |
token="mytoken", |
| 518 |
source="8005551234", |
| 519 |
targets=["5559876543"], |
| 520 |
) |
| 521 |
|
| 522 |
attach = AppriseAttachment.instantiate( |
| 523 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 524 |
) |
| 525 |
assert obj.notify(body="Test", attach=attach) is False |
| 526 |
|
| 527 |
|
| 528 |
@mock.patch("requests.post") |
| 529 |
def test_plugin_eight00com_attach_partial_failure(mock_post): |
| 530 |
"""NotifyEight00com() MMS partial target failure.""" |
| 531 |
|
| 532 |
def _side_effect(*args, **kwargs): |
| 533 |
|
| 534 |
if mock_post.call_count == 1: |
| 535 |
resp = requests.Request() |
| 536 |
resp.status_code = requests.codes.ok |
| 537 |
return resp |
| 538 |
|
| 539 |
resp = requests.Request() |
| 540 |
resp.status_code = requests.codes.internal_server_error |
| 541 |
resp.content = b"{}" |
| 542 |
return resp |
| 543 |
|
| 544 |
mock_post.side_effect = _side_effect |
| 545 |
|
| 546 |
obj = NotifyEight00com( |
| 547 |
token="mytoken", |
| 548 |
source="8005551234", |
| 549 |
targets=["5559876543", "4441234567"], |
| 550 |
) |
| 551 |
|
| 552 |
attach = AppriseAttachment.instantiate( |
| 553 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 554 |
) |
| 555 |
|
| 556 |
assert obj.notify(body="Test", attach=attach) is False |
| 557 |
assert mock_post.call_count == 2 |
| 558 |
|
| 559 |
|
| 560 |
@mock.patch("requests.post") |
| 561 |
def test_plugin_eight00com_apprise_integration(mock_post): |
| 562 |
"""NotifyEight00com() integration with Apprise.""" |
| 563 |
|
| 564 |
response = requests.Request() |
| 565 |
response.status_code = requests.codes.ok |
| 566 |
mock_post.return_value = response |
| 567 |
|
| 568 |
|
| 569 |
a = Apprise() |
| 570 |
assert a.add("eight00com://mytoken@8005551234/5559876543") is True |
| 571 |
assert a.notify(body="Integration test") is True |
| 572 |
assert mock_post.call_count == 1 |
| 573 |
|
| 574 |
mock_post.reset_mock() |
| 575 |
|
| 576 |
|
| 577 |
a2 = Apprise() |
| 578 |
assert ( |
| 579 |
a2.add("eight00com://?token=mytoken&from=8005551234&to=5559876543") |
| 580 |
is True |
| 581 |
) |
| 582 |
assert a2.notify(body="Integration test 2") is True |
| 583 |
assert mock_post.call_count == 1 |