API - return error when file from a zip archive exceeds size limit

This commit is contained in:
Sam 2022-11-04 22:27:42 +01:00
parent 4b1c51880f
commit d2db9240fa
2 changed files with 52 additions and 6 deletions

View File

@ -1177,6 +1177,40 @@ class TestPostWorkoutWithZipArchive(ApiTestCaseMixin):
) )
assert 'data' not in data assert 'data' not in data
def test_it_returns_error_if_a_file_from_archive_size_exceeds_limit(
self,
app_with_max_file_size: Flask,
user_1: User,
sport_1_cycling: Sport,
) -> None:
file_path = os.path.join(
app_with_max_file_size.root_path, 'tests/files/gpx_test.zip'
)
# 'gpx_test.zip' contains 3 gpx files (same data) and 1 non-gpx file
with open(file_path, 'rb') as zip_file:
client, auth_token = self.get_test_client_and_auth_token(
app_with_max_file_size, user_1.email
)
response = client.post(
'/api/workouts',
data=dict(
file=(zip_file, 'gpx_test.zip'), data='{"sport_id": 1}'
),
headers=dict(
content_type='multipart/form-data',
Authorization=f'Bearer {auth_token}',
),
)
data = self.assert_400(
response,
'at least one file in zip archive exceeds size limit, '
'please check the archive',
'fail',
)
assert 'data' not in data
class TestPostAndGetWorkoutWithGpx(ApiTestCaseMixin): class TestPostAndGetWorkoutWithGpx(ApiTestCaseMixin):
def workout_assertion( def workout_assertion(

View File

@ -349,15 +349,27 @@ def process_zip_archive(
does not exceed defined limit. does not exceed defined limit.
""" """
with zipfile.ZipFile(common_params['file_path'], "r") as zip_ref: with zipfile.ZipFile(common_params['file_path'], "r") as zip_ref:
info_list = [ max_file_size = current_app.config['max_single_file_size']
zip_info gpx_files_count = 0
for zip_info in zip_ref.infolist() files_with_invalid_size_count = 0
if is_gpx_file(zip_info.filename) for zip_info in zip_ref.infolist():
] if is_gpx_file(zip_info.filename):
if len(info_list) > current_app.config['gpx_limit_import']: gpx_files_count += 1
if zip_info.file_size > max_file_size:
files_with_invalid_size_count += 1
if gpx_files_count > current_app.config['gpx_limit_import']:
raise WorkoutException( raise WorkoutException(
'fail', 'the number of files in the archive exceeds the limit' 'fail', 'the number of files in the archive exceeds the limit'
) )
if files_with_invalid_size_count > 0:
raise WorkoutException(
'fail',
'at least one file in zip archive exceeds size limit, '
'please check the archive',
)
zip_ref.extractall(extract_dir) zip_ref.extractall(extract_dir)
new_workouts = [] new_workouts = []