| 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 inspect import cleandoc |
| 29 |
import json |
| 30 |
|
| 31 |
|
| 32 |
import logging |
| 33 |
from os.path import dirname, getsize, join |
| 34 |
from unittest import mock |
| 35 |
|
| 36 |
import pytest |
| 37 |
import requests |
| 38 |
|
| 39 |
from apprise import Apprise, AppriseAsset, AttachmentManager |
| 40 |
from apprise.apprise_attachment import AppriseAttachment |
| 41 |
from apprise.attachment import AttachBase |
| 42 |
from apprise.common import ContentLocation |
| 43 |
from apprise.logger import LogCapture |
| 44 |
|
| 45 |
logging.disable(logging.CRITICAL) |
| 46 |
|
| 47 |
TEST_VAR_DIR = join(dirname(__file__), "var") |
| 48 |
|
| 49 |
|
| 50 |
A_MGR = AttachmentManager() |
| 51 |
|
| 52 |
|
| 53 |
def test_apprise_attachment(): |
| 54 |
""" |
| 55 |
API: AppriseAttachment basic testing |
| 56 |
|
| 57 |
""" |
| 58 |
|
| 59 |
|
| 60 |
aa = AppriseAttachment() |
| 61 |
|
| 62 |
|
| 63 |
assert len(aa) == 0 |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
assert not aa |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
aa = AppriseAttachment(asset=AppriseAsset(), cache=300) |
| 72 |
|
| 73 |
|
| 74 |
assert len(aa) == 0 |
| 75 |
|
| 76 |
|
| 77 |
path = join(TEST_VAR_DIR, "apprise-test.gif") |
| 78 |
assert aa.add(path) |
| 79 |
|
| 80 |
|
| 81 |
assert len(aa) == 1 |
| 82 |
|
| 83 |
|
| 84 |
assert aa[0].cache == 300 |
| 85 |
|
| 86 |
|
| 87 |
assert aa |
| 88 |
|
| 89 |
|
| 90 |
response = AppriseAttachment.instantiate(path, cache=True) |
| 91 |
assert isinstance(response, AttachBase) |
| 92 |
assert aa.add(response, asset=AppriseAsset()) |
| 93 |
|
| 94 |
|
| 95 |
assert len(aa) == 2 |
| 96 |
|
| 97 |
|
| 98 |
assert aa[1].cache is True |
| 99 |
|
| 100 |
|
| 101 |
aa = AppriseAttachment() |
| 102 |
|
| 103 |
|
| 104 |
attachments = ( |
| 105 |
path, |
| 106 |
f"file://{path}?name=newfilename.gif?cache=120", |
| 107 |
AppriseAttachment.instantiate( |
| 108 |
f"file://{path}?name=anotherfilename.gif", cache=100 |
| 109 |
), |
| 110 |
) |
| 111 |
|
| 112 |
|
| 113 |
assert aa.add(attachments, cache=False) |
| 114 |
|
| 115 |
|
| 116 |
assert len(aa) == 3 |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
assert aa[0].cache is False |
| 121 |
|
| 122 |
|
| 123 |
assert aa[1].cache is False |
| 124 |
assert aa[2].cache == 100 |
| 125 |
|
| 126 |
|
| 127 |
attachment = aa.pop() |
| 128 |
assert isinstance(attachment, AttachBase) |
| 129 |
|
| 130 |
assert attachment |
| 131 |
assert len(aa) == 2 |
| 132 |
assert attachment.path == path |
| 133 |
assert attachment.name == "anotherfilename.gif" |
| 134 |
assert attachment.mimetype == "image/gif" |
| 135 |
|
| 136 |
|
| 137 |
assert isinstance(aa[0], AttachBase) |
| 138 |
assert isinstance(aa[1], AttachBase) |
| 139 |
|
| 140 |
with pytest.raises(IndexError): |
| 141 |
aa[2] |
| 142 |
|
| 143 |
|
| 144 |
for count, a in enumerate(aa): |
| 145 |
assert isinstance(a, AttachBase) |
| 146 |
|
| 147 |
|
| 148 |
assert count < len(aa) |
| 149 |
|
| 150 |
|
| 151 |
expected_size = getsize(path) * len(aa) |
| 152 |
|
| 153 |
|
| 154 |
assert aa.size() == expected_size |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
aa = AppriseAttachment(attachments) |
| 159 |
|
| 160 |
|
| 161 |
assert len(aa) == 3 |
| 162 |
|
| 163 |
|
| 164 |
aa.clear() |
| 165 |
assert len(aa) == 0 |
| 166 |
assert not aa |
| 167 |
|
| 168 |
assert aa.add( |
| 169 |
AppriseAttachment.instantiate( |
| 170 |
f"file://{path}?name=andanother.png&cache=Yes" |
| 171 |
) |
| 172 |
) |
| 173 |
assert aa.add( |
| 174 |
AppriseAttachment.instantiate( |
| 175 |
f"file://{path}?name=andanother.png&cache=No" |
| 176 |
) |
| 177 |
) |
| 178 |
AppriseAttachment.instantiate( |
| 179 |
f"file://{path}?name=andanother.png&cache=600" |
| 180 |
) |
| 181 |
assert aa.add( |
| 182 |
AppriseAttachment.instantiate( |
| 183 |
f"file://{path}?name=andanother.png&cache=600" |
| 184 |
) |
| 185 |
) |
| 186 |
|
| 187 |
assert len(aa) == 3 |
| 188 |
assert aa[0].cache is True |
| 189 |
assert aa[1].cache is False |
| 190 |
assert aa[2].cache == 600 |
| 191 |
|
| 192 |
|
| 193 |
assert not aa.add( |
| 194 |
AppriseAttachment.instantiate( |
| 195 |
f"file://{path}?name=andanother.png&cache=-600" |
| 196 |
) |
| 197 |
) |
| 198 |
|
| 199 |
|
| 200 |
assert not aa.add( |
| 201 |
AppriseAttachment.instantiate( |
| 202 |
f"file://{path}?name=andanother.png", cache="invalid" |
| 203 |
) |
| 204 |
) |
| 205 |
|
| 206 |
|
| 207 |
assert len(aa) == 3 |
| 208 |
|
| 209 |
|
| 210 |
aa.clear() |
| 211 |
|
| 212 |
|
| 213 |
assert aa.add(None) is False |
| 214 |
assert aa.add(object()) is False |
| 215 |
assert aa.add(42) is False |
| 216 |
|
| 217 |
|
| 218 |
assert len(aa) == 0 |
| 219 |
|
| 220 |
|
| 221 |
attachments = ( |
| 222 |
None, |
| 223 |
object(), |
| 224 |
42, |
| 225 |
"garbage://", |
| 226 |
) |
| 227 |
|
| 228 |
|
| 229 |
assert aa.add(attachments) is False |
| 230 |
|
| 231 |
|
| 232 |
assert len(aa) == 0 |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
with pytest.raises(TypeError): |
| 237 |
AppriseAttachment("garbage://") |
| 238 |
|
| 239 |
|
| 240 |
aa = AppriseAttachment(location=ContentLocation.LOCAL) |
| 241 |
|
| 242 |
|
| 243 |
aa = AppriseAttachment(location=ContentLocation.HOSTED) |
| 244 |
assert len(aa) == 0 |
| 245 |
|
| 246 |
|
| 247 |
aa.add(attachments) |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
assert len(aa) == 0 |
| 252 |
|
| 253 |
|
| 254 |
aa = AppriseAttachment(location=ContentLocation.INACCESSIBLE) |
| 255 |
assert len(aa) == 0 |
| 256 |
|
| 257 |
|
| 258 |
aa.add(attachments) |
| 259 |
|
| 260 |
|
| 261 |
assert len(aa) == 0 |
| 262 |
|
| 263 |
with pytest.raises(TypeError): |
| 264 |
|
| 265 |
AppriseAttachment(location="invalid") |
| 266 |
|
| 267 |
|
| 268 |
aa = AppriseAttachment("file://non-existant-file.png") |
| 269 |
|
| 270 |
assert len(aa) == 1 |
| 271 |
|
| 272 |
assert aa |
| 273 |
|
| 274 |
|
| 275 |
assert not aa[0] |
| 276 |
|
| 277 |
|
| 278 |
assert len(aa[0]) == 0 |
| 279 |
|
| 280 |
|
| 281 |
assert aa.size() == 0 |
| 282 |
|
| 283 |
|
| 284 |
@mock.patch("requests.request") |
| 285 |
def test_apprise_attachment_truncate(mock_request): |
| 286 |
""" |
| 287 |
API: AppriseAttachment when truncation in place |
| 288 |
|
| 289 |
""" |
| 290 |
|
| 291 |
|
| 292 |
response = requests.Request() |
| 293 |
response.status_code = requests.codes.ok |
| 294 |
|
| 295 |
|
| 296 |
mock_request.return_value = response |
| 297 |
|
| 298 |
|
| 299 |
ap_obj = Apprise() |
| 300 |
|
| 301 |
|
| 302 |
ap_obj.add("json://localhost/?method=GET&overflow=truncate") |
| 303 |
|
| 304 |
|
| 305 |
aa = AppriseAttachment() |
| 306 |
|
| 307 |
|
| 308 |
assert len(aa) == 0 |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
assert not aa |
| 313 |
|
| 314 |
|
| 315 |
assert aa.add(join(TEST_VAR_DIR, "apprise-test.gif")) |
| 316 |
assert aa.add(join(TEST_VAR_DIR, "apprise-test.png")) |
| 317 |
|
| 318 |
assert mock_request.call_count == 0 |
| 319 |
assert ap_obj.notify(body="body", title="title", attach=aa) |
| 320 |
|
| 321 |
assert mock_request.call_count == 1 |
| 322 |
|
| 323 |
|
| 324 |
details = mock_request.call_args_list[0] |
| 325 |
assert details[0][0] == "GET" |
| 326 |
dataset = json.loads(details[1]["data"]) |
| 327 |
assert len(dataset["attachments"]) == 1 |
| 328 |
|
| 329 |
|
| 330 |
mock_request.reset_mock() |
| 331 |
|
| 332 |
|
| 333 |
ap_obj = Apprise() |
| 334 |
|
| 335 |
|
| 336 |
ap_obj.add("json://localhost/?method=GET&overflow=upstream") |
| 337 |
|
| 338 |
|
| 339 |
aa = AppriseAttachment() |
| 340 |
|
| 341 |
|
| 342 |
assert aa.add(join(TEST_VAR_DIR, "apprise-test.gif")) |
| 343 |
assert aa.add(join(TEST_VAR_DIR, "apprise-test.png")) |
| 344 |
|
| 345 |
assert mock_request.call_count == 0 |
| 346 |
assert ap_obj.notify(body="body", title="title", attach=aa) |
| 347 |
|
| 348 |
assert mock_request.call_count == 1 |
| 349 |
|
| 350 |
|
| 351 |
details = mock_request.call_args_list[0] |
| 352 |
assert details[0][0] == "GET" |
| 353 |
dataset = json.loads(details[1]["data"]) |
| 354 |
assert len(dataset["attachments"]) == 2 |
| 355 |
|
| 356 |
|
| 357 |
def test_apprise_attachment_instantiate(): |
| 358 |
""" |
| 359 |
API: AppriseAttachment.instantiate() |
| 360 |
|
| 361 |
""" |
| 362 |
assert ( |
| 363 |
AppriseAttachment.instantiate("file://?", suppress_exceptions=True) |
| 364 |
is None |
| 365 |
) |
| 366 |
|
| 367 |
assert ( |
| 368 |
AppriseAttachment.instantiate("invalid://?", suppress_exceptions=True) |
| 369 |
is None |
| 370 |
) |
| 371 |
|
| 372 |
class BadAttachType(AttachBase): |
| 373 |
def __init__(self, **kwargs): |
| 374 |
super().__init__(**kwargs) |
| 375 |
|
| 376 |
|
| 377 |
raise TypeError() |
| 378 |
|
| 379 |
|
| 380 |
A_MGR["bad"] = BadAttachType |
| 381 |
|
| 382 |
with pytest.raises(TypeError): |
| 383 |
AppriseAttachment.instantiate("bad://path", suppress_exceptions=False) |
| 384 |
|
| 385 |
|
| 386 |
assert ( |
| 387 |
AppriseAttachment.instantiate("bad://path", suppress_exceptions=True) |
| 388 |
is None |
| 389 |
) |
| 390 |
|
| 391 |
|
| 392 |
def test_attachment_matrix_dynamic_importing(tmpdir): |
| 393 |
""" |
| 394 |
API: Apprise() Attachment Matrix Importing |
| 395 |
|
| 396 |
""" |
| 397 |
|
| 398 |
|
| 399 |
suite = tmpdir.mkdir("apprise_attach_test_suite") |
| 400 |
suite.join("__init__.py").write("") |
| 401 |
|
| 402 |
module_name = "badattach" |
| 403 |
|
| 404 |
|
| 405 |
base = suite.mkdir(module_name) |
| 406 |
base.join("__init__.py").write("") |
| 407 |
|
| 408 |
|
| 409 |
base.join("AttachBadFile1.py").write( |
| 410 |
cleandoc(""" |
| 411 |
class AttachBadFile1: |
| 412 |
pass |
| 413 |
""") |
| 414 |
) |
| 415 |
|
| 416 |
|
| 417 |
base.join("AttachBadFile2.py").write( |
| 418 |
cleandoc(""" |
| 419 |
class BadClassName: |
| 420 |
pass |
| 421 |
""") |
| 422 |
) |
| 423 |
|
| 424 |
|
| 425 |
base.join("AttachBadFile3.py").write("""raise ImportError()""") |
| 426 |
|
| 427 |
|
| 428 |
base.join("AttachGoober.py").write( |
| 429 |
cleandoc(""" |
| 430 |
from apprise import AttachBase |
| 431 |
class AttachGoober(AttachBase): |
| 432 |
# This class tests the fact we have a new class name, but we're |
| 433 |
# trying to over-ride items previously used |
| 434 |
|
| 435 |
# The default simple (insecure) protocol |
| 436 |
protocol = 'http' |
| 437 |
|
| 438 |
# The default secure protocol |
| 439 |
secure_protocol = 'https' |
| 440 |
""") |
| 441 |
) |
| 442 |
|
| 443 |
|
| 444 |
base.join("AttachBugger.py").write( |
| 445 |
cleandoc(""" |
| 446 |
from apprise import AttachBase |
| 447 |
class AttachBugger(AttachBase): |
| 448 |
# This class tests the fact we have a new class name, but we're |
| 449 |
# trying to over-ride items previously used |
| 450 |
|
| 451 |
# The default simple (insecure) protocol |
| 452 |
protocol = ('http', 'bugger-test' ) |
| 453 |
|
| 454 |
# The default secure protocol |
| 455 |
secure_protocol = ('https', 'bugger-tests') |
| 456 |
""") |
| 457 |
) |
| 458 |
|
| 459 |
A_MGR.load_modules(path=str(base), name=module_name) |
| 460 |
|
| 461 |
|
| 462 |
def test_attachment_manager_no_import_failures(): |
| 463 |
"""Verify all native attach plugins load without TRACE import errors.""" |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
logging.disable(logging.NOTSET) |
| 468 |
|
| 469 |
|
| 470 |
A_MGR.unload_modules() |
| 471 |
|
| 472 |
with LogCapture(level=logging.TRACE) as stream: |
| 473 |
|
| 474 |
len(A_MGR) |
| 475 |
|
| 476 |
|
| 477 |
content = stream.getvalue() |
| 478 |
|
| 479 |
|
| 480 |
logging.disable(logging.CRITICAL) |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
failures = [ |
| 486 |
line for line in content.splitlines() if "import failed" in line |
| 487 |
] |
| 488 |
assert not failures, ( |
| 489 |
"Attachment plugin import failures detected:\n" + "\n".join(failures) |
| 490 |
) |
| 491 |
|
| 492 |
|
| 493 |
A_MGR.unload_modules() |