From 586c6113d04c718426fd89192ba89465f27e7cfe Mon Sep 17 00:00:00 2001 From: Chris Caron Date: Sat, 7 Dec 2024 16:46:37 -0500 Subject: [PATCH] attachment test cases improved --- apprise/attachment/base.py | 29 +++++++++++++------------ apprise/attachment/file.py | 6 +++++- test/test_attach_file.py | 44 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 15 deletions(-) diff --git a/apprise/attachment/base.py b/apprise/attachment/base.py index 4fa70806bf..ed938a9848 100644 --- a/apprise/attachment/base.py +++ b/apprise/attachment/base.py @@ -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() diff --git a/apprise/attachment/file.py b/apprise/attachment/file.py index e24e1fbede..e23f5b6bb0 100644 --- a/apprise/attachment/file.py +++ b/apprise/attachment/file.py @@ -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 \ diff --git a/test/test_attach_file.py b/test/test_attach_file.py index 8c3697846d..44209f6c44 100644 --- a/test/test_attach_file.py +++ b/test/test_attach_file.py @@ -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,13 @@ def test_attach_file(): aa = AppriseAttachment(location=ContentLocation.HOSTED) assert aa.add(path) is False + response = AppriseAttachment.instantiate(path) + assert len(response) > 0 + + # Test the inability to get our file size + with mock.patch('os.path.getsize', side_effect=(True, OSError)): + assert len(response) == 0 + def test_attach_file_base64(): """