Skip to content

Commit 86e8092

Browse files
authored
Improved 'empty attachment handling' when feature is disabled (#236)
1 parent 4c038de commit 86e8092

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

apprise_api/api/tests/test_attachment.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
from unittest.mock import mock_open
2929
from ..utils import Attachment, HTTPAttachment
3030
from ..utils import parse_attachments
31+
from ..urlfilter import AppriseURLFilter
32+
from .. import utils
3133
from django.test.utils import override_settings
34+
from django.conf import settings
3235
from tempfile import TemporaryDirectory
3336
from shutil import rmtree
3437
import base64
@@ -92,8 +95,25 @@ def test_form_file_attachment_parsing(self):
9295
"""
9396
Test the parsing of file attachments
9497
"""
95-
# Get ourselves a file to work with
98+
# Variation tests without any data
99+
result = parse_attachments(None, None)
100+
assert isinstance(result, list)
101+
assert len(result) == 0
102+
103+
result = parse_attachments([], [])
104+
assert isinstance(result, list)
105+
assert len(result) == 0
106+
107+
with override_settings(APPRISE_ATTACH_SIZE=0):
108+
result = parse_attachments(None, None)
109+
assert isinstance(result, list)
110+
assert len(result) == 0
96111

112+
result = parse_attachments([], [])
113+
assert isinstance(result, list)
114+
assert len(result) == 0
115+
116+
# Get ourselves a file to work with
97117
files_request = {
98118
'file1': SimpleUploadedFile(
99119
"attach.txt", b"content here", content_type="text/plain")
@@ -229,6 +249,20 @@ def test(*args, **kwargs):
229249
assert isinstance(result, list)
230250
assert len(result) == 3
231251

252+
with override_settings(APPRISE_ATTACH_DENY_URLS='*'):
253+
utils.ATTACH_URL_FILTER = AppriseURLFilter(
254+
settings.APPRISE_ATTACH_ALLOW_URLS,
255+
settings.APPRISE_ATTACH_DENY_URLS)
256+
257+
# We will fail to parse our URL based attachment
258+
with self.assertRaises(ValueError):
259+
parse_attachments(attachment_payload, {})
260+
261+
# Reload our configuration to default values
262+
utils.ATTACH_URL_FILTER = AppriseURLFilter(
263+
settings.APPRISE_ATTACH_ALLOW_URLS,
264+
settings.APPRISE_ATTACH_DENY_URLS)
265+
232266
# Garbage handling (integer, float, object, etc is invalid)
233267
attachment_payload = 5
234268
result = parse_attachments(attachment_payload, {})

apprise_api/api/utils.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,14 @@ def parse_attachments(attachment_payload, files_request):
257257
"""
258258
attachments = []
259259

260+
if settings.APPRISE_ATTACH_SIZE <= 0:
261+
if not (attachment_payload or files_request):
262+
# No further processing required
263+
return []
264+
265+
# Otherwise we need to raise an error
266+
raise ValueError("Attachment support has been disabled")
267+
260268
# Attachment Count
261269
count = sum([
262270
0 if not isinstance(attachment_payload, (set, tuple, list))
@@ -269,9 +277,6 @@ def parse_attachments(attachment_payload, files_request):
269277
attachment_payload = (attachment_payload, )
270278
count += 1
271279

272-
if settings.APPRISE_ATTACH_SIZE <= 0:
273-
raise ValueError("Attachment support has been disabled")
274-
275280
if settings.APPRISE_MAX_ATTACHMENTS > 0 and count > settings.APPRISE_MAX_ATTACHMENTS:
276281
raise ValueError(
277282
"There is a maximum of %d attachments" %

0 commit comments

Comments
 (0)