From 8a18ded2bcb6fc2ab5d36d365eaa718eaa6c58d3 Mon Sep 17 00:00:00 2001 From: Pouzor Date: Sat, 7 Mar 2026 01:01:24 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20remove=20trailing=20slash=20from=20GET/P?= =?UTF-8?q?OST=20routes=20to=20prevent=20307=E2=86=92403=20redirect?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit FastAPI's redirect_slashes=True causes GET /api/v1/canvas to 307 redirect to /api/v1/canvas/ — axios follows the redirect but drops the Authorization header, resulting in 403. Fixed by declaring routes as empty string instead of '/' so no redirect is issued. Same fix applied to nodes and edges routes. Updated all tests to use paths without trailing slashes. --- backend/app/api/routes/canvas.py | 2 +- backend/app/api/routes/edges.py | 4 ++-- backend/app/api/routes/nodes.py | 4 ++-- backend/tests/test_auth.py | 2 +- backend/tests/test_edges.py | 16 ++++++++-------- backend/tests/test_nodes.py | 14 +++++++------- 6 files changed, 21 insertions(+), 21 deletions(-) diff --git a/backend/app/api/routes/canvas.py b/backend/app/api/routes/canvas.py index 7d28770..4b0de28 100644 --- a/backend/app/api/routes/canvas.py +++ b/backend/app/api/routes/canvas.py @@ -12,7 +12,7 @@ from app.schemas.canvas import CanvasSaveRequest, CanvasStateResponse router = APIRouter() -@router.get("/", response_model=CanvasStateResponse) +@router.get("", response_model=CanvasStateResponse) async def load_canvas(db: AsyncSession = Depends(get_db), _: str = Depends(get_current_user)): nodes = (await db.execute(select(Node))).scalars().all() edges = (await db.execute(select(Edge))).scalars().all() diff --git a/backend/app/api/routes/edges.py b/backend/app/api/routes/edges.py index 06e2b76..c546bac 100644 --- a/backend/app/api/routes/edges.py +++ b/backend/app/api/routes/edges.py @@ -10,13 +10,13 @@ from app.schemas.edges import EdgeCreate, EdgeResponse, EdgeUpdate router = APIRouter() -@router.get("/", response_model=list[EdgeResponse]) +@router.get("", response_model=list[EdgeResponse]) async def list_edges(db: AsyncSession = Depends(get_db), _: str = Depends(get_current_user)): result = await db.execute(select(Edge)) return result.scalars().all() -@router.post("/", response_model=EdgeResponse, status_code=status.HTTP_201_CREATED) +@router.post("", response_model=EdgeResponse, status_code=status.HTTP_201_CREATED) async def create_edge(body: EdgeCreate, db: AsyncSession = Depends(get_db), _: str = Depends(get_current_user)): edge = Edge(**body.model_dump()) db.add(edge) diff --git a/backend/app/api/routes/nodes.py b/backend/app/api/routes/nodes.py index b873032..d7025f1 100644 --- a/backend/app/api/routes/nodes.py +++ b/backend/app/api/routes/nodes.py @@ -10,13 +10,13 @@ from app.schemas.nodes import NodeCreate, NodeResponse, NodeUpdate router = APIRouter() -@router.get("/", response_model=list[NodeResponse]) +@router.get("", response_model=list[NodeResponse]) async def list_nodes(db: AsyncSession = Depends(get_db), _: str = Depends(get_current_user)): result = await db.execute(select(Node)) return result.scalars().all() -@router.post("/", response_model=NodeResponse, status_code=status.HTTP_201_CREATED) +@router.post("", response_model=NodeResponse, status_code=status.HTTP_201_CREATED) async def create_node(body: NodeCreate, db: AsyncSession = Depends(get_db), _: str = Depends(get_current_user)): node = Node(**body.model_dump()) db.add(node) diff --git a/backend/tests/test_auth.py b/backend/tests/test_auth.py index 932b71b..355f540 100644 --- a/backend/tests/test_auth.py +++ b/backend/tests/test_auth.py @@ -29,7 +29,7 @@ async def test_login_wrong_username(client: AsyncClient, mock_credentials): async def test_protected_route_requires_auth(client: AsyncClient): - res = await client.get("/api/v1/nodes/") + res = await client.get("/api/v1/nodes") assert res.status_code == 403 diff --git a/backend/tests/test_edges.py b/backend/tests/test_edges.py index dab56d2..5ca1d1b 100644 --- a/backend/tests/test_edges.py +++ b/backend/tests/test_edges.py @@ -16,14 +16,14 @@ async def headers(client: AsyncClient): @pytest.fixture async def two_nodes(client: AsyncClient, headers: dict): - n1 = (await client.post("/api/v1/nodes/", json={"type": "router", "label": "R1", "status": "online"}, headers=headers)).json() - n2 = (await client.post("/api/v1/nodes/", json={"type": "switch", "label": "SW1", "status": "online"}, headers=headers)).json() + n1 = (await client.post("/api/v1/nodes", json={"type": "router", "label": "R1", "status": "online"}, headers=headers)).json() + n2 = (await client.post("/api/v1/nodes", json={"type": "switch", "label": "SW1", "status": "online"}, headers=headers)).json() return n1["id"], n2["id"] async def test_create_edge(client: AsyncClient, headers: dict, two_nodes): src, tgt = two_nodes - res = await client.post("/api/v1/edges/", json={"source": src, "target": tgt, "type": "ethernet"}, headers=headers) + res = await client.post("/api/v1/edges", json={"source": src, "target": tgt, "type": "ethernet"}, headers=headers) assert res.status_code == 201 data = res.json() assert data["source"] == src @@ -33,22 +33,22 @@ async def test_create_edge(client: AsyncClient, headers: dict, two_nodes): async def test_create_vlan_edge(client: AsyncClient, headers: dict, two_nodes): src, tgt = two_nodes - res = await client.post("/api/v1/edges/", json={"source": src, "target": tgt, "type": "vlan", "vlan_id": 20, "label": "VLAN 20"}, headers=headers) + res = await client.post("/api/v1/edges", json={"source": src, "target": tgt, "type": "vlan", "vlan_id": 20, "label": "VLAN 20"}, headers=headers) assert res.status_code == 201 assert res.json()["vlan_id"] == 20 async def test_list_edges(client: AsyncClient, headers: dict, two_nodes): src, tgt = two_nodes - await client.post("/api/v1/edges/", json={"source": src, "target": tgt, "type": "ethernet"}, headers=headers) - res = await client.get("/api/v1/edges/", headers=headers) + await client.post("/api/v1/edges", json={"source": src, "target": tgt, "type": "ethernet"}, headers=headers) + res = await client.get("/api/v1/edges", headers=headers) assert res.status_code == 200 assert len(res.json()) == 1 async def test_delete_edge(client: AsyncClient, headers: dict, two_nodes): src, tgt = two_nodes - edge_id = (await client.post("/api/v1/edges/", json={"source": src, "target": tgt, "type": "wifi"}, headers=headers)).json()["id"] + edge_id = (await client.post("/api/v1/edges", json={"source": src, "target": tgt, "type": "wifi"}, headers=headers)).json()["id"] res = await client.delete(f"/api/v1/edges/{edge_id}", headers=headers) assert res.status_code == 204 - assert len((await client.get("/api/v1/edges/", headers=headers)).json()) == 0 + assert len((await client.get("/api/v1/edges", headers=headers)).json()) == 0 diff --git a/backend/tests/test_nodes.py b/backend/tests/test_nodes.py index e3a55b7..37f9f0b 100644 --- a/backend/tests/test_nodes.py +++ b/backend/tests/test_nodes.py @@ -15,14 +15,14 @@ async def headers(client: AsyncClient): async def test_list_nodes_empty(client: AsyncClient, headers: dict): - res = await client.get("/api/v1/nodes/", headers=headers) + res = await client.get("/api/v1/nodes", headers=headers) assert res.status_code == 200 assert res.json() == [] async def test_create_node(client: AsyncClient, headers: dict): payload = {"type": "server", "label": "My Server", "ip": "192.168.1.10", "status": "unknown"} - res = await client.post("/api/v1/nodes/", json=payload, headers=headers) + res = await client.post("/api/v1/nodes", json=payload, headers=headers) assert res.status_code == 201 data = res.json() assert data["label"] == "My Server" @@ -31,7 +31,7 @@ async def test_create_node(client: AsyncClient, headers: dict): async def test_get_node(client: AsyncClient, headers: dict): - create = await client.post("/api/v1/nodes/", json={"type": "router", "label": "Router", "status": "online"}, headers=headers) + create = await client.post("/api/v1/nodes", json={"type": "router", "label": "Router", "status": "online"}, headers=headers) node_id = create.json()["id"] res = await client.get(f"/api/v1/nodes/{node_id}", headers=headers) assert res.status_code == 200 @@ -44,7 +44,7 @@ async def test_get_node_not_found(client: AsyncClient, headers: dict): async def test_update_node(client: AsyncClient, headers: dict): - create = await client.post("/api/v1/nodes/", json={"type": "server", "label": "Old", "status": "unknown"}, headers=headers) + create = await client.post("/api/v1/nodes", json={"type": "server", "label": "Old", "status": "unknown"}, headers=headers) node_id = create.json()["id"] res = await client.patch(f"/api/v1/nodes/{node_id}", json={"label": "New", "ip": "10.0.0.1"}, headers=headers) assert res.status_code == 200 @@ -53,7 +53,7 @@ async def test_update_node(client: AsyncClient, headers: dict): async def test_delete_node(client: AsyncClient, headers: dict): - create = await client.post("/api/v1/nodes/", json={"type": "switch", "label": "Switch", "status": "unknown"}, headers=headers) + create = await client.post("/api/v1/nodes", json={"type": "switch", "label": "Switch", "status": "unknown"}, headers=headers) node_id = create.json()["id"] res = await client.delete(f"/api/v1/nodes/{node_id}", headers=headers) assert res.status_code == 204 @@ -62,6 +62,6 @@ async def test_delete_node(client: AsyncClient, headers: dict): async def test_list_nodes_returns_all(client: AsyncClient, headers: dict): for i in range(3): - await client.post("/api/v1/nodes/", json={"type": "generic", "label": f"Node {i}", "status": "unknown"}, headers=headers) - res = await client.get("/api/v1/nodes/", headers=headers) + await client.post("/api/v1/nodes", json={"type": "generic", "label": f"Node {i}", "status": "unknown"}, headers=headers) + res = await client.get("/api/v1/nodes", headers=headers) assert len(res.json()) == 3