Skip to content

Commit

Permalink
attachment test cases improved
Browse files Browse the repository at this point in the history
  • Loading branch information
caronc committed Dec 7, 2024
1 parent 79aae7b commit f6bc8b6
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 15 deletions.
29 changes: 15 additions & 14 deletions apprise/attachment/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,25 +269,26 @@ def exists(self, retrieve_if_missing=True):
cache = self.template_args['cache']['default'] \
if self.cache is None else self.cache

if self.download_path and os.path.isfile(self.download_path) \
and cache:
try:
if self.download_path and os.path.isfile(self.download_path) \
and cache:

# We have enough reason to look further into our cached content
# and verify it has not expired.
if cache is True:
# return our fixed content as is; we will always cache it
return True
# We have enough reason to look further into our cached content
# and verify it has not expired.
if cache is True:
# return our fixed content as is; we will always cache it
return True

# Verify our cache time to determine whether we will get our
# content again.
try:
age_in_sec = time.time() - os.stat(self.download_path).st_mtime
# Verify our cache time to determine whether we will get our
# content again.
age_in_sec = \
time.time() - os.stat(self.download_path).st_mtime
if age_in_sec <= cache:
return True

except (OSError, IOError):
# The file is not present
pass
except (OSError, IOError):
# The file is not present
pass

return False if not retrieve_if_missing else self.download()

Expand Down
6 changes: 5 additions & 1 deletion apprise/attachment/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ def download(self, **kwargs):
# Ensure any existing content set has been invalidated
self.invalidate()

if not os.path.isfile(self.dirty_path):
try:
if not os.path.isfile(self.dirty_path):
return False

except OSError:
return False

if self.max_file_size > 0 and \
Expand Down
54 changes: 54 additions & 0 deletions test/test_attach_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,31 @@ def test_file_expiry(tmpdir):
assert aa.exists()


def test_attach_mimetype():
"""
API: AttachFile MimeType()
"""
# Simple gif test
path = join(TEST_VAR_DIR, 'apprise-test.gif')
response = AppriseAttachment.instantiate(path)
assert isinstance(response, AttachFile)
assert response.path == path
assert response.name == 'apprise-test.gif'
assert response.mimetype == 'image/gif'

# Force mimetype
response._mimetype = None
response.detected_mimetype = None

assert response.mimetype == 'image/gif'

response._mimetype = None
response.detected_mimetype = None
with mock.patch('mimetypes.guess_type', side_effect=TypeError):
assert response.mimetype == 'application/octet-stream'


def test_attach_file():
"""
API: AttachFile()
Expand All @@ -105,6 +130,18 @@ def test_attach_file():
# results from cache
assert response.download()

with mock.patch('os.path.isfile', side_effect=OSError):
assert response.exists() is False

with mock.patch('os.path.isfile', return_value=False):
assert response.exists() is False

# Test that our file exists
assert response.exists() is True
response.cache = True
# Leverage always-cached flag
assert response.exists() is True

# On Windows, it is `file://D%3A%5Ca%5Capprise%5Capprise%5Ctest%5Cvar%5Capprise-test.gif`. # noqa E501
# TODO: Review - is this correct?
path_in_url = urllib.parse.quote(path)
Expand Down Expand Up @@ -213,6 +250,23 @@ def test_attach_file():
aa = AppriseAttachment(location=ContentLocation.HOSTED)
assert aa.add(path) is False

response = AppriseAttachment.instantiate(path)
assert len(response) > 0

# Get file
assert response.download()

# Test the inability to get our file size
with mock.patch('os.path.getsize', side_effect=(0, OSError)):
assert len(response) == 0

# get file again
assert response.download()
with mock.patch('os.path.isfile', return_value=True):
response.cache = True
with mock.patch('os.path.getsize', side_effect=OSError):
assert len(response) == 0


def test_attach_file_base64():
"""
Expand Down

0 comments on commit f6bc8b6

Please sign in to comment.