Rewrite Plex tests to use mocked payloads (#44044)
This commit is contained in:
parent
caf14b78d1
commit
0426b211f6
48 changed files with 1113 additions and 989 deletions
|
@ -15,11 +15,11 @@ from homeassistant.config_entries import (
|
|||
ENTRY_STATE_SETUP_RETRY,
|
||||
)
|
||||
from homeassistant.const import CONF_TOKEN, CONF_URL, CONF_VERIFY_SSL, STATE_IDLE
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from .const import DEFAULT_DATA, DEFAULT_OPTIONS
|
||||
from .const import DEFAULT_DATA, DEFAULT_OPTIONS, PLEX_DIRECT_URL
|
||||
from .helpers import trigger_plex_update, wait_for_debouncer
|
||||
from .mock_classes import MockGDM, MockPlexAccount, MockPlexServer
|
||||
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed
|
||||
|
||||
|
@ -31,7 +31,7 @@ async def test_set_config_entry_unique_id(hass, entry, mock_plex_server):
|
|||
|
||||
assert (
|
||||
hass.config_entries.async_entries(const.DOMAIN)[0].unique_id
|
||||
== mock_plex_server.machineIdentifier
|
||||
== mock_plex_server.machine_identifier
|
||||
)
|
||||
|
||||
|
||||
|
@ -79,9 +79,9 @@ async def test_unload_config_entry(hass, entry, mock_plex_server):
|
|||
assert entry is config_entries[0]
|
||||
assert entry.state == ENTRY_STATE_LOADED
|
||||
|
||||
server_id = mock_plex_server.machineIdentifier
|
||||
server_id = mock_plex_server.machine_identifier
|
||||
loaded_server = hass.data[const.DOMAIN][const.SERVERS][server_id]
|
||||
assert loaded_server.plex_server == mock_plex_server
|
||||
assert loaded_server == mock_plex_server
|
||||
|
||||
websocket = hass.data[const.DOMAIN][const.WEBSOCKETS][server_id]
|
||||
await hass.config_entries.async_unload(entry.entry_id)
|
||||
|
@ -89,7 +89,7 @@ async def test_unload_config_entry(hass, entry, mock_plex_server):
|
|||
assert entry.state == ENTRY_STATE_NOT_LOADED
|
||||
|
||||
|
||||
async def test_setup_with_photo_session(hass, entry, mock_websocket, setup_plex_server):
|
||||
async def test_setup_with_photo_session(hass, entry, setup_plex_server):
|
||||
"""Test setup component with config."""
|
||||
await setup_plex_server(session_type="photo")
|
||||
|
||||
|
@ -97,7 +97,9 @@ async def test_setup_with_photo_session(hass, entry, mock_websocket, setup_plex_
|
|||
assert entry.state == ENTRY_STATE_LOADED
|
||||
await hass.async_block_till_done()
|
||||
|
||||
media_player = hass.states.get("media_player.plex_product_title")
|
||||
media_player = hass.states.get(
|
||||
"media_player.plex_plex_for_android_tv_shield_android_tv"
|
||||
)
|
||||
assert media_player.state == STATE_IDLE
|
||||
|
||||
await wait_for_debouncer(hass)
|
||||
|
@ -106,14 +108,17 @@ async def test_setup_with_photo_session(hass, entry, mock_websocket, setup_plex_
|
|||
assert sensor.state == "0"
|
||||
|
||||
|
||||
async def test_setup_when_certificate_changed(hass, entry):
|
||||
async def test_setup_when_certificate_changed(
|
||||
hass,
|
||||
requests_mock,
|
||||
empty_payload,
|
||||
plex_server_accounts,
|
||||
plex_server_default,
|
||||
plextv_account,
|
||||
plextv_resources,
|
||||
):
|
||||
"""Test setup component when the Plex certificate has changed."""
|
||||
|
||||
old_domain = "1-2-3-4.1234567890abcdef1234567890abcdef.plex.direct"
|
||||
old_url = f"https://{old_domain}:32400"
|
||||
|
||||
OLD_HOSTNAME_DATA = copy.deepcopy(DEFAULT_DATA)
|
||||
OLD_HOSTNAME_DATA[const.PLEX_SERVER_CONFIG][CONF_URL] = old_url
|
||||
await async_setup_component(hass, "persistent_notification", {})
|
||||
|
||||
class WrongCertHostnameException(requests.exceptions.SSLError):
|
||||
"""Mock the exception showing a mismatched hostname."""
|
||||
|
@ -123,6 +128,12 @@ async def test_setup_when_certificate_changed(hass, entry):
|
|||
f"hostname '{old_domain}' doesn't match"
|
||||
)
|
||||
|
||||
old_domain = "1-2-3-4.1111111111ffffff1111111111ffffff.plex.direct"
|
||||
old_url = f"https://{old_domain}:32400"
|
||||
|
||||
OLD_HOSTNAME_DATA = copy.deepcopy(DEFAULT_DATA)
|
||||
OLD_HOSTNAME_DATA[const.PLEX_SERVER_CONFIG][CONF_URL] = old_url
|
||||
|
||||
old_entry = MockConfigEntry(
|
||||
domain=const.DOMAIN,
|
||||
data=OLD_HOSTNAME_DATA,
|
||||
|
@ -130,46 +141,45 @@ async def test_setup_when_certificate_changed(hass, entry):
|
|||
unique_id=DEFAULT_DATA["server_id"],
|
||||
)
|
||||
|
||||
requests_mock.get("https://plex.tv/users/account", text=plextv_account)
|
||||
requests_mock.get("https://plex.tv/api/resources", text=plextv_resources)
|
||||
requests_mock.get(old_url, exc=WrongCertHostnameException)
|
||||
|
||||
# Test with account failure
|
||||
with patch(
|
||||
"plexapi.server.PlexServer", side_effect=WrongCertHostnameException
|
||||
), patch(
|
||||
"plexapi.myplex.MyPlexAccount", side_effect=plexapi.exceptions.Unauthorized
|
||||
):
|
||||
old_entry.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(old_entry.entry_id) is False
|
||||
await hass.async_block_till_done()
|
||||
requests_mock.get(f"{old_url}/accounts", status_code=401)
|
||||
old_entry.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(old_entry.entry_id) is False
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert old_entry.state == ENTRY_STATE_SETUP_ERROR
|
||||
await hass.config_entries.async_unload(old_entry.entry_id)
|
||||
|
||||
# Test with no servers found
|
||||
with patch(
|
||||
"plexapi.server.PlexServer", side_effect=WrongCertHostnameException
|
||||
), patch("plexapi.myplex.MyPlexAccount", return_value=MockPlexAccount(servers=0)):
|
||||
assert await hass.config_entries.async_setup(old_entry.entry_id) is False
|
||||
await hass.async_block_till_done()
|
||||
requests_mock.get(f"{old_url}/accounts", text=plex_server_accounts)
|
||||
requests_mock.get("https://plex.tv/api/resources", text=empty_payload)
|
||||
|
||||
assert await hass.config_entries.async_setup(old_entry.entry_id) is False
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert old_entry.state == ENTRY_STATE_SETUP_ERROR
|
||||
await hass.config_entries.async_unload(old_entry.entry_id)
|
||||
|
||||
# Test with success
|
||||
with patch(
|
||||
"plexapi.server.PlexServer", side_effect=WrongCertHostnameException
|
||||
), patch("plexapi.myplex.MyPlexAccount", return_value=MockPlexAccount()):
|
||||
assert await hass.config_entries.async_setup(old_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
new_url = PLEX_DIRECT_URL
|
||||
requests_mock.get("https://plex.tv/api/resources", text=plextv_resources)
|
||||
requests_mock.get(new_url, text=plex_server_default)
|
||||
requests_mock.get(f"{new_url}/accounts", text=plex_server_accounts)
|
||||
|
||||
assert await hass.config_entries.async_setup(old_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.config_entries.async_entries(const.DOMAIN)) == 1
|
||||
assert old_entry.state == ENTRY_STATE_LOADED
|
||||
|
||||
assert (
|
||||
old_entry.data[const.PLEX_SERVER_CONFIG][CONF_URL]
|
||||
== entry.data[const.PLEX_SERVER_CONFIG][CONF_URL]
|
||||
)
|
||||
assert old_entry.data[const.PLEX_SERVER_CONFIG][CONF_URL] == new_url
|
||||
|
||||
|
||||
async def test_tokenless_server(hass, entry, mock_websocket, setup_plex_server):
|
||||
async def test_tokenless_server(entry, setup_plex_server):
|
||||
"""Test setup with a server with token auth disabled."""
|
||||
TOKENLESS_DATA = copy.deepcopy(DEFAULT_DATA)
|
||||
TOKENLESS_DATA[const.PLEX_SERVER_CONFIG].pop(CONF_TOKEN, None)
|
||||
|
@ -179,18 +189,13 @@ async def test_tokenless_server(hass, entry, mock_websocket, setup_plex_server):
|
|||
assert entry.state == ENTRY_STATE_LOADED
|
||||
|
||||
|
||||
async def test_bad_token_with_tokenless_server(hass, entry):
|
||||
async def test_bad_token_with_tokenless_server(
|
||||
hass, entry, mock_websocket, setup_plex_server, requests_mock
|
||||
):
|
||||
"""Test setup with a bad token and a server with token auth disabled."""
|
||||
with patch("plexapi.server.PlexServer", return_value=MockPlexServer()), patch(
|
||||
"plexapi.myplex.MyPlexAccount", side_effect=plexapi.exceptions.Unauthorized
|
||||
), patch(
|
||||
"homeassistant.components.plex.GDM", return_value=MockGDM(disabled=True)
|
||||
), patch(
|
||||
"homeassistant.components.plex.PlexWebsocket", autospec=True
|
||||
) as mock_websocket:
|
||||
entry.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
requests_mock.get("https://plex.tv/users/account", status_code=401)
|
||||
|
||||
await setup_plex_server()
|
||||
|
||||
assert entry.state == ENTRY_STATE_LOADED
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue