@@ -124,6 +124,18 @@ def test_bearer_auth_middleware_calls_get_token_verifier_in_resource_mode(
124124 mock_get_verifier .assert_called_once_with (resource = "https://api.example.com" )
125125
126126
127+ def test_bearer_auth_middleware_throws_for_invalid_mode (
128+ valid_server_config : AuthServerConfig ,
129+ ):
130+ """Test that bearer_auth_middleware throws a ValueError for an invalid mode."""
131+ auth = MCPAuth (server = valid_server_config )
132+ with pytest .raises (
133+ ValueError ,
134+ match = "mode_or_verify must be 'jwt' or a callable function that verifies tokens." ,
135+ ):
136+ auth .bearer_auth_middleware (mode_or_verify = "invalid_mode" ) # type: ignore
137+
138+
127139@patch ("mcpauth.auth.resource_server_handler.validate_server_config" )
128140def test_metadata_route_throws_in_resource_mode (
129141 mock_validate : MagicMock , valid_resource_config : ResourceServerConfig
@@ -164,6 +176,26 @@ def test_metadata_route_calls_handler_method(
164176 mock_create_route .assert_called_once ()
165177
166178
179+ @patch (
180+ "mcpauth.auth.authorization_server_handler.AuthorizationServerHandler.create_metadata_route"
181+ )
182+ @patch ("mcpauth.auth.authorization_server_handler.validate_server_config" )
183+ def test_metadata_route_throws_if_route_is_not_route_instance (
184+ mock_validate : MagicMock ,
185+ mock_create_route : MagicMock ,
186+ valid_server_config : AuthServerConfig ,
187+ ):
188+ """Test that metadata_route throws an error if the created route is not a Route instance."""
189+ # Ensure the mock returns a router-like object with a routes attribute
190+ # containing something that is not a Route instance
191+ mock_create_route .return_value = MagicMock (routes = [MagicMock ()])
192+ auth = MCPAuth (server = valid_server_config )
193+ with pytest .warns (DeprecationWarning ):
194+ with pytest .raises (IndexError , match = "No metadata endpoint route was created" ):
195+ auth .metadata_route () # pyright: ignore[reportDeprecated]
196+ mock_create_route .assert_called_once ()
197+
198+
167199@patch (
168200 "mcpauth.auth.resource_server_handler.ResourceServerHandler.create_metadata_route"
169201)
@@ -176,4 +208,20 @@ def test_resource_metadata_router_calls_handler_method(
176208 """Test that resource_metadata_router calls the handler's create_metadata_route method."""
177209 auth = MCPAuth (protected_resources = valid_resource_config )
178210 auth .resource_metadata_router ()
179- mock_create_route .assert_called_once ()
211+ mock_create_route .assert_called_once ()
212+
213+
214+ @patch ("mcpauth.middleware.create_bearer_auth.create_bearer_auth" )
215+ def test_bearer_auth_middleware_with_callable_verifier (
216+ mock_create_bearer_auth : MagicMock , valid_server_config : AuthServerConfig
217+ ):
218+ """Test that bearer_auth_middleware works with a callable verifier."""
219+ auth = MCPAuth (server = valid_server_config )
220+ verifier = MagicMock ()
221+ with patch ("mcpauth.MCPAuthHandler.get_token_verifier" ):
222+ auth .bearer_auth_middleware (mode_or_verify = verifier )
223+
224+ mock_create_bearer_auth .assert_called_once ()
225+ # Check that the verifier is passed to create_bearer_auth
226+ args , _ = mock_create_bearer_auth .call_args
227+ assert args [0 ] == verifier
0 commit comments