-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathtest_api_helpers.py
More file actions
47 lines (32 loc) · 1.46 KB
/
test_api_helpers.py
File metadata and controls
47 lines (32 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# SPDX-License-Identifier: LicenseRef-Blockscout
"""Unit tests for the handle_rest_errors decorator in blockscout_mcp_server.api.helpers."""
import json
import pytest
from starlette.requests import Request
from blockscout_mcp_server.api.helpers import handle_rest_errors
from blockscout_mcp_server.tools.common import CreditsExhaustedError
def _make_request() -> Request:
"""Create a minimal Starlette Request for use in decorator tests."""
scope = {"type": "http", "method": "GET", "path": "/", "query_string": b"", "headers": []}
return Request(scope)
@pytest.mark.asyncio
async def test_handle_rest_errors_credits_exhausted_returns_402():
"""handle_rest_errors converts CreditsExhaustedError into an HTTP 402 response."""
message = "Out of credits"
@handle_rest_errors
async def handler(request: Request):
raise CreditsExhaustedError(message)
response = await handler(_make_request())
assert response.status_code == 402
body = json.loads(response.body)
assert body["error"] == message
@pytest.mark.asyncio
async def test_handle_rest_errors_value_error_still_returns_400():
"""CreditsExhaustedError branch does not intercept ValueError; it still maps to 400."""
@handle_rest_errors
async def handler(request: Request):
raise ValueError("bad input")
response = await handler(_make_request())
assert response.status_code == 400
body = json.loads(response.body)
assert body["error"] == "bad input"