1717
1818# First-Party
1919from cforge .commands .server .serve import serve
20- from tests .conftest import get_open_port
20+ from tests .conftest import get_open_port , invoke_typer_command
2121
2222
2323class TestServeCommand :
@@ -26,15 +26,15 @@ class TestServeCommand:
2626 def test_serve_with_defaults (self ) -> None :
2727 """Test serve command with default parameters."""
2828 with patch ("cforge.commands.server.serve.uvicorn.run" ) as mock_run :
29- serve ( )
29+ invoke_typer_command ( serve )
3030 mock_run .assert_called_once ()
3131 args , kwargs = mock_run .call_args
3232 assert "mcpgateway.main:app" in args
3333
3434 def test_serve_with_custom_host_port (self ) -> None :
3535 """Test serve command with custom host and port."""
3636 with patch ("cforge.commands.server.serve.uvicorn.run" ) as mock_run :
37- serve ( host = "0.0.0.0" , port = 8080 )
37+ invoke_typer_command ( serve , host = "0.0.0.0" , port = 8080 )
3838 mock_run .assert_called_once ()
3939 _ , kwargs = mock_run .call_args
4040 assert kwargs .get ("host" ) == "0.0.0.0"
@@ -43,11 +43,37 @@ def test_serve_with_custom_host_port(self) -> None:
4343 def test_serve_with_reload (self ) -> None :
4444 """Test serve command with reload enabled."""
4545 with patch ("cforge.commands.server.serve.uvicorn.run" ) as mock_run :
46- serve ( reload = True )
46+ invoke_typer_command ( serve , reload = True )
4747 mock_run .assert_called_once ()
4848 _ , kwargs = mock_run .call_args
4949 assert kwargs .get ("reload" ) is True
5050
51+ def test_serve_with_no_auth_sets_auth_required_false (self ) -> None :
52+ """Test serve command with --no-auth sets auth_required to False."""
53+ with patch ("cforge.commands.server.serve.uvicorn.run" ) as mock_run , patch ("cforge.commands.server.serve.set_serve_settings" ) as mock_set_settings :
54+ invoke_typer_command (serve , no_auth = True , headless = False )
55+ mock_run .assert_called_once ()
56+ # When headless=False (default), UI and admin API are enabled (not headless = True)
57+ # When no_auth=True, auth_required should be False (not no_auth = False)
58+ mock_set_settings .assert_called_once_with (
59+ mcpgateway_ui_enabled = True ,
60+ mcpgateway_admin_api_enabled = True ,
61+ auth_required = False ,
62+ )
63+
64+ def test_serve_without_no_auth_sets_auth_required_true (self ) -> None :
65+ """Test serve command without --no-auth sets auth_required to True."""
66+ with patch ("cforge.commands.server.serve.uvicorn.run" ) as mock_run , patch ("cforge.commands.server.serve.set_serve_settings" ) as mock_set_settings :
67+ invoke_typer_command (serve , no_auth = False , headless = False )
68+ mock_run .assert_called_once ()
69+ # When headless=False (default), UI and admin API are enabled (not headless = True)
70+ # When no_auth=False (default), auth_required should be True (not no_auth = True)
71+ mock_set_settings .assert_called_once_with (
72+ mcpgateway_ui_enabled = True ,
73+ mcpgateway_admin_api_enabled = True ,
74+ auth_required = True ,
75+ )
76+
5177
5278class TestServeCommandIntegration :
5379 """Integration tests for the serve command"""
@@ -64,7 +90,7 @@ def test_serve_starts_and_responds(self, mock_settings):
6490 # thread does not block process exit.
6591 server_thread = threading .Thread (
6692 target = serve ,
67- kwargs = {"host" : "127.0.0.1" , "port" : port , "reload" : False , "workers" : 1 , "log_level" : "error" },
93+ kwargs = {"host" : "127.0.0.1" , "port" : port , "reload" : False , "workers" : 1 , "log_level" : "error" , "no_auth" : False },
6894 daemon = True ,
6995 )
7096 server_thread .start ()
0 commit comments