From b0892375cd260a8d3e40af15002a4d498a7d19c1 Mon Sep 17 00:00:00 2001 From: Teknium Date: Sat, 11 Apr 2026 14:03:51 -0700 Subject: [PATCH] fix: mock aiohttp server in startup guard tests to avoid port binding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The startup guard tests called connect() which bound a real aiohttp server on port 8080 — flaky in any environment where the port is in use. Mock AppRunner, TCPSite, and ClientSession instead. --- tests/gateway/test_sms.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/tests/gateway/test_sms.py b/tests/gateway/test_sms.py index dbdb3b42b7..d8a1589bdf 100644 --- a/tests/gateway/test_sms.py +++ b/tests/gateway/test_sms.py @@ -298,7 +298,14 @@ class TestStartupGuard: @pytest.mark.asyncio async def test_insecure_flag_allows_start_without_url(self): - with patch.dict(os.environ, {"SMS_INSECURE_NO_SIGNATURE": "true"}): + mock_session = AsyncMock() + with patch.dict(os.environ, {"SMS_INSECURE_NO_SIGNATURE": "true"}), \ + patch("aiohttp.web.AppRunner") as mock_runner_cls, \ + patch("aiohttp.web.TCPSite") as mock_site_cls, \ + patch("aiohttp.ClientSession", return_value=mock_session): + mock_runner_cls.return_value.setup = AsyncMock() + mock_runner_cls.return_value.cleanup = AsyncMock() + mock_site_cls.return_value.start = AsyncMock() adapter = self._make_adapter() result = await adapter.connect() assert result is True @@ -306,12 +313,19 @@ class TestStartupGuard: @pytest.mark.asyncio async def test_webhook_url_allows_start(self): - adapter = self._make_adapter( - extra_env={"SMS_WEBHOOK_URL": "https://example.com/webhooks/twilio"} - ) - result = await adapter.connect() - assert result is True - await adapter.disconnect() + mock_session = AsyncMock() + with patch("aiohttp.web.AppRunner") as mock_runner_cls, \ + patch("aiohttp.web.TCPSite") as mock_site_cls, \ + patch("aiohttp.ClientSession", return_value=mock_session): + mock_runner_cls.return_value.setup = AsyncMock() + mock_runner_cls.return_value.cleanup = AsyncMock() + mock_site_cls.return_value.start = AsyncMock() + adapter = self._make_adapter( + extra_env={"SMS_WEBHOOK_URL": "https://example.com/webhooks/twilio"} + ) + result = await adapter.connect() + assert result is True + await adapter.disconnect() # ── Twilio signature validation ────────────────────────────────────