Skip to content

Commit 0fd6390

Browse files
authored
Fix of incorrect file name (#43)
* Fix of incorrect file name * Fix of incorrect file name * Fix of incorrect file name * Fix of incorrect file name * Fix of incorrect file name * Fix of incorrect file name * Fix of incorrect file name
1 parent 02dfaa4 commit 0fd6390

3 files changed

Lines changed: 45 additions & 5 deletions

File tree

print_service/routes/file.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ async def upload_file(
173173
if len(memory_file) > settings.MAX_SIZE:
174174
raise HTTPException(413, f'Content too large, {settings.MAX_SIZE} bytes allowed')
175175
await saved_file.write(memory_file)
176-
if not check_pdf_ok(memory_file):
177-
await aiofiles.os.remove(path)
178-
raise HTTPException(415, 'File corrupted')
176+
if not check_pdf_ok(memory_file):
177+
await aiofiles.os.remove(path)
178+
raise HTTPException(415, 'File corrupted')
179179
await file.close()
180180

181181
return {

print_service/utils/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ def generate_pin(session: Session):
3838
def generate_filename(original_filename: str):
3939
datestr = date.today().isoformat()
4040
salt = ''.join(random.choice(settings.PIN_SYMBOLS) for _ in range(128))
41-
ext = re.findall(r'\w+', original_filename.split('.')[-1])[0]
41+
ext_list = re.findall(r'\w+', original_filename.split('.')[-1])
42+
if not ext_list:
43+
raise HTTPException(422, "Unprocessable file instance")
44+
ext = ext_list[0]
4245
return f'{datestr}-{salt}.{ext}'
4346

4447

tests/test_routes/test_file.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import asyncio
2+
import datetime
23
import json
4+
import time
35
from concurrent.futures import ThreadPoolExecutor
46

57
import pytest
@@ -8,7 +10,7 @@
810

911
from print_service.models import File
1012
from print_service.settings import get_settings
11-
from print_service.utils import check_pdf_ok, get_file
13+
from print_service.utils import check_pdf_ok, generate_filename, get_file
1214

1315

1416
url = '/file'
@@ -131,3 +133,38 @@ def test_upload_and_print_encrypted_file(pin_pdf, client):
131133
assert res.status_code == status.HTTP_200_OK
132134
res2 = client.get(f"{url}/{pin}")
133135
assert res2.status_code == status.HTTP_200_OK
136+
137+
138+
def test_incorrect_filename(union_member_user, client, dbsession):
139+
body1 = {
140+
"surname": union_member_user['surname'],
141+
"number": union_member_user['union_number'],
142+
"filename": "filepdf.",
143+
"options": {"pages": "", "copies": 1, "two_sided": False},
144+
}
145+
body2 = {
146+
"surname": union_member_user['surname'],
147+
"number": union_member_user['union_number'],
148+
"filename": "ffilepdf.412.-.",
149+
"options": {"pages": "", "copies": 1, "two_sided": False},
150+
}
151+
body3 = {
152+
"surname": union_member_user['surname'],
153+
"number": union_member_user['union_number'],
154+
"filename": "filepdf.421.doc.pdf...24...",
155+
"options": {"pages": "", "copies": 1, "two_sided": False},
156+
}
157+
body4 = {
158+
"surname": union_member_user['surname'],
159+
"number": union_member_user['union_number'],
160+
"filename": "&^$%#**$@)(",
161+
"options": {"pages": "", "copies": 1, "two_sided": False},
162+
}
163+
res1 = client.post(url, data=json.dumps(body1))
164+
assert res1.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
165+
res2 = client.post(url, data=json.dumps(body2))
166+
assert res2.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
167+
res3 = client.post(url, data=json.dumps(body3))
168+
assert res3.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
169+
res4 = client.post(url, data=json.dumps(body4))
170+
assert res4.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY

0 commit comments

Comments
 (0)