| 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 logging |
| 30 |
import mimetypes |
| 31 |
from os.path import dirname, getsize, join |
| 32 |
import re |
| 33 |
from typing import ClassVar |
| 34 |
from unittest import mock |
| 35 |
|
| 36 |
import pytest |
| 37 |
import requests |
| 38 |
|
| 39 |
from apprise import Apprise, AppriseAttachment, NotificationManager, exception |
| 40 |
from apprise.attachment.http import AttachHTTP |
| 41 |
from apprise.common import ContentLocation |
| 42 |
from apprise.plugins import NotifyBase |
| 43 |
|
| 44 |
logging.disable(logging.CRITICAL) |
| 45 |
|
| 46 |
TEST_VAR_DIR = join(dirname(__file__), "var") |
| 47 |
|
| 48 |
|
| 49 |
N_MGR = NotificationManager() |
| 50 |
|
| 51 |
|
| 52 |
REQUEST_EXCEPTIONS = ( |
| 53 |
requests.ConnectionError(0, "requests.ConnectionError() not handled"), |
| 54 |
requests.RequestException(0, "requests.RequestException() not handled"), |
| 55 |
requests.HTTPError(0, "requests.HTTPError() not handled"), |
| 56 |
requests.ReadTimeout(0, "requests.ReadTimeout() not handled"), |
| 57 |
requests.TooManyRedirects(0, "requests.TooManyRedirects() not handled"), |
| 58 |
|
| 59 |
OSError("SystemError"), |
| 60 |
) |
| 61 |
|
| 62 |
|
| 63 |
def test_attach_http_parse_url(): |
| 64 |
""" |
| 65 |
API: AttachHTTP().parse_url() |
| 66 |
|
| 67 |
""" |
| 68 |
|
| 69 |
|
| 70 |
assert AttachHTTP.parse_url("garbage://") is None |
| 71 |
|
| 72 |
|
| 73 |
assert AttachHTTP.parse_url("http://") is None |
| 74 |
|
| 75 |
|
| 76 |
def test_attach_http_query_string_dictionary(): |
| 77 |
""" |
| 78 |
API: AttachHTTP() Query String Dictionary |
| 79 |
|
| 80 |
""" |
| 81 |
|
| 82 |
|
| 83 |
results = AttachHTTP.parse_url("http://localhost?verify=no&rto=9&cto=8") |
| 84 |
assert isinstance(results, dict) |
| 85 |
|
| 86 |
|
| 87 |
obj = AttachHTTP(**results) |
| 88 |
assert isinstance(obj, AttachHTTP) |
| 89 |
|
| 90 |
|
| 91 |
assert re.search(r"[?&]verify=no", obj.url()) |
| 92 |
|
| 93 |
|
| 94 |
assert re.search(r"[?&]cto=8", obj.url()) |
| 95 |
|
| 96 |
assert re.search(r"[?&]rto=9", obj.url()) |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
results = AttachHTTP.parse_url("http://localhost?dl=1&_var=test") |
| 102 |
assert isinstance(results, dict) |
| 103 |
|
| 104 |
|
| 105 |
obj = AttachHTTP(**results) |
| 106 |
assert isinstance(obj, AttachHTTP) |
| 107 |
|
| 108 |
|
| 109 |
assert not re.search(r"[?&]verify=yes", obj.url()) |
| 110 |
|
| 111 |
|
| 112 |
assert re.search(r"[?&]dl=1", obj.url()) |
| 113 |
assert re.search(r"[?&]_var=test", obj.url()) |
| 114 |
|
| 115 |
|
| 116 |
@mock.patch("requests.request") |
| 117 |
@mock.patch("requests.get") |
| 118 |
def test_attach_http(mock_get, mock_request): |
| 119 |
""" |
| 120 |
API: AttachHTTP() object |
| 121 |
|
| 122 |
""" |
| 123 |
|
| 124 |
|
| 125 |
class GoodNotification(NotifyBase): |
| 126 |
def __init__(self, *args, **kwargs): |
| 127 |
super().__init__(*args, **kwargs) |
| 128 |
|
| 129 |
def notify(self, *args, **kwargs): |
| 130 |
|
| 131 |
return True |
| 132 |
|
| 133 |
def url(self): |
| 134 |
|
| 135 |
return "" |
| 136 |
|
| 137 |
|
| 138 |
N_MGR["good"] = GoodNotification |
| 139 |
|
| 140 |
|
| 141 |
path = join(TEST_VAR_DIR, "apprise-test.gif") |
| 142 |
|
| 143 |
class DummyResponse: |
| 144 |
"""A dummy response used to manage our object.""" |
| 145 |
|
| 146 |
status_code = requests.codes.ok |
| 147 |
headers: ClassVar[dict[str, str]] = { |
| 148 |
"Content-Length": getsize(path), |
| 149 |
"Content-Type": "text/plain", |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
ptr = None |
| 154 |
|
| 155 |
|
| 156 |
_keepalive_chunk_ref = 0 |
| 157 |
|
| 158 |
def close(self): |
| 159 |
return |
| 160 |
|
| 161 |
def iter_content(self, chunk_size=1024): |
| 162 |
"""Lazy function (generator) to read a file piece by piece. |
| 163 |
|
| 164 |
Default chunk size: 1k. |
| 165 |
""" |
| 166 |
|
| 167 |
while True: |
| 168 |
self._keepalive_chunk_ref += 1 |
| 169 |
if 16 % self._keepalive_chunk_ref == 0: |
| 170 |
|
| 171 |
yield "" |
| 172 |
|
| 173 |
data = self.ptr.read(chunk_size) |
| 174 |
if not data: |
| 175 |
break |
| 176 |
yield data |
| 177 |
|
| 178 |
def raise_for_status(self): |
| 179 |
return |
| 180 |
|
| 181 |
def __enter__(self): |
| 182 |
self.ptr = open(path, "rb") |
| 183 |
return self |
| 184 |
|
| 185 |
def __exit__(self, *args, **kwargs): |
| 186 |
self.ptr.close() |
| 187 |
|
| 188 |
|
| 189 |
dummy_response = DummyResponse() |
| 190 |
mock_get.return_value = dummy_response |
| 191 |
|
| 192 |
|
| 193 |
results = AttachHTTP.parse_url( |
| 194 |
"http://user:pass@localhost/apprise.gif?DL=1&cache=300" |
| 195 |
) |
| 196 |
assert isinstance(results, dict) |
| 197 |
attachment = AttachHTTP(**results) |
| 198 |
assert isinstance(attachment.url(), str) is True |
| 199 |
|
| 200 |
|
| 201 |
assert mock_get.call_count == 0 |
| 202 |
assert attachment |
| 203 |
assert mock_get.call_count == 1 |
| 204 |
assert "params" in mock_get.call_args_list[0][1] |
| 205 |
assert "DL" in mock_get.call_args_list[0][1]["params"] |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
assert "cache" not in mock_get.call_args_list[0][1]["params"] |
| 210 |
|
| 211 |
with mock.patch("os.unlink", side_effect=OSError()): |
| 212 |
|
| 213 |
attachment.invalidate() |
| 214 |
|
| 215 |
results = AttachHTTP.parse_url( |
| 216 |
"http://user:pass@localhost/apprise.gif?+key=value&cache=True" |
| 217 |
) |
| 218 |
assert isinstance(results, dict) |
| 219 |
attachment = AttachHTTP(**results) |
| 220 |
assert isinstance(attachment.url(), str) is True |
| 221 |
|
| 222 |
|
| 223 |
assert re.search(r"[?&]mime=", attachment.url()) is None |
| 224 |
assert re.search(r"[?&]name=", attachment.url()) is None |
| 225 |
|
| 226 |
assert attachment.name == "apprise.gif" |
| 227 |
|
| 228 |
assert attachment.mimetype == "text/plain" |
| 229 |
|
| 230 |
|
| 231 |
attachment.detected_mimetype = None |
| 232 |
attachment._mimetype = None |
| 233 |
|
| 234 |
|
| 235 |
assert attachment.mimetype == "image/gif" |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
attachment.detected_mimetype = None |
| 241 |
|
| 242 |
|
| 243 |
assert attachment.mimetype == "image/gif" |
| 244 |
|
| 245 |
results = AttachHTTP.parse_url( |
| 246 |
"http://localhost:3000/noname.gif?name=usethis.jpg&mime=image/jpeg" |
| 247 |
) |
| 248 |
assert isinstance(results, dict) |
| 249 |
attachment = AttachHTTP(**results) |
| 250 |
assert isinstance(attachment.url(), str) is True |
| 251 |
|
| 252 |
assert re.search(r"[?&]mime=image/jpeg", attachment.url()) |
| 253 |
assert re.search(r"[?&]name=usethis.jpg", attachment.url()) |
| 254 |
|
| 255 |
assert attachment.name == "usethis.jpg" |
| 256 |
assert attachment.mimetype == "image/jpeg" |
| 257 |
|
| 258 |
|
| 259 |
assert attachment.download() |
| 260 |
assert attachment |
| 261 |
assert len(attachment) == getsize(path) |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
attachment = AttachHTTP(**results) |
| 273 |
attachment.location = ContentLocation.INACCESSIBLE |
| 274 |
assert attachment.path is None |
| 275 |
|
| 276 |
assert attachment.download() is False |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
results = AttachHTTP.parse_url("http://localhost") |
| 282 |
assert isinstance(results, dict) |
| 283 |
attachment = AttachHTTP(**results) |
| 284 |
assert isinstance(attachment.url(), str) is True |
| 285 |
|
| 286 |
|
| 287 |
assert re.search(r"[?&]mime=", attachment.url()) is None |
| 288 |
assert re.search(r"[?&]name=", attachment.url()) is None |
| 289 |
|
| 290 |
assert attachment.mimetype == "text/plain" |
| 291 |
|
| 292 |
|
| 293 |
attachment.detected_mimetype = None |
| 294 |
attachment._mimetype = None |
| 295 |
|
| 296 |
|
| 297 |
assert attachment.mimetype == "application/octet-stream" |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
assert ( |
| 302 |
attachment.name == f"{AttachHTTP.unknown_filename}" |
| 303 |
f"{mimetypes.guess_extension(attachment.mimetype)}" |
| 304 |
) |
| 305 |
assert attachment |
| 306 |
assert len(attachment) == getsize(path) |
| 307 |
|
| 308 |
|
| 309 |
dummy_response.headers["Content-Length"] = AttachHTTP.max_file_size + 1 |
| 310 |
results = AttachHTTP.parse_url("http://localhost/toobig.jpg") |
| 311 |
assert isinstance(results, dict) |
| 312 |
attachment = AttachHTTP(**results) |
| 313 |
|
| 314 |
assert not attachment |
| 315 |
assert isinstance(attachment.url(), str) is True |
| 316 |
|
| 317 |
|
| 318 |
assert re.search(r"[?&]mime=", attachment.url()) is None |
| 319 |
assert re.search(r"[?&]name=", attachment.url()) is None |
| 320 |
assert attachment.mimetype is None |
| 321 |
assert attachment.name is None |
| 322 |
assert len(attachment) == 0 |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
del dummy_response.headers["Content-Length"] |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
results = AttachHTTP.parse_url("http://localhost/no-length.gif") |
| 332 |
assert isinstance(results, dict) |
| 333 |
attachment = AttachHTTP(**results) |
| 334 |
assert isinstance(attachment.url(), str) is True |
| 335 |
|
| 336 |
|
| 337 |
assert re.search(r"[?&]mime=", attachment.url()) is None |
| 338 |
assert re.search(r"[?&]name=", attachment.url()) is None |
| 339 |
|
| 340 |
|
| 341 |
assert attachment.mimetype == "text/plain" |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
assert attachment.name == "no-length.gif" |
| 346 |
assert attachment |
| 347 |
assert len(attachment) == getsize(path) |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
max_file_size = AttachHTTP.max_file_size |
| 352 |
AttachHTTP.max_file_size = getsize(path) |
| 353 |
|
| 354 |
dummy_response.headers["Content-Disposition"] = ( |
| 355 |
'attachment; filename="myimage.gif"' |
| 356 |
) |
| 357 |
|
| 358 |
|
| 359 |
del dummy_response.headers["Content-Type"] |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
results = AttachHTTP.parse_url("http://user@localhost/ignore-filename.gif") |
| 364 |
assert isinstance(results, dict) |
| 365 |
attachment = AttachHTTP(**results) |
| 366 |
assert isinstance(attachment.url(), str) is True |
| 367 |
|
| 368 |
|
| 369 |
assert re.search(r"[?&]mime=", attachment.url()) is None |
| 370 |
assert re.search(r"[?&]name=", attachment.url()) is None |
| 371 |
assert attachment.mimetype == "image/gif" |
| 372 |
|
| 373 |
|
| 374 |
assert attachment.name == "myimage.gif" |
| 375 |
assert attachment |
| 376 |
assert len(attachment) == getsize(path) |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
AttachHTTP.max_file_size = getsize(path) - 1 |
| 382 |
results = AttachHTTP.parse_url("http://localhost/toobig.jpg") |
| 383 |
assert isinstance(results, dict) |
| 384 |
attachment = AttachHTTP(**results) |
| 385 |
|
| 386 |
assert not attachment |
| 387 |
assert isinstance(attachment.url(), str) is True |
| 388 |
|
| 389 |
|
| 390 |
assert re.search(r"[?&]mime=", attachment.url()) is None |
| 391 |
assert re.search(r"[?&]name=", attachment.url()) is None |
| 392 |
assert attachment.mimetype is None |
| 393 |
assert attachment.name is None |
| 394 |
assert len(attachment) == 0 |
| 395 |
|
| 396 |
|
| 397 |
AttachHTTP.max_file_size = 0 |
| 398 |
results = AttachHTTP.parse_url("http://user@localhost") |
| 399 |
assert isinstance(results, dict) |
| 400 |
attachment = AttachHTTP(**results) |
| 401 |
assert isinstance(attachment.url(), str) is True |
| 402 |
|
| 403 |
|
| 404 |
assert re.search(r"[?&]mime=", attachment.url()) is None |
| 405 |
assert re.search(r"[?&]name=", attachment.url()) is None |
| 406 |
assert attachment.mimetype == "image/gif" |
| 407 |
|
| 408 |
|
| 409 |
assert attachment.name == "myimage.gif" |
| 410 |
assert attachment |
| 411 |
assert len(attachment) == getsize(path) |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
dummy_response.headers = {"Content-Length": "invalid"} |
| 416 |
results = AttachHTTP.parse_url("http://localhost/invalid-length.gif") |
| 417 |
assert isinstance(results, dict) |
| 418 |
attachment = AttachHTTP(**results) |
| 419 |
assert isinstance(attachment.url(), str) is True |
| 420 |
|
| 421 |
|
| 422 |
assert re.search(r"[?&]mime=", attachment.url()) is None |
| 423 |
assert re.search(r"[?&]name=", attachment.url()) is None |
| 424 |
assert attachment.mimetype == "image/gif" |
| 425 |
|
| 426 |
|
| 427 |
assert attachment.name == "invalid-length.gif" |
| 428 |
assert attachment |
| 429 |
|
| 430 |
|
| 431 |
dummy_response.headers = {} |
| 432 |
results = AttachHTTP.parse_url("http://user@localhost") |
| 433 |
assert isinstance(results, dict) |
| 434 |
attachment = AttachHTTP(**results) |
| 435 |
|
| 436 |
assert attachment |
| 437 |
assert isinstance(attachment.url(), str) is True |
| 438 |
|
| 439 |
|
| 440 |
assert re.search(r"[?&]mime=", attachment.url()) is None |
| 441 |
assert re.search(r"[?&]name=", attachment.url()) is None |
| 442 |
|
| 443 |
|
| 444 |
attachment.detected_name = None |
| 445 |
assert attachment.mimetype == attachment.unknown_mimetype |
| 446 |
assert attachment.name.startswith(AttachHTTP.unknown_filename) |
| 447 |
assert len(attachment) == getsize(path) |
| 448 |
|
| 449 |
|
| 450 |
mock_get.return_value = None |
| 451 |
for exception_ in REQUEST_EXCEPTIONS: |
| 452 |
aa = AppriseAttachment.instantiate( |
| 453 |
"http://localhost/exception.gif?cache=30" |
| 454 |
) |
| 455 |
assert isinstance(aa, AttachHTTP) |
| 456 |
|
| 457 |
mock_get.side_effect = exception_ |
| 458 |
assert not aa |
| 459 |
|
| 460 |
|
| 461 |
AttachHTTP.max_file_size = max_file_size |
| 462 |
|
| 463 |
|
| 464 |
mock_get.side_effect = None |
| 465 |
mock_get.return_value = DummyResponse() |
| 466 |
|
| 467 |
|
| 468 |
response = requests.Request() |
| 469 |
response.status_code = requests.codes.ok |
| 470 |
response.content = "" |
| 471 |
mock_request.return_value = response |
| 472 |
|
| 473 |
mock_get.reset_mock() |
| 474 |
mock_request.reset_mock() |
| 475 |
assert mock_get.call_count == 0 |
| 476 |
|
| 477 |
apobj = Apprise() |
| 478 |
assert apobj.add("form://localhost") |
| 479 |
assert apobj.add("json://localhost") |
| 480 |
assert apobj.add("xml://localhost") |
| 481 |
assert len(apobj) == 3 |
| 482 |
assert ( |
| 483 |
apobj.notify( |
| 484 |
body="one attachment split 3 times", |
| 485 |
attach="http://localhost/test.gif", |
| 486 |
) |
| 487 |
is True |
| 488 |
) |
| 489 |
|
| 490 |
|
| 491 |
assert mock_request.call_count == 3 |
| 492 |
|
| 493 |
assert mock_get.call_count == 1 |
| 494 |
|
| 495 |
mock_get.reset_mock() |
| 496 |
mock_request.reset_mock() |
| 497 |
apobj = Apprise() |
| 498 |
for n in range(10): |
| 499 |
assert apobj.add(f"json://localhost?:entry={n}&method=post") |
| 500 |
assert apobj.add(f"form://localhost?:entry={n}&method=post") |
| 501 |
assert apobj.add(f"xml://localhost?:entry={n}&method=post") |
| 502 |
|
| 503 |
assert ( |
| 504 |
apobj.notify( |
| 505 |
body="one attachment split 30 times", |
| 506 |
attach="http://localhost/test.gif", |
| 507 |
) |
| 508 |
is True |
| 509 |
) |
| 510 |
|
| 511 |
|
| 512 |
assert mock_request.call_count == 30 |
| 513 |
|
| 514 |
assert mock_get.call_count == 1 |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
mock_get.reset_mock() |
| 520 |
mock_request.reset_mock() |
| 521 |
|
| 522 |
AttachHTTP.max_file_size = getsize(path) |
| 523 |
|
| 524 |
dummy_response.headers["Content-Disposition"] = ( |
| 525 |
'attachment; filename="myimage.gif"' |
| 526 |
) |
| 527 |
results = AttachHTTP.parse_url("http://user@localhost/filename.gif") |
| 528 |
assert isinstance(results, dict) |
| 529 |
obj = AttachHTTP(**results) |
| 530 |
|
| 531 |
|
| 532 |
assert isinstance(obj.base64(), str) |
| 533 |
|
| 534 |
assert isinstance(obj.base64(encoding=None), bytes) |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
class RedirectResponse: |
| 539 |
"""Simulates a 301 response returned when allow_redirects=False.""" |
| 540 |
|
| 541 |
status_code = requests.codes.moved_permanently |
| 542 |
headers: ClassVar[dict[str, str]] = {} |
| 543 |
|
| 544 |
def raise_for_status(self): |
| 545 |
return |
| 546 |
|
| 547 |
def __enter__(self): |
| 548 |
return self |
| 549 |
|
| 550 |
def __exit__(self, *args, **kwargs): |
| 551 |
return |
| 552 |
|
| 553 |
mock_get.return_value = RedirectResponse() |
| 554 |
results = AttachHTTP.parse_url( |
| 555 |
"http://user@localhost/filename.gif?redirect=no" |
| 556 |
) |
| 557 |
assert isinstance(results, dict) |
| 558 |
obj_no_redir = AttachHTTP(**results) |
| 559 |
assert obj_no_redir.redirects is False |
| 560 |
|
| 561 |
assert obj_no_redir.download() is False |
| 562 |
|
| 563 |
mock_get.return_value = dummy_response |
| 564 |
|
| 565 |
|
| 566 |
with mock.patch( |
| 567 |
"builtins.open", |
| 568 |
new_callable=mock.mock_open, |
| 569 |
read_data="mocked file content", |
| 570 |
) as mock_file: |
| 571 |
mock_file.side_effect = FileNotFoundError |
| 572 |
with pytest.raises(exception.AppriseFileNotFound): |
| 573 |
obj.base64() |
| 574 |
|
| 575 |
mock_file.side_effect = OSError |
| 576 |
with pytest.raises(exception.AppriseDiskIOError): |
| 577 |
obj.base64() |