mirror of
https://github.com/zvx-echo6/central.git
synced 2026-05-21 18:14:44 +02:00
feat(gui): implement first-run setup wizard (1b-8) (#24)
* feat(gui): implement first-run setup wizard (1b-8) Add a 5-step setup wizard that replaces the single-step /setup: 1. Create Operator - create initial operator account 2. System Settings - configure map tile URL and attribution 3. API Keys - optionally add API keys for adapters 4. Configure Adapters - enable/disable adapters with region picker 5. Finish Setup - review and complete setup Key changes: - Update middleware to handle wizard URL structure and step routing - Add wizard routes for each step with proper auth checks - Create new templates using base_wizard.html for consistent styling - Add audit events for system.update and setup.complete - Update tests for new middleware behavior Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix(gui): handle CSRF errors on wizard paths Update csrf_exception_handler to re-render wizard forms with error message instead of redirecting to /login when CSRF validation fails. - /setup/operator: re-render with error - /setup/system: re-render with current system values + error - /setup/keys: re-render with current keys list + error - /setup/adapters: re-render with current adapter config + error - /setup/finish: re-render with summary data + error - /setup: redirect to /setup (middleware routes to appropriate step) Add error display to setup_keys.html and setup_finish.html templates. Add 7 new CSRF handler tests for wizard paths. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix(gui): region picker render + click-to-draw Bug A: Maps render blank on /setup/adapters for FIRMS and USGS because Leaflet computed zero dimensions before container layout settled. Fix: add setTimeout invalidateSize() after map creation. Bug B: No click-to-draw functionality - only drag corners. Fix: add L.Control.Draw for rectangle drawing with CREATED event handler to replace existing rectangle. Both fixes applied to: - setup_adapters.html (wizard inline JS) - _region_picker.html (standalone edit page) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix(gui): handle revisiting /setup/operator after operator created When an operator already exists, /setup/operator now shows a confirmation page instead of the create form. This prevents: - Unique constraint violations on duplicate username - Silent creation of duplicate operators GET /setup/operator: queries config.operators; if any exist, renders confirmation state with existing_operator context. POST /setup/operator: checks operator count before INSERT; if non-zero, renders confirmation state without inserting. Template updated with conditional to show "Operator Already Configured" message when existing_operator is set. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix(csrf): replace fastapi-csrf-protect with session-bound CSRF Fixes CSRF race condition where every GET rotated the CSRF token, causing POST failures when users had multiple tabs or slow connections. Changes: - Remove fastapi-csrf-protect dependency - Add session-bound CSRF tokens stored in config.sessions table - Add pre-auth CSRF for unauthenticated routes (/login, /setup/operator) - Add csrf.py module for pre-auth token generation/validation - Update routes to use new CSRF token handling - Add migration 013 to add csrf_token column to sessions The session-bound approach ensures CSRF tokens remain stable for the duration of a session, eliminating the race condition. Note: Route tests (test_wizard.py, test_adapters.py, etc.) need refactoring to mock get_settings() instead of CsrfProtect dependency. Core auth/CSRF handler tests pass (74 tests). Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(csrf): update test suite for session-bound CSRF tokens - Add CSRF fixtures to conftest.py for pre-auth and session CSRF - Update test_wizard.py: use bypass_pre_auth_csrf and patch_route_settings - Update test_adapters.py: set request.state.csrf_token and form mock data - Update test_api_keys.py: add CSRF token to form data for POST routes - Update test_streams.py: change return_value to side_effect for CSRF support - Update test_region_picker.py: add CSRF token handling - Update test_config_store.py: set CENTRAL_CSRF_SECRET env var in fixture All 285 tests now pass with session-bound CSRF validation. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com> Co-authored-by: Matt Johnson <mj@k7zvx.com>
This commit is contained in:
parent
96ec88883c
commit
494ad1c799
28 changed files with 2897 additions and 377 deletions
|
|
@ -14,23 +14,18 @@ class TestCsrfExceptionHandlerRegistered:
|
|||
"""Verify CSRF exception handler is properly registered."""
|
||||
|
||||
def test_csrf_exception_handler_is_registered(self):
|
||||
"""The app has a CsrfProtectError exception handler registered."""
|
||||
"""The app has a CsrfValidationError exception handler registered."""
|
||||
from central.gui import app
|
||||
from fastapi_csrf_protect.exceptions import CsrfProtectError
|
||||
from central.gui.auth import CsrfValidationError
|
||||
|
||||
assert CsrfProtectError in app.exception_handlers, \
|
||||
"CsrfProtectError handler should be registered"
|
||||
assert CsrfValidationError in app.exception_handlers, \
|
||||
"CsrfValidationError handler should be registered"
|
||||
|
||||
def test_csrf_subclasses_are_caught(self):
|
||||
"""MissingTokenError and TokenValidationError inherit from CsrfProtectError."""
|
||||
from fastapi_csrf_protect.exceptions import (
|
||||
CsrfProtectError,
|
||||
MissingTokenError,
|
||||
TokenValidationError,
|
||||
)
|
||||
def test_csrf_validation_error_is_exception(self):
|
||||
"""CsrfValidationError is a proper Exception subclass."""
|
||||
from central.gui.auth import CsrfValidationError
|
||||
|
||||
assert issubclass(MissingTokenError, CsrfProtectError)
|
||||
assert issubclass(TokenValidationError, CsrfProtectError)
|
||||
assert issubclass(CsrfValidationError, Exception)
|
||||
|
||||
|
||||
class TestCsrfExceptionHandlerBehavior:
|
||||
|
|
@ -40,10 +35,10 @@ class TestCsrfExceptionHandlerBehavior:
|
|||
"""CSRF handler checks request path for /login."""
|
||||
import inspect
|
||||
from central.gui import _create_app
|
||||
from fastapi_csrf_protect.exceptions import CsrfProtectError
|
||||
from central.gui.auth import CsrfValidationError
|
||||
|
||||
app = _create_app()
|
||||
handler = app.exception_handlers.get(CsrfProtectError)
|
||||
handler = app.exception_handlers.get(CsrfValidationError)
|
||||
|
||||
# Verify handler source contains /login path check
|
||||
source = inspect.getsource(handler)
|
||||
|
|
@ -54,17 +49,16 @@ class TestCsrfExceptionHandlerBehavior:
|
|||
async def test_logout_csrf_error_redirects_to_login(self):
|
||||
"""CSRF error on /logout should redirect to /login."""
|
||||
from central.gui import _create_app
|
||||
from fastapi_csrf_protect.exceptions import TokenValidationError
|
||||
from central.gui.auth import CsrfValidationError
|
||||
from fastapi.responses import RedirectResponse
|
||||
|
||||
app = _create_app()
|
||||
from fastapi_csrf_protect.exceptions import CsrfProtectError
|
||||
handler = app.exception_handlers.get(CsrfProtectError)
|
||||
handler = app.exception_handlers.get(CsrfValidationError)
|
||||
|
||||
mock_request = MagicMock()
|
||||
mock_request.url.path = "/logout"
|
||||
|
||||
exc = TokenValidationError("Invalid token")
|
||||
exc = CsrfValidationError("Invalid token")
|
||||
|
||||
result = await handler(mock_request, exc)
|
||||
|
||||
|
|
@ -75,17 +69,16 @@ class TestCsrfExceptionHandlerBehavior:
|
|||
async def test_adapters_csrf_error_redirects_to_adapters(self):
|
||||
"""CSRF error on /adapters/{name} should redirect to /adapters."""
|
||||
from central.gui import _create_app
|
||||
from fastapi_csrf_protect.exceptions import TokenValidationError
|
||||
from central.gui.auth import CsrfValidationError
|
||||
from fastapi.responses import RedirectResponse
|
||||
|
||||
app = _create_app()
|
||||
from fastapi_csrf_protect.exceptions import CsrfProtectError
|
||||
handler = app.exception_handlers.get(CsrfProtectError)
|
||||
handler = app.exception_handlers.get(CsrfValidationError)
|
||||
|
||||
mock_request = MagicMock()
|
||||
mock_request.url.path = "/adapters/nws"
|
||||
|
||||
exc = TokenValidationError("Invalid token")
|
||||
exc = CsrfValidationError("Invalid token")
|
||||
|
||||
result = await handler(mock_request, exc)
|
||||
|
||||
|
|
@ -94,16 +87,171 @@ class TestCsrfExceptionHandlerBehavior:
|
|||
|
||||
|
||||
class TestCsrfHandlerNoTraceback:
|
||||
"""Verify exception handler doesn't expose Python internals."""
|
||||
"""Verify exception handler does not expose Python internals."""
|
||||
|
||||
def test_handler_exists_and_is_async(self):
|
||||
"""The CSRF handler should be an async function."""
|
||||
import inspect
|
||||
from central.gui import _create_app
|
||||
from fastapi_csrf_protect.exceptions import CsrfProtectError
|
||||
from central.gui.auth import CsrfValidationError
|
||||
|
||||
app = _create_app()
|
||||
handler = app.exception_handlers.get(CsrfProtectError)
|
||||
handler = app.exception_handlers.get(CsrfValidationError)
|
||||
|
||||
assert handler is not None
|
||||
assert inspect.iscoroutinefunction(handler)
|
||||
|
||||
|
||||
class TestCsrfHandlerWizardPaths:
|
||||
"""Test CSRF exception handler for wizard paths."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_operator_csrf_error_renders_form_with_error(self):
|
||||
"""CSRF error on /setup/operator re-renders form with error message."""
|
||||
from central.gui import _create_app
|
||||
from central.gui.auth import CsrfValidationError
|
||||
|
||||
app = _create_app()
|
||||
handler = app.exception_handlers.get(CsrfValidationError)
|
||||
|
||||
mock_request = MagicMock()
|
||||
mock_request.url.path = "/setup/operator"
|
||||
|
||||
exc = CsrfValidationError("Invalid token")
|
||||
|
||||
result = await handler(mock_request, exc)
|
||||
|
||||
# Should be HTML response, not redirect
|
||||
assert hasattr(result, "body")
|
||||
assert result.status_code == 200
|
||||
body = result.body.decode() if hasattr(result.body, "decode") else str(result.body)
|
||||
assert "session expired" in body.lower()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_system_csrf_error_renders_form_with_error(self):
|
||||
"""CSRF error on /setup/system re-renders form with error message."""
|
||||
from central.gui import _create_app
|
||||
from central.gui.auth import CsrfValidationError
|
||||
|
||||
app = _create_app()
|
||||
handler = app.exception_handlers.get(CsrfValidationError)
|
||||
|
||||
mock_request = MagicMock()
|
||||
mock_request.url.path = "/setup/system"
|
||||
|
||||
exc = CsrfValidationError("Invalid token")
|
||||
|
||||
with patch("central.gui.db.get_pool", return_value=None):
|
||||
result = await handler(mock_request, exc)
|
||||
|
||||
assert hasattr(result, "body")
|
||||
assert result.status_code == 200
|
||||
body = result.body.decode() if hasattr(result.body, "decode") else str(result.body)
|
||||
assert "session expired" in body.lower()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_keys_csrf_error_renders_form_with_error(self):
|
||||
"""CSRF error on /setup/keys re-renders form with error message."""
|
||||
from central.gui import _create_app
|
||||
from central.gui.auth import CsrfValidationError
|
||||
|
||||
app = _create_app()
|
||||
handler = app.exception_handlers.get(CsrfValidationError)
|
||||
|
||||
mock_request = MagicMock()
|
||||
mock_request.url.path = "/setup/keys"
|
||||
|
||||
exc = CsrfValidationError("Invalid token")
|
||||
|
||||
with patch("central.gui.db.get_pool", return_value=None):
|
||||
result = await handler(mock_request, exc)
|
||||
|
||||
assert hasattr(result, "body")
|
||||
assert result.status_code == 200
|
||||
body = result.body.decode() if hasattr(result.body, "decode") else str(result.body)
|
||||
assert "session expired" in body.lower()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_adapters_csrf_error_renders_form_with_error(self):
|
||||
"""CSRF error on /setup/adapters re-renders form with error message."""
|
||||
from central.gui import _create_app
|
||||
from central.gui.auth import CsrfValidationError
|
||||
|
||||
app = _create_app()
|
||||
handler = app.exception_handlers.get(CsrfValidationError)
|
||||
|
||||
mock_request = MagicMock()
|
||||
mock_request.url.path = "/setup/adapters"
|
||||
|
||||
exc = CsrfValidationError("Invalid token")
|
||||
|
||||
with patch("central.gui.db.get_pool", return_value=None):
|
||||
result = await handler(mock_request, exc)
|
||||
|
||||
assert hasattr(result, "body")
|
||||
assert result.status_code == 200
|
||||
body = result.body.decode() if hasattr(result.body, "decode") else str(result.body)
|
||||
assert "session expired" in body.lower()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_finish_csrf_error_renders_form_with_error(self):
|
||||
"""CSRF error on /setup/finish re-renders form with error message."""
|
||||
from central.gui import _create_app
|
||||
from central.gui.auth import CsrfValidationError
|
||||
|
||||
app = _create_app()
|
||||
handler = app.exception_handlers.get(CsrfValidationError)
|
||||
|
||||
mock_request = MagicMock()
|
||||
mock_request.url.path = "/setup/finish"
|
||||
|
||||
exc = CsrfValidationError("Invalid token")
|
||||
|
||||
with patch("central.gui.db.get_pool", return_value=None):
|
||||
result = await handler(mock_request, exc)
|
||||
|
||||
assert hasattr(result, "body")
|
||||
assert result.status_code == 200
|
||||
body = result.body.decode() if hasattr(result.body, "decode") else str(result.body)
|
||||
assert "session expired" in body.lower()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_base_csrf_error_redirects_to_setup(self):
|
||||
"""CSRF error on /setup redirects to /setup (middleware routes to step)."""
|
||||
from central.gui import _create_app
|
||||
from central.gui.auth import CsrfValidationError
|
||||
from fastapi.responses import RedirectResponse
|
||||
|
||||
app = _create_app()
|
||||
handler = app.exception_handlers.get(CsrfValidationError)
|
||||
|
||||
mock_request = MagicMock()
|
||||
mock_request.url.path = "/setup"
|
||||
|
||||
exc = CsrfValidationError("Invalid token")
|
||||
|
||||
result = await handler(mock_request, exc)
|
||||
|
||||
assert isinstance(result, RedirectResponse)
|
||||
assert result.status_code == 302
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_login_csrf_error_still_works(self):
|
||||
"""CSRF error on /login still renders login form with error (regression test)."""
|
||||
from central.gui import _create_app
|
||||
from central.gui.auth import CsrfValidationError
|
||||
|
||||
app = _create_app()
|
||||
handler = app.exception_handlers.get(CsrfValidationError)
|
||||
|
||||
mock_request = MagicMock()
|
||||
mock_request.url.path = "/login"
|
||||
|
||||
exc = CsrfValidationError("Invalid token")
|
||||
|
||||
result = await handler(mock_request, exc)
|
||||
|
||||
assert hasattr(result, "body")
|
||||
assert result.status_code == 200
|
||||
body = result.body.decode() if hasattr(result.body, "decode") else str(result.body)
|
||||
assert "session expired" in body.lower()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue