attachment test cases improved

pull/1225/head
Chris Caron 2024-12-07 16:46:37 -05:00
parent 79aae7bdb1
commit f6bc8b6323
3 changed files with 74 additions and 15 deletions

View File

@ -269,25 +269,26 @@ class AttachBase(URLBase):
cache = self.template_args['cache']['default'] \ cache = self.template_args['cache']['default'] \
if self.cache is None else self.cache if self.cache is None else self.cache
if self.download_path and os.path.isfile(self.download_path) \ try:
and cache: if self.download_path and os.path.isfile(self.download_path) \
and cache:
# We have enough reason to look further into our cached content # We have enough reason to look further into our cached content
# and verify it has not expired. # and verify it has not expired.
if cache is True: if cache is True:
# return our fixed content as is; we will always cache it # return our fixed content as is; we will always cache it
return True return True
# Verify our cache time to determine whether we will get our # Verify our cache time to determine whether we will get our
# content again. # content again.
try: age_in_sec = \
age_in_sec = time.time() - os.stat(self.download_path).st_mtime time.time() - os.stat(self.download_path).st_mtime
if age_in_sec <= cache: if age_in_sec <= cache:
return True return True
except (OSError, IOError): except (OSError, IOError):
# The file is not present # The file is not present
pass pass
return False if not retrieve_if_missing else self.download() return False if not retrieve_if_missing else self.download()

View File

@ -101,7 +101,11 @@ class AttachFile(AttachBase):
# Ensure any existing content set has been invalidated # Ensure any existing content set has been invalidated
self.invalidate() 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 return False
if self.max_file_size > 0 and \ if self.max_file_size > 0 and \

View File

@ -89,6 +89,31 @@ def test_file_expiry(tmpdir):
assert aa.exists() 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(): def test_attach_file():
""" """
API: AttachFile() API: AttachFile()
@ -105,6 +130,18 @@ def test_attach_file():
# results from cache # results from cache
assert response.download() 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 # On Windows, it is `file://D%3A%5Ca%5Capprise%5Capprise%5Ctest%5Cvar%5Capprise-test.gif`. # noqa E501
# TODO: Review - is this correct? # TODO: Review - is this correct?
path_in_url = urllib.parse.quote(path) path_in_url = urllib.parse.quote(path)
@ -213,6 +250,23 @@ def test_attach_file():
aa = AppriseAttachment(location=ContentLocation.HOSTED) aa = AppriseAttachment(location=ContentLocation.HOSTED)
assert aa.add(path) is False 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(): def test_attach_file_base64():
""" """