mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 08:47:57 +00:00
Split reauth tests in plex (#89212)
This commit is contained in:
parent
c7b30b61de
commit
0c65af93af
@ -1,5 +1,6 @@
|
||||
"""Fixtures for Plex tests."""
|
||||
from unittest.mock import patch
|
||||
from collections.abc import Generator
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
@ -18,6 +19,15 @@ def plex_server_url(entry):
|
||||
return entry.data[PLEX_SERVER_CONFIG][CONF_URL].split(":", 1)[-1]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_setup_entry() -> Generator[AsyncMock, None, None]:
|
||||
"""Override async_setup_entry."""
|
||||
with patch(
|
||||
"homeassistant.components.plex.async_setup_entry", return_value=True
|
||||
) as mock_setup_entry:
|
||||
yield mock_setup_entry
|
||||
|
||||
|
||||
@pytest.fixture(name="album", scope="session")
|
||||
def album_fixture():
|
||||
"""Load album payload and return it."""
|
||||
|
@ -2,7 +2,7 @@
|
||||
import copy
|
||||
from http import HTTPStatus
|
||||
import ssl
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import plexapi.exceptions
|
||||
import pytest
|
||||
@ -42,7 +42,6 @@ from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from .const import DEFAULT_OPTIONS, MOCK_SERVERS, MOCK_TOKEN, PLEX_DIRECT_URL
|
||||
from .helpers import trigger_plex_update, wait_for_debouncer
|
||||
from .mock_classes import MockGDM
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
@ -718,31 +717,22 @@ async def test_integration_discovery(hass: HomeAssistant) -> None:
|
||||
assert flow["step_id"] == "user"
|
||||
|
||||
|
||||
async def test_trigger_reauth(
|
||||
async def test_reauth(
|
||||
hass: HomeAssistant,
|
||||
entry,
|
||||
mock_plex_server,
|
||||
mock_websocket,
|
||||
entry: MockConfigEntry,
|
||||
mock_plex_calls: None,
|
||||
current_request_with_host: None,
|
||||
mock_setup_entry: AsyncMock,
|
||||
) -> None:
|
||||
"""Test setup and reauthorization of a Plex token."""
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
assert entry.state is ConfigEntryState.LOADED
|
||||
|
||||
with patch(
|
||||
"plexapi.server.PlexServer.clients", side_effect=plexapi.exceptions.Unauthorized
|
||||
), patch("plexapi.server.PlexServer", side_effect=plexapi.exceptions.Unauthorized):
|
||||
trigger_plex_update(mock_websocket)
|
||||
await wait_for_debouncer(hass)
|
||||
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
||||
assert entry.state is not ConfigEntryState.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"]
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_REAUTH},
|
||||
data=entry.data,
|
||||
)
|
||||
flow_id = result["flow_id"]
|
||||
|
||||
with patch("plexauth.PlexAuth.initiate_auth"), patch(
|
||||
"plexauth.PlexAuth.token", return_value="BRAND_NEW_TOKEN"
|
||||
@ -767,38 +757,33 @@ async def test_trigger_reauth(
|
||||
assert entry.data[PLEX_SERVER_CONFIG][CONF_URL] == PLEX_DIRECT_URL
|
||||
assert entry.data[PLEX_SERVER_CONFIG][CONF_TOKEN] == "BRAND_NEW_TOKEN"
|
||||
|
||||
mock_setup_entry.assert_called_once()
|
||||
|
||||
async def test_trigger_reauth_multiple_servers_available(
|
||||
|
||||
async def test_reauth_multiple_servers_available(
|
||||
hass: HomeAssistant,
|
||||
entry,
|
||||
mock_plex_server,
|
||||
mock_websocket,
|
||||
entry: MockConfigEntry,
|
||||
mock_plex_calls: None,
|
||||
current_request_with_host: None,
|
||||
requests_mock: requests_mock.Mocker,
|
||||
plextv_resources_two_servers,
|
||||
plextv_resources_two_servers: str,
|
||||
mock_setup_entry: AsyncMock,
|
||||
) -> None:
|
||||
"""Test setup and reauthorization of a Plex token when multiple servers are available."""
|
||||
assert entry.state is ConfigEntryState.LOADED
|
||||
|
||||
requests_mock.get(
|
||||
"https://plex.tv/api/resources",
|
||||
text=plextv_resources_two_servers,
|
||||
)
|
||||
|
||||
with patch(
|
||||
"plexapi.server.PlexServer.clients", side_effect=plexapi.exceptions.Unauthorized
|
||||
), patch("plexapi.server.PlexServer", side_effect=plexapi.exceptions.Unauthorized):
|
||||
trigger_plex_update(mock_websocket)
|
||||
await wait_for_debouncer(hass)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
||||
assert entry.state is not ConfigEntryState.LOADED
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_REAUTH},
|
||||
data=entry.data,
|
||||
)
|
||||
|
||||
flows = hass.config_entries.flow.async_progress()
|
||||
assert len(flows) == 1
|
||||
assert flows[0]["context"]["source"] == SOURCE_REAUTH
|
||||
|
||||
flow_id = flows[0]["flow_id"]
|
||||
flow_id = result["flow_id"]
|
||||
|
||||
with patch("plexauth.PlexAuth.initiate_auth"), patch(
|
||||
"plexauth.PlexAuth.token", return_value="BRAND_NEW_TOKEN"
|
||||
@ -823,6 +808,8 @@ async def test_trigger_reauth_multiple_servers_available(
|
||||
assert entry.data[PLEX_SERVER_CONFIG][CONF_URL] == PLEX_DIRECT_URL
|
||||
assert entry.data[PLEX_SERVER_CONFIG][CONF_TOKEN] == "BRAND_NEW_TOKEN"
|
||||
|
||||
mock_setup_entry.assert_called_once()
|
||||
|
||||
|
||||
async def test_client_request_missing(hass: HomeAssistant) -> None:
|
||||
"""Test when client headers are not set properly."""
|
||||
|
@ -15,7 +15,7 @@ from homeassistant.components.plex.models import (
|
||||
TRANSIENT_SECTION,
|
||||
UNKNOWN_SECTION,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState
|
||||
from homeassistant.const import (
|
||||
CONF_TOKEN,
|
||||
CONF_URL,
|
||||
@ -336,3 +336,27 @@ async def test_setup_with_limited_credentials(
|
||||
|
||||
assert len(hass.config_entries.async_entries(const.DOMAIN)) == 1
|
||||
assert entry.state is ConfigEntryState.LOADED
|
||||
|
||||
|
||||
async def test_trigger_reauth(
|
||||
hass: HomeAssistant,
|
||||
entry: MockConfigEntry,
|
||||
mock_plex_server,
|
||||
mock_websocket,
|
||||
) -> None:
|
||||
"""Test setup and reauthorization of a Plex token."""
|
||||
|
||||
assert entry.state is ConfigEntryState.LOADED
|
||||
|
||||
with patch(
|
||||
"plexapi.server.PlexServer.clients", side_effect=plexapi.exceptions.Unauthorized
|
||||
), patch("plexapi.server.PlexServer", side_effect=plexapi.exceptions.Unauthorized):
|
||||
trigger_plex_update(mock_websocket)
|
||||
await wait_for_debouncer(hass)
|
||||
|
||||
assert len(hass.config_entries.async_entries(const.DOMAIN)) == 1
|
||||
assert entry.state is not ConfigEntryState.LOADED
|
||||
|
||||
flows = hass.config_entries.flow.async_progress()
|
||||
assert len(flows) == 1
|
||||
assert flows[0]["context"]["source"] == SOURCE_REAUTH
|
||||
|
Loading…
x
Reference in New Issue
Block a user