Skip to content

Commit 1e1417f

Browse files
committed
Type hints for tests/unit/app/endpoints/test_shields.py
1 parent 1cd5342 commit 1e1417f

1 file changed

Lines changed: 56 additions & 22 deletions

File tree

tests/unit/app/endpoints/test_shields.py

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
"""Unit tests for the /shields REST API endpoint."""
22

3+
from typing import Any
34
import pytest
4-
5+
from pytest_mock import MockerFixture
56
from fastapi import HTTPException, Request, status
67

78
from llama_stack_client import APIConnectionError
89

10+
from authentication.interface import AuthTuple
11+
912
from app.endpoints.shields import shields_endpoint_handler
1013
from configuration import AppConfig
1114
from tests.unit.utils.auth_helpers import mock_authorization_resolvers
1215

1316

1417
@pytest.mark.asyncio
15-
async def test_shields_endpoint_handler_configuration_not_loaded(mocker):
18+
async def test_shields_endpoint_handler_configuration_not_loaded(
19+
mocker: MockerFixture,
20+
) -> None:
1621
"""Test the shields endpoint handler if configuration is not loaded."""
1722
mock_authorization_resolvers(mocker)
1823

@@ -29,7 +34,9 @@ async def test_shields_endpoint_handler_configuration_not_loaded(mocker):
2934
"headers": [(b"authorization", b"Bearer invalid-token")],
3035
}
3136
)
32-
auth = ("user_id", "user_name", "token")
37+
38+
# Authorization tuple required by URL endpoint handler
39+
auth: AuthTuple = ("test_user_id", "test_user", True, "test_token")
3340

3441
with pytest.raises(HTTPException) as e:
3542
await shields_endpoint_handler(request=request, auth=auth)
@@ -38,12 +45,14 @@ async def test_shields_endpoint_handler_configuration_not_loaded(mocker):
3845

3946

4047
@pytest.mark.asyncio
41-
async def test_shields_endpoint_handler_improper_llama_stack_configuration(mocker):
48+
async def test_shields_endpoint_handler_improper_llama_stack_configuration(
49+
mocker: MockerFixture,
50+
) -> None:
4251
"""Test the shields endpoint handler if Llama Stack configuration is not proper."""
4352
mock_authorization_resolvers(mocker)
4453

4554
# configuration for tests
46-
config_dict = {
55+
config_dict: dict[str, Any] = {
4756
"name": "test",
4857
"service": {
4958
"host": "localhost",
@@ -80,20 +89,25 @@ async def test_shields_endpoint_handler_improper_llama_stack_configuration(mocke
8089
"headers": [(b"authorization", b"Bearer invalid-token")],
8190
}
8291
)
83-
auth = ("test_user", "token", {})
92+
93+
# Authorization tuple required by URL endpoint handler
94+
auth: AuthTuple = ("test_user_id", "test_user", True, "test_token")
95+
8496
with pytest.raises(HTTPException) as e:
8597
await shields_endpoint_handler(request=request, auth=auth)
8698
assert e.value.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
8799
assert e.detail["response"] == "Llama stack is not configured"
88100

89101

90102
@pytest.mark.asyncio
91-
async def test_shields_endpoint_handler_configuration_loaded(mocker):
103+
async def test_shields_endpoint_handler_configuration_loaded(
104+
mocker: MockerFixture,
105+
) -> None:
92106
"""Test the shields endpoint handler if configuration is loaded."""
93107
mock_authorization_resolvers(mocker)
94108

95109
# configuration for tests
96-
config_dict = {
110+
config_dict: dict[str, Any] = {
97111
"name": "foo",
98112
"service": {
99113
"host": "localhost",
@@ -124,7 +138,9 @@ async def test_shields_endpoint_handler_configuration_loaded(mocker):
124138
"headers": [(b"authorization", b"Bearer invalid-token")],
125139
}
126140
)
127-
auth = ("test_user", "token", {})
141+
142+
# Authorization tuple required by URL endpoint handler
143+
auth: AuthTuple = ("test_user_id", "test_user", True, "test_token")
128144

129145
with pytest.raises(HTTPException) as e:
130146
await shields_endpoint_handler(request=request, auth=auth)
@@ -133,12 +149,14 @@ async def test_shields_endpoint_handler_configuration_loaded(mocker):
133149

134150

135151
@pytest.mark.asyncio
136-
async def test_shields_endpoint_handler_unable_to_retrieve_shields_list(mocker):
152+
async def test_shields_endpoint_handler_unable_to_retrieve_shields_list(
153+
mocker: MockerFixture,
154+
) -> None:
137155
"""Test the shields endpoint handler if configuration is loaded."""
138156
mock_authorization_resolvers(mocker)
139157

140158
# configuration for tests
141-
config_dict = {
159+
config_dict: dict[str, Any] = {
142160
"name": "foo",
143161
"service": {
144162
"host": "localhost",
@@ -177,18 +195,23 @@ async def test_shields_endpoint_handler_unable_to_retrieve_shields_list(mocker):
177195
"headers": [(b"authorization", b"Bearer invalid-token")],
178196
}
179197
)
180-
auth = ("test_user", "token", {})
198+
199+
# Authorization tuple required by URL endpoint handler
200+
auth: AuthTuple = ("test_user_id", "test_user", True, "test_token")
201+
181202
response = await shields_endpoint_handler(request=request, auth=auth)
182203
assert response is not None
183204

184205

185206
@pytest.mark.asyncio
186-
async def test_shields_endpoint_llama_stack_connection_error(mocker):
207+
async def test_shields_endpoint_llama_stack_connection_error(
208+
mocker: MockerFixture,
209+
) -> None:
187210
"""Test the shields endpoint when LlamaStack connection fails."""
188211
mock_authorization_resolvers(mocker)
189212

190213
# configuration for tests
191-
config_dict = {
214+
config_dict: dict[str, Any] = {
192215
"name": "foo",
193216
"service": {
194217
"host": "localhost",
@@ -214,7 +237,7 @@ async def test_shields_endpoint_llama_stack_connection_error(mocker):
214237
# mock AsyncLlamaStackClientHolder to raise APIConnectionError
215238
# when shields.list() method is called
216239
mock_client = mocker.AsyncMock()
217-
mock_client.shields.list.side_effect = APIConnectionError(request=None)
240+
mock_client.shields.list.side_effect = APIConnectionError(request=None) # type: ignore
218241
mock_client_holder = mocker.patch(
219242
"app.endpoints.shields.AsyncLlamaStackClientHolder"
220243
)
@@ -229,7 +252,9 @@ async def test_shields_endpoint_llama_stack_connection_error(mocker):
229252
"headers": [(b"authorization", b"Bearer invalid-token")],
230253
}
231254
)
232-
auth = ("test_user", "token", {})
255+
256+
# Authorization tuple required by URL endpoint handler
257+
auth: AuthTuple = ("test_user_id", "test_user", True, "test_token")
233258

234259
with pytest.raises(HTTPException) as e:
235260
await shields_endpoint_handler(request=request, auth=auth)
@@ -238,12 +263,14 @@ async def test_shields_endpoint_llama_stack_connection_error(mocker):
238263

239264

240265
@pytest.mark.asyncio
241-
async def test_shields_endpoint_handler_success_with_shields_data(mocker):
266+
async def test_shields_endpoint_handler_success_with_shields_data(
267+
mocker: MockerFixture,
268+
) -> None:
242269
"""Test the shields endpoint handler with successful response and shields data."""
243270
mock_authorization_resolvers(mocker)
244271

245272
# configuration for tests
246-
config_dict = {
273+
config_dict: dict[str, Any] = {
247274
"name": "foo",
248275
"service": {
249276
"host": "localhost",
@@ -299,7 +326,10 @@ async def test_shields_endpoint_handler_success_with_shields_data(mocker):
299326
"headers": [(b"authorization", b"Bearer invalid-token")],
300327
}
301328
)
302-
auth = ("test_user", "token", {})
329+
330+
# Authorization tuple required by URL endpoint handler
331+
auth: AuthTuple = ("test_user_id", "test_user", True, "test_token")
332+
303333
response = await shields_endpoint_handler(request=request, auth=auth)
304334

305335
assert response is not None
@@ -310,12 +340,14 @@ async def test_shields_endpoint_handler_success_with_shields_data(mocker):
310340

311341

312342
@pytest.mark.asyncio
313-
async def test_shields_endpoint_handler_general_exception(mocker):
343+
async def test_shields_endpoint_handler_general_exception(
344+
mocker: MockerFixture,
345+
) -> None:
314346
"""Test the shields endpoint handler when a general exception occurs."""
315347
mock_authorization_resolvers(mocker)
316348

317349
# configuration for tests
318-
config_dict = {
350+
config_dict: dict[str, Any] = {
319351
"name": "foo",
320352
"service": {
321353
"host": "localhost",
@@ -356,7 +388,9 @@ async def test_shields_endpoint_handler_general_exception(mocker):
356388
"headers": [(b"authorization", b"Bearer invalid-token")],
357389
}
358390
)
359-
auth = ("test_user", "token", {})
391+
392+
# Authorization tuple required by URL endpoint handler
393+
auth: AuthTuple = ("test_user_id", "test_user", True, "test_token")
360394

361395
with pytest.raises(HTTPException) as e:
362396
await shields_endpoint_handler(request=request, auth=auth)

0 commit comments

Comments
 (0)