mirror of https://github.com/caronc/apprise
attachment test cases improved
parent
79aae7bdb1
commit
f6bc8b6323
|
@ -269,25 +269,26 @@ class AttachBase(URLBase):
|
|||
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()
|
||||
|
||||
|
|
|
@ -101,7 +101,11 @@ class AttachFile(AttachBase):
|
|||
# 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 \
|
||||
|
|
|
@ -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()
|
||||
|
@ -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)
|
||||
|
@ -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():
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue