| 1 |
# BSD 2-Clause License |
| 2 |
# |
| 3 |
# Apprise - Push Notification Library. |
| 4 |
# Copyright (c) 2026, Chris Caron <[email protected]> |
| 5 |
# |
| 6 |
# Redistribution and use in source and binary forms, with or without |
| 7 |
# modification, are permitted provided that the following conditions are met: |
| 8 |
# |
| 9 |
# 1. Redistributions of source code must retain the above copyright notice, |
| 10 |
# this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 |
# this list of conditions and the following disclaimer in the documentation |
| 14 |
# and/or other materials provided with the distribution. |
| 15 |
# |
| 16 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 |
# POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
| 28 |
# Disable logging for a cleaner testing output |
| 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 |
# bad entry |
| 55 |
assert AttachFile.parse_url("garbage://") is None |
| 56 |
|
| 57 |
# no file path specified |
| 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 |
# Our file is now available |
| 73 |
assert aa.exists() |
| 74 |
|
| 75 |
# Our second call has the file already downloaded, but now compares |
| 76 |
# it's date against when we consider it to have expire. We're well |
| 77 |
# under 30 seconds here (our set value), so this will succeed |
| 78 |
assert aa.exists() |
| 79 |
|
| 80 |
with mock.patch("time.time", return_value=time.time() + 31): |
| 81 |
# This will force a re-download as our cache will have |
| 82 |
# expired |
| 83 |
assert aa.exists() |
| 84 |
|
| 85 |
with mock.patch("time.time", side_effect=OSError): |
| 86 |
# We will throw an exception |
| 87 |
assert aa.exists() |
| 88 |
|
| 89 |
|
| 90 |
def test_attach_mimetype(): |
| 91 |
""" |
| 92 |
API: AttachFile MimeType() |
| 93 |
|
| 94 |
""" |
| 95 |
# Simple gif test |
| 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 |
# Force mimetype |
| 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 |
# Simple gif test |
| 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 |
# Download is successful and has already been called by now; below pulls |
| 128 |
# results from cache |
| 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 |
# Test that our file exists |
| 138 |
assert response.exists() is True |
| 139 |
response.cache = True |
| 140 |
# Leverage always-cached flag |
| 141 |
assert response.exists() is True |
| 142 |
|
| 143 |
# On Windows, it is: |
| 144 |
# `file://D%3A%5Ca%5Capprise%5Capprise%5Ctest%5Cvar%5Capprise-test.gif` |
| 145 |
path_in_url = urllib.parse.quote(path) |
| 146 |
assert response.url().startswith(f"file://{path_in_url}") |
| 147 |
|
| 148 |
# No mime-type and/or filename over-ride was specified, so it |
| 149 |
# won't show up in the generated URL |
| 150 |
assert re.search(r"[?&]mime=", response.url()) is None |
| 151 |
assert re.search(r"[?&]name=", response.url()) is None |
| 152 |
|
| 153 |
# Test case where location is simply set to INACCESSIBLE |
| 154 |
# Below is a bad example, but it proves the section of code properly works. |
| 155 |
# Ideally a server admin may wish to just disable all File based |
| 156 |
# attachments entirely. In this case, they simply just need to change the |
| 157 |
# global singleton at the start of their program like: |
| 158 |
# |
| 159 |
# import apprise |
| 160 |
# apprise.attachment.AttachFile.location = \ |
| 161 |
# apprise.ContentLocation.INACCESSIBLE |
| 162 |
# |
| 163 |
response = AppriseAttachment.instantiate(path) |
| 164 |
assert isinstance(response, AttachFile) |
| 165 |
response.location = ContentLocation.INACCESSIBLE |
| 166 |
assert response.path is None |
| 167 |
# Downloads just don't work period |
| 168 |
assert response.download() is False |
| 169 |
|
| 170 |
# File handling (even if image is set to maxium allowable) |
| 171 |
response = AppriseAttachment.instantiate(path) |
| 172 |
assert isinstance(response, AttachFile) |
| 173 |
with mock.patch("os.path.getsize", return_value=AttachBase.max_file_size): |
| 174 |
# It will still work |
| 175 |
assert response.path == path |
| 176 |
|
| 177 |
# File handling when size is to large |
| 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 |
# We can't work in this case |
| 184 |
assert response.path is None |
| 185 |
|
| 186 |
# File handling when image is not available |
| 187 |
response = AppriseAttachment.instantiate(path) |
| 188 |
assert isinstance(response, AttachFile) |
| 189 |
with mock.patch("os.path.isfile", return_value=False): |
| 190 |
# This triggers a full check and will fail the isfile() check |
| 191 |
assert response.path is None |
| 192 |
|
| 193 |
# The call to AttachBase.path automatically triggers a call to download() |
| 194 |
# but this same is done with a call to AttachBase.name as well. Above |
| 195 |
# test cases reference 'path' right after instantiation; here we reference |
| 196 |
# 'name' |
| 197 |
response = AppriseAttachment.instantiate(path) |
| 198 |
assert response.name == "apprise-test.gif" |
| 199 |
assert response.path == path |
| 200 |
assert response.mimetype == "image/gif" |
| 201 |
# No mime-type and/or filename over-ride was specified, so therefore it |
| 202 |
# won't show up in the generated URL |
| 203 |
assert re.search(r"[?&]mime=", response.url()) is None |
| 204 |
assert re.search(r"[?&]name=", response.url()) is None |
| 205 |
|
| 206 |
# continuation to cheking 'name' instead of 'path' first where our call |
| 207 |
# to download() fails |
| 208 |
response = AppriseAttachment.instantiate(path) |
| 209 |
assert isinstance(response, AttachFile) |
| 210 |
with mock.patch("os.path.isfile", return_value=False): |
| 211 |
# This triggers a full check and will fail the isfile() check |
| 212 |
assert response.name is None |
| 213 |
|
| 214 |
# The call to AttachBase.path automatically triggers a call to download() |
| 215 |
# but this same is done with a call to AttachBase.mimetype as well. Above |
| 216 |
# test cases reference 'path' right after instantiation; here we reference |
| 217 |
# 'mimetype' |
| 218 |
response = AppriseAttachment.instantiate(path) |
| 219 |
assert response.mimetype == "image/gif" |
| 220 |
assert response.name == "apprise-test.gif" |
| 221 |
assert response.path == path |
| 222 |
# No mime-type and/or filename over-ride was specified, so therefore it |
| 223 |
# won't show up in the generated URL |
| 224 |
assert re.search(r"[?&]mime=", response.url()) is None |
| 225 |
assert re.search(r"[?&]name=", response.url()) is None |
| 226 |
|
| 227 |
# continuation to cheking 'name' instead of 'path' first where our call |
| 228 |
# to download() fails |
| 229 |
response = AppriseAttachment.instantiate(path) |
| 230 |
assert isinstance(response, AttachFile) |
| 231 |
with mock.patch("os.path.isfile", return_value=False): |
| 232 |
# download() fails so we don't have a mimetpe |
| 233 |
assert response.mimetype is None |
| 234 |
assert response.name is None |
| 235 |
assert response.path is None |
| 236 |
# This triggers a full check and will fail the isfile() check |
| 237 |
|
| 238 |
# Force a mime-type and new name |
| 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 |
# Test hosted configuration and that we can't add a valid file |
| 250 |
aa = AppriseAttachment(location=ContentLocation.HOSTED) |
| 251 |
# No entries defined yet |
| 252 |
assert bool(aa) is False |
| 253 |
assert aa.sync() is False |
| 254 |
|
| 255 |
# Entry count does not impact sync if told to act that way |
| 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 |
# Get file |
| 263 |
assert response.download() |
| 264 |
|
| 265 |
# Test the inability to get our file size |
| 266 |
with mock.patch("os.path.getsize", side_effect=(0, OSError)): |
| 267 |
assert len(response) == 0 |
| 268 |
|
| 269 |
# get file again |
| 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 |
# Simple gif test |
| 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 |
# now test our base64 output |
| 291 |
assert isinstance(response.base64(), str) |
| 292 |
# No encoding if we choose |
| 293 |
assert isinstance(response.base64(encoding=None), bytes) |
| 294 |
|
| 295 |
# Error cases: |
| 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() |