| 1 |
from functools import wraps |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from ninja import NinjaAPI, Router |
| 6 |
from ninja.testing import TestClient |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
def operation_decorator(func): |
| 11 |
"""Decorator that adds a header after validation (operation level)""" |
| 12 |
|
| 13 |
@wraps(func) |
| 14 |
def wrapper(request, *args, **kwargs): |
| 15 |
result = func(request, *args, **kwargs) |
| 16 |
if isinstance(result, dict): |
| 17 |
result["operation_decorated"] = True |
| 18 |
return result |
| 19 |
|
| 20 |
return wrapper |
| 21 |
|
| 22 |
|
| 23 |
def view_decorator(func): |
| 24 |
"""Decorator that adds a header before validation (view level)""" |
| 25 |
|
| 26 |
@wraps(func) |
| 27 |
def wrapper(request, *args, **kwargs): |
| 28 |
response = func(request, *args, **kwargs) |
| 29 |
response["X-View-Decorator"] = "applied" |
| 30 |
return response |
| 31 |
|
| 32 |
return wrapper |
| 33 |
|
| 34 |
|
| 35 |
def counter_decorator(func): |
| 36 |
"""Decorator that counts calls""" |
| 37 |
|
| 38 |
@wraps(func) |
| 39 |
def wrapper(request, *args, **kwargs): |
| 40 |
wrapper.call_count = getattr(wrapper, "call_count", 0) + 1 |
| 41 |
result = func(request, *args, **kwargs) |
| 42 |
if isinstance(result, dict): |
| 43 |
result["call_count"] = wrapper.call_count |
| 44 |
return result |
| 45 |
|
| 46 |
return wrapper |
| 47 |
|
| 48 |
|
| 49 |
def test_router_add_decorator_operation_mode(): |
| 50 |
"""Test add_decorator on router with OPERATION mode""" |
| 51 |
api = NinjaAPI() |
| 52 |
router = Router() |
| 53 |
|
| 54 |
|
| 55 |
router.add_decorator(operation_decorator, mode="operation") |
| 56 |
|
| 57 |
@router.get("/test") |
| 58 |
def endpoint(request): |
| 59 |
return {"message": "test"} |
| 60 |
|
| 61 |
api.add_router("/", router) |
| 62 |
client = TestClient(api) |
| 63 |
|
| 64 |
response = client.get("/test") |
| 65 |
assert response.status_code == 200 |
| 66 |
assert response.json() == {"message": "test", "operation_decorated": True} |
| 67 |
|
| 68 |
|
| 69 |
def test_router_add_decorator_view_mode(): |
| 70 |
"""Test add_decorator on router with VIEW mode""" |
| 71 |
api = NinjaAPI() |
| 72 |
router = Router() |
| 73 |
|
| 74 |
|
| 75 |
router.add_decorator(view_decorator, mode="view") |
| 76 |
|
| 77 |
@router.get("/test") |
| 78 |
def endpoint(request): |
| 79 |
return {"message": "test"} |
| 80 |
|
| 81 |
api.add_router("/", router) |
| 82 |
client = TestClient(api) |
| 83 |
|
| 84 |
response = client.get("/test") |
| 85 |
assert response.status_code == 200 |
| 86 |
assert response["X-View-Decorator"] == "applied" |
| 87 |
assert response.json() == {"message": "test"} |
| 88 |
|
| 89 |
|
| 90 |
def test_api_add_decorator_operation_mode(): |
| 91 |
"""Test add_decorator on API with OPERATION mode""" |
| 92 |
api = NinjaAPI() |
| 93 |
|
| 94 |
|
| 95 |
api.add_decorator(operation_decorator, mode="operation") |
| 96 |
|
| 97 |
@api.get("/test1") |
| 98 |
def endpoint1(request): |
| 99 |
return {"message": "test1"} |
| 100 |
|
| 101 |
@api.get("/test2") |
| 102 |
def endpoint2(request): |
| 103 |
return {"message": "test2"} |
| 104 |
|
| 105 |
client = TestClient(api) |
| 106 |
|
| 107 |
|
| 108 |
response = client.get("/test1") |
| 109 |
assert response.status_code == 200 |
| 110 |
assert response.json() == {"message": "test1", "operation_decorated": True} |
| 111 |
|
| 112 |
response = client.get("/test2") |
| 113 |
assert response.status_code == 200 |
| 114 |
assert response.json() == {"message": "test2", "operation_decorated": True} |
| 115 |
|
| 116 |
|
| 117 |
def test_api_add_decorator_view_mode(): |
| 118 |
"""Test add_decorator on API with VIEW mode""" |
| 119 |
api = NinjaAPI() |
| 120 |
|
| 121 |
|
| 122 |
api.add_decorator(view_decorator, mode="view") |
| 123 |
|
| 124 |
@api.get("/test") |
| 125 |
def endpoint(request): |
| 126 |
return {"message": "test"} |
| 127 |
|
| 128 |
client = TestClient(api) |
| 129 |
|
| 130 |
response = client.get("/test") |
| 131 |
assert response.status_code == 200 |
| 132 |
assert response["X-View-Decorator"] == "applied" |
| 133 |
|
| 134 |
|
| 135 |
def test_multiple_decorators(): |
| 136 |
"""Test multiple decorators on same router""" |
| 137 |
api = NinjaAPI() |
| 138 |
router = Router() |
| 139 |
|
| 140 |
|
| 141 |
router.add_decorator(operation_decorator, mode="operation") |
| 142 |
router.add_decorator(counter_decorator, mode="operation") |
| 143 |
|
| 144 |
@router.get("/test") |
| 145 |
def endpoint(request): |
| 146 |
return {"message": "test"} |
| 147 |
|
| 148 |
api.add_router("/", router) |
| 149 |
client = TestClient(api) |
| 150 |
|
| 151 |
response = client.get("/test") |
| 152 |
assert response.status_code == 200 |
| 153 |
result = response.json() |
| 154 |
assert result["message"] == "test" |
| 155 |
assert result["operation_decorated"] is True |
| 156 |
assert result["call_count"] == 1 |
| 157 |
|
| 158 |
|
| 159 |
response = client.get("/test") |
| 160 |
result = response.json() |
| 161 |
assert result["call_count"] == 2 |
| 162 |
|
| 163 |
|
| 164 |
def test_decorator_cascading(): |
| 165 |
"""Test that decorators cascade from API to router to child router""" |
| 166 |
api = NinjaAPI() |
| 167 |
parent_router = Router() |
| 168 |
child_router = Router() |
| 169 |
|
| 170 |
|
| 171 |
api.add_decorator( |
| 172 |
lambda f: wraps(f)(lambda req, *a, **k: {**f(req, *a, **k), "api": True}) |
| 173 |
) |
| 174 |
|
| 175 |
|
| 176 |
parent_router.add_decorator( |
| 177 |
lambda f: wraps(f)(lambda req, *a, **k: {**f(req, *a, **k), "parent": True}) |
| 178 |
) |
| 179 |
|
| 180 |
|
| 181 |
child_router.add_decorator( |
| 182 |
lambda f: wraps(f)(lambda req, *a, **k: {**f(req, *a, **k), "child": True}) |
| 183 |
) |
| 184 |
|
| 185 |
@child_router.get("/test") |
| 186 |
def endpoint(request): |
| 187 |
return {"message": "test"} |
| 188 |
|
| 189 |
parent_router.add_router("/child", child_router) |
| 190 |
api.add_router("/parent", parent_router) |
| 191 |
|
| 192 |
client = TestClient(api) |
| 193 |
response = client.get("/parent/child/test") |
| 194 |
assert response.status_code == 200 |
| 195 |
result = response.json() |
| 196 |
assert result == { |
| 197 |
"message": "test", |
| 198 |
"api": True, |
| 199 |
"parent": True, |
| 200 |
"child": True, |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
def test_api_decorator_applies_to_new_routers(): |
| 205 |
"""Test that API-level decorators apply to routers added after decorator""" |
| 206 |
api = NinjaAPI() |
| 207 |
|
| 208 |
|
| 209 |
api.add_decorator(operation_decorator, mode="operation") |
| 210 |
|
| 211 |
|
| 212 |
router = Router() |
| 213 |
|
| 214 |
@router.get("/test") |
| 215 |
def endpoint(request): |
| 216 |
return {"message": "test"} |
| 217 |
|
| 218 |
api.add_router("/", router) |
| 219 |
|
| 220 |
client = TestClient(api) |
| 221 |
response = client.get("/test") |
| 222 |
assert response.status_code == 200 |
| 223 |
assert response.json() == {"message": "test", "operation_decorated": True} |
| 224 |
|
| 225 |
|
| 226 |
def test_mix_view_and_operation_decorators(): |
| 227 |
"""Test mixing VIEW and OPERATION mode decorators""" |
| 228 |
api = NinjaAPI() |
| 229 |
router = Router() |
| 230 |
|
| 231 |
|
| 232 |
router.add_decorator(view_decorator, mode="view") |
| 233 |
router.add_decorator(operation_decorator, mode="operation") |
| 234 |
|
| 235 |
@router.get("/test") |
| 236 |
def endpoint(request): |
| 237 |
return {"message": "test"} |
| 238 |
|
| 239 |
api.add_router("/", router) |
| 240 |
client = TestClient(api) |
| 241 |
|
| 242 |
response = client.get("/test") |
| 243 |
assert response.status_code == 200 |
| 244 |
assert response["X-View-Decorator"] == "applied" |
| 245 |
assert response.json() == {"message": "test", "operation_decorated": True} |
| 246 |
|
| 247 |
|
| 248 |
def test_decorator_with_path_params(): |
| 249 |
"""Test decorators work with path parameters""" |
| 250 |
api = NinjaAPI() |
| 251 |
router = Router() |
| 252 |
|
| 253 |
def param_decorator(func): |
| 254 |
@wraps(func) |
| 255 |
def wrapper(request, *args, **kwargs): |
| 256 |
result = func(request, *args, **kwargs) |
| 257 |
if isinstance(result, dict): |
| 258 |
result["decorated"] = True |
| 259 |
return result |
| 260 |
|
| 261 |
return wrapper |
| 262 |
|
| 263 |
router.add_decorator(param_decorator, mode="operation") |
| 264 |
|
| 265 |
@router.get("/test/{item_id}") |
| 266 |
def endpoint(request, item_id: int): |
| 267 |
return {"item_id": item_id} |
| 268 |
|
| 269 |
api.add_router("/", router) |
| 270 |
client = TestClient(api) |
| 271 |
|
| 272 |
response = client.get("/test/123") |
| 273 |
assert response.status_code == 200 |
| 274 |
assert response.json() == {"item_id": 123, "decorated": True} |
| 275 |
|
| 276 |
|
| 277 |
def test_invalid_decorator_mode(): |
| 278 |
"""Test that invalid decorator mode raises ValueError""" |
| 279 |
router = Router() |
| 280 |
|
| 281 |
with pytest.raises(ValueError, match="Invalid decorator mode"): |
| 282 |
router.add_decorator(operation_decorator, mode="invalid") |