Skip to content

Commit 0190f41

Browse files
committed
remove assertions in endpoint code, move to if checks
Signed-off-by: Jordan Dubrick <jdubrick@redhat.com>
1 parent 8dfc973 commit 0190f41

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

src/app/endpoints/saved_prompts.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from typing import Annotated, Any
44

5-
from fastapi import APIRouter, Depends, Request
5+
from fastapi import APIRouter, Depends, HTTPException, Request
66

77
from authentication import get_auth_dependency
88
from authentication.interface import AuthTuple
@@ -72,9 +72,14 @@ async def get_saved_prompts_config_handler(
7272
max_prompts_per_user = saved_prompts_config.max_prompts_per_user
7373
max_display_name_length = saved_prompts_config.max_display_name_length
7474
max_content_length = saved_prompts_config.max_content_length
75-
assert max_prompts_per_user is not None
76-
assert max_display_name_length is not None
77-
assert max_content_length is not None
75+
if (
76+
max_prompts_per_user is None
77+
or max_display_name_length is None
78+
or max_content_length is None
79+
):
80+
logger.error("Saved prompts configuration limits are not set")
81+
error_response = InternalServerErrorResponse.generic()
82+
raise HTTPException(**error_response.model_dump())
7883

7984
return SavedPromptsConfigResponse(
8085
max_prompts_per_user=max_prompts_per_user,

tests/unit/app/endpoints/test_saved_prompts.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,34 @@ async def test_get_saved_prompts_config_configuration_not_loaded(
127127
)
128128

129129

130+
@pytest.mark.asyncio
131+
async def test_get_saved_prompts_config_incomplete_limits(
132+
mocker: MockerFixture,
133+
minimal_config: AppConfig,
134+
saved_prompts_http_request: Request,
135+
) -> None:
136+
"""GET /saved-prompts/config returns 500 when a limit is unexpectedly None.
137+
138+
The model validator on ``SavedPromptsConfiguration`` always fills in
139+
defaults, so this simulates the defensive branch that guards against a
140+
limit slipping through as ``None`` at runtime.
141+
"""
142+
mock_authorization_resolvers(mocker)
143+
minimal_config.configuration.saved_prompts.max_prompts_per_user = None
144+
mocker.patch("app.endpoints.saved_prompts.configuration", minimal_config)
145+
146+
with pytest.raises(HTTPException) as exc_info:
147+
await get_saved_prompts_config_handler(
148+
auth=MOCK_AUTH,
149+
request=saved_prompts_http_request,
150+
)
151+
152+
assert exc_info.value.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
153+
detail = exc_info.value.detail
154+
assert isinstance(detail, dict)
155+
assert detail["response"] == "Internal server error" # type: ignore[index]
156+
157+
130158
@pytest.mark.asyncio
131159
async def test_get_saved_prompts_config_forbidden_without_get_config_action(
132160
mocker: MockerFixture,

0 commit comments

Comments
 (0)