1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Add support to reauthorize expired Plex tokens (#40469)

This commit is contained in:
jjlawren
2020-09-28 09:14:54 -05:00
committed by GitHub
parent eda68f127c
commit 3596eb39f2
5 changed files with 116 additions and 18 deletions

View File

@@ -24,6 +24,7 @@ from homeassistant.config import async_process_ha_core_config
from homeassistant.config_entries import (
ENTRY_STATE_LOADED,
SOURCE_INTEGRATION_DISCOVERY,
SOURCE_REAUTH,
)
from homeassistant.const import (
CONF_HOST,
@@ -723,3 +724,53 @@ async def test_integration_discovery(hass):
== mock_gdm.entries[0]["data"]["Resource-Identifier"]
)
assert flow["step_id"] == "user"
async def test_trigger_reauth(hass, entry, mock_plex_server, mock_websocket):
"""Test setup and reauthorization of a Plex token."""
await async_process_ha_core_config(
hass,
{"internal_url": "http://example.local:8123"},
)
assert entry.state == ENTRY_STATE_LOADED
with patch.object(
mock_plex_server, "clients", side_effect=plexapi.exceptions.Unauthorized
), patch("plexapi.server.PlexServer", side_effect=plexapi.exceptions.Unauthorized):
trigger_plex_update(mock_websocket)
await hass.async_block_till_done()
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
assert entry.state != ENTRY_STATE_LOADED
flows = hass.config_entries.flow.async_progress()
assert len(flows) == 1
assert flows[0]["context"]["source"] == SOURCE_REAUTH
flow_id = flows[0]["flow_id"]
with patch("plexapi.myplex.MyPlexAccount", return_value=MockPlexAccount()), patch(
"plexapi.server.PlexServer", return_value=mock_plex_server
), patch("plexauth.PlexAuth.initiate_auth"), patch(
"plexauth.PlexAuth.token", return_value="BRAND_NEW_TOKEN"
):
result = await hass.config_entries.flow.async_configure(flow_id, user_input={})
assert result["type"] == "external"
result = await hass.config_entries.flow.async_configure(result["flow_id"])
assert result["type"] == "external_done"
result = await hass.config_entries.flow.async_configure(result["flow_id"])
assert result["type"] == "abort"
assert result["reason"] == "reauth_successful"
assert result["flow_id"] == flow_id
assert len(hass.config_entries.flow.async_progress()) == 0
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
assert entry.state == ENTRY_STATE_LOADED
assert entry.data[CONF_SERVER] == mock_plex_server.friendlyName
assert entry.data[CONF_SERVER_IDENTIFIER] == mock_plex_server.machineIdentifier
assert entry.data[PLEX_SERVER_CONFIG][CONF_URL] == mock_plex_server._baseurl
assert entry.data[PLEX_SERVER_CONFIG][CONF_TOKEN] == "BRAND_NEW_TOKEN"