11"""Unit tests for the /info REST API endpoint."""
22
33import pytest
4- from fastapi import Request
4+ from fastapi import Request , HTTPException , status
5+
6+ from llama_stack_client import APIConnectionError
7+ from llama_stack_client .types import VersionInfo
58
69from app .endpoints .info import info_endpoint_handler
710from configuration import AppConfig
1114@pytest .mark .asyncio
1215async def test_info_endpoint (mocker ):
1316 """Test the info endpoint handler."""
17+ mock_authorization_resolvers (mocker )
18+
19+ # configuration for tests
1420 config_dict = {
1521 "name" : "foo" ,
1622 "service" : {
@@ -36,6 +42,14 @@ async def test_info_endpoint(mocker):
3642 cfg = AppConfig ()
3743 cfg .init_from_dict (config_dict )
3844
45+ # Mock the LlamaStack client
46+ mock_client = mocker .AsyncMock ()
47+ mock_client .inspect .version .return_value = VersionInfo (version = "0.1.2" )
48+ mock_lsc = mocker .patch ("client.AsyncLlamaStackClientHolder.get_client" )
49+ mock_lsc .return_value = mock_client
50+ mock_config = mocker .Mock ()
51+ mocker .patch ("app.endpoints.models.configuration" , mock_config )
52+
3953 # Mock configuration
4054 mocker .patch ("configuration.configuration" , cfg )
4155
@@ -50,4 +64,62 @@ async def test_info_endpoint(mocker):
5064 response = await info_endpoint_handler (auth = auth , request = request )
5165 assert response is not None
5266 assert response .name is not None
53- assert response .version is not None
67+ assert response .service_version is not None
68+ assert response .llama_stack_version == "0.1.2"
69+
70+
71+ @pytest .mark .asyncio
72+ async def test_info_endpoint_connection_error (mocker ):
73+ """Test the info endpoint handler."""
74+ mock_authorization_resolvers (mocker )
75+
76+ # configuration for tests
77+ config_dict = {
78+ "name" : "foo" ,
79+ "service" : {
80+ "host" : "localhost" ,
81+ "port" : 8080 ,
82+ "auth_enabled" : False ,
83+ "workers" : 1 ,
84+ "color_log" : True ,
85+ "access_log" : True ,
86+ },
87+ "llama_stack" : {
88+ "api_key" : "xyzzy" ,
89+ "url" : "http://x.y.com:1234" ,
90+ "use_as_library_client" : False ,
91+ },
92+ "user_data_collection" : {
93+ "feedback_enabled" : False ,
94+ },
95+ "customization" : None ,
96+ "authorization" : {"access_rules" : []},
97+ "authentication" : {"module" : "noop" },
98+ }
99+ cfg = AppConfig ()
100+ cfg .init_from_dict (config_dict )
101+
102+ # Mock the LlamaStack client
103+ mock_client = mocker .AsyncMock ()
104+ mock_client .inspect .version .side_effect = APIConnectionError (request = None )
105+ mock_lsc = mocker .patch ("client.AsyncLlamaStackClientHolder.get_client" )
106+ mock_lsc .return_value = mock_client
107+ mock_config = mocker .Mock ()
108+ mocker .patch ("app.endpoints.models.configuration" , mock_config )
109+
110+ # Mock configuration
111+ mocker .patch ("configuration.configuration" , cfg )
112+
113+ mock_authorization_resolvers (mocker )
114+
115+ request = Request (
116+ scope = {
117+ "type" : "http" ,
118+ }
119+ )
120+ auth = ("test_user" , "token" , {})
121+
122+ with pytest .raises (HTTPException ) as e :
123+ await info_endpoint_handler (auth = auth , request = request )
124+ assert e .value .status_code == status .HTTP_500_INTERNAL_SERVER_ERROR
125+ assert e .detail ["response" ] == "Unable to connect to Llama Stack"
0 commit comments