API - fix notes emptying

This commit is contained in:
Sam 2021-07-14 14:54:20 +02:00
parent 66b9c77560
commit 70ae3863e2
3 changed files with 87 additions and 55 deletions

View File

@ -1,6 +1,8 @@
import json import json
from typing import Dict from typing import Dict
from uuid import uuid4
import pytest
from flask import Flask from flask import Flask
from fittrackee.users.models import User from fittrackee.users.models import User
@ -81,9 +83,19 @@ class TestEditWorkoutWithGpx(ApiTestCaseMixin):
assert data['data']['workouts'][0]['title'] == 'Workout test' assert data['data']['workouts'][0]['title'] == 'Workout test'
assert_workout_data_with_gpx(data, sport_2_running.id) assert_workout_data_with_gpx(data, sport_2_running.id)
def test_it_adds_notes_for_an_workout_with_gpx( @pytest.mark.parametrize(
'input_description,input_notes',
[
('empty notes', ''),
('short notes', 'test workout'),
('notes with special characters', 'test \nworkout'),
],
)
def test_it_adds_notes_to_a_workout_with_gpx(
self, self,
app: Flask, app: Flask,
input_description: str,
input_notes: str,
user_1: User, user_1: User,
sport_1_cycling: Sport, sport_1_cycling: Sport,
sport_2_running: Sport, sport_2_running: Sport,
@ -95,7 +107,7 @@ class TestEditWorkoutWithGpx(ApiTestCaseMixin):
response = client.patch( response = client.patch(
f'/api/workouts/{workout_short_id}', f'/api/workouts/{workout_short_id}',
content_type='application/json', content_type='application/json',
data=json.dumps(dict(notes="test notes")), data=json.dumps(dict(notes=input_notes)),
headers=dict(Authorization=f'Bearer {token}'), headers=dict(Authorization=f'Bearer {token}'),
) )
@ -103,8 +115,33 @@ class TestEditWorkoutWithGpx(ApiTestCaseMixin):
assert response.status_code == 200 assert response.status_code == 200
assert 'success' in data['status'] assert 'success' in data['status']
assert len(data['data']['workouts']) == 1 assert len(data['data']['workouts']) == 1
assert data['data']['workouts'][0]['title'] == 'just a workout' assert data['data']['workouts'][0]['notes'] == input_notes
assert data['data']['workouts'][0]['notes'] == 'test notes'
def test_it_empties_workout_notes(
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
sport_2_running: Sport,
gpx_file: str,
) -> None:
token, workout_short_id = post_an_workout(
app, gpx_file, notes=uuid4().hex
)
client = app.test_client()
response = client.patch(
f'/api/workouts/{workout_short_id}',
content_type='application/json',
data=json.dumps(dict(notes='')),
headers=dict(Authorization=f'Bearer {token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['workouts']) == 1
assert data['data']['workouts'][0]['notes'] == ''
def test_it_raises_403_when_editing_an_workout_from_different_user( def test_it_raises_403_when_editing_an_workout_from_different_user(
self, self,
@ -283,9 +320,19 @@ class TestEditWorkoutWithoutGpx(ApiTestCaseMixin):
assert records[3]['workout_date'] == 'Tue, 15 May 2018 15:05:00 GMT' assert records[3]['workout_date'] == 'Tue, 15 May 2018 15:05:00 GMT'
assert records[3]['value'] == 8.0 assert records[3]['value'] == 8.0
def test_it_adds_notes_to_an_workout_wo_gpx( @pytest.mark.parametrize(
'input_description,input_notes',
[
('empty notes', ''),
('short notes', 'test workout'),
('notes with special characters', 'test \nworkout'),
],
)
def test_it_adds_notes_to_a_workout_wo_gpx(
self, self,
app: Flask, app: Flask,
input_description: str,
input_notes: str,
user_1: User, user_1: User,
sport_1_cycling: Sport, sport_1_cycling: Sport,
workout_cycling_user_1: Workout, workout_cycling_user_1: Workout,
@ -296,7 +343,7 @@ class TestEditWorkoutWithoutGpx(ApiTestCaseMixin):
response = client.patch( response = client.patch(
f'/api/workouts/{workout_short_id}', f'/api/workouts/{workout_short_id}',
content_type='application/json', content_type='application/json',
data=json.dumps(dict(notes='test notes')), data=json.dumps(dict(notes=input_notes)),
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
@ -304,52 +351,31 @@ class TestEditWorkoutWithoutGpx(ApiTestCaseMixin):
assert response.status_code == 200 assert response.status_code == 200
assert 'success' in data['status'] assert 'success' in data['status']
assert len(data['data']['workouts']) == 1 assert len(data['data']['workouts']) == 1
assert 'creation_date' in data['data']['workouts'][0] assert data['data']['workouts'][0]['notes'] == input_notes
assert (
data['data']['workouts'][0]['workout_date']
== 'Mon, 01 Jan 2018 00:00:00 GMT'
)
assert data['data']['workouts'][0]['user'] == 'test'
assert data['data']['workouts'][0]['sport_id'] == sport_1_cycling.id
assert data['data']['workouts'][0]['duration'] == '1:00:00'
assert data['data']['workouts'][0]['title'] is None
assert data['data']['workouts'][0]['ascent'] is None
assert data['data']['workouts'][0]['ave_speed'] == 10.0
assert data['data']['workouts'][0]['descent'] is None
assert data['data']['workouts'][0]['distance'] == 10.0
assert data['data']['workouts'][0]['max_alt'] is None
assert data['data']['workouts'][0]['max_speed'] == 10.0
assert data['data']['workouts'][0]['min_alt'] is None
assert data['data']['workouts'][0]['moving'] == '1:00:00'
assert data['data']['workouts'][0]['pauses'] is None
assert data['data']['workouts'][0]['with_gpx'] is False
assert data['data']['workouts'][0]['map'] is None
assert data['data']['workouts'][0]['weather_start'] is None
assert data['data']['workouts'][0]['weather_end'] is None
assert data['data']['workouts'][0]['notes'] == 'test notes'
records = data['data']['workouts'][0]['records'] def test_it_empties_workout_notes(
assert len(records) == 4 self,
assert records[0]['sport_id'] == sport_1_cycling.id app: Flask,
assert records[0]['workout_id'] == workout_short_id user_1: User,
assert records[0]['record_type'] == 'MS' sport_1_cycling: Sport,
assert records[0]['workout_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT' workout_cycling_user_1: Workout,
assert records[0]['value'] == 10.0 ) -> None:
assert records[1]['sport_id'] == sport_1_cycling.id workout_short_id = workout_cycling_user_1.short_id
assert records[1]['workout_id'] == workout_short_id workout_cycling_user_1.notes = uuid4().hex
assert records[1]['record_type'] == 'LD' client, auth_token = self.get_test_client_and_auth_token(app)
assert records[1]['workout_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT'
assert records[1]['value'] == '1:00:00' response = client.patch(
assert records[2]['sport_id'] == sport_1_cycling.id f'/api/workouts/{workout_short_id}',
assert records[2]['workout_id'] == workout_short_id content_type='application/json',
assert records[2]['record_type'] == 'FD' data=json.dumps(dict(notes='')),
assert records[2]['workout_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT' headers=dict(Authorization=f'Bearer {auth_token}'),
assert records[2]['value'] == 10.0 )
assert records[3]['sport_id'] == sport_1_cycling.id
assert records[3]['workout_id'] == workout_short_id data = json.loads(response.data.decode())
assert records[3]['record_type'] == 'AS' assert response.status_code == 200
assert records[3]['workout_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT' assert 'success' in data['status']
assert records[3]['value'] == 10.0 assert len(data['data']['workouts']) == 1
assert data['data']['workouts'][0]['notes'] == ''
def test_returns_403_when_editing_an_workout_wo_gpx_from_different_user( def test_returns_403_when_editing_an_workout_wo_gpx_from_different_user(
self, self,

View File

@ -1,6 +1,6 @@
import json import json
from io import BytesIO from io import BytesIO
from typing import Tuple from typing import Optional, Tuple
from uuid import uuid4 from uuid import uuid4
from flask import Flask from flask import Flask
@ -12,7 +12,9 @@ def get_random_short_id() -> str:
return encode_uuid(uuid4()) return encode_uuid(uuid4())
def post_an_workout(app: Flask, gpx_file: str) -> Tuple[str, str]: def post_an_workout(
app: Flask, gpx_file: str, notes: Optional[str] = None
) -> Tuple[str, str]:
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -20,11 +22,15 @@ def post_an_workout(app: Flask, gpx_file: str) -> Tuple[str, str]:
content_type='application/json', content_type='application/json',
) )
token = json.loads(resp_login.data.decode())['auth_token'] token = json.loads(resp_login.data.decode())['auth_token']
workout_data = '{"sport_id": 1'
if notes is not None:
workout_data += f', "notes": "{notes}"'
workout_data += '}'
response = client.post( response = client.post(
'/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}', data=workout_data,
), ),
headers=dict( headers=dict(
content_type='multipart/form-data', Authorization=f'Bearer {token}' content_type='multipart/form-data', Authorization=f'Bearer {token}'

View File

@ -202,7 +202,7 @@ def edit_workout(
workout.sport_id = workout_data.get('sport_id') workout.sport_id = workout_data.get('sport_id')
if workout_data.get('title'): if workout_data.get('title'):
workout.title = workout_data.get('title') workout.title = workout_data.get('title')
if workout_data.get('notes'): if workout_data.get('notes') is not None:
workout.notes = workout_data.get('notes') workout.notes = workout_data.get('notes')
if not workout.gpx: if not workout.gpx:
if workout_data.get('workout_date'): if workout_data.get('workout_date'):