test: 补充基于 TestClient 的路由级集成测试,锁死并验证 group_id 包含斜杠时的路径转换器匹配行为
This commit is contained in:
@@ -92,3 +92,57 @@ def test_create_and_update_website_group(db_session, monkeypatch):
|
||||
# Verify that local binding was updated
|
||||
db_session.refresh(b)
|
||||
assert b.target_group_name == "UpdatedGroup"
|
||||
|
||||
|
||||
def test_update_website_group_route_handles_slash(db_session, monkeypatch):
|
||||
from fastapi.testclient import TestClient
|
||||
from app.main import app
|
||||
from app.database import get_db
|
||||
from app.utils.auth import get_current_user
|
||||
|
||||
w = Website(
|
||||
name="W1",
|
||||
site_type="sub2api",
|
||||
base_url="http://w1",
|
||||
enabled=True,
|
||||
auth_config_json="{}",
|
||||
timeout_seconds=30
|
||||
)
|
||||
db_session.add(w)
|
||||
db_session.commit()
|
||||
db_session.refresh(w)
|
||||
|
||||
called_update = []
|
||||
|
||||
class FakeClient:
|
||||
def __init__(self, *args, **kwargs):
|
||||
pass
|
||||
def __enter__(self):
|
||||
return self
|
||||
def __exit__(self, *args):
|
||||
pass
|
||||
def update_group(self, endpoint_template, group_id, body):
|
||||
called_update.append((endpoint_template, group_id, body))
|
||||
return {"id": group_id, "name": body["name"], "description": body.get("description")}
|
||||
|
||||
monkeypatch.setattr("app.routers.websites._client", lambda website: FakeClient())
|
||||
|
||||
# Set dependency overrides
|
||||
app.dependency_overrides[get_db] = lambda: db_session
|
||||
app.dependency_overrides[get_current_user] = lambda: None
|
||||
|
||||
try:
|
||||
client = TestClient(app)
|
||||
# request URL with urlencoded slash: g%2F123
|
||||
resp = client.put(
|
||||
f"/api/websites/{w.id}/groups/g%2F123",
|
||||
json={"name": "UpdatedGroup", "description": "UpdatedDesc"}
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
data = resp.json()
|
||||
assert data["id"] == "g/123"
|
||||
assert data["name"] == "UpdatedGroup"
|
||||
assert len(called_update) == 1
|
||||
assert called_update[0][1] == "g/123"
|
||||
finally:
|
||||
app.dependency_overrides.clear()
|
||||
|
||||
Reference in New Issue
Block a user