Split reauth tests in plex (#89212)

This commit is contained in:
epenet 2023-03-06 10:02:32 +01:00 committed by GitHub
parent c7b30b61de
commit 0c65af93af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 43 deletions

View File

@ -1,5 +1,6 @@
"""Fixtures for Plex tests.""" """Fixtures for Plex tests."""
from unittest.mock import patch from collections.abc import Generator
from unittest.mock import AsyncMock, patch
import pytest import pytest
@ -18,6 +19,15 @@ def plex_server_url(entry):
return entry.data[PLEX_SERVER_CONFIG][CONF_URL].split(":", 1)[-1] 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") @pytest.fixture(name="album", scope="session")
def album_fixture(): def album_fixture():
"""Load album payload and return it.""" """Load album payload and return it."""

View File

@ -2,7 +2,7 @@
import copy import copy
from http import HTTPStatus from http import HTTPStatus
import ssl import ssl
from unittest.mock import patch from unittest.mock import AsyncMock, patch
import plexapi.exceptions import plexapi.exceptions
import pytest import pytest
@ -42,7 +42,6 @@ from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType from homeassistant.data_entry_flow import FlowResultType
from .const import DEFAULT_OPTIONS, MOCK_SERVERS, MOCK_TOKEN, PLEX_DIRECT_URL 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 .mock_classes import MockGDM
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
@ -718,31 +717,22 @@ async def test_integration_discovery(hass: HomeAssistant) -> None:
assert flow["step_id"] == "user" assert flow["step_id"] == "user"
async def test_trigger_reauth( async def test_reauth(
hass: HomeAssistant, hass: HomeAssistant,
entry, entry: MockConfigEntry,
mock_plex_server, mock_plex_calls: None,
mock_websocket,
current_request_with_host: None, current_request_with_host: None,
mock_setup_entry: AsyncMock,
) -> None: ) -> None:
"""Test setup and reauthorization of a Plex token.""" """Test setup and reauthorization of a Plex token."""
entry.add_to_hass(hass)
assert entry.state is ConfigEntryState.LOADED result = await hass.config_entries.flow.async_init(
DOMAIN,
with patch( context={"source": SOURCE_REAUTH},
"plexapi.server.PlexServer.clients", side_effect=plexapi.exceptions.Unauthorized data=entry.data,
), patch("plexapi.server.PlexServer", side_effect=plexapi.exceptions.Unauthorized): )
trigger_plex_update(mock_websocket) flow_id = result["flow_id"]
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"]
with patch("plexauth.PlexAuth.initiate_auth"), patch( with patch("plexauth.PlexAuth.initiate_auth"), patch(
"plexauth.PlexAuth.token", return_value="BRAND_NEW_TOKEN" "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_URL] == PLEX_DIRECT_URL
assert entry.data[PLEX_SERVER_CONFIG][CONF_TOKEN] == "BRAND_NEW_TOKEN" 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, hass: HomeAssistant,
entry, entry: MockConfigEntry,
mock_plex_server, mock_plex_calls: None,
mock_websocket,
current_request_with_host: None, current_request_with_host: None,
requests_mock: requests_mock.Mocker, requests_mock: requests_mock.Mocker,
plextv_resources_two_servers, plextv_resources_two_servers: str,
mock_setup_entry: AsyncMock,
) -> None: ) -> None:
"""Test setup and reauthorization of a Plex token when multiple servers are available.""" """Test setup and reauthorization of a Plex token when multiple servers are available."""
assert entry.state is ConfigEntryState.LOADED
requests_mock.get( requests_mock.get(
"https://plex.tv/api/resources", "https://plex.tv/api/resources",
text=plextv_resources_two_servers, text=plextv_resources_two_servers,
) )
with patch( entry.add_to_hass(hass)
"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 result = await hass.config_entries.flow.async_init(
assert entry.state is not ConfigEntryState.LOADED DOMAIN,
context={"source": SOURCE_REAUTH},
data=entry.data,
)
flows = hass.config_entries.flow.async_progress() flow_id = result["flow_id"]
assert len(flows) == 1
assert flows[0]["context"]["source"] == SOURCE_REAUTH
flow_id = flows[0]["flow_id"]
with patch("plexauth.PlexAuth.initiate_auth"), patch( with patch("plexauth.PlexAuth.initiate_auth"), patch(
"plexauth.PlexAuth.token", return_value="BRAND_NEW_TOKEN" "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_URL] == PLEX_DIRECT_URL
assert entry.data[PLEX_SERVER_CONFIG][CONF_TOKEN] == "BRAND_NEW_TOKEN" 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: async def test_client_request_missing(hass: HomeAssistant) -> None:
"""Test when client headers are not set properly.""" """Test when client headers are not set properly."""

View File

@ -15,7 +15,7 @@ from homeassistant.components.plex.models import (
TRANSIENT_SECTION, TRANSIENT_SECTION,
UNKNOWN_SECTION, UNKNOWN_SECTION,
) )
from homeassistant.config_entries import ConfigEntryState from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState
from homeassistant.const import ( from homeassistant.const import (
CONF_TOKEN, CONF_TOKEN,
CONF_URL, CONF_URL,
@ -336,3 +336,27 @@ async def test_setup_with_limited_credentials(
assert len(hass.config_entries.async_entries(const.DOMAIN)) == 1 assert len(hass.config_entries.async_entries(const.DOMAIN)) == 1
assert entry.state is ConfigEntryState.LOADED 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