From 520d92af57d30935888692a1fca79cd74affbe60 Mon Sep 17 00:00:00 2001 From: Jacob Magar Date: Sat, 14 Mar 2026 03:43:20 -0400 Subject: [PATCH] feat(elicitation): add is_configured() and apply_runtime_config() to settings --- tests/test_setup.py | 35 +++++++++++++++++++++++++++++++++++ unraid_mcp/config/settings.py | 18 ++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/tests/test_setup.py b/tests/test_setup.py index 8ca5f50..bb70a7a 100644 --- a/tests/test_setup.py +++ b/tests/test_setup.py @@ -1,3 +1,5 @@ +from unittest.mock import patch + import pytest from unraid_mcp.core.exceptions import CredentialsNotConfiguredError, ToolError @@ -17,3 +19,36 @@ def test_credentials_not_configured_error_is_exception(): def test_credentials_not_configured_error_is_not_tool_error(): """CredentialsNotConfiguredError must NOT be a ToolError — it bypasses MCP protocol error handling.""" assert not issubclass(CredentialsNotConfiguredError, ToolError) + + +def test_settings_is_configured_true(): + from unraid_mcp.config import settings + + with ( + patch.object(settings, "UNRAID_API_URL", "https://example.com"), + patch.object(settings, "UNRAID_API_KEY", "key123"), + ): + assert settings.is_configured() is True + + +def test_settings_is_configured_false_when_missing(): + from unraid_mcp.config import settings + + with ( + patch.object(settings, "UNRAID_API_URL", None), + patch.object(settings, "UNRAID_API_KEY", None), + ): + assert settings.is_configured() is False + + +def test_settings_apply_runtime_config_updates_module_globals(): + from unraid_mcp.config import settings + + original_url = settings.UNRAID_API_URL + original_key = settings.UNRAID_API_KEY + settings.apply_runtime_config("https://newurl.com/graphql", "newkey") + assert settings.UNRAID_API_URL == "https://newurl.com/graphql" + assert settings.UNRAID_API_KEY == "newkey" + # Reset so other tests are not affected + settings.UNRAID_API_URL = original_url + settings.UNRAID_API_KEY = original_key diff --git a/unraid_mcp/config/settings.py b/unraid_mcp/config/settings.py index 90aa05d..19a8acf 100644 --- a/unraid_mcp/config/settings.py +++ b/unraid_mcp/config/settings.py @@ -108,6 +108,24 @@ def validate_required_config() -> tuple[bool, list[str]]: return len(missing) == 0, missing +def is_configured() -> bool: + """Return True if both required credentials are present.""" + return bool(UNRAID_API_URL and UNRAID_API_KEY) + + +def apply_runtime_config(api_url: str, api_key: str) -> None: + """Update module-level credential globals at runtime (post-elicitation). + + Also sets matching environment variables so submodules that read + os.getenv() after import see the new values. + """ + global UNRAID_API_URL, UNRAID_API_KEY + UNRAID_API_URL = api_url + UNRAID_API_KEY = api_key + os.environ["UNRAID_API_URL"] = api_url + os.environ["UNRAID_API_KEY"] = api_key + + def get_config_summary() -> dict[str, Any]: """Get a summary of current configuration (safe for logging).