From 0eaae0e171cd4934998782c2552e16e57aaa6608 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 13 Jul 2022 09:39:33 +0200 Subject: [PATCH] API - add user agent when generating static map - fix #210 --- fittrackee/tests/mixins.py | 14 ++++- .../workouts/test_workouts_api_1_post.py | 63 ++++++++++++++++++- fittrackee/workouts/utils/maps.py | 2 + 3 files changed, 75 insertions(+), 4 deletions(-) diff --git a/fittrackee/tests/mixins.py b/fittrackee/tests/mixins.py index 22eb89e6..472a24a5 100644 --- a/fittrackee/tests/mixins.py +++ b/fittrackee/tests/mixins.py @@ -1,5 +1,5 @@ import json -from typing import Any, Dict, Optional, Tuple +from typing import Dict, Optional, Tuple from flask import Flask from flask.testing import FlaskClient @@ -115,10 +115,20 @@ class ApiTestCaseMixin(RandomMixin): class CallArgsMixin: + """call args are returned differently between Python 3.7 and 3.7+""" + @staticmethod - def get_args(call_args: Any) -> Any: + def get_args(call_args: Tuple) -> Tuple: if len(call_args) == 2: args, _ = call_args else: _, args, _ = call_args return args + + @staticmethod + def get_kwargs(call_args: Tuple) -> Dict: + if len(call_args) == 2: + _, kwargs = call_args + else: + _, _, kwargs = call_args + return kwargs diff --git a/fittrackee/tests/workouts/test_workouts_api_1_post.py b/fittrackee/tests/workouts/test_workouts_api_1_post.py index 1cc1bb94..a77dbd0e 100644 --- a/fittrackee/tests/workouts/test_workouts_api_1_post.py +++ b/fittrackee/tests/workouts/test_workouts_api_1_post.py @@ -9,6 +9,7 @@ from unittest.mock import Mock import pytest from flask import Flask +from fittrackee import VERSION from fittrackee.users.models import User from fittrackee.workouts.models import Sport, Workout @@ -442,7 +443,7 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin): assert len(data['data']['workouts']) == 1 assert data['data']['workouts'][0]['notes'] == input_notes - def test_it_calls_configured_tile_server_for_static_map( + def test_it_calls_configured_tile_server_for_static_map_when_default_static_map_to_false( # noqa self, app: Flask, user_1: User, @@ -473,7 +474,36 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin): in call_args[0] ) - def test_it_calls_default_tile_server_for_static_map( + def test_it_calls_static_map_with_fittrackee_user_agent_when_default_static_map_to_false( # noqa + self, + app: 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, 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}', + ), + ) + + call_kwargs = self.get_kwargs(static_map_get_mock.call_args) + + assert call_kwargs['headers'] == { + 'User-Agent': f'FitTrackee v{VERSION}' + } + + def test_it_calls_default_tile_server_for_static_map_when_default_static_map_to_true( # noqa self, app_default_static_map: Flask, user_1: User, @@ -504,6 +534,35 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin): not in call_args[0] ) + def test_it_calls_static_map_with_fittrackee_user_agent_when_default_static_map_to_true( # noqa + 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, 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}', + ), + ) + + call_kwargs = self.get_kwargs(static_map_get_mock.call_args) + + assert call_kwargs['headers'] == { + 'User-Agent': f'FitTrackee v{VERSION}' + } + def test_it_returns_500_if_gpx_file_has_not_tracks( self, app: Flask, diff --git a/fittrackee/workouts/utils/maps.py b/fittrackee/workouts/utils/maps.py index b9f4e6c2..57cec069 100644 --- a/fittrackee/workouts/utils/maps.py +++ b/fittrackee/workouts/utils/maps.py @@ -4,6 +4,7 @@ from typing import List from flask import current_app from staticmap import Line, StaticMap +from fittrackee import VERSION from fittrackee.files import get_absolute_file_path @@ -12,6 +13,7 @@ def generate_map(map_filepath: str, map_data: List) -> None: Generate and save map image from map data """ m = StaticMap(400, 225, 10) + m.headers = {'User-Agent': f'FitTrackee v{VERSION}'} if not current_app.config['TILE_SERVER']['DEFAULT_STATICMAP']: m.url_template = current_app.config['TILE_SERVER']['URL'].replace( '{s}.', ''