""" Integration tests for channel inbound endpoints: - /channel/web/message - /channel/feishu/event - /channel/feishu/verify - /channel/hooks/{path} - /channel/dingtalk/callback """ from __future__ import annotations import pytest pytestmark = [pytest.mark.asyncio, pytest.mark.integration] class TestWebMessage: async def test_web_message_with_invalid_json_returns_400(self, test_client): response = await test_client.post( "/channel/web/message", content=b"not-json", headers={"Content-Type": "application/json"}, ) if response.status_code == 503: pytest.skip("Channel gateway not initialized") assert response.status_code == 400, response.text async def test_web_message_with_valid_json_returns_accepted(self, test_client): response = await test_client.post( "/channel/web/message", json={ "session_id": "intg-test-session", "content": "hello from integration test", "sender": "tester", }, ) if response.status_code == 503: pytest.skip("Channel gateway not initialized") assert response.status_code in (202, 400), response.text if response.status_code == 202: payload = response.json() assert "trace_id" in payload assert payload["status"] in ("accepted", "skipped") class TestFeishuEvent: async def test_feishu_url_verification_returns_challenge(self, test_client): challenge = "test_challenge_value_123" response = await test_client.post( "/channel/feishu/event", json={ "type": "url_verification", "challenge": challenge, }, ) if response.status_code == 503: pytest.skip("Channel gateway not initialized") assert response.status_code == 200, response.text assert response.json()["challenge"] == challenge async def test_feishu_event_without_type_returns_code_0(self, test_client): response = await test_client.post( "/channel/feishu/event", json={"event": {"message": {"content": "test"}}}, ) if response.status_code == 503: pytest.skip("Channel gateway not initialized") assert response.status_code == 200, response.text assert response.json().get("code") == 0 async def test_feishu_verify_endpoint(self, test_client): response = await test_client.get( "/channel/feishu/verify", params={"challenge": "abc", "token": "any"}, ) if response.status_code == 503: pytest.skip("Channel gateway not initialized") if response.status_code == 403: pytest.skip("Feishu verification token mismatch") assert response.status_code == 200, response.text class TestHooksMessage: async def test_hooks_with_nonexistent_path_returns_404(self, test_client): response = await test_client.post( "/channel/hooks/nonexistent_path_xyz", json={"event": "test"}, ) if response.status_code == 503: pytest.skip("Channel gateway not initialized") assert response.status_code == 404, response.text async def test_hooks_with_invalid_json_returns_400(self, test_client): response = await test_client.post( "/channel/hooks/test_path", content=b"not-json", headers={"Content-Type": "application/json"}, ) if response.status_code == 503: pytest.skip("Channel gateway not initialized") if response.status_code == 404: pytest.skip("Hook mapping not configured") assert response.status_code == 400, response.text class TestDingTalkCallback: async def test_dingtalk_callback_returns_accepted(self, test_client): response = await test_client.post( "/channel/dingtalk/callback", json={"msgtype": "text", "text": {"content": "hello"}}, ) if response.status_code == 503: pytest.skip("Channel gateway not initialized") assert response.status_code == 202, response.text payload = response.json() assert payload.get("status") == "accepted"