| 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 os.path import dirname, join |
| 31 |
import re |
| 32 |
import time |
| 33 |
from unittest import mock |
| 34 |
import urllib |
| 35 |
|
| 36 |
import pytest |
| 37 |
|
| 38 |
from apprise import AppriseAttachment, exception |
| 39 |
from apprise.attachment.base import AttachBase |
| 40 |
from apprise.attachment.file import AttachFile |
| 41 |
from apprise.common import ContentLocation |
| 42 |
|
| 43 |
logging.disable(logging.CRITICAL) |
| 44 |
|
| 45 |
TEST_VAR_DIR = join(dirname(__file__), "var") |
| 46 |
|
| 47 |
|
| 48 |
def test_attach_file_parse_url(): |
| 49 |
""" |
| 50 |
API: AttachFile().parse_url() |
| 51 |
|
| 52 |
""" |
| 53 |
|
| 54 |
|
| 55 |
assert AttachFile.parse_url("garbage://") is None |
| 56 |
|
| 57 |
|
| 58 |
assert AttachFile.parse_url("file://") is None |
| 59 |
|
| 60 |
|
| 61 |
def test_file_expiry(tmpdir): |
| 62 |
""" |
| 63 |
API: AttachFile Expiry |
| 64 |
""" |
| 65 |
path = join(TEST_VAR_DIR, "apprise-test.gif") |
| 66 |
image = tmpdir.mkdir("apprise_file").join("test.jpg") |
| 67 |
with open(path, "rb") as data: |
| 68 |
image.write(data) |
| 69 |
|
| 70 |
aa = AppriseAttachment.instantiate(str(image), cache=30) |
| 71 |
|
| 72 |
|
| 73 |
assert aa.exists() |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
assert aa.exists() |
| 79 |
|
| 80 |
with mock.patch("time.time", return_value=time.time() + 31): |
| 81 |
|
| 82 |
|
| 83 |
assert aa.exists() |
| 84 |
|
| 85 |
with mock.patch("time.time", side_effect=OSError): |
| 86 |
|
| 87 |
assert aa.exists() |
| 88 |
|
| 89 |
|
| 90 |
def test_attach_mimetype(): |
| 91 |
""" |
| 92 |
API: AttachFile MimeType() |
| 93 |
|
| 94 |
""" |
| 95 |
|
| 96 |
path = join(TEST_VAR_DIR, "apprise-test.gif") |
| 97 |
response = AppriseAttachment.instantiate(path) |
| 98 |
assert isinstance(response, AttachFile) |
| 99 |
assert response.path == path |
| 100 |
assert response.name == "apprise-test.gif" |
| 101 |
assert response.mimetype == "image/gif" |
| 102 |
|
| 103 |
|
| 104 |
response._mimetype = None |
| 105 |
response.detected_mimetype = None |
| 106 |
|
| 107 |
assert response.mimetype == "image/gif" |
| 108 |
|
| 109 |
response._mimetype = None |
| 110 |
response.detected_mimetype = None |
| 111 |
with mock.patch("mimetypes.guess_type", side_effect=TypeError): |
| 112 |
assert response.mimetype == "application/octet-stream" |
| 113 |
|
| 114 |
|
| 115 |
def test_attach_file(): |
| 116 |
""" |
| 117 |
API: AttachFile() |
| 118 |
|
| 119 |
""" |
| 120 |
|
| 121 |
path = join(TEST_VAR_DIR, "apprise-test.gif") |
| 122 |
response = AppriseAttachment.instantiate(path) |
| 123 |
assert isinstance(response, AttachFile) |
| 124 |
assert response.path == path |
| 125 |
assert response.name == "apprise-test.gif" |
| 126 |
assert response.mimetype == "image/gif" |
| 127 |
|
| 128 |
|
| 129 |
assert response.download() |
| 130 |
|
| 131 |
with mock.patch("os.path.isfile", side_effect=OSError): |
| 132 |
assert response.exists() is False |
| 133 |
|
| 134 |
with mock.patch("os.path.isfile", return_value=False): |
| 135 |
assert response.exists() is False |
| 136 |
|
| 137 |
|
| 138 |
assert response.exists() is True |
| 139 |
response.cache = True |
| 140 |
|
| 141 |
assert response.exists() is True |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
path_in_url = urllib.parse.quote(path) |
| 146 |
assert response.url().startswith(f"file://{path_in_url}") |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
assert re.search(r"[?&]mime=", response.url()) is None |
| 151 |
assert re.search(r"[?&]name=", response.url()) is None |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
response = AppriseAttachment.instantiate(path) |
| 164 |
assert isinstance(response, AttachFile) |
| 165 |
response.location = ContentLocation.INACCESSIBLE |
| 166 |
assert response.path is None |
| 167 |
|
| 168 |
assert response.download() is False |
| 169 |
|
| 170 |
|
| 171 |
response = AppriseAttachment.instantiate(path) |
| 172 |
assert isinstance(response, AttachFile) |
| 173 |
with mock.patch("os.path.getsize", return_value=AttachBase.max_file_size): |
| 174 |
|
| 175 |
assert response.path == path |
| 176 |
|
| 177 |
|
| 178 |
response = AppriseAttachment.instantiate(path) |
| 179 |
assert isinstance(response, AttachFile) |
| 180 |
with mock.patch( |
| 181 |
"os.path.getsize", return_value=AttachBase.max_file_size + 1 |
| 182 |
): |
| 183 |
|
| 184 |
assert response.path is None |
| 185 |
|
| 186 |
|
| 187 |
response = AppriseAttachment.instantiate(path) |
| 188 |
assert isinstance(response, AttachFile) |
| 189 |
with mock.patch("os.path.isfile", return_value=False): |
| 190 |
|
| 191 |
assert response.path is None |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
response = AppriseAttachment.instantiate(path) |
| 198 |
assert response.name == "apprise-test.gif" |
| 199 |
assert response.path == path |
| 200 |
assert response.mimetype == "image/gif" |
| 201 |
|
| 202 |
|
| 203 |
assert re.search(r"[?&]mime=", response.url()) is None |
| 204 |
assert re.search(r"[?&]name=", response.url()) is None |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
response = AppriseAttachment.instantiate(path) |
| 209 |
assert isinstance(response, AttachFile) |
| 210 |
with mock.patch("os.path.isfile", return_value=False): |
| 211 |
|
| 212 |
assert response.name is None |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
response = AppriseAttachment.instantiate(path) |
| 219 |
assert response.mimetype == "image/gif" |
| 220 |
assert response.name == "apprise-test.gif" |
| 221 |
assert response.path == path |
| 222 |
|
| 223 |
|
| 224 |
assert re.search(r"[?&]mime=", response.url()) is None |
| 225 |
assert re.search(r"[?&]name=", response.url()) is None |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
response = AppriseAttachment.instantiate(path) |
| 230 |
assert isinstance(response, AttachFile) |
| 231 |
with mock.patch("os.path.isfile", return_value=False): |
| 232 |
|
| 233 |
assert response.mimetype is None |
| 234 |
assert response.name is None |
| 235 |
assert response.path is None |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
response = AppriseAttachment.instantiate( |
| 240 |
"file://{}?mime={}&name={}".format(path, "image/jpeg", "test.jpeg") |
| 241 |
) |
| 242 |
assert isinstance(response, AttachFile) |
| 243 |
assert response.path == path |
| 244 |
assert response.name == "test.jpeg" |
| 245 |
assert response.mimetype == "image/jpeg" |
| 246 |
assert re.search(r"[?&]mime=image/jpeg", response.url(), re.I) |
| 247 |
assert re.search(r"[?&]name=test\.jpeg", response.url(), re.I) |
| 248 |
|
| 249 |
|
| 250 |
aa = AppriseAttachment(location=ContentLocation.HOSTED) |
| 251 |
|
| 252 |
assert bool(aa) is False |
| 253 |
assert aa.sync() is False |
| 254 |
|
| 255 |
|
| 256 |
assert aa.sync(abort_if_empty=False) is True |
| 257 |
assert aa.add(path) is False |
| 258 |
|
| 259 |
response = AppriseAttachment.instantiate(path) |
| 260 |
assert len(response) > 0 |
| 261 |
|
| 262 |
|
| 263 |
assert response.download() |
| 264 |
|
| 265 |
|
| 266 |
with mock.patch("os.path.getsize", side_effect=(0, OSError)): |
| 267 |
assert len(response) == 0 |
| 268 |
|
| 269 |
|
| 270 |
assert response.download() |
| 271 |
with mock.patch("os.path.isfile", return_value=True): |
| 272 |
response.cache = True |
| 273 |
with mock.patch("os.path.getsize", side_effect=OSError): |
| 274 |
assert len(response) == 0 |
| 275 |
|
| 276 |
|
| 277 |
def test_attach_file_base64(): |
| 278 |
""" |
| 279 |
API: AttachFile() with base64 encoding |
| 280 |
|
| 281 |
""" |
| 282 |
|
| 283 |
|
| 284 |
path = join(TEST_VAR_DIR, "apprise-test.gif") |
| 285 |
response = AppriseAttachment.instantiate(path) |
| 286 |
assert isinstance(response, AttachFile) |
| 287 |
assert response.name == "apprise-test.gif" |
| 288 |
assert response.mimetype == "image/gif" |
| 289 |
|
| 290 |
|
| 291 |
assert isinstance(response.base64(), str) |
| 292 |
|
| 293 |
assert isinstance(response.base64(encoding=None), bytes) |
| 294 |
|
| 295 |
|
| 296 |
with ( |
| 297 |
mock.patch("os.path.isfile", return_value=False), |
| 298 |
pytest.raises(exception.AppriseFileNotFound), |
| 299 |
): |
| 300 |
response.base64() |
| 301 |
|
| 302 |
with mock.patch( |
| 303 |
"builtins.open", |
| 304 |
new_callable=mock.mock_open, |
| 305 |
read_data="mocked file content", |
| 306 |
) as mock_file: |
| 307 |
mock_file.side_effect = FileNotFoundError |
| 308 |
with pytest.raises(exception.AppriseFileNotFound): |
| 309 |
response.base64() |
| 310 |
|
| 311 |
mock_file.side_effect = OSError |
| 312 |
with pytest.raises(exception.AppriseDiskIOError): |
| 313 |
response.base64() |