mirror of
http://88.130.71.182:3000/BlitTech/badoHair_be.git
synced 2026-06-12 23:23:22 +00:00
Initial Commit
This commit is contained in:
59
tests/test_error_handling.py
Normal file
59
tests/test_error_handling.py
Normal file
@@ -0,0 +1,59 @@
|
||||
"""Verify the error envelope is consistent across all error types."""
|
||||
|
||||
|
||||
async def test_404_uses_error_envelope(anon_client, mock_db):
|
||||
from unittest.mock import AsyncMock
|
||||
mock_db.fetchrow = AsyncMock(return_value=None)
|
||||
|
||||
r = await anon_client.get("/api/v1/products/00000000-0000-0000-0000-000000000000")
|
||||
assert r.status_code == 404
|
||||
body = r.json()
|
||||
assert body["success"] is False
|
||||
assert "code" in body["error"]
|
||||
assert "message" in body["error"]
|
||||
assert "data" not in body or body.get("data") is None
|
||||
|
||||
|
||||
async def test_422_uses_error_envelope(auth_client):
|
||||
r = await auth_client.post("/api/v1/orders", json={"items": []})
|
||||
assert r.status_code == 422
|
||||
body = r.json()
|
||||
assert body["success"] is False
|
||||
assert body["error"]["code"] == "VALIDATION_ERROR"
|
||||
assert isinstance(body["error"]["details"], list)
|
||||
|
||||
|
||||
async def test_401_uses_error_envelope(anon_client):
|
||||
from unittest.mock import patch
|
||||
from app.exceptions import UnauthorizedError
|
||||
# Provide a bad token
|
||||
r = await anon_client.get("/api/v1/auth/me", headers={"Authorization": "Bearer invalid.token.here"})
|
||||
assert r.status_code == 401
|
||||
body = r.json()
|
||||
assert body["success"] is False
|
||||
assert body["error"]["code"] == "UNAUTHORIZED"
|
||||
|
||||
|
||||
async def test_403_uses_error_envelope(auth_client):
|
||||
r = await auth_client.get("/api/v1/admin/products")
|
||||
assert r.status_code == 403
|
||||
body = r.json()
|
||||
assert body["success"] is False
|
||||
assert body["error"]["code"] == "FORBIDDEN"
|
||||
|
||||
|
||||
async def test_409_out_of_stock_uses_error_envelope(auth_client, mock_db):
|
||||
from unittest.mock import AsyncMock
|
||||
mock_db.fetchrow = AsyncMock(return_value={
|
||||
"id": "33333333-3333-3333-3333-333333333333",
|
||||
"name": "Test Product",
|
||||
"price": 100.0,
|
||||
"stock_quantity": 0,
|
||||
})
|
||||
r = await auth_client.post("/api/v1/orders", json={
|
||||
"items": [{"product_id": "33333333-3333-3333-3333-333333333333", "quantity": 1}]
|
||||
})
|
||||
assert r.status_code == 409
|
||||
body = r.json()
|
||||
assert body["success"] is False
|
||||
assert body["error"]["code"] == "OUT_OF_STOCK"
|
||||
Reference in New Issue
Block a user