Skip to content

Commit 1dd3e12

Browse files
authored
Merge pull request #19 from contextforge-org/ServeNoAuthFlag-9
Serve no auth flag 9
2 parents aaa9fd3 + 5e96ae4 commit 1dd3e12

2 files changed

Lines changed: 33 additions & 5 deletions

File tree

cforge/commands/server/serve.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def serve(
3232
port: int = typer.Option(DEFAULT_PORT, "--port", help="Port to bind to"),
3333
reload: bool = typer.Option(False, "--reload", help="Enable auto-reload for development"),
3434
headless: bool = typer.Option(False, "--headless", help="Run without the admin UI"),
35+
no_auth: bool = typer.Option(False, "--no-auth", help="Run without requiring authorization"),
3536
workers: int = typer.Option(1, "--workers", help="Number of worker processes"),
3637
log_level: str = typer.Option("info", "--log-level", help="Log level (debug, info, warning, error, critical)"),
3738
) -> None:
@@ -42,6 +43,7 @@ def serve(
4243
set_serve_settings(
4344
mcpgateway_ui_enabled=not headless,
4445
mcpgateway_admin_api_enabled=not headless,
46+
auth_required=not no_auth,
4547
)
4648
uvicorn.run(
4749
DEFAULT_APP,

tests/commands/server/test_serve.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
# First-Party
1919
from 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

2323
class 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

5278
class 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

Comments
 (0)