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