@@ -91,6 +91,18 @@ def mock_protected_method(self, request: Request) -> BaseResponse:
9191 code = ResponseCode .OK , message = "protected endpoint" , timestamp = BaseResponse .current_timestamp ()
9292 )
9393
94+ def mock_unlimited_unprotected_method (self , request : Request ) -> BaseResponse :
95+ """Mock unlimited unprotected method."""
96+ return BaseResponse (
97+ code = ResponseCode .OK , message = "unlimited unprotected endpoint" , timestamp = BaseResponse .current_timestamp ()
98+ )
99+
100+ def mock_unlimited_protected_method (self , request : Request ) -> BaseResponse :
101+ """Mock unlimited protected method."""
102+ return BaseResponse (
103+ code = ResponseCode .OK , message = "unlimited protected endpoint" , timestamp = BaseResponse .current_timestamp ()
104+ )
105+
94106 def validate_config (self , config_data : dict [str , Any ]) -> TemplateServerConfig :
95107 """Validate configuration from the config.json file.
96108
@@ -104,6 +116,20 @@ def setup_routes(self) -> None:
104116 super ().setup_routes ()
105117 self .add_unauthenticated_route ("/unauthenticated-endpoint" , self .mock_unprotected_method , BaseResponse , ["GET" ])
106118 self .add_authenticated_route ("/authenticated-endpoint" , self .mock_protected_method , BaseResponse , ["POST" ])
119+ self .add_unauthenticated_route (
120+ "/unlimited-unauthenticated-endpoint" ,
121+ self .mock_unlimited_unprotected_method ,
122+ BaseResponse ,
123+ ["GET" ],
124+ limited = False ,
125+ )
126+ self .add_authenticated_route (
127+ "/unlimited-authenticated-endpoint" ,
128+ self .mock_unlimited_protected_method ,
129+ BaseResponse ,
130+ ["POST" ],
131+ limited = False ,
132+ )
107133
108134
109135class TestTemplateServer :
@@ -485,11 +511,64 @@ def test_add_authenticated_route(self, mock_template_server: MockTemplateServer)
485511 assert "POST" in test_route .methods
486512 assert test_route .response_model == BaseResponse
487513
514+ def test_limited_parameter_with_rate_limiting_enabled (
515+ self , mock_template_server_config : TemplateServerConfig
516+ ) -> None :
517+ """Test that limited=True applies rate limiting when limiter is enabled."""
518+ mock_template_server_config .rate_limit .enabled = True
519+ server = MockTemplateServer (config = mock_template_server_config )
520+
521+ # Get the limited routes
522+ api_routes = [route for route in server .app .routes if isinstance (route , APIRoute )]
523+ limited_route = next ((route for route in api_routes if route .path == "/unauthenticated-endpoint" ), None )
524+ unlimited_route = next (
525+ (route for route in api_routes if route .path == "/unlimited-unauthenticated-endpoint" ), None
526+ )
527+
528+ assert limited_route is not None
529+ assert unlimited_route is not None
530+
531+ # Limited route should have the limiter wrapper
532+ assert hasattr (limited_route .endpoint , "__wrapped__" )
533+ # Unlimited route should not have the limiter wrapper
534+ assert not hasattr (unlimited_route .endpoint , "__wrapped__" )
535+
536+ def test_authenticated_route_limited_parameter (self , mock_template_server_config : TemplateServerConfig ) -> None :
537+ """Test that limited parameter works correctly for authenticated routes."""
538+ mock_template_server_config .rate_limit .enabled = True
539+ server = MockTemplateServer (config = mock_template_server_config )
540+
541+ # Get the authenticated routes
542+ api_routes = [route for route in server .app .routes if isinstance (route , APIRoute )]
543+ limited_route = next ((route for route in api_routes if route .path == "/authenticated-endpoint" ), None )
544+ unlimited_route = next (
545+ (route for route in api_routes if route .path == "/unlimited-authenticated-endpoint" ), None
546+ )
547+
548+ assert limited_route is not None
549+ assert unlimited_route is not None
550+
551+ # Both routes should have authentication dependencies
552+ assert len (limited_route .dependencies ) > 0
553+ assert len (unlimited_route .dependencies ) > 0
554+
555+ # Limited route should have the limiter wrapper
556+ assert hasattr (limited_route .endpoint , "__wrapped__" )
557+ # Unlimited route should not have the limiter wrapper
558+ assert not hasattr (unlimited_route .endpoint , "__wrapped__" )
559+
488560 def test_setup_routes (self , mock_template_server : MockTemplateServer ) -> None :
489561 """Test that routes are set up correctly."""
490562 api_routes = [route for route in mock_template_server .app .routes if isinstance (route , APIRoute )]
491563 routes = [route .path for route in api_routes ]
492- expected_endpoints = ["/health" , "/metrics" , "/unauthenticated-endpoint" , "/authenticated-endpoint" ]
564+ expected_endpoints = [
565+ "/health" ,
566+ "/metrics" ,
567+ "/unauthenticated-endpoint" ,
568+ "/authenticated-endpoint" ,
569+ "/unlimited-unauthenticated-endpoint" ,
570+ "/unlimited-authenticated-endpoint" ,
571+ ]
493572 for endpoint in expected_endpoints :
494573 assert endpoint in routes
495574
0 commit comments