API - fix adding workouts notes with control characters - fix #80

This commit is contained in:
Sam 2021-07-14 14:29:10 +02:00
parent 2c37e378d8
commit 66b9c77560
2 changed files with 20 additions and 6 deletions

View File

@ -5,6 +5,7 @@ from datetime import datetime
from io import BytesIO from io import BytesIO
from typing import Dict from typing import Dict
import pytest
from flask import Flask from flask import Flask
from fittrackee.users.models import User from fittrackee.users.models import User
@ -292,8 +293,22 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin):
) )
assert_workout_data_with_gpx(data) assert_workout_data_with_gpx(data)
def test_it_adds_get_an_workout_with_gpx_notes( @pytest.mark.parametrize(
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str 'input_description,input_notes',
[
('empty notes', ''),
('short notes', 'test workout'),
('notes with special characters', 'test \nworkout'),
],
)
def test_it_adds_a_workout_with_gpx_notes(
self,
input_description: str,
input_notes: str,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
gpx_file: str,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(app)
@ -301,7 +316,7 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin):
'/api/workouts', '/api/workouts',
data=dict( data=dict(
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'), file=(BytesIO(str.encode(gpx_file)), 'example.gpx'),
data='{"sport_id": 1, "notes": "test workout"}', data=f'{{"sport_id": 1, "notes": "{input_notes}"}}',
), ),
headers=dict( headers=dict(
content_type='multipart/form-data', content_type='multipart/form-data',
@ -313,8 +328,7 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin):
assert response.status_code == 201 assert response.status_code == 201
assert 'created' in data['status'] assert 'created' in data['status']
assert len(data['data']['workouts']) == 1 assert len(data['data']['workouts']) == 1
assert 'just a workout' == data['data']['workouts'][0]['title'] assert data['data']['workouts'][0]['notes'] == input_notes
assert 'test workout' == data['data']['workouts'][0]['notes']
def test_it_returns_500_if_gpx_file_has_not_tracks( def test_it_returns_500_if_gpx_file_has_not_tracks(
self, self,

View File

@ -879,7 +879,7 @@ def post_workout(auth_user_id: int) -> Union[Tuple[Dict, int], HttpResponse]:
if error_response: if error_response:
return error_response return error_response
workout_data = json.loads(request.form['data']) workout_data = json.loads(request.form['data'], strict=False)
if not workout_data or workout_data.get('sport_id') is None: if not workout_data or workout_data.get('sport_id') is None:
return InvalidPayloadErrorResponse() return InvalidPayloadErrorResponse()