From 3ada1250f1b52f041c1ff233327385fc914ead3f Mon Sep 17 00:00:00 2001 From: vantage-ola Date: Wed, 18 Sep 2024 14:15:59 +0100 Subject: [PATCH] feature: edit and delete laptimes, +tests --- tracknow/backend/routes.py | 58 ++++++++++++++++++++++++++ tracknow/backend/tests/test_laptime.py | 57 +++++++++++++++++++++++++ 2 files changed, 115 insertions(+) diff --git a/tracknow/backend/routes.py b/tracknow/backend/routes.py index 25c5b24..67cee1d 100644 --- a/tracknow/backend/routes.py +++ b/tracknow/backend/routes.py @@ -236,6 +236,60 @@ def add_laptime(): return jsonify({"Laptime Added Successfully": laptime.to_dict(), "by": loggedin_user.username}), 201 + +# User edits laptime +@routes.route('/api/v1/user/laptime/edit/', methods=['PUT']) +@require_api_key +@jwt_required() +def edit_user_laptime(id): + user_id = get_jwt_identity() + laptime = Laptime.query.filter_by(id=id, user_id=user_id).first() + + if not laptime: + return jsonify({'msg': 'Laptime not found or you do not have permission to edit it'}), 404 + + data = request.get_json() + + # Update only the fields that are provided in the request + if 'title' in data: + laptime.title = data['title'] + if 'car' in data: + laptime.car = data['car'] + if 'track' in data: + laptime.track = data['track'] + if 'time' in data: + laptime.time = data['time'] + if 'simracing' in data: + laptime.simracing = data['simracing'] + if 'platform' in data: + laptime.platform = data['platform'] + if 'youtube_link' in data: + laptime.youtube_link = data['youtube_link'] + if 'comment' in data: + laptime.comment = data['comment'] + if 'image' in data: + laptime.image = data['image'] + + db.session.commit() + + return jsonify({'msg': 'Laptime updated successfully', 'laptime': laptime.to_dict()}), 200 + +# User deletes laptimes +@routes.route('/api/v1/user/laptime/delete/', methods=['DELETE']) +@require_api_key +@jwt_required() +def del_user_laptime(id): + user_id = get_jwt_identity() + laptime = Laptime.query.filter_by(id=id, user_id=user_id).first() + + if not laptime: + return jsonify({'msg': 'Laptime not found or you do not have permission to delete it'}), 404 + + db.session.delete(laptime) + db.session.commit() + + return jsonify({'msg': 'Laptime deleted successfully'}), 200 + # Logged in user gets all the laptimes they posted on tracknow. @routes.route('/api/v1/user/laptimes', methods=['GET']) @require_api_key @@ -258,6 +312,10 @@ def get_user_laptimes(): def get_user_laptime(id): user_id = get_jwt_identity() laptime = Laptime.query.filter_by(id=id, user_id=user_id).first() + + if laptime is None: + return jsonify({'msg': 'Laptime not found'}), 404 + return jsonify(laptime.to_dict()), 200 # Global - get all laptimes posted around the world. diff --git a/tracknow/backend/tests/test_laptime.py b/tracknow/backend/tests/test_laptime.py index ebac4d7..65c9c4e 100644 --- a/tracknow/backend/tests/test_laptime.py +++ b/tracknow/backend/tests/test_laptime.py @@ -130,3 +130,60 @@ def test_user_laptimes(test_client, init_database): } response = test_client.get('/api/v1/users/1/laptimes?page=4', headers=headers, content_type='appication/json') + + +def test_edit_laptime(test_client, init_database): + + login_response = test_client.post('/api/v1/login', data=json.dumps(user), headers=header, content_type='application/json') + token = login_response.json['token'] + headers = { + 'x-api-key': api_key, + 'Authorization': f'Bearer {token}' + } + + response = test_client.post('/api/v1/user/laptimes', data=json.dumps(laptime_data), headers=headers, content_type='application/json') + assert response.status_code == 201 + laptime_id = response.json['Laptime Added Successfully']['id'] + + edit_data = { + "title": "Updated Test Laptime", + "time": "1.07.999", + "comment": "even faster af boi" + } + edit_response = test_client.put(f'/api/v1/user/laptime/edit/{laptime_id}', data=json.dumps(edit_data), headers=headers, content_type='application/json') + + assert edit_response.status_code == 200 + assert edit_response.json['msg'] == 'Laptime updated successfully' + assert edit_response.json['laptime']['title'] == "Updated Test Laptime" + assert edit_response.json['laptime']['time'] == "1.07.999" + assert edit_response.json['laptime']['comment'] == "even faster af boi" + + # Verify that unchanged fields remain the same + assert edit_response.json['laptime']['car'] == "Formula 1 2020" + assert edit_response.json['laptime']['track'] == "Bathurst" + +def test_delete_laptime(test_client, init_database): + + login_response = test_client.post('/api/v1/login', data=json.dumps(user), headers=header, content_type='application/json') + token = login_response.json['token'] + headers = { + 'x-api-key': api_key, + 'Authorization': f'Bearer {token}' + } + + + response = test_client.post('/api/v1/user/laptimes', data=json.dumps(laptime_data), headers=headers, content_type='application/json') + assert response.status_code == 201 + laptime_id = response.json['Laptime Added Successfully']['id'] + + + delete_response = test_client.delete(f'/api/v1/user/laptime/delete/{laptime_id}', headers=headers, content_type='application/json') + + assert delete_response.status_code == 200 + assert delete_response.json['msg'] == 'Laptime deleted successfully' + + # Verify that the laptime has been deleted by attempting to retrieve it + get_response = test_client.get(f'/api/v1/user/laptimes/{laptime_id}', headers=headers, content_type='application/json') + assert get_response.status_code == 404 + assert 'msg' in get_response.json + assert get_response.json['msg'] == 'Laptime not found' \ No newline at end of file