From 3c4718cc2770946fb8f7a0afb2d1a3ebecb34212 Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 16 Jul 2021 19:17:17 +0200 Subject: [PATCH] API - allow keeping default tile server for static map - #83 --- .env.example | 1 + fittrackee/config.py | 3 ++ fittrackee/tests/fixtures/fixtures_app.py | 8 +++++ .../workouts/test_workouts_api_1_post.py | 31 +++++++++++++++++++ fittrackee/workouts/utils.py | 11 +++---- 5 files changed, 47 insertions(+), 7 deletions(-) diff --git a/.env.example b/.env.example index 624b16c1..c7cd6992 100644 --- a/.env.example +++ b/.env.example @@ -25,4 +25,5 @@ export REDIS_URL= # Activities export TILE_SERVER_URL= export MAP_ATTRIBUTION= +#export DEFAULT_STATICMAP=False export WEATHER_API_KEY= diff --git a/fittrackee/config.py b/fittrackee/config.py index 7bdb9148..d1bbd7f2 100644 --- a/fittrackee/config.py +++ b/fittrackee/config.py @@ -42,6 +42,9 @@ class BaseConfig: 'target="_blank" rel="noopener noreferrer">OpenStreetMap' ' contributors', ), + 'DEFAULT_STATICMAP': ( + os.environ.get('DEFAULT_STATICMAP', 'False') == 'True' + ), } diff --git a/fittrackee/tests/fixtures/fixtures_app.py b/fittrackee/tests/fixtures/fixtures_app.py index 1de0b0ae..810f92fa 100644 --- a/fittrackee/tests/fixtures/fixtures_app.py +++ b/fittrackee/tests/fixtures/fixtures_app.py @@ -75,6 +75,14 @@ def app(monkeypatch: pytest.MonkeyPatch) -> Generator: monkeypatch.delenv('TILE_SERVER_URL') if os.getenv('MAP_ATTRIBUTION'): monkeypatch.delenv('MAP_ATTRIBUTION') + if os.getenv('DEFAULT_STATICMAP'): + monkeypatch.delenv('DEFAULT_STATICMAP') + yield from get_app(with_config=True) + + +@pytest.fixture +def app_default_static_map(monkeypatch: pytest.MonkeyPatch) -> Generator: + monkeypatch.setenv('DEFAULT_STATICMAP', 'True') yield from get_app(with_config=True) diff --git a/fittrackee/tests/workouts/test_workouts_api_1_post.py b/fittrackee/tests/workouts/test_workouts_api_1_post.py index 3850696a..aecf1142 100644 --- a/fittrackee/tests/workouts/test_workouts_api_1_post.py +++ b/fittrackee/tests/workouts/test_workouts_api_1_post.py @@ -360,6 +360,37 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin): in call_args[0] ) + def test_it_calls_default_tile_server_for_static_map( + self, + app_default_static_map: Flask, + user_1: User, + sport_1_cycling: Sport, + gpx_file: str, + static_map_get_mock: Mock, + ) -> None: + client, auth_token = self.get_test_client_and_auth_token( + app_default_static_map + ) + 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}', + ), + ) + + call_args = self.get_args(static_map_get_mock.call_args) + assert ( + app_default_static_map.config['TILE_SERVER']['URL'] + .replace('{s}.', '') + .replace('/{z}/{x}/{y}.png', '') + not in call_args[0] + ) + def test_it_returns_500_if_gpx_file_has_not_tracks( self, app: Flask, diff --git a/fittrackee/workouts/utils.py b/fittrackee/workouts/utils.py index 3e07080d..35cf6598 100644 --- a/fittrackee/workouts/utils.py +++ b/fittrackee/workouts/utils.py @@ -265,14 +265,11 @@ def generate_map(map_filepath: str, map_data: List) -> None: """ Generate and save map image from map data """ - m = StaticMap( - 400, - 225, - 10, - url_template=current_app.config['TILE_SERVER']['URL'].replace( + m = StaticMap(400, 225, 10) + if not current_app.config['TILE_SERVER']['DEFAULT_STATICMAP']: + m.url_template = current_app.config['TILE_SERVER']['URL'].replace( '{s}.', '' - ), - ) + ) line = Line(map_data, '#3388FF', 4) m.add_line(line) image = m.render()