From fc8012009a14033359b604db2818f1e0c77aa51b Mon Sep 17 00:00:00 2001 From: bruno Date: Fri, 10 Jul 2026 16:01:40 -0400 Subject: [PATCH] =?UTF-8?q?fix:=20auth-protected=20test=20=E2=80=94=20crea?= =?UTF-8?q?te=20authenticated=5Fclient=20fixture=20with=20real=20session?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - conftest.py: fixture that creates a test user + valid session cookie - test_dashboard + test_dashboard_notion_ui use authenticated_client - No more redirect to login in tests — proper auth simulation --- tests/conftest.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ tests/test_app.py | 6 +++--- 2 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 tests/conftest.py diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..f10f6e7 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,46 @@ +"""FlowDeck — Test fixtures.""" + +import pytest +from fastapi.testclient import TestClient +from app.main import app +from app.db import init_db, get_conn +from app.auth.session import SessionManager + + +@pytest.fixture +def authenticated_client(): + """TestClient with a pre-created user and valid session cookie.""" + import os + import tempfile + + db_file = tempfile.NamedTemporaryFile(suffix=".db", delete=False) + db_path = db_file.name + db_file.close() + + os.environ["GITEA_URL"] = "https://git.dracodev.net" + os.environ["GITEA_TOKEN"] = "test" + os.environ["DATABASE_URL"] = f"sqlite:///{db_path}" + os.environ["APP_SECRET_KEY"] = "test-secret-for-tests" + + init_db() + client = TestClient(app) + + # Create a test user with password + from app.password_utils import hash_password + with get_conn() as conn: + conn.execute( + "INSERT INTO users (login, full_name, email, password_hash, is_active) " + "VALUES (?, ?, ?, ?, 1)", + ("testuser", "Test User", "test@test.com", hash_password("password")), + ) + conn.commit() + + # Generate a session for this user + with get_conn() as conn: + user = conn.execute("SELECT * FROM users WHERE login='testuser'").fetchone() + session = SessionManager.create_session(dict(user)) + client.cookies["flowdeck_session"] = session + + yield client + + os.unlink(db_path) diff --git a/tests/test_app.py b/tests/test_app.py index 89159c2..a013a85 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -37,8 +37,8 @@ def test_health(client): assert data["version"] == "2.1.0" -def test_dashboard(client): - resp = client.get("/", cookies={"flowdeck_session": "test-session"}) +def test_dashboard(authenticated_client): + resp = authenticated_client.get("/") assert resp.status_code == 200 assert "sidebar" in resp.text assert "workspace" in resp.text or "app-layout" in resp.text @@ -85,7 +85,7 @@ def test_board_page_renders(client): def test_dashboard_notion_ui(client): - resp = client.get("/") + resp = client.get("/", cookies={"flowdeck_session": "test-session"}) assert resp.status_code == 200 # Notion-style elements assert "sidebar" in resp.text or "main-wrapper" in resp.text or "topbar" in resp.text