test_api_errors.py 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. def _auth_headers(client, username):
  2. client.post("/api/v1/auth/register", json={"username": username, "password": "password123"})
  3. token = client.post("/api/v1/auth/login", data={"username": username, "password": "password123"}).json()["access_token"]
  4. return {"Authorization": f"Bearer {token}"}
  5. def test_regular_user_cannot_assign_persona_to_another_owner(client):
  6. # Public personas are allowed, but ownership must come from the token.
  7. u = client.post("/api/v1/auth/register", json={"username": "err_user1", "password": "password123", "role": "u"}).json()
  8. token = client.post("/api/v1/auth/login", data={"username": "err_user1", "password": "password123"}).json()["access_token"]
  9. headers = {"Authorization": f"Bearer {token}"}
  10. response = client.post(
  11. "/api/v1/personas/",
  12. params={"owner_id": 999},
  13. json={"name": "P", "bio": "B", "theories": [], "is_public": True},
  14. headers=headers
  15. )
  16. assert response.status_code == 200
  17. assert response.json()["owner_id"] == u["id"]
  18. def test_create_forum_rejects_empty_participants_before_legacy_creator_param(client):
  19. u = client.post("/api/v1/auth/register", json={"username": "err_user2", "password": "password123", "role": "u"}).json()
  20. token = client.post("/api/v1/auth/login", data={"username": "err_user2", "password": "password123"}).json()["access_token"]
  21. headers = {"Authorization": f"Bearer {token}"}
  22. response = client.post(
  23. "/api/v1/forums/",
  24. params={"creator_id": 999},
  25. json={"topic": "T", "participant_ids": []},
  26. headers=headers
  27. )
  28. assert response.status_code == 400
  29. assert any("请至少选择一位智能体" in item["msg"] for item in response.json()["detail"])
  30. def test_get_forum_not_found(client):
  31. assert client.get("/api/v1/forums/999/messages").status_code == 401
  32. response = client.get("/api/v1/forums/999/messages", headers=_auth_headers(client, "missing-reader"))
  33. assert response.status_code == 404
  34. assert "Forum not found" in response.json()["detail"]
  35. def test_post_message_forum_not_found(client):
  36. assert client.post(
  37. "/api/v1/forums/999/messages",
  38. json={"forum_id": 999, "persona_id": 1, "speaker_name": "S", "content": "C", "turn_count": 1}
  39. ).status_code == 401
  40. response = client.post(
  41. "/api/v1/forums/999/messages",
  42. json={"forum_id": 999, "persona_id": 1, "speaker_name": "S", "content": "C", "turn_count": 1},
  43. headers=_auth_headers(client, "missing-writer"),
  44. )
  45. assert response.status_code == 404
  46. assert "Forum not found" in response.json()["detail"]
  47. def test_post_message_persona_not_found(client):
  48. # Register and login
  49. u = client.post("/api/v1/auth/register", json={"username": "msg_user", "password": "password123", "role": "u"}).json()
  50. token = client.post("/api/v1/auth/login", data={"username": "msg_user", "password": "password123"}).json()["access_token"]
  51. headers = {"Authorization": f"Bearer {token}"}
  52. # Create forum
  53. persona = client.post("/api/v1/personas/", json={"name": "Message Persona", "bio": "B"}, headers=headers).json()
  54. f = client.post("/api/v1/forums/", json={"topic": "T", "participant_ids": [persona["id"]]}, headers=headers).json()
  55. response = client.post(
  56. f"/api/v1/forums/{f['id']}/messages",
  57. json={"forum_id": f['id'], "persona_id": 999, "speaker_name": "S", "content": "C", "turn_count": 1},
  58. headers=headers
  59. )
  60. assert response.status_code == 404
  61. def test_chat_agent_invalid_initialization(client):
  62. # Mocking failure during agent init inside endpoint
  63. from unittest.mock import patch
  64. with patch("app.api.v1.endpoints.agents.ParticipantAgent", side_effect=Exception("Init Failed")):
  65. response = client.post(
  66. "/api/v1/agents/chat",
  67. json={
  68. "agent_name": "FailAgent",
  69. "persona_json": {"name": "Fail"},
  70. "context_messages": []
  71. }
  72. )
  73. assert response.status_code == 400
  74. assert "Failed to initialize agent" in response.json()["detail"]