API - delete user preferences on user account deletion
This commit is contained in:
parent
9f168c102e
commit
1e86a91e25
@ -5,7 +5,7 @@ from unittest.mock import patch
|
||||
|
||||
from flask import Flask
|
||||
|
||||
from fittrackee.users.models import User
|
||||
from fittrackee.users.models import User, UserSportPreference
|
||||
from fittrackee.workouts.models import Sport, Workout
|
||||
|
||||
from ..api_test_case import ApiTestCaseMixin
|
||||
@ -944,6 +944,22 @@ class TestDeleteUser(ApiTestCaseMixin):
|
||||
|
||||
assert response.status_code == 204
|
||||
|
||||
def test_user_with_preferences_can_delete_its_own_account(
|
||||
self,
|
||||
app: Flask,
|
||||
user_1: User,
|
||||
sport_1_cycling: Sport,
|
||||
user_sport_1_preference: UserSportPreference,
|
||||
) -> None:
|
||||
client, auth_token = self.get_test_client_and_auth_token(app)
|
||||
|
||||
response = client.delete(
|
||||
'/api/users/test',
|
||||
headers=dict(Authorization=f'Bearer {auth_token}'),
|
||||
)
|
||||
|
||||
assert response.status_code == 204
|
||||
|
||||
def test_user_with_picture_can_delete_its_own_account(
|
||||
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
|
||||
) -> None:
|
||||
|
@ -14,10 +14,11 @@ from fittrackee.responses import (
|
||||
UserNotFoundErrorResponse,
|
||||
handle_error_and_return_response,
|
||||
)
|
||||
from fittrackee.workouts.models import Record, Workout, WorkoutSegment
|
||||
from fittrackee.workouts.utils_files import get_absolute_file_path
|
||||
|
||||
from .decorators import authenticate, authenticate_as_admin
|
||||
from .models import User, Workout
|
||||
from .models import User, UserSportPreference
|
||||
|
||||
users_blueprint = Blueprint('users', __name__)
|
||||
|
||||
@ -555,9 +556,15 @@ def delete_user(
|
||||
'no other user has admin rights'
|
||||
)
|
||||
|
||||
for workout in Workout.query.filter_by(user_id=user.id).all():
|
||||
db.session.delete(workout)
|
||||
db.session.flush()
|
||||
db.session.query(UserSportPreference).filter(
|
||||
UserSportPreference.user_id == user.id
|
||||
).delete()
|
||||
db.session.query(Record).filter(Record.user_id == user.id).delete()
|
||||
db.session.query(WorkoutSegment).filter(
|
||||
WorkoutSegment.workout_id == Workout.id, Workout.user_id == user.id
|
||||
).delete(synchronize_session=False)
|
||||
db.session.query(Workout).filter(Workout.user_id == user.id).delete()
|
||||
db.session.flush()
|
||||
user_picture = user.picture
|
||||
db.session.delete(user)
|
||||
db.session.commit()
|
||||
|
Loading…
Reference in New Issue
Block a user