Skip to content

Commit

Permalink
feature: edit and delete laptimes, +tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vantage-ola committed Sep 18, 2024
1 parent 819f46a commit 3ada125
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
58 changes: 58 additions & 0 deletions tracknow/backend/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<int:id>', 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/<int:id>', 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
Expand All @@ -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.
Expand Down
57 changes: 57 additions & 0 deletions tracknow/backend/tests/test_laptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

0 comments on commit 3ada125

Please sign in to comment.