Skip to content

Commit ff57549

Browse files
geofffranksclaude
andcommitted
test(oauth): make token-request unwrap test hermetic
test_local_impl_token_request_unwraps drove a real refresh through HA's LocalOAuth2Implementation._token_request, which builds an aiohttp clientsession whose pycares DNS resolver leaves a daemon _run_safe_shutdown_loop thread. The CI matrix runs Python 3.12, which backtracks to homeassistant 2025.1.4 + pytest-homeassistant-custom-component 0.13.205 -- a build predating the lingering-thread whitelist for that thread -- so teardown failed the test. It passed locally on 3.13 / 0.13.316, which whitelists it. Patch the superclass _token_request (HA's HTTP boundary) instead of mocking the network, so no clientsession or pycares thread is created. The mixin unwrap wiring is still exercised; {code,data} envelope handling stays covered by the three pure _unwrap_utec_token tests and test_oauth_wiring.py. Version-agnostic: passes on both the old and new pytest-HA-CC. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent a89d46c commit ff57549

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

tests/test_oauth.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
goes stale. These cover the unwrap and its wiring into the implementations.
88
"""
99

10-
from aioresponses import aioresponses
10+
from unittest.mock import AsyncMock, patch
11+
1112
import pytest
1213

14+
from homeassistant.helpers import config_entry_oauth2_flow
15+
1316
from custom_components.u_tec.const import DOMAIN, OAUTH2_AUTHORIZE, OAUTH2_TOKEN
1417
from custom_components.u_tec.oauth import (
1518
UtecLocalOAuth2Implementation,
@@ -43,12 +46,23 @@ def test_unwrap_raises_when_no_access_token_present():
4346

4447

4548
async def test_local_impl_token_request_unwraps(hass):
46-
"""_token_request against the real {code,data} envelope returns standard fields."""
49+
"""The mixin unwraps whatever the underlying HA token request returns.
50+
51+
We patch the superclass ``_token_request`` (HA's HTTP boundary) rather than
52+
mock the network: hitting it for real spins up an aiohttp clientsession whose
53+
pycares DNS resolver leaves a daemon ``_run_safe_shutdown_loop`` thread that
54+
older pytest-homeassistant-custom-component builds flag as a lingering-thread
55+
failure. Patching the boundary keeps the test hermetic and version-agnostic
56+
while still proving the mixin is wired in and unwraps the {code,data} envelope.
57+
"""
4758
impl = UtecLocalOAuth2Implementation(
4859
hass, DOMAIN, "client-id", "client-secret", OAUTH2_AUTHORIZE, OAUTH2_TOKEN,
4960
)
50-
with aioresponses() as mock:
51-
mock.post(OAUTH2_TOKEN, payload=_WRAPPED)
61+
with patch.object(
62+
config_entry_oauth2_flow.LocalOAuth2Implementation,
63+
"_token_request",
64+
new=AsyncMock(return_value=_WRAPPED),
65+
):
5266
result = await impl._token_request(
5367
{"grant_type": "refresh_token", "refresh_token": "r"}
5468
)

0 commit comments

Comments
 (0)