fix: update E2E tests for RESTful endpoints

This commit is contained in:
qzl
2026-02-26 14:43:52 +08:00
parent 8e493ae7fd
commit 2994cc708c
3 changed files with 23 additions and 25 deletions
+1 -1
View File
@@ -78,7 +78,7 @@ def test_agent_chat_flow_e2e() -> None:
)
try:
response = request_context.post(
"/api/v1/agent-chat/run",
"/api/v1/agent-chat",
data=json.dumps({"message": "hello"}),
headers={"Content-Type": "application/json"},
)
+20 -22
View File
@@ -11,37 +11,35 @@ import uvicorn
from app import app
from core.auth.models import CurrentUser
from v1.profile.dependencies import get_current_user, get_profile_service
from v1.profile.schemas import ProfileResponse, ProfileUpdateRequest
from v1.users.dependencies import get_current_user, get_user_service
from v1.users.schemas import UserResponse, UserUpdateRequest
class FakeProfileService:
class FakeUserService:
"""Fake service for E2E testing."""
def __init__(self, profile: ProfileResponse) -> None:
self._profile = profile
def __init__(self, user: UserResponse) -> None:
self._user = user
async def get_me(self) -> ProfileResponse:
return self._profile
async def get_me(self) -> UserResponse:
return self._user
async def update_me(self, update: ProfileUpdateRequest) -> ProfileResponse:
return ProfileResponse(
id=self._profile.id,
async def update_me(self, update: UserUpdateRequest) -> UserResponse:
return UserResponse(
id=self._user.id,
username=(
update.username
if update.username is not None
else self._profile.username
update.username if update.username is not None else self._user.username
),
avatar_url=(
update.avatar_url
if update.avatar_url is not None
else self._profile.avatar_url
else self._user.avatar_url
),
bio=update.bio if update.bio is not None else self._profile.bio,
bio=update.bio if update.bio is not None else self._user.bio,
)
async def get_by_username(self, username: str) -> ProfileResponse:
return self._profile
async def get_by_username(self, username: str) -> UserResponse:
return self._user
def _find_free_port() -> int:
@@ -71,13 +69,13 @@ def _start_server(host: str, port: int):
def test_profile_flow_e2e() -> None:
user_id = UUID("00000000-0000-0000-0000-000000000001")
profile = ProfileResponse(
user = UserResponse(
id=str(user_id),
username="demo",
avatar_url=None,
bio=None,
)
app.dependency_overrides[get_profile_service] = lambda: FakeProfileService(profile) # type: ignore[return-value]
app.dependency_overrides[get_user_service] = lambda: FakeUserService(user) # type: ignore[return-value]
app.dependency_overrides[get_current_user] = lambda: CurrentUser(id=user_id)
host = "127.0.0.1"
@@ -90,19 +88,19 @@ def test_profile_flow_e2e() -> None:
base_url=f"http://{host}:{port}"
)
try:
me = request_context.get("/api/v1/profile/me")
me = request_context.get("/api/v1/users/me")
assert me.status == 200
assert me.json()["username"] == "demo"
updated = request_context.patch(
"/api/v1/profile/me",
"/api/v1/users/me",
data=json.dumps({"username": "updated"}),
headers={"Content-Type": "application/json"},
)
assert updated.status == 200
assert updated.json()["username"] == "updated"
public = request_context.get("/api/v1/profile/demo")
public = request_context.get("/api/v1/users/demo")
assert public.status == 200
assert public.json()["username"] == "demo"
finally:
@@ -52,7 +52,7 @@ def test_run_route_returns_response() -> None:
client = TestClient(app)
try:
response = client.post("/api/v1/agent-chat/run", json={"message": "hello"})
response = client.post("/api/v1/agent-chat", json={"message": "hello"})
assert response.status_code == 200
body = response.json()
assert body["output"] == "hello"
@@ -72,7 +72,7 @@ def test_run_route_validates_payload() -> None:
client = TestClient(app)
try:
response = client.post("/api/v1/agent-chat/run", json={"message": ""})
response = client.post("/api/v1/agent-chat", json={"message": ""})
assert response.status_code == 422
finally:
app.dependency_overrides = {}