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: try:
response = request_context.post( response = request_context.post(
"/api/v1/agent-chat/run", "/api/v1/agent-chat",
data=json.dumps({"message": "hello"}), data=json.dumps({"message": "hello"}),
headers={"Content-Type": "application/json"}, headers={"Content-Type": "application/json"},
) )
+20 -22
View File
@@ -11,37 +11,35 @@ import uvicorn
from app import app from app import app
from core.auth.models import CurrentUser from core.auth.models import CurrentUser
from v1.profile.dependencies import get_current_user, get_profile_service from v1.users.dependencies import get_current_user, get_user_service
from v1.profile.schemas import ProfileResponse, ProfileUpdateRequest from v1.users.schemas import UserResponse, UserUpdateRequest
class FakeProfileService: class FakeUserService:
"""Fake service for E2E testing.""" """Fake service for E2E testing."""
def __init__(self, profile: ProfileResponse) -> None: def __init__(self, user: UserResponse) -> None:
self._profile = profile self._user = user
async def get_me(self) -> ProfileResponse: async def get_me(self) -> UserResponse:
return self._profile return self._user
async def update_me(self, update: ProfileUpdateRequest) -> ProfileResponse: async def update_me(self, update: UserUpdateRequest) -> UserResponse:
return ProfileResponse( return UserResponse(
id=self._profile.id, id=self._user.id,
username=( username=(
update.username update.username if update.username is not None else self._user.username
if update.username is not None
else self._profile.username
), ),
avatar_url=( avatar_url=(
update.avatar_url update.avatar_url
if update.avatar_url is not None 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: async def get_by_username(self, username: str) -> UserResponse:
return self._profile return self._user
def _find_free_port() -> int: def _find_free_port() -> int:
@@ -71,13 +69,13 @@ def _start_server(host: str, port: int):
def test_profile_flow_e2e() -> None: def test_profile_flow_e2e() -> None:
user_id = UUID("00000000-0000-0000-0000-000000000001") user_id = UUID("00000000-0000-0000-0000-000000000001")
profile = ProfileResponse( user = UserResponse(
id=str(user_id), id=str(user_id),
username="demo", username="demo",
avatar_url=None, avatar_url=None,
bio=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) app.dependency_overrides[get_current_user] = lambda: CurrentUser(id=user_id)
host = "127.0.0.1" host = "127.0.0.1"
@@ -90,19 +88,19 @@ def test_profile_flow_e2e() -> None:
base_url=f"http://{host}:{port}" base_url=f"http://{host}:{port}"
) )
try: try:
me = request_context.get("/api/v1/profile/me") me = request_context.get("/api/v1/users/me")
assert me.status == 200 assert me.status == 200
assert me.json()["username"] == "demo" assert me.json()["username"] == "demo"
updated = request_context.patch( updated = request_context.patch(
"/api/v1/profile/me", "/api/v1/users/me",
data=json.dumps({"username": "updated"}), data=json.dumps({"username": "updated"}),
headers={"Content-Type": "application/json"}, headers={"Content-Type": "application/json"},
) )
assert updated.status == 200 assert updated.status == 200
assert updated.json()["username"] == "updated" 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.status == 200
assert public.json()["username"] == "demo" assert public.json()["username"] == "demo"
finally: finally:
@@ -52,7 +52,7 @@ def test_run_route_returns_response() -> None:
client = TestClient(app) client = TestClient(app)
try: 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 assert response.status_code == 200
body = response.json() body = response.json()
assert body["output"] == "hello" assert body["output"] == "hello"
@@ -72,7 +72,7 @@ def test_run_route_validates_payload() -> None:
client = TestClient(app) client = TestClient(app)
try: 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 assert response.status_code == 422
finally: finally:
app.dependency_overrides = {} app.dependency_overrides = {}