API - fix user agent infos in emails after dependencies update
(user-agent parsing has been removed in Werkzeug 2.1)
This commit is contained in:
@ -19,6 +19,7 @@ from flask_sqlalchemy import SQLAlchemy
|
||||
from sqlalchemy.exc import ProgrammingError
|
||||
|
||||
from fittrackee.emails.email import EmailService
|
||||
from fittrackee.request import CustomRequest
|
||||
|
||||
VERSION = __version__ = '0.6.1'
|
||||
db = SQLAlchemy()
|
||||
@ -35,9 +36,17 @@ logging.basicConfig(
|
||||
appLog = logging.getLogger('fittrackee')
|
||||
|
||||
|
||||
class CustomFlask(Flask):
|
||||
# add custom Request to handle user-agent parsing
|
||||
# (removed in Werkzeug 2.1)
|
||||
request_class = CustomRequest
|
||||
|
||||
|
||||
def create_app() -> Flask:
|
||||
# instantiate the app
|
||||
app = Flask(__name__, static_folder='dist/static', template_folder='dist')
|
||||
app = CustomFlask(
|
||||
__name__, static_folder='dist/static', template_folder='dist'
|
||||
)
|
||||
|
||||
# set config
|
||||
with app.app_context():
|
||||
@ -105,7 +114,7 @@ def create_app() -> Flask:
|
||||
appLog.setLevel(logging.DEBUG)
|
||||
|
||||
# Enable CORS
|
||||
@app.after_request
|
||||
@app.after_request # type: ignore
|
||||
def after_request(response: Response) -> Response:
|
||||
response.headers.add('Access-Control-Allow-Origin', '*')
|
||||
response.headers.add(
|
||||
|
24
fittrackee/request.py
Normal file
24
fittrackee/request.py
Normal file
@ -0,0 +1,24 @@
|
||||
from typing import Optional, Tuple
|
||||
|
||||
from flask import Request
|
||||
from ua_parser import user_agent_parser
|
||||
from werkzeug.user_agent import UserAgent as IUserAgent
|
||||
|
||||
|
||||
class UserAgent(IUserAgent):
|
||||
def __init__(self, string: str):
|
||||
super().__init__(string)
|
||||
self.platform, self.browser = self._parse_user_agent(self.string)
|
||||
|
||||
@staticmethod
|
||||
def _parse_user_agent(
|
||||
user_agent: str,
|
||||
) -> Tuple[Optional[str], Optional[str]]:
|
||||
parsed_string = user_agent_parser.Parse(user_agent)
|
||||
platform = parsed_string.get('os', {}).get('family')
|
||||
browser = parsed_string.get('user_agent', {}).get('family')
|
||||
return platform, browser
|
||||
|
||||
|
||||
class CustomRequest(Request):
|
||||
user_agent_class = UserAgent
|
@ -4,6 +4,7 @@ from uuid import uuid4
|
||||
import pytest
|
||||
|
||||
from fittrackee.files import display_readable_file_size
|
||||
from fittrackee.request import UserAgent
|
||||
from fittrackee.utils import get_readable_duration
|
||||
|
||||
|
||||
@ -42,3 +43,28 @@ class TestReadableDuration:
|
||||
readable_duration = get_readable_duration(30, locale)
|
||||
|
||||
assert readable_duration == expected_duration
|
||||
|
||||
|
||||
class TestParseUserAgent:
|
||||
string = (
|
||||
'Mozilla/5.0 (X11; Linux x86_64; rv:98.0) '
|
||||
'Gecko/20100101 Firefox/98.0'
|
||||
)
|
||||
|
||||
def test_it_returns_browser_name(self) -> None:
|
||||
user_agent = UserAgent(self.string)
|
||||
assert user_agent.browser == 'Firefox'
|
||||
|
||||
def test_it_returns_other_as_brother_name_when_empty_string_provided(
|
||||
self,
|
||||
) -> None:
|
||||
user_agent = UserAgent('')
|
||||
assert user_agent.browser == 'Other'
|
||||
|
||||
def test_it_returns_operating_system(self) -> None:
|
||||
user_agent = UserAgent(self.string)
|
||||
assert user_agent.platform == 'Linux'
|
||||
|
||||
def test_it_returns_other_as_os_when_empty_string_provided(self) -> None:
|
||||
user_agent = UserAgent('')
|
||||
assert user_agent.platform == 'Other'
|
||||
|
@ -285,8 +285,8 @@ class TestUserRegistration(ApiTestCaseMixin):
|
||||
{
|
||||
'username': username,
|
||||
'fittrackee_url': 'http://0.0.0.0:5000',
|
||||
'operating_system': 'linux',
|
||||
'browser_name': 'firefox',
|
||||
'operating_system': 'Linux',
|
||||
'browser_name': 'Firefox',
|
||||
'account_confirmation_url': (
|
||||
'http://0.0.0.0:5000/account-confirmation'
|
||||
f'?token={expected_token}'
|
||||
@ -807,8 +807,8 @@ class TestUserAccountUpdate(ApiTestCaseMixin):
|
||||
{
|
||||
'username': user_1.username,
|
||||
'fittrackee_url': 'http://0.0.0.0:5000',
|
||||
'operating_system': 'linux',
|
||||
'browser_name': 'firefox',
|
||||
'operating_system': 'Linux',
|
||||
'browser_name': 'Firefox',
|
||||
'new_email_address': new_email,
|
||||
},
|
||||
)
|
||||
@ -849,8 +849,8 @@ class TestUserAccountUpdate(ApiTestCaseMixin):
|
||||
{
|
||||
'username': user_1.username,
|
||||
'fittrackee_url': 'http://0.0.0.0:5000',
|
||||
'operating_system': 'linux',
|
||||
'browser_name': 'firefox',
|
||||
'operating_system': 'Linux',
|
||||
'browser_name': 'Firefox',
|
||||
'email_confirmation_url': (
|
||||
f'http://0.0.0.0:5000/email-update?token={expected_token}'
|
||||
),
|
||||
@ -1009,8 +1009,8 @@ class TestUserAccountUpdate(ApiTestCaseMixin):
|
||||
{
|
||||
'username': user_1.username,
|
||||
'fittrackee_url': 'http://0.0.0.0:5000',
|
||||
'operating_system': 'linux',
|
||||
'browser_name': 'firefox',
|
||||
'operating_system': 'Linux',
|
||||
'browser_name': 'Firefox',
|
||||
},
|
||||
)
|
||||
|
||||
@ -1690,8 +1690,8 @@ class TestPasswordResetRequest(ApiTestCaseMixin):
|
||||
f'http://0.0.0.0:5000/password-reset?token={token}'
|
||||
),
|
||||
'fittrackee_url': 'http://0.0.0.0:5000',
|
||||
'operating_system': 'linux',
|
||||
'browser_name': 'firefox',
|
||||
'operating_system': 'Linux',
|
||||
'browser_name': 'Firefox',
|
||||
},
|
||||
)
|
||||
|
||||
@ -1903,8 +1903,8 @@ class TestPasswordUpdate(ApiTestCaseMixin):
|
||||
{
|
||||
'username': user_1.username,
|
||||
'fittrackee_url': 'http://0.0.0.0:5000',
|
||||
'operating_system': 'linux',
|
||||
'browser_name': 'firefox',
|
||||
'operating_system': 'Linux',
|
||||
'browser_name': 'Firefox',
|
||||
},
|
||||
)
|
||||
|
||||
@ -2130,8 +2130,8 @@ class TestResendAccountConfirmationEmail(ApiTestCaseMixin):
|
||||
{
|
||||
'username': inactive_user.username,
|
||||
'fittrackee_url': 'http://0.0.0.0:5000',
|
||||
'operating_system': 'linux',
|
||||
'browser_name': 'firefox',
|
||||
'operating_system': 'Linux',
|
||||
'browser_name': 'Firefox',
|
||||
'account_confirmation_url': (
|
||||
'http://0.0.0.0:5000/account-confirmation'
|
||||
f'?token={expected_token}'
|
||||
|
Reference in New Issue
Block a user