53 lines
1.9 KiB
Python
53 lines
1.9 KiB
Python
"""A rejected token during operation opens the re-authentication flow."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import timedelta
|
|
from unittest.mock import MagicMock
|
|
|
|
from freezegun.api import FrozenDateTimeFactory
|
|
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
|
|
from homeassistant.config_entries import SOURCE_REAUTH
|
|
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_ON
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.exceptions import HomeAssistantError
|
|
import pytest
|
|
from pytest_homeassistant_custom_component.common import async_fire_time_changed
|
|
|
|
from custom_components.ninlight.api import NinlightAuthError
|
|
from custom_components.ninlight.const import DOMAIN
|
|
from tests.helpers import entity_id_for
|
|
|
|
|
|
def reauth_flows(hass: HomeAssistant) -> list[str]:
|
|
return [
|
|
flow["context"]["source"]
|
|
for flow in hass.config_entries.flow.async_progress_by_handler(DOMAIN)
|
|
]
|
|
|
|
|
|
async def test_rejected_write_starts_reauth(
|
|
hass: HomeAssistant, init_integration, mock_client: MagicMock
|
|
) -> None:
|
|
mock_client.set_state.side_effect = NinlightAuthError("401")
|
|
with pytest.raises(HomeAssistantError) as raised:
|
|
await hass.services.async_call(
|
|
LIGHT_DOMAIN,
|
|
SERVICE_TURN_ON,
|
|
{ATTR_ENTITY_ID: entity_id_for(hass, "light", "light")},
|
|
blocking=True,
|
|
)
|
|
assert raised.value.translation_key == "auth_failed"
|
|
await hass.async_block_till_done()
|
|
assert reauth_flows(hass) == [SOURCE_REAUTH]
|
|
|
|
|
|
async def test_rejected_poll_starts_reauth(
|
|
hass: HomeAssistant, init_integration, mock_client: MagicMock, freezer: FrozenDateTimeFactory
|
|
) -> None:
|
|
mock_client.get_state.side_effect = NinlightAuthError("401") # lamp with auth_all
|
|
freezer.tick(timedelta(seconds=31))
|
|
async_fire_time_changed(hass)
|
|
await hass.async_block_till_done()
|
|
assert reauth_flows(hass) == [SOURCE_REAUTH]
|