From 70ae3863e287c988cf965b369570023464c9c428 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 14 Jul 2021 14:54:20 +0200 Subject: [PATCH] API - fix notes emptying --- .../workouts/test_workouts_api_2_patch.py | 128 +++++++++++------- fittrackee/tests/workouts/utils.py | 12 +- fittrackee/workouts/utils.py | 2 +- 3 files changed, 87 insertions(+), 55 deletions(-) diff --git a/fittrackee/tests/workouts/test_workouts_api_2_patch.py b/fittrackee/tests/workouts/test_workouts_api_2_patch.py index da2ea4a5..8ad7dba0 100644 --- a/fittrackee/tests/workouts/test_workouts_api_2_patch.py +++ b/fittrackee/tests/workouts/test_workouts_api_2_patch.py @@ -1,6 +1,8 @@ import json from typing import Dict +from uuid import uuid4 +import pytest from flask import Flask from fittrackee.users.models import User @@ -81,9 +83,19 @@ class TestEditWorkoutWithGpx(ApiTestCaseMixin): assert data['data']['workouts'][0]['title'] == 'Workout test' 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, app: Flask, + input_description: str, + input_notes: str, user_1: User, sport_1_cycling: Sport, sport_2_running: Sport, @@ -95,7 +107,7 @@ class TestEditWorkoutWithGpx(ApiTestCaseMixin): response = client.patch( f'/api/workouts/{workout_short_id}', content_type='application/json', - data=json.dumps(dict(notes="test notes")), + data=json.dumps(dict(notes=input_notes)), headers=dict(Authorization=f'Bearer {token}'), ) @@ -103,8 +115,33 @@ class TestEditWorkoutWithGpx(ApiTestCaseMixin): assert response.status_code == 200 assert 'success' in data['status'] assert len(data['data']['workouts']) == 1 - assert data['data']['workouts'][0]['title'] == 'just a workout' - assert data['data']['workouts'][0]['notes'] == 'test notes' + assert data['data']['workouts'][0]['notes'] == input_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( self, @@ -283,9 +320,19 @@ class TestEditWorkoutWithoutGpx(ApiTestCaseMixin): assert records[3]['workout_date'] == 'Tue, 15 May 2018 15:05:00 GMT' 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, app: Flask, + input_description: str, + input_notes: str, user_1: User, sport_1_cycling: Sport, workout_cycling_user_1: Workout, @@ -296,7 +343,7 @@ class TestEditWorkoutWithoutGpx(ApiTestCaseMixin): response = client.patch( f'/api/workouts/{workout_short_id}', 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}'), ) @@ -304,52 +351,31 @@ class TestEditWorkoutWithoutGpx(ApiTestCaseMixin): assert response.status_code == 200 assert 'success' in data['status'] assert len(data['data']['workouts']) == 1 - assert 'creation_date' in data['data']['workouts'][0] - 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' + assert data['data']['workouts'][0]['notes'] == input_notes - records = data['data']['workouts'][0]['records'] - assert len(records) == 4 - assert records[0]['sport_id'] == sport_1_cycling.id - assert records[0]['workout_id'] == workout_short_id - assert records[0]['record_type'] == 'MS' - assert records[0]['workout_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT' - assert records[0]['value'] == 10.0 - assert records[1]['sport_id'] == sport_1_cycling.id - assert records[1]['workout_id'] == workout_short_id - assert records[1]['record_type'] == 'LD' - assert records[1]['workout_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT' - assert records[1]['value'] == '1:00:00' - assert records[2]['sport_id'] == sport_1_cycling.id - assert records[2]['workout_id'] == workout_short_id - assert records[2]['record_type'] == 'FD' - assert records[2]['workout_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT' - assert records[2]['value'] == 10.0 - assert records[3]['sport_id'] == sport_1_cycling.id - assert records[3]['workout_id'] == workout_short_id - assert records[3]['record_type'] == 'AS' - assert records[3]['workout_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT' - assert records[3]['value'] == 10.0 + def test_it_empties_workout_notes( + self, + app: Flask, + user_1: User, + sport_1_cycling: Sport, + workout_cycling_user_1: Workout, + ) -> None: + workout_short_id = workout_cycling_user_1.short_id + workout_cycling_user_1.notes = uuid4().hex + client, auth_token = self.get_test_client_and_auth_token(app) + + response = client.patch( + f'/api/workouts/{workout_short_id}', + content_type='application/json', + data=json.dumps(dict(notes='')), + headers=dict(Authorization=f'Bearer {auth_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_returns_403_when_editing_an_workout_wo_gpx_from_different_user( self, diff --git a/fittrackee/tests/workouts/utils.py b/fittrackee/tests/workouts/utils.py index 4fc89708..8b8d101d 100644 --- a/fittrackee/tests/workouts/utils.py +++ b/fittrackee/tests/workouts/utils.py @@ -1,6 +1,6 @@ import json from io import BytesIO -from typing import Tuple +from typing import Optional, Tuple from uuid import uuid4 from flask import Flask @@ -12,7 +12,9 @@ def get_random_short_id() -> str: 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() resp_login = client.post( '/api/auth/login', @@ -20,11 +22,15 @@ def post_an_workout(app: Flask, gpx_file: str) -> Tuple[str, str]: content_type='application/json', ) 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( '/api/workouts', data=dict( file=(BytesIO(str.encode(gpx_file)), 'example.gpx'), - data='{"sport_id": 1}', + data=workout_data, ), headers=dict( content_type='multipart/form-data', Authorization=f'Bearer {token}' diff --git a/fittrackee/workouts/utils.py b/fittrackee/workouts/utils.py index c901e94a..bd004905 100644 --- a/fittrackee/workouts/utils.py +++ b/fittrackee/workouts/utils.py @@ -202,7 +202,7 @@ def edit_workout( workout.sport_id = workout_data.get('sport_id') if 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') if not workout.gpx: if workout_data.get('workout_date'):