| 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 |
from json import loads |
| 29 |
|
| 30 |
|
| 31 |
import logging |
| 32 |
import os |
| 33 |
from unittest import mock |
| 34 |
from urllib.parse import urlparse |
| 35 |
|
| 36 |
from helpers import AppriseURLTester |
| 37 |
import pytest |
| 38 |
import requests |
| 39 |
|
| 40 |
from apprise import Apprise, AppriseAttachment, NotifyType |
| 41 |
from apprise.plugins.serwersms import NotifySerwerSMS |
| 42 |
|
| 43 |
|
| 44 |
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var") |
| 45 |
|
| 46 |
logging.disable(logging.CRITICAL) |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
SUCCESS_RESPONSE = {"success": True} |
| 51 |
FAILURE_RESPONSE = {"success": False, "error": {"code": 101, "message": "err"}} |
| 52 |
FAILURE_NO_ERROR = {"success": False} |
| 53 |
|
| 54 |
|
| 55 |
apprise_url_tests = ( |
| 56 |
( |
| 57 |
"serwersms://", |
| 58 |
{ |
| 59 |
|
| 60 |
"instance": TypeError, |
| 61 |
}, |
| 62 |
), |
| 63 |
( |
| 64 |
"serwersms://:@/", |
| 65 |
{ |
| 66 |
|
| 67 |
"instance": TypeError, |
| 68 |
}, |
| 69 |
), |
| 70 |
( |
| 71 |
"serwersms://user@SenderA/+48123456789", |
| 72 |
{ |
| 73 |
|
| 74 |
"instance": TypeError, |
| 75 |
}, |
| 76 |
), |
| 77 |
( |
| 78 |
"serwersms://user:pass@/+48123456789", |
| 79 |
{ |
| 80 |
|
| 81 |
"instance": TypeError, |
| 82 |
}, |
| 83 |
), |
| 84 |
( |
| 85 |
"serwersms://user:pass@!!invalid!!", |
| 86 |
{ |
| 87 |
|
| 88 |
"instance": TypeError, |
| 89 |
}, |
| 90 |
), |
| 91 |
( |
| 92 |
"serwersms://user:pass@SenderA", |
| 93 |
{ |
| 94 |
|
| 95 |
"instance": NotifySerwerSMS, |
| 96 |
|
| 97 |
"notify_response": False, |
| 98 |
}, |
| 99 |
), |
| 100 |
( |
| 101 |
"serwersms://user:pass@SenderA/+48123456789", |
| 102 |
{ |
| 103 |
|
| 104 |
"instance": NotifySerwerSMS, |
| 105 |
"privacy_url": "serwersms://u...r:****@SenderA/+48123456789?", |
| 106 |
"requests_response_text": SUCCESS_RESPONSE, |
| 107 |
}, |
| 108 |
), |
| 109 |
( |
| 110 |
"serwersms://user:pass@SenderA/%23456", |
| 111 |
{ |
| 112 |
|
| 113 |
"instance": NotifySerwerSMS, |
| 114 |
"privacy_url": "serwersms://u...r:****@SenderA/%23456?", |
| 115 |
"requests_response_text": SUCCESS_RESPONSE, |
| 116 |
}, |
| 117 |
), |
| 118 |
( |
| 119 |
"serwersms://user:pass@SenderA/+48123456789/%23789", |
| 120 |
{ |
| 121 |
|
| 122 |
"instance": NotifySerwerSMS, |
| 123 |
"requests_response_text": SUCCESS_RESPONSE, |
| 124 |
}, |
| 125 |
), |
| 126 |
( |
| 127 |
"serwersms://user:pass@SenderA/+48111222333?to=%23999", |
| 128 |
{ |
| 129 |
|
| 130 |
"instance": NotifySerwerSMS, |
| 131 |
"requests_response_text": SUCCESS_RESPONSE, |
| 132 |
}, |
| 133 |
), |
| 134 |
( |
| 135 |
"serwersms://user:pass@SenderA/+48123456789", |
| 136 |
{ |
| 137 |
|
| 138 |
"instance": NotifySerwerSMS, |
| 139 |
"response": False, |
| 140 |
"requests_response_code": requests.codes.internal_server_error, |
| 141 |
}, |
| 142 |
), |
| 143 |
( |
| 144 |
"serwersms://user:pass@SenderA/+48123456789", |
| 145 |
{ |
| 146 |
|
| 147 |
"instance": NotifySerwerSMS, |
| 148 |
"response": False, |
| 149 |
"requests_response_code": 999, |
| 150 |
}, |
| 151 |
), |
| 152 |
( |
| 153 |
"serwersms://user:pass@SenderA/+48123456789", |
| 154 |
{ |
| 155 |
|
| 156 |
"instance": NotifySerwerSMS, |
| 157 |
"test_requests_exceptions": True, |
| 158 |
}, |
| 159 |
), |
| 160 |
( |
| 161 |
"serwersms://user:pass@SenderA/%23123", |
| 162 |
{ |
| 163 |
|
| 164 |
"instance": NotifySerwerSMS, |
| 165 |
"response": False, |
| 166 |
"requests_response_code": requests.codes.internal_server_error, |
| 167 |
}, |
| 168 |
), |
| 169 |
( |
| 170 |
"serwersms://user:pass@SenderA/%23123", |
| 171 |
{ |
| 172 |
|
| 173 |
"instance": NotifySerwerSMS, |
| 174 |
"test_requests_exceptions": True, |
| 175 |
}, |
| 176 |
), |
| 177 |
) |
| 178 |
|
| 179 |
|
| 180 |
def test_plugin_serwersms_urls(): |
| 181 |
"""NotifySerwerSMS() Apprise URLs.""" |
| 182 |
|
| 183 |
|
| 184 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 185 |
|
| 186 |
|
| 187 |
@mock.patch("requests.post") |
| 188 |
def test_plugin_serwersms_init(mock_post): |
| 189 |
"""NotifySerwerSMS() Initialisation tests.""" |
| 190 |
|
| 191 |
|
| 192 |
with pytest.raises(TypeError): |
| 193 |
NotifySerwerSMS(sender="SenderA", targets=["+48123456789"]) |
| 194 |
|
| 195 |
|
| 196 |
with pytest.raises(TypeError): |
| 197 |
NotifySerwerSMS( |
| 198 |
user="user", |
| 199 |
sender="SenderA", |
| 200 |
targets=["+48123456789"], |
| 201 |
) |
| 202 |
|
| 203 |
|
| 204 |
with pytest.raises(TypeError): |
| 205 |
NotifySerwerSMS( |
| 206 |
user="user", |
| 207 |
password="pass", |
| 208 |
targets=["+48123456789"], |
| 209 |
) |
| 210 |
|
| 211 |
|
| 212 |
with pytest.raises(TypeError): |
| 213 |
NotifySerwerSMS( |
| 214 |
user="user", |
| 215 |
password="pass", |
| 216 |
sender="A" * 12, |
| 217 |
targets=["+48123456789"], |
| 218 |
) |
| 219 |
|
| 220 |
|
| 221 |
with pytest.raises(TypeError): |
| 222 |
NotifySerwerSMS( |
| 223 |
user="user", |
| 224 |
password="pass", |
| 225 |
sender="!invalid", |
| 226 |
targets=["+48123456789"], |
| 227 |
) |
| 228 |
|
| 229 |
|
| 230 |
obj = NotifySerwerSMS( |
| 231 |
user="user", |
| 232 |
password="pass", |
| 233 |
sender="SenderA", |
| 234 |
) |
| 235 |
assert obj is not None |
| 236 |
assert len(obj) == 1 |
| 237 |
|
| 238 |
|
| 239 |
obj = NotifySerwerSMS( |
| 240 |
user="user", |
| 241 |
password="pass", |
| 242 |
sender="SenderA", |
| 243 |
targets=["+48123456789"], |
| 244 |
) |
| 245 |
assert len(obj.target_phones) == 1 |
| 246 |
assert len(obj.target_groups) == 0 |
| 247 |
|
| 248 |
|
| 249 |
obj = NotifySerwerSMS( |
| 250 |
user="user", |
| 251 |
password="pass", |
| 252 |
sender="SenderA", |
| 253 |
targets=["#456"], |
| 254 |
) |
| 255 |
assert len(obj.target_phones) == 0 |
| 256 |
assert len(obj.target_groups) == 1 |
| 257 |
assert obj.target_groups[0] == "456" |
| 258 |
|
| 259 |
|
| 260 |
obj = NotifySerwerSMS( |
| 261 |
user="user", |
| 262 |
password="pass", |
| 263 |
sender="SenderA", |
| 264 |
targets=["%23789"], |
| 265 |
) |
| 266 |
assert len(obj.target_groups) == 1 |
| 267 |
assert obj.target_groups[0] == "789" |
| 268 |
|
| 269 |
|
| 270 |
obj = NotifySerwerSMS( |
| 271 |
user="user", |
| 272 |
password="pass", |
| 273 |
sender="SenderA", |
| 274 |
targets=["notaphone", "+48123456789"], |
| 275 |
) |
| 276 |
assert len(obj.invalid_targets) == 1 |
| 277 |
assert len(obj.target_phones) == 1 |
| 278 |
|
| 279 |
|
| 280 |
@mock.patch("requests.post") |
| 281 |
def test_plugin_serwersms_send_phone(mock_post): |
| 282 |
"""NotifySerwerSMS() send to phone numbers.""" |
| 283 |
|
| 284 |
|
| 285 |
mock_post.return_value = mock.Mock() |
| 286 |
mock_post.return_value.status_code = requests.codes.ok |
| 287 |
mock_post.return_value.content = b'{"success": true}' |
| 288 |
|
| 289 |
obj = NotifySerwerSMS( |
| 290 |
user="myuser", |
| 291 |
password="mypass", |
| 292 |
sender="AppName", |
| 293 |
targets=["+48111222333", "+48444555666"], |
| 294 |
) |
| 295 |
|
| 296 |
|
| 297 |
assert obj.send(body="Hello", title="Title") is True |
| 298 |
assert mock_post.call_count == 2 |
| 299 |
|
| 300 |
|
| 301 |
call_url = mock_post.call_args_list[0][0][0] |
| 302 |
assert urlparse(call_url).hostname == "api2.serwersms.pl" |
| 303 |
|
| 304 |
|
| 305 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 306 |
assert payload["username"] == "myuser" |
| 307 |
assert payload["password"] == "mypass" |
| 308 |
assert payload["sender"] == "AppName" |
| 309 |
assert payload["text"] == "Hello" |
| 310 |
assert payload["phone"] == "+48111222333" |
| 311 |
assert "group_id" not in payload |
| 312 |
|
| 313 |
|
| 314 |
payload2 = loads(mock_post.call_args_list[1][1]["data"]) |
| 315 |
assert payload2["phone"] == "+48444555666" |
| 316 |
|
| 317 |
|
| 318 |
@mock.patch("requests.post") |
| 319 |
def test_plugin_serwersms_send_group(mock_post): |
| 320 |
"""NotifySerwerSMS() send to contact groups.""" |
| 321 |
|
| 322 |
|
| 323 |
mock_post.return_value = mock.Mock() |
| 324 |
mock_post.return_value.status_code = requests.codes.ok |
| 325 |
mock_post.return_value.content = b'{"success": true}' |
| 326 |
|
| 327 |
obj = NotifySerwerSMS( |
| 328 |
user="myuser", |
| 329 |
password="mypass", |
| 330 |
sender="AppName", |
| 331 |
targets=["#100", "#200"], |
| 332 |
) |
| 333 |
|
| 334 |
|
| 335 |
assert obj.send(body="Hello") is True |
| 336 |
assert mock_post.call_count == 2 |
| 337 |
|
| 338 |
|
| 339 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 340 |
assert payload["group_id"] == "100" |
| 341 |
assert "phone" not in payload |
| 342 |
|
| 343 |
payload2 = loads(mock_post.call_args_list[1][1]["data"]) |
| 344 |
assert payload2["group_id"] == "200" |
| 345 |
|
| 346 |
|
| 347 |
@mock.patch("requests.post") |
| 348 |
def test_plugin_serwersms_send_mixed(mock_post): |
| 349 |
"""NotifySerwerSMS() send to phones and groups together.""" |
| 350 |
|
| 351 |
mock_post.return_value = mock.Mock() |
| 352 |
mock_post.return_value.status_code = requests.codes.ok |
| 353 |
mock_post.return_value.content = b'{"success": true}' |
| 354 |
|
| 355 |
obj = NotifySerwerSMS( |
| 356 |
user="u", |
| 357 |
password="p", |
| 358 |
sender="Sender", |
| 359 |
targets=["+48123456789", "#500"], |
| 360 |
) |
| 361 |
|
| 362 |
|
| 363 |
assert obj.send(body="msg") is True |
| 364 |
assert mock_post.call_count == 2 |
| 365 |
|
| 366 |
|
| 367 |
p1 = loads(mock_post.call_args_list[0][1]["data"]) |
| 368 |
assert "phone" in p1 |
| 369 |
|
| 370 |
|
| 371 |
p2 = loads(mock_post.call_args_list[1][1]["data"]) |
| 372 |
assert "group_id" in p2 |
| 373 |
|
| 374 |
|
| 375 |
@mock.patch("requests.post") |
| 376 |
def test_plugin_serwersms_no_targets(mock_post): |
| 377 |
"""NotifySerwerSMS() with no targets returns False without calling API.""" |
| 378 |
|
| 379 |
obj = NotifySerwerSMS( |
| 380 |
user="u", |
| 381 |
password="p", |
| 382 |
sender="Sender", |
| 383 |
) |
| 384 |
assert obj.send(body="msg") is False |
| 385 |
assert mock_post.call_count == 0 |
| 386 |
|
| 387 |
|
| 388 |
@mock.patch("requests.post") |
| 389 |
def test_plugin_serwersms_http_error(mock_post): |
| 390 |
"""NotifySerwerSMS() handles HTTP errors gracefully.""" |
| 391 |
|
| 392 |
mock_post.return_value = mock.Mock() |
| 393 |
mock_post.return_value.status_code = requests.codes.internal_server_error |
| 394 |
mock_post.return_value.content = b"" |
| 395 |
|
| 396 |
obj = NotifySerwerSMS( |
| 397 |
user="u", |
| 398 |
password="p", |
| 399 |
sender="Sender", |
| 400 |
targets=["+48123456789", "#100"], |
| 401 |
) |
| 402 |
|
| 403 |
|
| 404 |
assert obj.send(body="msg") is False |
| 405 |
assert mock_post.call_count == 2 |
| 406 |
|
| 407 |
|
| 408 |
@mock.patch("requests.post") |
| 409 |
def test_plugin_serwersms_http_error_partial(mock_post): |
| 410 |
"""NotifySerwerSMS() returns False on any partial HTTP failure.""" |
| 411 |
|
| 412 |
|
| 413 |
good = mock.Mock() |
| 414 |
good.status_code = requests.codes.ok |
| 415 |
good.content = b'{"success": true}' |
| 416 |
|
| 417 |
bad = mock.Mock() |
| 418 |
bad.status_code = 999 |
| 419 |
bad.content = b"" |
| 420 |
|
| 421 |
mock_post.side_effect = [good, bad] |
| 422 |
|
| 423 |
obj = NotifySerwerSMS( |
| 424 |
user="u", |
| 425 |
password="p", |
| 426 |
sender="Sender", |
| 427 |
targets=["+48111000000", "+48222000000"], |
| 428 |
) |
| 429 |
|
| 430 |
|
| 431 |
assert obj.send(body="msg") is False |
| 432 |
assert mock_post.call_count == 2 |
| 433 |
|
| 434 |
|
| 435 |
@mock.patch("requests.post") |
| 436 |
def test_plugin_serwersms_request_exception(mock_post): |
| 437 |
"""NotifySerwerSMS() handles RequestException gracefully.""" |
| 438 |
|
| 439 |
mock_post.side_effect = requests.RequestException("Connection refused") |
| 440 |
|
| 441 |
obj = NotifySerwerSMS( |
| 442 |
user="u", |
| 443 |
password="p", |
| 444 |
sender="Sender", |
| 445 |
targets=["+48123456789"], |
| 446 |
) |
| 447 |
|
| 448 |
assert obj.send(body="msg") is False |
| 449 |
|
| 450 |
|
| 451 |
@mock.patch("requests.post") |
| 452 |
def test_plugin_serwersms_request_exception_group(mock_post): |
| 453 |
"""NotifySerwerSMS() handles RequestException on group send.""" |
| 454 |
|
| 455 |
mock_post.side_effect = requests.RequestException("timeout") |
| 456 |
|
| 457 |
obj = NotifySerwerSMS( |
| 458 |
user="u", |
| 459 |
password="p", |
| 460 |
sender="Sender", |
| 461 |
targets=["#100"], |
| 462 |
) |
| 463 |
|
| 464 |
assert obj.send(body="msg") is False |
| 465 |
|
| 466 |
|
| 467 |
@mock.patch("requests.post") |
| 468 |
def test_plugin_serwersms_api_failure_with_error(mock_post): |
| 469 |
"""NotifySerwerSMS() handles API success=false with error object.""" |
| 470 |
|
| 471 |
mock_post.return_value = mock.Mock() |
| 472 |
mock_post.return_value.status_code = requests.codes.ok |
| 473 |
mock_post.return_value.content = ( |
| 474 |
b'{"success": false, "error": {"code": 101, "message": "Auth fail"}}' |
| 475 |
) |
| 476 |
|
| 477 |
obj = NotifySerwerSMS( |
| 478 |
user="u", |
| 479 |
password="p", |
| 480 |
sender="Sender", |
| 481 |
targets=["+48123456789"], |
| 482 |
) |
| 483 |
|
| 484 |
assert obj.send(body="msg") is False |
| 485 |
|
| 486 |
|
| 487 |
@mock.patch("requests.post") |
| 488 |
def test_plugin_serwersms_api_failure_no_error(mock_post): |
| 489 |
"""NotifySerwerSMS() handles API success=false without error detail.""" |
| 490 |
|
| 491 |
mock_post.return_value = mock.Mock() |
| 492 |
mock_post.return_value.status_code = requests.codes.ok |
| 493 |
mock_post.return_value.content = b'{"success": false}' |
| 494 |
|
| 495 |
obj = NotifySerwerSMS( |
| 496 |
user="u", |
| 497 |
password="p", |
| 498 |
sender="Sender", |
| 499 |
targets=["#200"], |
| 500 |
) |
| 501 |
|
| 502 |
assert obj.send(body="msg") is False |
| 503 |
|
| 504 |
|
| 505 |
@mock.patch("requests.post") |
| 506 |
def test_plugin_serwersms_api_bad_json(mock_post): |
| 507 |
"""NotifySerwerSMS() handles unparseable JSON response body.""" |
| 508 |
|
| 509 |
mock_post.return_value = mock.Mock() |
| 510 |
mock_post.return_value.status_code = requests.codes.ok |
| 511 |
mock_post.return_value.content = b"not-json" |
| 512 |
|
| 513 |
obj = NotifySerwerSMS( |
| 514 |
user="u", |
| 515 |
password="p", |
| 516 |
sender="Sender", |
| 517 |
targets=["+48123456789"], |
| 518 |
) |
| 519 |
|
| 520 |
|
| 521 |
assert obj.send(body="msg") is False |
| 522 |
|
| 523 |
|
| 524 |
@mock.patch("requests.post") |
| 525 |
def test_plugin_serwersms_api_null_json(mock_post): |
| 526 |
"""NotifySerwerSMS() handles null JSON response body.""" |
| 527 |
|
| 528 |
mock_post.return_value = mock.Mock() |
| 529 |
mock_post.return_value.status_code = requests.codes.ok |
| 530 |
|
| 531 |
mock_post.return_value.content = b"null" |
| 532 |
|
| 533 |
obj = NotifySerwerSMS( |
| 534 |
user="u", |
| 535 |
password="p", |
| 536 |
sender="Sender", |
| 537 |
targets=["+48123456789"], |
| 538 |
) |
| 539 |
|
| 540 |
assert obj.send(body="msg") is False |
| 541 |
|
| 542 |
|
| 543 |
@mock.patch("requests.post") |
| 544 |
def test_plugin_serwersms_url(mock_post): |
| 545 |
"""NotifySerwerSMS() url() and url_identifier() tests.""" |
| 546 |
|
| 547 |
obj = NotifySerwerSMS( |
| 548 |
user="myuser", |
| 549 |
password="mypass", |
| 550 |
sender="AppName", |
| 551 |
targets=["+48123456789", "#100"], |
| 552 |
) |
| 553 |
|
| 554 |
|
| 555 |
full_url = obj.url() |
| 556 |
assert full_url.startswith("serwersms://") |
| 557 |
assert "myuser" in full_url |
| 558 |
assert "mypass" in full_url |
| 559 |
assert "AppName" in full_url |
| 560 |
|
| 561 |
|
| 562 |
priv = obj.url(privacy=True) |
| 563 |
assert "mypass" not in priv |
| 564 |
assert "myuser" not in priv |
| 565 |
assert "AppName" in priv |
| 566 |
|
| 567 |
|
| 568 |
ident = obj.url_identifier |
| 569 |
assert "serwersms" in ident |
| 570 |
assert "myuser" in ident |
| 571 |
assert "AppName" in ident |
| 572 |
assert "+48123456789" not in str(ident) |
| 573 |
|
| 574 |
|
| 575 |
@mock.patch("requests.post") |
| 576 |
def test_plugin_serwersms_url_roundtrip(mock_post): |
| 577 |
"""NotifySerwerSMS() round-trips through url() -> parse_url().""" |
| 578 |
|
| 579 |
obj = NotifySerwerSMS( |
| 580 |
user="myuser", |
| 581 |
password="mypass", |
| 582 |
sender="AppName", |
| 583 |
targets=["+48123456789", "#100"], |
| 584 |
) |
| 585 |
|
| 586 |
|
| 587 |
result = NotifySerwerSMS.parse_url(obj.url()) |
| 588 |
assert result is not None |
| 589 |
|
| 590 |
obj2 = NotifySerwerSMS(**result) |
| 591 |
assert obj.url_identifier == obj2.url_identifier |
| 592 |
assert len(obj.target_phones) == len(obj2.target_phones) |
| 593 |
assert len(obj.target_groups) == len(obj2.target_groups) |
| 594 |
|
| 595 |
|
| 596 |
@mock.patch("requests.post") |
| 597 |
def test_plugin_serwersms_url_invalid_targets(mock_post): |
| 598 |
"""NotifySerwerSMS() preserves invalid targets in url() round-trip.""" |
| 599 |
|
| 600 |
obj = NotifySerwerSMS( |
| 601 |
user="u", |
| 602 |
password="p", |
| 603 |
sender="S", |
| 604 |
targets=["+48123456789", "badtarget"], |
| 605 |
) |
| 606 |
|
| 607 |
assert len(obj.invalid_targets) == 1 |
| 608 |
assert len(obj.target_phones) == 1 |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
url = obj.url() |
| 613 |
assert "badtarget" in url |
| 614 |
|
| 615 |
|
| 616 |
def test_plugin_serwersms_parse_url(): |
| 617 |
"""NotifySerwerSMS() parse_url() edge cases.""" |
| 618 |
|
| 619 |
|
| 620 |
result = NotifySerwerSMS.parse_url( |
| 621 |
"serwersms://user:pass@SenderA/+48123456789/%23100" |
| 622 |
) |
| 623 |
assert result is not None |
| 624 |
assert result["user"] == "user" |
| 625 |
assert result["password"] == "pass" |
| 626 |
assert result["sender"] == "SenderA" |
| 627 |
|
| 628 |
assert "+48123456789" in result["targets"] |
| 629 |
assert "#100" in result["targets"] |
| 630 |
|
| 631 |
|
| 632 |
result = NotifySerwerSMS.parse_url( |
| 633 |
"serwersms://user:pass@SenderA?to=%2348123456789" |
| 634 |
) |
| 635 |
assert result is not None |
| 636 |
assert len(result["targets"]) >= 1 |
| 637 |
|
| 638 |
|
| 639 |
result = NotifySerwerSMS.parse_url( |
| 640 |
"serwersms://user:pass@SenderA/+48111?from=NewSender" |
| 641 |
) |
| 642 |
assert result["sender"] == "NewSender" |
| 643 |
|
| 644 |
|
| 645 |
result = NotifySerwerSMS.parse_url( |
| 646 |
"serwersms://user:pass@SenderA/+48111?from=Old&sender=New" |
| 647 |
) |
| 648 |
assert result["sender"] == "New" |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
assert NotifySerwerSMS.parse_url(None) is None |
| 653 |
|
| 654 |
|
| 655 |
@mock.patch("requests.post") |
| 656 |
def test_plugin_serwersms_apprise_integration(mock_post): |
| 657 |
"""NotifySerwerSMS() integration with Apprise object.""" |
| 658 |
|
| 659 |
mock_post.return_value = mock.Mock() |
| 660 |
mock_post.return_value.status_code = requests.codes.ok |
| 661 |
mock_post.return_value.content = b'{"success": true}' |
| 662 |
|
| 663 |
aobj = Apprise() |
| 664 |
assert aobj.add("serwersms://user:pass@AppName/+48123456789") |
| 665 |
|
| 666 |
assert aobj.notify( |
| 667 |
body="Integration test", |
| 668 |
title="Title", |
| 669 |
notify_type=NotifyType.INFO, |
| 670 |
) |
| 671 |
assert mock_post.call_count == 1 |
| 672 |
|
| 673 |
|
| 674 |
@mock.patch("requests.post") |
| 675 |
def test_plugin_serwersms_mms_phone_success(mock_post): |
| 676 |
"""NotifySerwerSMS() MMS to phone -- success path.""" |
| 677 |
|
| 678 |
mock_post.return_value = mock.Mock() |
| 679 |
mock_post.return_value.status_code = requests.codes.ok |
| 680 |
mock_post.return_value.content = b'{"success": true}' |
| 681 |
|
| 682 |
obj = NotifySerwerSMS( |
| 683 |
user="myuser", |
| 684 |
password="mypass", |
| 685 |
sender="AppName", |
| 686 |
targets=["+48123456789"], |
| 687 |
) |
| 688 |
|
| 689 |
attach = AppriseAttachment.instantiate( |
| 690 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 691 |
) |
| 692 |
assert obj.notify(body="MMS test", attach=attach) is True |
| 693 |
assert mock_post.call_count == 1 |
| 694 |
|
| 695 |
|
| 696 |
call_kwargs = mock_post.call_args[1] |
| 697 |
assert call_kwargs["files"] is not None |
| 698 |
|
| 699 |
|
| 700 |
assert call_kwargs["data"]["phone"] == "+48123456789" |
| 701 |
assert call_kwargs["data"]["sender"] == "AppName" |
| 702 |
assert call_kwargs["data"]["text"] == "MMS test" |
| 703 |
|
| 704 |
|
| 705 |
call_url = mock_post.call_args[0][0] |
| 706 |
assert "send_mms" in call_url |
| 707 |
|
| 708 |
|
| 709 |
assert "Content-Type" not in call_kwargs["headers"] |
| 710 |
|
| 711 |
|
| 712 |
@mock.patch("requests.post") |
| 713 |
def test_plugin_serwersms_mms_group_success(mock_post): |
| 714 |
"""NotifySerwerSMS() MMS to group -- success path.""" |
| 715 |
|
| 716 |
mock_post.return_value = mock.Mock() |
| 717 |
mock_post.return_value.status_code = requests.codes.ok |
| 718 |
mock_post.return_value.content = b'{"success": true}' |
| 719 |
|
| 720 |
obj = NotifySerwerSMS( |
| 721 |
user="u", |
| 722 |
password="p", |
| 723 |
sender="Sender", |
| 724 |
targets=["#500"], |
| 725 |
) |
| 726 |
|
| 727 |
attach = AppriseAttachment.instantiate( |
| 728 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 729 |
) |
| 730 |
assert obj.notify(body="Group MMS", attach=attach) is True |
| 731 |
assert mock_post.call_count == 1 |
| 732 |
|
| 733 |
|
| 734 |
call_kwargs = mock_post.call_args[1] |
| 735 |
assert call_kwargs["data"]["group_id"] == "500" |
| 736 |
assert "phone" not in call_kwargs["data"] |
| 737 |
|
| 738 |
|
| 739 |
@mock.patch("requests.post") |
| 740 |
def test_plugin_serwersms_mms_guard1_inaccessible(mock_post): |
| 741 |
"""NotifySerwerSMS() MMS -- guard 1 inaccessible attachment.""" |
| 742 |
|
| 743 |
mock_post.return_value = mock.Mock() |
| 744 |
mock_post.return_value.status_code = requests.codes.ok |
| 745 |
mock_post.return_value.content = b'{"success": true}' |
| 746 |
|
| 747 |
obj = NotifySerwerSMS( |
| 748 |
user="u", |
| 749 |
password="p", |
| 750 |
sender="Sender", |
| 751 |
targets=["+48123456789"], |
| 752 |
) |
| 753 |
|
| 754 |
attach = AppriseAttachment.instantiate( |
| 755 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 756 |
) |
| 757 |
|
| 758 |
|
| 759 |
with mock.patch("os.path.isfile", return_value=False): |
| 760 |
assert obj.notify(body="Test", attach=attach) is False |
| 761 |
|
| 762 |
|
| 763 |
assert mock_post.call_count == 0 |
| 764 |
|
| 765 |
|
| 766 |
@mock.patch("requests.post") |
| 767 |
def test_plugin_serwersms_mms_guard2_oserror(mock_post): |
| 768 |
"""NotifySerwerSMS() MMS -- guard 2 OSError on open.""" |
| 769 |
|
| 770 |
mock_post.return_value = mock.Mock() |
| 771 |
mock_post.return_value.status_code = requests.codes.ok |
| 772 |
mock_post.return_value.content = b'{"success": true}' |
| 773 |
|
| 774 |
obj = NotifySerwerSMS( |
| 775 |
user="u", |
| 776 |
password="p", |
| 777 |
sender="Sender", |
| 778 |
targets=["+48123456789"], |
| 779 |
) |
| 780 |
|
| 781 |
attach = AppriseAttachment.instantiate( |
| 782 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 783 |
) |
| 784 |
|
| 785 |
with mock.patch("builtins.open", side_effect=OSError("IO fail")): |
| 786 |
assert obj.notify(body="Test", attach=attach) is False |
| 787 |
|
| 788 |
|
| 789 |
assert mock_post.call_count == 0 |
| 790 |
|
| 791 |
|
| 792 |
@mock.patch("requests.post") |
| 793 |
def test_plugin_serwersms_mms_http_error(mock_post): |
| 794 |
"""NotifySerwerSMS() MMS -- HTTP error response.""" |
| 795 |
|
| 796 |
mock_post.return_value = mock.Mock() |
| 797 |
mock_post.return_value.status_code = requests.codes.internal_server_error |
| 798 |
mock_post.return_value.content = b"" |
| 799 |
|
| 800 |
obj = NotifySerwerSMS( |
| 801 |
user="u", |
| 802 |
password="p", |
| 803 |
sender="Sender", |
| 804 |
targets=["+48123456789"], |
| 805 |
) |
| 806 |
|
| 807 |
attach = AppriseAttachment.instantiate( |
| 808 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 809 |
) |
| 810 |
assert obj.notify(body="Test", attach=attach) is False |
| 811 |
assert mock_post.call_count == 1 |
| 812 |
|
| 813 |
|
| 814 |
@mock.patch("requests.post") |
| 815 |
def test_plugin_serwersms_mms_api_failure_with_error(mock_post): |
| 816 |
"""NotifySerwerSMS() MMS -- API success=false with error object.""" |
| 817 |
|
| 818 |
mock_post.return_value = mock.Mock() |
| 819 |
mock_post.return_value.status_code = requests.codes.ok |
| 820 |
mock_post.return_value.content = ( |
| 821 |
b'{"success": false, "error": {"code": 201, "message": "MMS fail"}}' |
| 822 |
) |
| 823 |
|
| 824 |
obj = NotifySerwerSMS( |
| 825 |
user="u", |
| 826 |
password="p", |
| 827 |
sender="Sender", |
| 828 |
targets=["+48123456789"], |
| 829 |
) |
| 830 |
|
| 831 |
attach = AppriseAttachment.instantiate( |
| 832 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 833 |
) |
| 834 |
assert obj.notify(body="Test", attach=attach) is False |
| 835 |
|
| 836 |
|
| 837 |
@mock.patch("requests.post") |
| 838 |
def test_plugin_serwersms_mms_api_failure_no_error(mock_post): |
| 839 |
"""NotifySerwerSMS() MMS -- API success=false without error detail.""" |
| 840 |
|
| 841 |
mock_post.return_value = mock.Mock() |
| 842 |
mock_post.return_value.status_code = requests.codes.ok |
| 843 |
mock_post.return_value.content = b'{"success": false}' |
| 844 |
|
| 845 |
obj = NotifySerwerSMS( |
| 846 |
user="u", |
| 847 |
password="p", |
| 848 |
sender="Sender", |
| 849 |
targets=["+48123456789"], |
| 850 |
) |
| 851 |
|
| 852 |
attach = AppriseAttachment.instantiate( |
| 853 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 854 |
) |
| 855 |
assert obj.notify(body="Test", attach=attach) is False |
| 856 |
|
| 857 |
|
| 858 |
@mock.patch("requests.post") |
| 859 |
def test_plugin_serwersms_mms_bad_json(mock_post): |
| 860 |
"""NotifySerwerSMS() MMS -- unparseable JSON response.""" |
| 861 |
|
| 862 |
mock_post.return_value = mock.Mock() |
| 863 |
mock_post.return_value.status_code = requests.codes.ok |
| 864 |
mock_post.return_value.content = b"not-json" |
| 865 |
|
| 866 |
obj = NotifySerwerSMS( |
| 867 |
user="u", |
| 868 |
password="p", |
| 869 |
sender="Sender", |
| 870 |
targets=["+48123456789"], |
| 871 |
) |
| 872 |
|
| 873 |
attach = AppriseAttachment.instantiate( |
| 874 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 875 |
) |
| 876 |
|
| 877 |
assert obj.notify(body="Test", attach=attach) is False |
| 878 |
|
| 879 |
|
| 880 |
@mock.patch("requests.post") |
| 881 |
def test_plugin_serwersms_mms_null_json(mock_post): |
| 882 |
"""NotifySerwerSMS() MMS -- null JSON response body.""" |
| 883 |
|
| 884 |
mock_post.return_value = mock.Mock() |
| 885 |
mock_post.return_value.status_code = requests.codes.ok |
| 886 |
|
| 887 |
mock_post.return_value.content = b"null" |
| 888 |
|
| 889 |
obj = NotifySerwerSMS( |
| 890 |
user="u", |
| 891 |
password="p", |
| 892 |
sender="Sender", |
| 893 |
targets=["+48123456789"], |
| 894 |
) |
| 895 |
|
| 896 |
attach = AppriseAttachment.instantiate( |
| 897 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 898 |
) |
| 899 |
assert obj.notify(body="Test", attach=attach) is False |
| 900 |
|
| 901 |
|
| 902 |
@mock.patch("requests.post") |
| 903 |
def test_plugin_serwersms_mms_request_exception(mock_post): |
| 904 |
"""NotifySerwerSMS() MMS -- RequestException guard.""" |
| 905 |
|
| 906 |
mock_post.side_effect = requests.RequestException("Connection refused") |
| 907 |
|
| 908 |
obj = NotifySerwerSMS( |
| 909 |
user="u", |
| 910 |
password="p", |
| 911 |
sender="Sender", |
| 912 |
targets=["+48123456789"], |
| 913 |
) |
| 914 |
|
| 915 |
attach = AppriseAttachment.instantiate( |
| 916 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 917 |
) |
| 918 |
assert obj.notify(body="Test", attach=attach) is False |