diff --git a/gateway/config.py b/gateway/config.py index a29f73069248..71c08f6ab6aa 100644 --- a/gateway/config.py +++ b/gateway/config.py @@ -1637,10 +1637,8 @@ def _enable_from_env(platform: Platform) -> PlatformConfig: feishu_app_id = os.getenv("FEISHU_APP_ID") feishu_app_secret = os.getenv("FEISHU_APP_SECRET") if feishu_app_id and feishu_app_secret: - if Platform.FEISHU not in config.platforms: - config.platforms[Platform.FEISHU] = PlatformConfig() - config.platforms[Platform.FEISHU].enabled = True - config.platforms[Platform.FEISHU].extra.update({ + feishu_config = _enable_from_env(Platform.FEISHU) + feishu_config.extra.update({ "app_id": feishu_app_id, "app_secret": feishu_app_secret, "domain": os.getenv("FEISHU_DOMAIN", "feishu"), @@ -1648,13 +1646,13 @@ def _enable_from_env(platform: Platform) -> PlatformConfig: }) feishu_encrypt_key = os.getenv("FEISHU_ENCRYPT_KEY", "") if feishu_encrypt_key: - config.platforms[Platform.FEISHU].extra["encrypt_key"] = feishu_encrypt_key + feishu_config.extra["encrypt_key"] = feishu_encrypt_key feishu_verification_token = os.getenv("FEISHU_VERIFICATION_TOKEN", "") if feishu_verification_token: - config.platforms[Platform.FEISHU].extra["verification_token"] = feishu_verification_token + feishu_config.extra["verification_token"] = feishu_verification_token feishu_home = os.getenv("FEISHU_HOME_CHANNEL") if feishu_home: - config.platforms[Platform.FEISHU].home_channel = HomeChannel( + feishu_config.home_channel = HomeChannel( platform=Platform.FEISHU, chat_id=feishu_home, name=os.getenv("FEISHU_HOME_CHANNEL_NAME", "Home"), diff --git a/tests/gateway/test_config.py b/tests/gateway/test_config.py index 2ccb63d8864c..cb07f87bdacb 100644 --- a/tests/gateway/test_config.py +++ b/tests/gateway/test_config.py @@ -1045,3 +1045,52 @@ def test_existing_platform_configs_accept_home_channel_env_overrides(self): home = config.platforms[platform].home_channel assert home is not None, f"{platform.value}: home_channel should not be None" assert (home.chat_id, home.name) == expected, platform.value + + +class TestFeishuEnvOverrideRespectsExplicitEnabled: + """Feishu env overrides must not force-enable when config explicitly sets enabled=false.""" + + def test_feishu_env_vars_respect_explicit_disabled(self, monkeypatch): + """When config.yaml has feishu.enabled: false, env vars must NOT override it.""" + monkeypatch.setenv("FEISHU_APP_ID", "test_app_id") + monkeypatch.setenv("FEISHU_APP_SECRET", "test_secret") + config = GatewayConfig(platforms={ + Platform.FEISHU: PlatformConfig( + enabled=False, + extra={"_enabled_explicit": True}, + ), + }) + _apply_env_overrides(config) + assert not config.platforms[Platform.FEISHU].enabled + + def test_feishu_env_vars_enable_when_not_explicitly_disabled(self, monkeypatch): + """When config has no explicit enabled setting, env vars should enable Feishu.""" + monkeypatch.setenv("FEISHU_APP_ID", "test_app_id") + monkeypatch.setenv("FEISHU_APP_SECRET", "test_secret") + config = GatewayConfig(platforms={}) + _apply_env_overrides(config) + assert config.platforms[Platform.FEISHU].enabled + + def test_feishu_env_vars_populate_extra_fields(self, monkeypatch): + """Env vars should populate app_id, app_secret, domain, connection_mode.""" + monkeypatch.setenv("FEISHU_APP_ID", "my_id") + monkeypatch.setenv("FEISHU_APP_SECRET", "my_secret") + monkeypatch.setenv("FEISHU_DOMAIN", "lark") + monkeypatch.setenv("FEISHU_CONNECTION_MODE", "long_polling") + config = GatewayConfig(platforms={}) + _apply_env_overrides(config) + extra = config.platforms[Platform.FEISHU].extra + assert extra["app_id"] == "my_id" + assert extra["app_secret"] == "my_secret" + assert extra["domain"] == "lark" + assert extra["connection_mode"] == "long_polling" + + def test_feishu_env_vars_enable_when_config_has_no_enabled_key(self, monkeypatch): + """When feishu config exists but has no enabled key, env vars should enable.""" + monkeypatch.setenv("FEISHU_APP_ID", "test_id") + monkeypatch.setenv("FEISHU_APP_SECRET", "test_secret") + config = GatewayConfig(platforms={ + Platform.FEISHU: PlatformConfig(), # enabled defaults to False + }) + _apply_env_overrides(config) + assert config.platforms[Platform.FEISHU].enabled