|
| 1 | +"""Integration tests for the /root endpoint.""" |
| 2 | + |
| 3 | +from typing import Generator, Any |
| 4 | +import pytest |
| 5 | +from pytest_mock import MockerFixture |
| 6 | + |
| 7 | +from fastapi import Request, status |
| 8 | +from llama_stack_client.types import VersionInfo |
| 9 | +from authentication.interface import AuthTuple |
| 10 | + |
| 11 | +from configuration import AppConfig |
| 12 | +from app.endpoints.root import root_endpoint_handler |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture(name="mock_llama_stack_client") |
| 16 | +def mock_llama_stack_client_fixture( |
| 17 | + mocker: MockerFixture, |
| 18 | +) -> Generator[Any, None, None]: |
| 19 | + """Mock only the external Llama Stack client. |
| 20 | +
|
| 21 | + This is the only external dependency we mock for integration tests, |
| 22 | + as it represents an external service call. |
| 23 | +
|
| 24 | + Parameters: |
| 25 | + mocker (pytest_mock.MockerFixture): The pytest-mock fixture used to apply the patch. |
| 26 | +
|
| 27 | + Yields: |
| 28 | + AsyncMock: A mocked Llama Stack client configured for tests. |
| 29 | + """ |
| 30 | + mock_holder_class = mocker.patch("app.endpoints.info.AsyncLlamaStackClientHolder") |
| 31 | + |
| 32 | + mock_client = mocker.AsyncMock() |
| 33 | + # Mock the version endpoint to return a known version |
| 34 | + mock_client.inspect.version.return_value = VersionInfo(version="0.2.22") |
| 35 | + |
| 36 | + # Create a mock holder instance |
| 37 | + mock_holder_instance = mock_holder_class.return_value |
| 38 | + mock_holder_instance.get_client.return_value = mock_client |
| 39 | + |
| 40 | + yield mock_client |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.asyncio |
| 44 | +async def test_root_endpoint( |
| 45 | + test_config: AppConfig, |
| 46 | + test_request: Request, |
| 47 | + test_auth: AuthTuple, |
| 48 | +) -> None: |
| 49 | + """Test that Root endpoint returns index HTML page. |
| 50 | +
|
| 51 | + This integration test verifies: |
| 52 | + - Endpoint handler |
| 53 | + - No authentication is used |
| 54 | + - Response structure matches expected format |
| 55 | +
|
| 56 | + Parameters: |
| 57 | + test_config (AppConfig): Loads root configuration |
| 58 | + test_request (Request): FastAPI request |
| 59 | + test_auth (AuthTuple): noop authentication tuple |
| 60 | + """ |
| 61 | + # Fixtures with side effects (needed but not directly used) |
| 62 | + _ = test_config |
| 63 | + |
| 64 | + response = await root_endpoint_handler(auth=test_auth, request=test_request) |
| 65 | + |
| 66 | + assert response.media_type == "text/html" |
| 67 | + assert response.status_code == status.HTTP_200_OK |
| 68 | + # retrieve response body as a string |
| 69 | + body = response.body.decode("utf-8") |
| 70 | + assert "<title>Lightspeed core service</title>" in body |
| 71 | + assert "<h1>Lightspeed core service</h1>" in body |
0 commit comments