Skip to content

Commit 32aa534

Browse files
committed
Add test for file not found exception
1 parent 8e0916f commit 32aa534

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

s3file/middleware.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import logging
22
import os
33

4-
from botocore.exceptions import ClientError
54
from django.core.files.storage import default_storage
65

76
logger = logging.getLogger('s3file')
@@ -24,9 +23,9 @@ def __call__(self, request):
2423
def get_files_from_storage(paths):
2524
"""Return S3 file where the name does not include the path."""
2625
for path in paths:
27-
f = default_storage.open(path)
28-
f.name = os.path.basename(path)
2926
try:
27+
f = default_storage.open(path)
28+
f.name = os.path.basename(path)
3029
yield f
31-
except ClientError:
30+
except OSError:
3231
logger.exception("File not found: %s", path)

tests/test_middleware.py

+6
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,9 @@ def test_process_request(self, rf):
2626
S3FileMiddleware(lambda x: None)(request)
2727
assert request.FILES.getlist('file')
2828
assert request.FILES.get('file').read() == b's3file'
29+
30+
def test_process_request__no_file(self, rf, caplog):
31+
request = rf.post('/', data={'file': 'does_not_exist.txt', 's3file': 'file'})
32+
S3FileMiddleware(lambda x: None)(request)
33+
assert not request.FILES.getlist('file')
34+
assert 'File not found: does_not_exist.txt' in caplog.text

0 commit comments

Comments
 (0)