Skip to content

Commit a3ee3f2

Browse files
authored
Merge pull request #488 from Obiajulu-gif/work-order-status-validation
Work order status validation
2 parents 4497522 + ba45362 commit a3ee3f2

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

backend/app/routes/orders.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@
1616
router = APIRouter()
1717
matching_service = OrderMatchingService()
1818

19+
ALLOWED_ORDER_STATUSES = {"open", "matched", "filled", "cancelled", "expired"}
20+
21+
22+
def validate_order_status_filter(status: Optional[str]) -> Optional[str]:
23+
if status is None:
24+
return None
25+
normalized = status.lower()
26+
if normalized not in ALLOWED_ORDER_STATUSES:
27+
allowed = ", ".join(sorted(ALLOWED_ORDER_STATUSES))
28+
raise HTTPException(
29+
status_code=400,
30+
detail=f"Invalid order status '{status}'. Allowed values: {allowed}",
31+
)
32+
return normalized
33+
1934

2035
@router.post("/", response_model=OrderResponse, status_code=201)
2136
async def create_order(
@@ -63,6 +78,7 @@ async def list_orders(
6378
offset: Annotated[int, Query(ge=0)] = 0,
6479
db: AsyncSession = Depends(get_db),
6580
):
81+
status = validate_order_status_filter(status)
6682
cache_key = f"orders:{from_chain}:{to_chain}:{status}:{limit}:{offset}"
6783
cache = CacheService(get_redis())
6884
cached = await cache.get(cache_key)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import pytest
2+
from fastapi import HTTPException
3+
4+
from app.routes.orders import validate_order_status_filter
5+
6+
7+
def test_valid_order_status_filter_is_normalized():
8+
assert validate_order_status_filter("OPEN") == "open"
9+
10+
11+
def test_missing_order_status_filter_is_allowed():
12+
assert validate_order_status_filter(None) is None
13+
14+
15+
def test_invalid_order_status_filter_returns_clear_client_error():
16+
with pytest.raises(HTTPException) as exc:
17+
validate_order_status_filter("unknown")
18+
19+
assert exc.value.status_code == 400
20+
assert "Invalid order status 'unknown'" in exc.value.detail
21+
assert "Allowed values" in exc.value.detail

0 commit comments

Comments
 (0)