| 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 re |
| 31 |
import urllib |
| 32 |
|
| 33 |
import pytest |
| 34 |
|
| 35 |
from apprise import AppriseAttachment, exception |
| 36 |
from apprise.attachment.base import AttachBase |
| 37 |
from apprise.attachment.memory import AttachMemory |
| 38 |
from apprise.common import ContentLocation |
| 39 |
|
| 40 |
logging.disable(logging.CRITICAL) |
| 41 |
|
| 42 |
|
| 43 |
def test_attach_memory_parse_url(): |
| 44 |
""" |
| 45 |
API: AttachMemory().parse_url() |
| 46 |
|
| 47 |
""" |
| 48 |
|
| 49 |
|
| 50 |
assert AttachMemory.parse_url(object) is None |
| 51 |
|
| 52 |
|
| 53 |
assert AttachMemory.parse_url("memory://") |
| 54 |
|
| 55 |
|
| 56 |
mem = AttachMemory(content="string") |
| 57 |
|
| 58 |
assert mem.mimetype == "text/plain" |
| 59 |
|
| 60 |
assert re.match(r"^[a-z0-9-]+\.txt$", mem.name, re.I) |
| 61 |
|
| 62 |
|
| 63 |
with mem as fp: |
| 64 |
assert fp.getbuffer().nbytes == len(mem) |
| 65 |
|
| 66 |
|
| 67 |
mem = AttachMemory( |
| 68 |
content="<html/>", name="test.html", mimetype="text/html" |
| 69 |
) |
| 70 |
|
| 71 |
assert mem.mimetype == "text/html" |
| 72 |
assert mem.name == "test.html" |
| 73 |
|
| 74 |
|
| 75 |
assert mem.download() |
| 76 |
|
| 77 |
with pytest.raises(TypeError): |
| 78 |
|
| 79 |
AttachMemory(content=3) |
| 80 |
|
| 81 |
|
| 82 |
pointer = mem.open() |
| 83 |
assert pointer.read() == b"<html/>" |
| 84 |
|
| 85 |
|
| 86 |
mem = AttachMemory(content=b"binary-data", name="raw.dat") |
| 87 |
|
| 88 |
assert mem.mimetype == "application/octet-stream" |
| 89 |
assert mem.name == "raw.dat" |
| 90 |
|
| 91 |
|
| 92 |
mem = AttachMemory(content=b"binary-data") |
| 93 |
|
| 94 |
assert mem.mimetype == "application/octet-stream" |
| 95 |
|
| 96 |
assert re.match(r"^[a-z0-9-]+\.dat$", mem.name, re.I) |
| 97 |
|
| 98 |
|
| 99 |
def test_attach_memory(): |
| 100 |
""" |
| 101 |
API: AttachMemory() |
| 102 |
|
| 103 |
""" |
| 104 |
|
| 105 |
fname = "testfile" |
| 106 |
url = f"memory:///ignored/path/{fname}" |
| 107 |
|
| 108 |
|
| 109 |
response = AppriseAttachment.instantiate(url) |
| 110 |
assert isinstance(response, AttachMemory) |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
assert response.path is None |
| 115 |
assert bool(response) is False |
| 116 |
|
| 117 |
with response as memobj: |
| 118 |
memobj.write(b"content") |
| 119 |
|
| 120 |
|
| 121 |
assert response.name == fname |
| 122 |
assert response.path == response.name |
| 123 |
assert response.mimetype == "application/octet-stream" |
| 124 |
assert bool(response) is True |
| 125 |
|
| 126 |
|
| 127 |
fname_in_url = urllib.parse.quote(response.name) |
| 128 |
assert response.url().startswith(f"memory://{fname_in_url}") |
| 129 |
|
| 130 |
|
| 131 |
assert re.search(r"[?&]mime=", response.url()) is not None |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
response = AppriseAttachment.instantiate(url) |
| 144 |
assert isinstance(response, AttachMemory) |
| 145 |
with response as memobj: |
| 146 |
memobj.write(b"content") |
| 147 |
|
| 148 |
response.location = ContentLocation.INACCESSIBLE |
| 149 |
assert response.path is None |
| 150 |
|
| 151 |
assert response.download() is False |
| 152 |
|
| 153 |
|
| 154 |
response = AppriseAttachment.instantiate(url) |
| 155 |
assert isinstance(response, AttachMemory) |
| 156 |
with response as memobj: |
| 157 |
memobj.write(b"content") |
| 158 |
|
| 159 |
|
| 160 |
response = AppriseAttachment.instantiate(url) |
| 161 |
assert isinstance(response, AttachMemory) |
| 162 |
with response as memobj: |
| 163 |
memobj.write(b"content") |
| 164 |
|
| 165 |
|
| 166 |
prev_value = AttachBase.max_file_size |
| 167 |
AttachBase.max_file_size = len(response) - 1 |
| 168 |
|
| 169 |
assert response.path is None |
| 170 |
assert response.download() is False |
| 171 |
|
| 172 |
|
| 173 |
AttachBase.max_file_size = prev_value |
| 174 |
|
| 175 |
response = AppriseAttachment.instantiate( |
| 176 |
"memory://apprise-file.gif?mime=image/gif" |
| 177 |
) |
| 178 |
assert isinstance(response, AttachMemory) |
| 179 |
with response as memobj: |
| 180 |
memobj.write(b"content") |
| 181 |
|
| 182 |
assert response.name == "apprise-file.gif" |
| 183 |
assert response.path == response.name |
| 184 |
assert response.mimetype == "image/gif" |
| 185 |
|
| 186 |
|
| 187 |
assert re.search(r"[?&]mime=", response.url()) is not None |
| 188 |
assert "image/gif" in response.url() |
| 189 |
|
| 190 |
|
| 191 |
response = AppriseAttachment.instantiate( |
| 192 |
"memory://{}?mime={}&name={}".format( |
| 193 |
"ignored.gif", "image/jpeg", "test.jpeg" |
| 194 |
) |
| 195 |
) |
| 196 |
assert isinstance(response, AttachMemory) |
| 197 |
with response as memobj: |
| 198 |
memobj.write(b"content") |
| 199 |
|
| 200 |
assert response.name == "test.jpeg" |
| 201 |
assert response.path == response.name |
| 202 |
assert response.mimetype == "image/jpeg" |
| 203 |
|
| 204 |
assert re.search(r"[?&]mime=image/jpeg", response.url(), re.I) |
| 205 |
assert response.url().startswith("memory://test.jpeg") |
| 206 |
|
| 207 |
|
| 208 |
aa = AppriseAttachment(location=ContentLocation.HOSTED) |
| 209 |
assert aa.add(response) is False |
| 210 |
|
| 211 |
|
| 212 |
assert isinstance(response.base64(), str) |
| 213 |
|
| 214 |
assert isinstance(response.base64(encoding=None), bytes) |
| 215 |
|
| 216 |
response.invalidate() |
| 217 |
with pytest.raises(exception.AppriseFileNotFound): |
| 218 |
response.base64() |