API - add user agent when generating static map - fix #210
This commit is contained in:
parent
66a24b3f4f
commit
0eaae0e171
@ -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
|
||||
|
@ -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,
|
||||
|
@ -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}.', ''
|
||||
|
Loading…
Reference in New Issue
Block a user