API - allow deleting workout when gpx or map files missing - fix #193
This commit is contained in:
		@@ -1,8 +1,5 @@
 | 
			
		||||
import os
 | 
			
		||||
 | 
			
		||||
from flask import Flask
 | 
			
		||||
 | 
			
		||||
from fittrackee.files import get_absolute_file_path
 | 
			
		||||
from fittrackee.users.models import User
 | 
			
		||||
from fittrackee.workouts.models import Sport, Workout
 | 
			
		||||
 | 
			
		||||
@@ -64,21 +61,45 @@ class TestDeleteWorkoutWithGpx(ApiTestCaseMixin):
 | 
			
		||||
        data = self.assert_404(response)
 | 
			
		||||
        assert 'not found' in data['status']
 | 
			
		||||
 | 
			
		||||
    def test_it_returns_500_when_deleting_a_workout_with_gpx_invalid_file(
 | 
			
		||||
        self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
 | 
			
		||||
    def test_a_workout_with_gpx_can_be_deleted_if_gpx_file_is_invalid(
 | 
			
		||||
        self,
 | 
			
		||||
        app: Flask,
 | 
			
		||||
        user_1: User,
 | 
			
		||||
        sport_1_cycling: Sport,
 | 
			
		||||
        workout_cycling_user_1: Workout,
 | 
			
		||||
    ) -> None:
 | 
			
		||||
        token, workout_short_id = post_a_workout(app, gpx_file)
 | 
			
		||||
        client = app.test_client()
 | 
			
		||||
        gpx_filepath = get_gpx_filepath(1)
 | 
			
		||||
        gpx_filepath = get_absolute_file_path(gpx_filepath)
 | 
			
		||||
        os.remove(gpx_filepath)
 | 
			
		||||
 | 
			
		||||
        response = client.delete(
 | 
			
		||||
            f'/api/workouts/{workout_short_id}',
 | 
			
		||||
            headers=dict(Authorization=f'Bearer {token}'),
 | 
			
		||||
        workout_cycling_user_1.gpx = self.random_string()
 | 
			
		||||
        client, auth_token = self.get_test_client_and_auth_token(
 | 
			
		||||
            app, user_1.email
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        self.assert_500(response)
 | 
			
		||||
        response = client.delete(
 | 
			
		||||
            f'/api/workouts/{workout_cycling_user_1.short_id}',
 | 
			
		||||
            headers=dict(Authorization=f'Bearer {auth_token}'),
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        assert response.status_code == 204
 | 
			
		||||
 | 
			
		||||
    def test_a_workout_with_gpx_can_be_deleted_if_map_file_is_invalid(
 | 
			
		||||
        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.delete(
 | 
			
		||||
            f'/api/workouts/{workout_cycling_user_1.short_id}',
 | 
			
		||||
            headers=dict(Authorization=f'Bearer {auth_token}'),
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        assert response.status_code == 204
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class TestDeleteWorkoutWithoutGpx(ApiTestCaseMixin):
 | 
			
		||||
 
 | 
			
		||||
@@ -12,7 +12,7 @@ from sqlalchemy.orm.mapper import Mapper
 | 
			
		||||
from sqlalchemy.orm.session import Session, object_session
 | 
			
		||||
from sqlalchemy.types import JSON, Enum
 | 
			
		||||
 | 
			
		||||
from fittrackee import db
 | 
			
		||||
from fittrackee import appLog, db
 | 
			
		||||
from fittrackee.files import get_absolute_file_path
 | 
			
		||||
 | 
			
		||||
from .utils.convert import convert_in_duration, convert_value_to_integer
 | 
			
		||||
@@ -379,9 +379,15 @@ def on_workout_delete(
 | 
			
		||||
    @listens_for(db.Session, 'after_flush', once=True)
 | 
			
		||||
    def receive_after_flush(session: Session, context: Any) -> None:
 | 
			
		||||
        if old_record.map:
 | 
			
		||||
            try:
 | 
			
		||||
                os.remove(get_absolute_file_path(old_record.map))
 | 
			
		||||
            except OSError:
 | 
			
		||||
                appLog.error('map file not find when deleting workout')
 | 
			
		||||
        if old_record.gpx:
 | 
			
		||||
            try:
 | 
			
		||||
                os.remove(get_absolute_file_path(old_record.gpx))
 | 
			
		||||
            except OSError:
 | 
			
		||||
                appLog.error('gpx file not find when deleting workout')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class WorkoutSegment(BaseModel):
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user