| 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 |
from unittest import mock |
| 31 |
|
| 32 |
import pytest |
| 33 |
|
| 34 |
from apprise.attachment.base import AttachBase |
| 35 |
|
| 36 |
logging.disable(logging.CRITICAL) |
| 37 |
|
| 38 |
|
| 39 |
def test_mimetype_initialization(): |
| 40 |
""" |
| 41 |
API: AttachBase() mimetype initialization |
| 42 |
|
| 43 |
""" |
| 44 |
with ( |
| 45 |
mock.patch("mimetypes.init") as mock_init, |
| 46 |
mock.patch("mimetypes.inited", True), |
| 47 |
): |
| 48 |
AttachBase() |
| 49 |
assert mock_init.call_count == 0 |
| 50 |
|
| 51 |
with ( |
| 52 |
mock.patch("mimetypes.init") as mock_init, |
| 53 |
mock.patch("mimetypes.inited", False), |
| 54 |
): |
| 55 |
AttachBase() |
| 56 |
assert mock_init.call_count == 1 |
| 57 |
|
| 58 |
|
| 59 |
def test_attach_base(): |
| 60 |
""" |
| 61 |
API: AttachBase() |
| 62 |
|
| 63 |
""" |
| 64 |
|
| 65 |
with pytest.raises(TypeError): |
| 66 |
AttachBase(**{"mimetype": "invalid"}) |
| 67 |
|
| 68 |
|
| 69 |
AttachBase(**{"mimetype": "image/png"}) |
| 70 |
|
| 71 |
|
| 72 |
obj = AttachBase() |
| 73 |
|
| 74 |
|
| 75 |
str(obj) |
| 76 |
|
| 77 |
|
| 78 |
with pytest.raises(NotImplementedError): |
| 79 |
obj.download() |
| 80 |
|
| 81 |
|
| 82 |
assert AttachBase.parse_url(url="invalid://") is None |
| 83 |
|
| 84 |
|
| 85 |
results = AttachBase.parse_url(url="file://relative/path") |
| 86 |
assert isinstance(results, dict) |
| 87 |
|
| 88 |
assert results.get("mimetype") is None |
| 89 |
|
| 90 |
|
| 91 |
results = AttachBase.parse_url(url="file://relative/path?mime=image/jpeg") |
| 92 |
assert isinstance(results, dict) |
| 93 |
|
| 94 |
assert results.get("mimetype") == "image/jpeg" |
| 95 |
|
| 96 |
assert str(results) |
| 97 |
|
| 98 |
|
| 99 |
def test_attach_base_parse_url_name_sanitization(): |
| 100 |
""" |
| 101 |
API: AttachBase.parse_url() ?name= sanitization |
| 102 |
|
| 103 |
""" |
| 104 |
|
| 105 |
results = AttachBase.parse_url( |
| 106 |
url="file://path/to/file?name=thumbnail.jpg" |
| 107 |
) |
| 108 |
assert isinstance(results, dict) |
| 109 |
assert results.get("name") == "thumbnail.jpg" |
| 110 |
|
| 111 |
|
| 112 |
results = AttachBase.parse_url(url="file://path/to/file?name=/etc/passwd") |
| 113 |
assert isinstance(results, dict) |
| 114 |
assert results.get("name") == "passwd" |
| 115 |
|
| 116 |
|
| 117 |
results = AttachBase.parse_url( |
| 118 |
url="file://path/to/file?name=../../secret.jpg" |
| 119 |
) |
| 120 |
assert isinstance(results, dict) |
| 121 |
assert results.get("name") == "secret.jpg" |
| 122 |
|
| 123 |
|
| 124 |
results = AttachBase.parse_url(url="file://path/to/file?name=") |
| 125 |
assert isinstance(results, dict) |
| 126 |
assert "name" not in results |
| 127 |
|
| 128 |
|
| 129 |
results = AttachBase.parse_url(url="file://path/to/file?name=%20%20%20") |
| 130 |
assert isinstance(results, dict) |
| 131 |
assert "name" not in results |
| 132 |
|
| 133 |
|
| 134 |
results = AttachBase.parse_url(url="file://path/to/file?name=/") |
| 135 |
assert isinstance(results, dict) |
| 136 |
assert "name" not in results |