Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bonk1t committed Jan 28, 2024
1 parent 7929d31 commit 29d3291
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 14 deletions.
2 changes: 1 addition & 1 deletion nalgonda/models/agency_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class AgencyConfig(BaseModel):
agency_id: str | None = Field(None, description="Unique identifier for the configuration")
owner_id: str | None = Field(None, description="The user ID owning this configuration")
name: str = Field(..., description="Name of the agency")
shared_instructions: str = Field("Agency Manifesto")
shared_instructions: str = Field("", description="Agency Manifesto")
agents: list[str] = Field(default_factory=list, description="List of agent IDs used in the agency chart")
main_agent: str | None = Field(None, description="The main agent name")
agency_chart: list[conlist(str, min_length=2, max_length=2)] = Field( # type: ignore
Expand Down
4 changes: 2 additions & 2 deletions nalgonda/routers/v1/api/agency.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async def get_agency_config(
) -> AgencyConfig:
agency_config = storage.load_by_agency_id(agency_id)
if not agency_config:
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Agency configuration not found")
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Agency not found")
# check if the current_user has permissions to get the agency config
if agency_config.owner_id and agency_config.owner_id != current_user.id:
raise HTTPException(status_code=HTTP_403_FORBIDDEN, detail="Forbidden")
Expand All @@ -62,7 +62,7 @@ async def update_or_create_agency(
if agency_config.agency_id:
agency_config_db = storage.load_by_agency_id(agency_config.agency_id)
if not agency_config_db:
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Agency configuration not found")
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Agency not found")

Check warning on line 65 in nalgonda/routers/v1/api/agency.py

View check run for this annotation

Codecov / codecov/patch

nalgonda/routers/v1/api/agency.py#L65

Added line #L65 was not covered by tests
if agency_config_db.owner_id != current_user.id:
raise HTTPException(status_code=HTTP_403_FORBIDDEN, detail="Forbidden")
# check that all used agents belong to the current user
Expand Down
4 changes: 2 additions & 2 deletions nalgonda/routers/v1/api/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async def get_agent_config(
) -> AgentConfig:
agent_config = storage.load_by_agent_id(agent_id)
if not agent_config:
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Agent configuration not found")
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Agent not found")

Check warning on line 39 in nalgonda/routers/v1/api/agent.py

View check run for this annotation

Codecov / codecov/patch

nalgonda/routers/v1/api/agent.py#L39

Added line #L39 was not covered by tests
# check if the current user is the owner of the agent
if agent_config.owner_id and agent_config.owner_id != current_user.id:
raise HTTPException(status_code=HTTP_403_FORBIDDEN, detail="Forbidden")
Expand All @@ -58,7 +58,7 @@ async def create_or_update_agent(
if agent_config.agent_id:
agent_config_db = storage.load_by_agent_id(agent_config.agent_id)
if not agent_config_db:
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Agent configuration not found")
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Agent not found")

Check warning on line 61 in nalgonda/routers/v1/api/agent.py

View check run for this annotation

Codecov / codecov/patch

nalgonda/routers/v1/api/agent.py#L61

Added line #L61 was not covered by tests
if agent_config_db.owner_id != current_user.id:
raise HTTPException(status_code=HTTP_403_FORBIDDEN, detail="Forbidden")
# Ensure the agent name has not been changed
Expand Down
8 changes: 4 additions & 4 deletions nalgonda/routers/v1/api/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ async def create_session(
# check if the current_user has permissions to create a session for the agency
agency_config = storage.load_by_agency_id(agency_id)
if not agency_config:
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Agency configuration not found")
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Agency not found")
if agency_config.owner_id != current_user.id:
raise HTTPException(status_code=HTTP_403_FORBIDDEN, detail="Forbidden")

Check warning on line 43 in nalgonda/routers/v1/api/session.py

View check run for this annotation

Codecov / codecov/patch

nalgonda/routers/v1/api/session.py#L43

Added line #L43 was not covered by tests

logger.info(f"Creating a new session for the agency: {agency_id}, and user: {current_user.id}")

agency = await agency_manager.get_agency(agency_id, None)
if not agency:
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Agency not found, create an agency first")
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Agency not found")

Check warning on line 49 in nalgonda/routers/v1/api/session.py

View check run for this annotation

Codecov / codecov/patch

nalgonda/routers/v1/api/session.py#L49

Added line #L49 was not covered by tests

session_id = thread_manager.create_threads(agency)

Expand All @@ -65,7 +65,7 @@ async def post_agency_message(
# check if the current_user has permissions to send a message to the agency
agency_config = storage.load_by_agency_id(request.agency_id)
if not agency_config:
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Agency configuration not found")
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Agency not found")
if agency_config.owner_id != current_user.id:
raise HTTPException(status_code=HTTP_403_FORBIDDEN, detail="Forbidden")

Check warning on line 70 in nalgonda/routers/v1/api/session.py

View check run for this annotation

Codecov / codecov/patch

nalgonda/routers/v1/api/session.py#L66-L70

Added lines #L66 - L70 were not covered by tests

Expand All @@ -77,7 +77,7 @@ async def post_agency_message(

agency = await agency_manager.get_agency(agency_id, thread_id)
if not agency:
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Agency not found, create an agency first")
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Agency not found")

Check warning on line 80 in nalgonda/routers/v1/api/session.py

View check run for this annotation

Codecov / codecov/patch

nalgonda/routers/v1/api/session.py#L78-L80

Added lines #L78 - L80 were not covered by tests

try:
response = await process_message(user_message, agency)
Expand Down
6 changes: 3 additions & 3 deletions nalgonda/routers/v1/api/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async def get_tool_config(
) -> ToolConfig:
tool_config = storage.load_by_tool_id(tool_id)
if not tool_config:
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Tool configuration not found")
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Tool not found")

Check warning on line 36 in nalgonda/routers/v1/api/tool.py

View check run for this annotation

Codecov / codecov/patch

nalgonda/routers/v1/api/tool.py#L36

Added line #L36 was not covered by tests
# check if the current_user has permissions to get the tool config
if tool_config.owner_id and tool_config.owner_id != current_user.id:

Check warning on line 38 in nalgonda/routers/v1/api/tool.py

View check run for this annotation

Codecov / codecov/patch

nalgonda/routers/v1/api/tool.py#L38

Added line #L38 was not covered by tests
raise HTTPException(status_code=HTTP_403_FORBIDDEN, detail="Forbidden")
Expand All @@ -56,7 +56,7 @@ async def create_tool_version(
if tool_config.tool_id:
tool_config_db = storage.load_by_tool_id(tool_config.tool_id)
if not tool_config_db:
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Tool configuration not found")
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Tool not found")

Check warning on line 59 in nalgonda/routers/v1/api/tool.py

View check run for this annotation

Codecov / codecov/patch

nalgonda/routers/v1/api/tool.py#L59

Added line #L59 was not covered by tests
if tool_config_db.owner_id != current_user.id:
raise HTTPException(status_code=HTTP_403_FORBIDDEN, detail="Forbidden")

Expand All @@ -82,7 +82,7 @@ async def approve_tool(
):
tool_config = storage.load_by_tool_id(tool_id)
if not tool_config:
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Tool configuration not found")
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Tool not found")

Check warning on line 85 in nalgonda/routers/v1/api/tool.py

View check run for this annotation

Codecov / codecov/patch

nalgonda/routers/v1/api/tool.py#L85

Added line #L85 was not covered by tests

tool_config.approved = True

Expand Down
2 changes: 1 addition & 1 deletion nalgonda/routers/v1/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async def websocket_thread_endpoint(

agency = await agency_manager.get_agency(agency_id, thread_id)
if not agency:
await connection_manager.send_message("Agency not found, create an agency first", websocket)
await connection_manager.send_message("Agency not found", websocket)

Check warning on line 38 in nalgonda/routers/v1/websocket.py

View check run for this annotation

Codecov / codecov/patch

nalgonda/routers/v1/websocket.py#L38

Added line #L38 was not covered by tests
await connection_manager.disconnect(websocket)
await websocket.close()
return
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/v1/api/test_agency_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_get_agency_config_not_found(client, mock_get_current_active_user): # n
# Simulate non-existent agency by not setting up any data for it
response = client.get("/v1/api/agency?agency_id=non_existent_agency")
assert response.status_code == 404
assert response.json() == {"detail": "Agency configuration not found"}
assert response.json() == {"detail": "Agency not found"}


def test_create_agency_success(client, mock_firestore_client, mock_get_current_active_user): # noqa: ARG001
Expand Down
42 changes: 42 additions & 0 deletions tests/functional/v1/api/test_session_endpoints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from unittest.mock import AsyncMock, MagicMock, patch

from fastapi import status

from nalgonda.models.request_models import ThreadPostRequest
from nalgonda.services.agency_manager import AgencyManager
from nalgonda.services.thread_manager import ThreadManager
from tests.test_utils import TEST_USER_ID


def test_create_session_success(client, mock_firestore_client, mock_get_current_active_user): # noqa: ARG001
with patch.object(
AgencyManager, "get_agency", AsyncMock(return_value=MagicMock())
) as mock_get_agency, patch.object(
ThreadManager, "create_threads", MagicMock(return_value="new_session_id")
) as mock_create_threads, patch.object(AgencyManager, "cache_agency", AsyncMock()) as mock_cache_agency:
# mock Firestore to pass the security owner_id check
mock_firestore_client.setup_mock_data(
"agency_configs", "test_agency_id", {"name": "Test agency", "owner_id": TEST_USER_ID}
)

# Create request data
request_data = ThreadPostRequest(agency_id="test_agency_id")
# Create a test client
response = client.post("/v1/api/session", json=request_data.model_dump())
# Assertions
assert response.status_code == status.HTTP_200_OK
assert response.json() == {"session_id": "new_session_id"}
mock_get_agency.assert_awaited_once_with("test_agency_id", None)
mock_create_threads.assert_called_once_with(mock_get_agency.return_value)
mock_cache_agency.assert_awaited_once_with(mock_get_agency.return_value, "test_agency_id", "new_session_id")


def test_create_session_agency_not_found(client, mock_get_current_active_user): # noqa: ARG001
with patch.object(AgencyManager, "get_agency", AsyncMock(return_value=None)):
# Create request data
request_data = ThreadPostRequest(agency_id="test_agency_id")
# Create a test client
response = client.post("/v1/api/session", json=request_data.model_dump())
# Assertions
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.json() == {"detail": "Agency not found"}

0 comments on commit 29d3291

Please sign in to comment.