From 11c81e6113b17250f9bac3f44cca17a9ebe143f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20B=C3=BChler?= <17296905+buehlefs@users.noreply.github.com> Date: Mon, 19 Feb 2024 14:37:05 +0100 Subject: [PATCH] Fix unittests Create blueprint with routes together in register routes function --- muse_for_music/routes.py | 35 ++++++++++++++++++----------------- pyproject.toml | 8 ++++++++ 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/muse_for_music/routes.py b/muse_for_music/routes.py index 956a690..df89594 100644 --- a/muse_for_music/routes.py +++ b/muse_for_music/routes.py @@ -5,23 +5,6 @@ from .user_api import register_user_api from .api import register_api -UI_BP = Blueprint("ui_blueprint", __name__, url_prefix="/") - - -@UI_BP.before_request -def before_requests(): - nonce = getattr(g, 'nonce', '') - if not nonce: - g.nonce = token_urlsafe(32) - - -@UI_BP.after_request -def inject_csp_headers(response: Response): - nonce = getattr(g, 'nonce', '') - script_nonce = f"'nonce-{nonce}'" if nonce else "" - response.headers["Content-Security-Policy"] = f"default-src 'self'; script-src {script_nonce} 'self'; style-src 'self' 'unsafe-inline'" - return response - def register_routes(app: Flask, flask_static_digest): @@ -33,6 +16,24 @@ def register_routes(app: Flask, flask_static_digest): register_debug_routes(app) + UI_BP = Blueprint("ui_blueprint", __name__, url_prefix="/") + + + @UI_BP.before_request + def before_requests(): + nonce = getattr(g, 'nonce', '') + if not nonce: + g.nonce = token_urlsafe(32) + + + @UI_BP.after_request + def inject_csp_headers(response: Response): + nonce = getattr(g, 'nonce', '') + script_nonce = f"'nonce-{nonce}'" if nonce else "" + response.headers["Content-Security-Policy"] = f"default-src 'self'; script-src {script_nonce} 'self'; style-src 'self' 'unsafe-inline'" + return response + + @UI_BP.route('/', defaults={'path': ''}) @UI_BP.route('/') def index(path: str): diff --git a/pyproject.toml b/pyproject.toml index c1f6761..161dc2e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,6 +43,14 @@ sphinx-click = "^2.5.0" line-length = 90 include = '\.pyi?$' +[tool.pytest.ini_options] +pythonpath=[ + ".", +] +testpaths=[ + "tests", +] + [tool.sphinx] copyright-year = 2021 enable-autodoc = true