API - return 404 instead of 500 when map file not found - fix #192

This commit is contained in:
Sam 2022-06-22 11:04:34 +02:00
parent 3b47353a7b
commit 9c2ba31b96
3 changed files with 24 additions and 19 deletions

View File

@ -1158,6 +1158,27 @@ class TestGetWorkout(ApiTestCaseMixin):
self.assert_404_with_message(response, 'Map does not exist') self.assert_404_with_message(response, 'Map does not exist')
def test_it_returns_404_if_map_file_not_found(
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
workout_cycling_user_1: Workout,
) -> None:
map_ip = self.random_string()
workout_cycling_user_1.map = self.random_string()
workout_cycling_user_1.map_id = map_ip
client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get(
f'/api/workouts/map/{map_ip}',
headers=dict(Authorization=f'Bearer {auth_token}'),
)
self.assert_404_with_message(response, 'Map file does not exist')
class TestDownloadWorkoutGpx(ApiTestCaseMixin): class TestDownloadWorkoutGpx(ApiTestCaseMixin):
def test_it_returns_404_if_workout_does_not_exist( def test_it_returns_404_if_workout_does_not_exist(

View File

@ -10,7 +10,6 @@ from flask import Flask
from fittrackee.users.models import User from fittrackee.users.models import User
from fittrackee.workouts.models import Sport, Workout from fittrackee.workouts.models import Sport, Workout
from fittrackee.workouts.utils.short_id import decode_short_id
from ..mixins import ApiTestCaseMixin, CallArgsMixin from ..mixins import ApiTestCaseMixin, CallArgsMixin
@ -974,23 +973,6 @@ class TestPostAndGetWorkoutWithGpx(ApiTestCaseMixin):
) )
assert response.status_code == 200 assert response.status_code == 200
# error case in the same test to avoid generate a new map file
workout_uuid = decode_short_id(workout_short_id)
workout = Workout.query.filter_by(uuid=workout_uuid).first()
workout.map = 'incorrect path'
assert response.status_code == 200
assert 'success' in data['status']
assert '' in data['message']
assert len(data['data']['gpx']) != ''
response = client.get(
f'/api/workouts/map/{map_id}',
headers=dict(Authorization=f'Bearer {auth_token}'),
)
self.assert_500(response)
def test_it_gets_a_workout_created_with_gpx( def test_it_gets_a_workout_created_with_gpx(
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
) -> None: ) -> None:

View File

@ -13,7 +13,7 @@ from flask import (
send_from_directory, send_from_directory,
) )
from sqlalchemy import exc from sqlalchemy import exc
from werkzeug.exceptions import RequestEntityTooLarge from werkzeug.exceptions import NotFound, RequestEntityTooLarge
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
from fittrackee import appLog, db from fittrackee import appLog, db
@ -798,6 +798,8 @@ def get_map(map_id: int) -> Union[HttpResponse, Response]:
current_app.config['UPLOAD_FOLDER'], current_app.config['UPLOAD_FOLDER'],
workout.map, workout.map,
) )
except NotFound:
return NotFoundErrorResponse('Map file does not exist.')
except Exception as e: except Exception as e:
return handle_error_and_return_response(e) return handle_error_and_return_response(e)