Add reauth flow to webOS TV integration (#86168)

* Add reauth flow to webOS TV integration

* Remove unnecessary else
This commit is contained in:
Shay Levy 2023-01-18 18:48:38 +02:00 committed by GitHub
parent f2b348dbdf
commit c40c37e9ee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 217 additions and 37 deletions

View file

@ -9,11 +9,11 @@ from homeassistant import config_entries
from homeassistant.components import ssdp
from homeassistant.components.webostv.const import CONF_SOURCES, DOMAIN, LIVE_TV_APP_ID
from homeassistant.config_entries import SOURCE_SSDP
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_SOURCE
from homeassistant.const import CONF_CLIENT_SECRET, CONF_HOST, CONF_NAME, CONF_SOURCE
from homeassistant.data_entry_flow import FlowResultType
from . import setup_webostv
from .const import FAKE_UUID, HOST, MOCK_APPS, MOCK_INPUTS, TV_NAME
from .const import CLIENT_KEY, FAKE_UUID, HOST, MOCK_APPS, MOCK_INPUTS, TV_NAME
MOCK_USER_CONFIG = {
CONF_HOST: HOST,
@ -289,3 +289,64 @@ async def test_form_abort_uuid_configured(hass, client):
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "already_configured"
assert entry.data[CONF_HOST] == "new_host"
async def test_reauth_successful(hass, client, monkeypatch):
"""Test that the reauthorization is successful."""
entry = await setup_webostv(hass)
assert client
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_REAUTH, "entry_id": entry.entry_id},
data=entry.data,
)
assert result["step_id"] == "reauth_confirm"
result = await hass.config_entries.flow.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "reauth_confirm"
assert entry.data[CONF_CLIENT_SECRET] == CLIENT_KEY
monkeypatch.setattr(client, "client_key", "new_key")
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "reauth_successful"
assert entry.data[CONF_CLIENT_SECRET] == "new_key"
@pytest.mark.parametrize(
"side_effect,reason",
[
(WebOsTvPairError, "error_pairing"),
(ConnectionRefusedError, "reauth_unsuccessful"),
],
)
async def test_reauth_errors(hass, client, monkeypatch, side_effect, reason):
"""Test reauthorization errors."""
entry = await setup_webostv(hass)
assert client
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_REAUTH, "entry_id": entry.entry_id},
data=entry.data,
)
assert result["step_id"] == "reauth_confirm"
result = await hass.config_entries.flow.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "reauth_confirm"
monkeypatch.setattr(client, "connect", Mock(side_effect=side_effect))
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == reason

View file

@ -0,0 +1,39 @@
"""The tests for the LG webOS TV platform."""
from unittest.mock import Mock
from aiowebostv import WebOsTvPairError
from homeassistant.components.webostv.const import DOMAIN
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState
from homeassistant.const import CONF_CLIENT_SECRET
from . import setup_webostv
async def test_reauth_setup_entry(hass, client, monkeypatch):
"""Test reauth flow triggered by setup entry."""
monkeypatch.setattr(client, "is_connected", Mock(return_value=False))
monkeypatch.setattr(client, "connect", Mock(side_effect=WebOsTvPairError))
entry = await setup_webostv(hass)
assert entry.state == ConfigEntryState.SETUP_ERROR
flows = hass.config_entries.flow.async_progress()
assert len(flows) == 1
flow = flows[0]
assert flow.get("step_id") == "reauth_confirm"
assert flow.get("handler") == DOMAIN
assert "context" in flow
assert flow["context"].get("source") == SOURCE_REAUTH
assert flow["context"].get("entry_id") == entry.entry_id
async def test_key_update_setup_entry(hass, client, monkeypatch):
"""Test key update from setup entry."""
monkeypatch.setattr(client, "client_key", "new_key")
entry = await setup_webostv(hass)
assert entry.state == ConfigEntryState.LOADED
assert entry.data[CONF_CLIENT_SECRET] == "new_key"

View file

@ -4,6 +4,7 @@ from datetime import timedelta
from http import HTTPStatus
from unittest.mock import Mock
from aiowebostv import WebOsTvPairError
import pytest
from homeassistant.components import automation
@ -37,6 +38,7 @@ from homeassistant.components.webostv.media_player import (
SUPPORT_WEBOSTV,
SUPPORT_WEBOSTV_VOLUME,
)
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState
from homeassistant.const import (
ATTR_COMMAND,
ATTR_DEVICE_CLASS,
@ -763,3 +765,28 @@ async def test_get_image_https(
content = await resp.read()
assert content == b"https_image"
async def test_reauth_reconnect(hass, client, monkeypatch):
"""Test reauth flow triggered by reconnect."""
entry = await setup_webostv(hass)
monkeypatch.setattr(client, "is_connected", Mock(return_value=False))
monkeypatch.setattr(client, "connect", Mock(side_effect=WebOsTvPairError))
assert entry.state == ConfigEntryState.LOADED
async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=20))
await hass.async_block_till_done()
assert entry.state == ConfigEntryState.LOADED
flows = hass.config_entries.flow.async_progress()
assert len(flows) == 1
flow = flows[0]
assert flow.get("step_id") == "reauth_confirm"
assert flow.get("handler") == DOMAIN
assert "context" in flow
assert flow["context"].get("source") == SOURCE_REAUTH
assert flow["context"].get("entry_id") == entry.entry_id