|
24 | 24 | from .utils import AsyncFakeAuth, FakeAuth |
25 | 25 |
|
26 | 26 |
|
| 27 | +class ReportControllerBase(ControllerBase): |
| 28 | + @http_get("") |
| 29 | + def report(self): |
| 30 | + return {"controller": type(self).__name__} |
| 31 | + |
| 32 | + |
| 33 | +@api_controller("/alpha", urls_namespace="alpha") |
| 34 | +class AlphaReportController(ReportControllerBase): |
| 35 | + pass |
| 36 | + |
| 37 | + |
| 38 | +@api_controller("/beta", urls_namespace="beta") |
| 39 | +class BetaReportController(ReportControllerBase): |
| 40 | + pass |
| 41 | + |
| 42 | + |
27 | 43 | @api_controller |
28 | 44 | class SomeController: |
29 | 45 | pass |
@@ -384,3 +400,46 @@ def test_namespaced_controller_detail(client): |
384 | 400 |
|
385 | 401 | def test_default_url_name(client): |
386 | 402 | assert reverse("api-1.0.0:get_event", kwargs={"id": 5}) == "/api/events/5" |
| 403 | + |
| 404 | + |
| 405 | +def test_controller_subclass_routes_remain_isolated(): |
| 406 | + api = NinjaExtraAPI() |
| 407 | + api.register_controllers(AlphaReportController, BetaReportController) |
| 408 | + client = testing.TestClient(api) |
| 409 | + |
| 410 | + alpha_response = client.get("/alpha") |
| 411 | + beta_response = client.get("/beta") |
| 412 | + |
| 413 | + assert alpha_response.status_code == 200 |
| 414 | + assert beta_response.status_code == 200 |
| 415 | + assert alpha_response.json() == {"controller": "AlphaReportController"} |
| 416 | + assert beta_response.json() == {"controller": "BetaReportController"} |
| 417 | + |
| 418 | + |
| 419 | +def test_controller_multi_level_inheritance_routes_isolated(): |
| 420 | + """Test that route isolation works with multi-level inheritance.""" |
| 421 | + |
| 422 | + # Middle layer doesn't override the route |
| 423 | + class MiddleReportController(ReportControllerBase): |
| 424 | + pass |
| 425 | + |
| 426 | + @api_controller("/gamma") |
| 427 | + class GammaReportController(MiddleReportController): |
| 428 | + pass |
| 429 | + |
| 430 | + @api_controller("/delta") |
| 431 | + class DeltaReportController(MiddleReportController): |
| 432 | + pass |
| 433 | + |
| 434 | + api = NinjaExtraAPI() |
| 435 | + api.register_controllers(GammaReportController) |
| 436 | + api.register_controllers(DeltaReportController) |
| 437 | + client = testing.TestClient(api) |
| 438 | + |
| 439 | + gamma_response = client.get("/gamma") |
| 440 | + delta_response = client.get("/delta") |
| 441 | + |
| 442 | + assert gamma_response.status_code == 200 |
| 443 | + assert delta_response.status_code == 200 |
| 444 | + assert gamma_response.json() == {"controller": "GammaReportController"} |
| 445 | + assert delta_response.json() == {"controller": "DeltaReportController"} |
0 commit comments