Merge branch 'dev' into oauth2

This commit is contained in:
Sam
2022-06-25 21:23:15 +02:00
133 changed files with 2507 additions and 2407 deletions

View File

@ -1195,6 +1195,27 @@ class TestGetWorkout(ApiTestCaseMixin):
self.assert_404_with_message(response, 'Map does not exist')
def test_it_returns_404_if_map_file_not_found(
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
workout_cycling_user_1: Workout,
) -> None:
map_ip = self.random_string()
workout_cycling_user_1.map = self.random_string()
workout_cycling_user_1.map_id = map_ip
client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get(
f'/api/workouts/map/{map_ip}',
headers=dict(Authorization=f'Bearer {auth_token}'),
)
self.assert_404_with_message(response, 'Map file does not exist')
@pytest.mark.parametrize(
'client_scope, can_access',
[

View File

@ -1,5 +1,6 @@
import json
import os
import re
from datetime import datetime
from io import BytesIO
from typing import Dict, Optional
@ -10,7 +11,6 @@ from flask import Flask
from fittrackee.users.models import User
from fittrackee.workouts.models import Sport, Workout
from fittrackee.workouts.utils.short_id import decode_short_id
from ..mixins import ApiTestCaseMixin, CallArgsMixin
@ -251,6 +251,56 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin):
assert 'just a workout' == data['data']['workouts'][0]['title']
assert_workout_data_with_gpx(data)
def test_it_creates_workout_with_expecting_gpx_path(
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
) -> None:
client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
client.post(
'/api/workouts',
data=dict(
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'),
data='{"sport_id": 1}',
),
headers=dict(
content_type='multipart/form-data',
Authorization=f'Bearer {auth_token}',
),
)
workout = Workout.query.first()
assert re.match(
r'^workouts/1/2018-03-13_12-44-45_1_([\w\d_-]*).gpx$',
workout.gpx,
)
def test_it_creates_workout_with_expecting_map_path(
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
) -> None:
client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
client.post(
'/api/workouts',
data=dict(
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'),
data='{"sport_id": 1}',
),
headers=dict(
content_type='multipart/form-data',
Authorization=f'Bearer {auth_token}',
),
)
workout = Workout.query.first()
assert re.match(
r'^workouts/1/2018-03-13_12-44-45_1_([\w\d_-]*).png$',
workout.map,
)
def test_it_adds_a_workout_with_gpx_without_name(
self,
app: Flask,
@ -1052,23 +1102,6 @@ class TestPostAndGetWorkoutWithGpx(ApiTestCaseMixin):
)
assert response.status_code == 200
# error case in the same test to avoid generate a new map file
workout_uuid = decode_short_id(workout_short_id)
workout = Workout.query.filter_by(uuid=workout_uuid).first()
workout.map = 'incorrect path'
assert response.status_code == 200
assert 'success' in data['status']
assert '' in data['message']
assert len(data['data']['gpx']) != ''
response = client.get(
f'/api/workouts/map/{map_id}',
headers=dict(Authorization=f'Bearer {auth_token}'),
)
self.assert_500(response)
def test_it_gets_a_workout_created_with_gpx(
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
) -> None:

View File

@ -1,9 +1,6 @@
import os
import pytest
from flask import Flask
from fittrackee.files import get_absolute_file_path
from fittrackee.users.models import User
from fittrackee.workouts.models import Sport, Workout
@ -65,21 +62,45 @@ class TestDeleteWorkoutWithGpx(ApiTestCaseMixin):
data = self.assert_404(response)
assert 'not found' in data['status']
def test_it_returns_500_when_deleting_a_workout_with_gpx_invalid_file(
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
def test_a_workout_with_gpx_can_be_deleted_if_gpx_file_is_invalid(
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
workout_cycling_user_1: Workout,
) -> None:
token, workout_short_id = post_a_workout(app, gpx_file)
client = app.test_client()
gpx_filepath = get_gpx_filepath(1)
gpx_filepath = get_absolute_file_path(gpx_filepath)
os.remove(gpx_filepath)
response = client.delete(
f'/api/workouts/{workout_short_id}',
headers=dict(Authorization=f'Bearer {token}'),
workout_cycling_user_1.gpx = self.random_string()
client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
self.assert_500(response)
response = client.delete(
f'/api/workouts/{workout_cycling_user_1.short_id}',
headers=dict(Authorization=f'Bearer {auth_token}'),
)
assert response.status_code == 204
def test_a_workout_with_gpx_can_be_deleted_if_map_file_is_invalid(
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
workout_cycling_user_1: Workout,
) -> None:
map_ip = self.random_string()
workout_cycling_user_1.map = self.random_string()
workout_cycling_user_1.map_id = map_ip
client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.delete(
f'/api/workouts/{workout_cycling_user_1.short_id}',
headers=dict(Authorization=f'Bearer {auth_token}'),
)
assert response.status_code == 204
@pytest.mark.parametrize(
'client_scope, can_access',