API - use config from database for max workouts limit - fix #66

This commit is contained in:
Sam 2021-02-17 16:59:28 +01:00
parent d4d09a8f3d
commit 16ea2adfd9
3 changed files with 57 additions and 25 deletions

View File

@ -10,12 +10,11 @@ from fittrackee.application.utils import update_app_config_from_database
def get_app_config(
with_config: Optional[bool] = False,
with_federation: Optional[bool] = False,
max_workouts: Optional[int] = None,
) -> Optional[AppConfig]:
if with_config:
config = AppConfig()
config.federation_enabled = with_federation
config.gpx_limit_import = 10
config.gpx_limit_import = 10 if max_workouts is None else max_workouts
config.max_single_file_size = 1 * 1024 * 1024
config.max_zip_file_size = 1 * 1024 * 1024 * 10
config.max_users = 100
@ -27,13 +26,13 @@ def get_app_config(
def get_app(
with_config: Optional[bool] = False,
with_federation: Optional[bool] = False,
max_workouts: Optional[int] = None,
) -> Generator:
app = create_app()
with app.app_context():
try:
db.create_all()
app_db_config = get_app_config(with_config, with_federation)
app_db_config = get_app_config(with_config, max_workouts)
if app_db_config:
update_app_config_from_database(app, app_db_config)
yield app
@ -59,6 +58,12 @@ def app(monkeypatch: pytest.MonkeyPatch) -> Generator:
yield from get_app(with_config=True)
@pytest.fixture
def app_with_max_workouts(monkeypatch: pytest.MonkeyPatch) -> Generator:
monkeypatch.setenv('EMAIL_URL', 'smtp://none:none@0.0.0.0:1025')
yield from get_app(with_config=True, max_workouts=2)
@pytest.fixture
def app_no_config() -> Generator:
yield from get_app(with_config=False)
@ -76,20 +81,14 @@ def app_tls(monkeypatch: pytest.MonkeyPatch) -> Generator:
yield from get_app(with_config=True)
@pytest.fixture
def app_with_federation() -> Generator:
yield from get_app(with_config=True, with_federation=True)
@pytest.fixture
def app_wo_domain() -> Generator:
yield from get_app(with_config=True, with_federation=True)
yield from get_app(with_config=True)
@pytest.fixture()
def app_config() -> AppConfig:
config = AppConfig()
config.federation_enabled = False
config.gpx_limit_import = 10
config.max_single_file_size = 1048576
config.max_zip_file_size = 10485760

View File

@ -701,7 +701,7 @@ class TestPostWorkoutWithZipArchive:
assert 'just a workout' == data['data']['workouts'][0]['title']
assert_workout_data_with_gpx(data)
def test_it_returns_400_if_folder_is_present_in_zpi_archive(
def test_it_returns_400_if_folder_is_present_in_zip_archive(
self, app: Flask, user_1: User, sport_1_cycling: Sport
) -> None:
file_path = os.path.join(
@ -737,7 +737,7 @@ class TestPostWorkoutWithZipArchive:
assert 'fail' in data['status']
assert len(data['data']['workouts']) == 0
def test_it_returns_500_if_one_fle_in_zip_archive_is_invalid(
def test_it_returns_500_if_one_file_in_zip_archive_is_invalid(
self, app: Flask, user_1: User, sport_1_cycling: Sport
) -> None:
file_path = os.path.join(
@ -773,6 +773,48 @@ class TestPostWorkoutWithZipArchive:
assert 'Error during gpx processing.' in data['message']
assert 'data' not in data
def test_it_imports_only_max_number_of_files(
self,
app_with_max_workouts: Flask,
user_1: User,
sport_1_cycling: Sport,
) -> None:
file_path = os.path.join(
app_with_max_workouts.root_path, 'tests/files/gpx_test.zip'
)
# 'gpx_test.zip' contains 3 gpx files (same data) and 1 non-gpx file
with open(file_path, 'rb') as zip_file:
client = app_with_max_workouts.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='test@test.com', password='12345678')
),
content_type='application/json',
)
client.post(
'/api/workouts',
data=dict(
file=(zip_file, 'gpx_test.zip'), data='{"sport_id": 1}'
),
headers=dict(
content_type='multipart/form-data',
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
),
)
response = client.get(
'/api/workouts',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert len(data['data']['workouts']) == 2
class TestPostAndGetWorkoutWithGpx:
@staticmethod

View File

@ -14,7 +14,7 @@ from staticmap import Line, StaticMap
from werkzeug.datastructures import FileStorage
from werkzeug.utils import secure_filename
from fittrackee import appLog, db
from fittrackee import db
from fittrackee.users.models import User
from .exceptions import WorkoutException
@ -327,16 +327,7 @@ def process_zip_archive(common_params: Dict, extract_dir: str) -> List:
zip_ref.extractall(extract_dir)
new_workouts = []
gpx_files_limit = os.getenv('REACT_APP_GPX_LIMIT_IMPORT', 10)
if (
gpx_files_limit
and isinstance(gpx_files_limit, str)
and gpx_files_limit.isdigit()
):
gpx_files_limit = int(gpx_files_limit)
else:
gpx_files_limit = 10
appLog.warning('GPX limit not configured, set to 10.')
gpx_files_limit = current_app.config['gpx_limit_import']
gpx_files_ok = 0
for gpx_file in os.listdir(extract_dir):