Skip to content

Commit 204a2a2

Browse files
committed
Use correct status codes for /query endpoint
1 parent ab002e0 commit 204a2a2

File tree

9 files changed

+62
-14
lines changed

9 files changed

+62
-14
lines changed

src/app/endpoints/query.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,17 @@ def select_model_and_provider_id(
471471
Raises:
472472
HTTPException: If no suitable LLM model is found or the selected model is not available.
473473
"""
474+
# If no models are available, raise an exception
475+
if not models:
476+
message = "No LLM model found in available models"
477+
logger.error(message)
478+
raise HTTPException(
479+
status_code=status.HTTP_404_NOT_FOUND,
480+
detail={
481+
"response": constants.UNABLE_TO_PROCESS_RESPONSE,
482+
"cause": message,
483+
},
484+
)
474485
# If model_id and provider_id are provided in the request, use them
475486

476487
# If model_id is not provided in the request, check the configuration
@@ -504,7 +515,7 @@ def select_model_and_provider_id(
504515
message = "No LLM model found in available models"
505516
logger.error(message)
506517
raise HTTPException(
507-
status_code=status.HTTP_400_BAD_REQUEST,
518+
status_code=status.HTTP_404_NOT_FOUND,
508519
detail={
509520
"response": constants.UNABLE_TO_PROCESS_RESPONSE,
510521
"cause": message,
@@ -521,7 +532,7 @@ def select_model_and_provider_id(
521532
message = f"Model {model_id} from provider {provider_id} not found in available models"
522533
logger.error(message)
523534
raise HTTPException(
524-
status_code=status.HTTP_400_BAD_REQUEST,
535+
status_code=status.HTTP_404_NOT_FOUND,
525536
detail={
526537
"response": constants.UNABLE_TO_PROCESS_RESPONSE,
527538
"cause": message,

src/authentication/jwk_token.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,12 @@ async def __call__(self, request: Request) -> AuthTuple:
142142
) from exc
143143
except DecodeError as exc:
144144
raise HTTPException(
145-
status_code=status.HTTP_400_BAD_REQUEST,
145+
status_code=status.HTTP_401_UNAUTHORIZED,
146146
detail="Invalid token: decode error",
147147
) from exc
148148
except JoseError as exc:
149149
raise HTTPException(
150-
status_code=status.HTTP_400_BAD_REQUEST,
150+
status_code=status.HTTP_401_UNAUTHORIZED,
151151
detail="Invalid token: unknown error",
152152
) from exc
153153
except Exception as exc:

src/authentication/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ def extract_user_token(headers: Headers) -> str:
1515
"""
1616
authorization_header = headers.get("Authorization")
1717
if not authorization_header:
18-
raise HTTPException(status_code=400, detail="No Authorization header found")
18+
raise HTTPException(status_code=401, detail="No Authorization header found")
1919

2020
scheme_and_token = authorization_header.strip().split()
2121
if len(scheme_and_token) != 2 or scheme_and_token[0].lower() != "bearer":
2222
raise HTTPException(
23-
status_code=400, detail="No token found in Authorization header"
23+
status_code=401, detail="No token found in Authorization header"
2424
)
2525

2626
return scheme_and_token[1]

tests/e2e/features/environment.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ def before_scenario(context: Context, scenario: Scenario) -> None:
7878
context.scenario_config = (
7979
"tests/e2e/configuration/lightspeed-stack-invalid-feedback-storage.yaml"
8080
)
81+
if "no_models" in scenario.effective_tags:
82+
context.scenario_config = "tests/e2e/config/no-models-run.yaml"
8183

8284

8385
def after_scenario(context: Context, scenario: Scenario) -> None:

tests/e2e/features/query.feature

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,36 @@ Feature: Query endpoint API tests
5353
"""
5454
{"query": "Write a simple code for reversing string"}
5555
"""
56-
Then The status code of the response is 400
56+
Then The status code of the response is 401
5757
And The body of the response is the following
5858
"""
5959
{"detail": "No Authorization header found"}
6060
"""
6161

62+
Scenario: Check if LLM responds to sent question with error when not authenticated with invalid token
63+
Given The system is in default state
64+
And I set the Authorization header to Bearer invalid
65+
When I use "query" to ask question with authorization header
66+
"""
67+
{"query": "Write a simple code for reversing string"}
68+
"""
69+
Then The status code of the response is 401
70+
And The body of the response is the following
71+
"""
72+
{"detail":"Invalid token: decode error"}
73+
"""
74+
75+
Scenario: Check if LLM responds to sent question with error when model does not exist
76+
Given The system is in default state
77+
And I set the Authorization header to Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpva
78+
When I use "query" to ask question with authorization header
79+
"""
80+
{"query": "Write a simple code for reversing string", "model": "does-not-exist", "provider": "does-not-exist"}
81+
"""
82+
Then The status code of the response is 404
83+
And The body of the response contains Model does-not-exist from provider does-not-exist not found in available models
84+
85+
6286
Scenario: Check if LLM responds to sent question with error when attempting to access conversation
6387
Given The system is in default state
6488
And I set the Authorization header to Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpva
@@ -138,3 +162,14 @@ Scenario: Check if LLM responds for query request with error for missing query
138162
}
139163
"""
140164
Then The status code of the response is 200
165+
Scenario: Check if LLM responds with an error when no models are configured
166+
Given The service is started locally
167+
And REST API service prefix is /v1
168+
Given The system is in default state
169+
And I set the Authorization header to Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpva
170+
When I use "query" to ask question with authorization header
171+
"""
172+
{"query": "Write a simple code for reversing string"}
173+
"""
174+
Then The status code of the response is 404
175+
And The body of the response contains No models available

tests/unit/app/endpoints/test_authorized.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ async def test_authorized_dependency_unauthorized() -> None:
5555
headers_no_auth = Headers({})
5656
with pytest.raises(HTTPException) as exc_info:
5757
extract_user_token(headers_no_auth)
58-
assert exc_info.value.status_code == 400
58+
assert exc_info.value.status_code == 401
5959
assert exc_info.value.detail == "No Authorization header found"
6060

6161
# Test case 2: Invalid Authorization header format (400 error from extract_user_token)
6262
headers_invalid_auth = Headers({"Authorization": "InvalidFormat"})
6363
with pytest.raises(HTTPException) as exc_info:
6464
extract_user_token(headers_invalid_auth)
65-
assert exc_info.value.status_code == 400
65+
assert exc_info.value.status_code == 401
6666
assert exc_info.value.detail == "No token found in Authorization header"

tests/unit/authentication/test_jwk_token.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ async def test_no_bearer(
302302
with pytest.raises(HTTPException) as exc_info:
303303
await dependency(not_bearer_token_request)
304304

305-
assert exc_info.value.status_code == 400
305+
assert exc_info.value.status_code == 401
306306
assert exc_info.value.detail == "No token found in Authorization header"
307307

308308

tests/unit/authentication/test_noop_with_token.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ async def test_noop_with_token_auth_dependency_no_token() -> None:
8181
with pytest.raises(HTTPException) as exc_info:
8282
await dependency(request)
8383

84-
assert exc_info.value.status_code == 400
84+
assert exc_info.value.status_code == 401
8585
assert exc_info.value.detail == "No Authorization header found"
8686

8787

@@ -102,5 +102,5 @@ async def test_noop_with_token_auth_dependency_no_bearer() -> None:
102102
with pytest.raises(HTTPException) as exc_info:
103103
await dependency(request)
104104

105-
assert exc_info.value.status_code == 400
105+
assert exc_info.value.status_code == 401
106106
assert exc_info.value.detail == "No token found in Authorization header"

tests/unit/authentication/test_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def test_extract_user_token_no_header() -> None:
1919
try:
2020
extract_user_token(headers)
2121
except HTTPException as exc:
22-
assert exc.status_code == 400
22+
assert exc.status_code == 401
2323
assert exc.detail == "No Authorization header found"
2424

2525

@@ -29,5 +29,5 @@ def test_extract_user_token_invalid_format() -> None:
2929
try:
3030
extract_user_token(headers)
3131
except HTTPException as exc:
32-
assert exc.status_code == 400
32+
assert exc.status_code == 401
3333
assert exc.detail == "No token found in Authorization header"

0 commit comments

Comments
 (0)