import importlib.util from pathlib import Path import httpx import pytest from relay.config import Settings from .conftest import provision def setup_module_source(): path = Path(__file__).resolve().parents[1] / "scripts/configure_feishu_longconn.py" spec = importlib.util.spec_from_file_location("feishu_setup_test", path) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module def test_longconn_does_not_require_webhook_secrets(): settings = Settings(environment="test", admin_secret="a" * 40, internal_secret="b" * 40, feishu_enabled=True, feishu_app_id="cli_test", feishu_app_secret="fake-secret") assert settings.feishu_event_source == "longconn" with pytest.raises(ValueError): Settings(environment="test", admin_secret="a" * 40, internal_secret="b" * 40, feishu_enabled=True, feishu_app_id="cli_test", feishu_app_secret="fake-secret", feishu_event_source="webhook") def test_setup_checks_private_origin_and_never_returns_tokens(): module = setup_module_source() seen = [] def handler(request): seen.append(request) assert request.url.host == "open.w.cnooc.com.cn" if request.url.path.endswith("app_access_token/internal"): return httpx.Response(200, json={"code": 0, "app_access_token": "fake-sensitive-app-token"}) if request.url.path.endswith("tenant_access_token/internal"): return httpx.Response(200, json={"code": 0, "tenant_access_token": "fake-sensitive-tenant-token"}) return httpx.Response(200, json={"code": 0, "data": {"URL": "wss://ws.private.invalid/path?ticket=fake-sensitive-ticket"}}) with httpx.Client(transport=httpx.MockTransport(handler), follow_redirects=False) as client: result = module.check_platform("https://open.w.cnooc.com.cn", "cli_test", "fake-secret", client) assert len(seen) == 3 assert "fake-sensitive" not in str(result) assert "ws.private.invalid" in str(result) def test_setup_refuses_redirect_and_suppresses_response_body(): module = setup_module_source() with httpx.Client(transport=httpx.MockTransport(lambda request: httpx.Response(302, headers={"Location": "https://evil.invalid"}, text="fake-secret")), follow_redirects=False) as client: with pytest.raises(RuntimeError) as error: module.check_platform("https://open.w.cnooc.com.cn", "cli_test", "fake-secret", client) assert "302" in str(error.value) and "fake-secret" not in str(error.value) def test_env_update_preserves_other_settings_and_backs_up(tmp_path): module = setup_module_source() path = tmp_path / ".env" path.write_text("# existing\nHN_ADMIN_SECRET=fake-admin\nHN_FEISHU_ENABLED=false\nHN_FEISHU_ENABLED=false\nHN_PUBLIC_URL=https://10.77.76.13\n") module.update_env(path, {"HN_FEISHU_ENABLED": "true", "HN_FEISHU_APP_SECRET": "fake-platform-secret", "HN_OAUTH_ENABLED": "false"}) value = path.read_text() assert "HN_ADMIN_SECRET=fake-admin" in value and "HN_PUBLIC_URL=https://10.77.76.13" in value assert value.count("HN_FEISHU_ENABLED=") == 1 assert "HN_FEISHU_APP_SECRET=fake-platform-secret" in value assert len(list(tmp_path.glob(".env.before-feishu-*"))) == 1 async def test_admin_can_unbind_test_identity_without_revoking_device(env): directory, host = await provision(env) result = await env["client"].post(f"/admin/devices/{host}/unbind", headers=env["admin"]) assert result.status_code == 200 device = env["connector"].state.store.device(host) assert device["owner"] is None and device["revoked"] == 0 assert await env["redis"].get("hn:web-generation:" + host) == "2" from relay.device import bind_device code_result = await env["client"].post("/admin/test/binding-code", headers=env["admin"], json={"open_id": "ou_real", "text": "ignored"}) code = code_result.json()["message"].split(":")[-1] await bind_device(directory, env["connector_url"], code) assert env["connector"].state.store.device(host)["owner"] == "ou_real" async def test_unconfigured_webhook_and_unverified_oauth_are_closed(env): env["settings"].feishu_encrypt_key = "" env["settings"].feishu_verification_token = "" env["settings"].oauth_enabled = False assert (await env["client"].post("/feishu/event", json={})).status_code == 503 assert (await env["client"].post("/oauth/start", json={})).status_code == 503