mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Refactor Plex tests using fixtures (#40260)
* Refactor Plex tests using fixtures * Avoid unnecessary coroutine declaration
This commit is contained in:
@@ -24,27 +24,8 @@ from tests.async_mock import patch
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed
|
||||
|
||||
|
||||
async def test_set_config_entry_unique_id(hass):
|
||||
async def test_set_config_entry_unique_id(hass, entry, mock_plex_server):
|
||||
"""Test updating missing unique_id from config entry."""
|
||||
|
||||
mock_plex_server = MockPlexServer()
|
||||
|
||||
entry = MockConfigEntry(
|
||||
domain=const.DOMAIN,
|
||||
data=DEFAULT_DATA,
|
||||
options=DEFAULT_OPTIONS,
|
||||
unique_id=None,
|
||||
)
|
||||
|
||||
with patch("plexapi.server.PlexServer", return_value=mock_plex_server), patch(
|
||||
"plexapi.myplex.MyPlexAccount", return_value=MockPlexAccount()
|
||||
), patch("homeassistant.components.plex.PlexWebsocket.listen") as mock_listen:
|
||||
entry.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_listen.called
|
||||
|
||||
assert len(hass.config_entries.async_entries(const.DOMAIN)) == 1
|
||||
assert entry.state == ENTRY_STATE_LOADED
|
||||
|
||||
@@ -54,16 +35,8 @@ async def test_set_config_entry_unique_id(hass):
|
||||
)
|
||||
|
||||
|
||||
async def test_setup_config_entry_with_error(hass):
|
||||
async def test_setup_config_entry_with_error(hass, entry):
|
||||
"""Test setup component from config entry with errors."""
|
||||
|
||||
entry = MockConfigEntry(
|
||||
domain=const.DOMAIN,
|
||||
data=DEFAULT_DATA,
|
||||
options=DEFAULT_OPTIONS,
|
||||
unique_id=DEFAULT_DATA["server_id"],
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.plex.PlexServer.connect",
|
||||
side_effect=requests.exceptions.ConnectionError,
|
||||
@@ -87,91 +60,38 @@ async def test_setup_config_entry_with_error(hass):
|
||||
assert entry.state == ENTRY_STATE_SETUP_ERROR
|
||||
|
||||
|
||||
async def test_setup_with_insecure_config_entry(hass):
|
||||
async def test_setup_with_insecure_config_entry(hass, entry, setup_plex_server):
|
||||
"""Test setup component with config."""
|
||||
|
||||
mock_plex_server = MockPlexServer()
|
||||
|
||||
INSECURE_DATA = copy.deepcopy(DEFAULT_DATA)
|
||||
INSECURE_DATA[const.PLEX_SERVER_CONFIG][CONF_VERIFY_SSL] = False
|
||||
entry.data = INSECURE_DATA
|
||||
|
||||
entry = MockConfigEntry(
|
||||
domain=const.DOMAIN,
|
||||
data=INSECURE_DATA,
|
||||
options=DEFAULT_OPTIONS,
|
||||
unique_id=DEFAULT_DATA["server_id"],
|
||||
)
|
||||
|
||||
with patch("plexapi.server.PlexServer", return_value=mock_plex_server), patch(
|
||||
"plexapi.myplex.MyPlexAccount", return_value=MockPlexAccount()
|
||||
), patch("homeassistant.components.plex.PlexWebsocket.listen") as mock_listen:
|
||||
entry.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_listen.called
|
||||
await setup_plex_server(config_entry=entry)
|
||||
|
||||
assert len(hass.config_entries.async_entries(const.DOMAIN)) == 1
|
||||
assert entry.state == ENTRY_STATE_LOADED
|
||||
|
||||
|
||||
async def test_unload_config_entry(hass):
|
||||
async def test_unload_config_entry(hass, entry, mock_plex_server):
|
||||
"""Test unloading a config entry."""
|
||||
mock_plex_server = MockPlexServer()
|
||||
|
||||
entry = MockConfigEntry(
|
||||
domain=const.DOMAIN,
|
||||
data=DEFAULT_DATA,
|
||||
options=DEFAULT_OPTIONS,
|
||||
unique_id=DEFAULT_DATA["server_id"],
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
config_entries = hass.config_entries.async_entries(const.DOMAIN)
|
||||
assert len(config_entries) == 1
|
||||
assert entry is config_entries[0]
|
||||
|
||||
with patch("plexapi.server.PlexServer", return_value=mock_plex_server), patch(
|
||||
"plexapi.myplex.MyPlexAccount", return_value=MockPlexAccount()
|
||||
), patch("homeassistant.components.plex.PlexWebsocket.listen") as mock_listen:
|
||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
assert mock_listen.called
|
||||
|
||||
assert entry.state == ENTRY_STATE_LOADED
|
||||
|
||||
server_id = mock_plex_server.machineIdentifier
|
||||
loaded_server = hass.data[const.DOMAIN][const.SERVERS][server_id]
|
||||
|
||||
assert loaded_server.plex_server == mock_plex_server
|
||||
|
||||
with patch("homeassistant.components.plex.PlexWebsocket.close") as mock_close:
|
||||
await hass.config_entries.async_unload(entry.entry_id)
|
||||
assert mock_close.called
|
||||
|
||||
websocket = hass.data[const.DOMAIN][const.WEBSOCKETS][server_id]
|
||||
await hass.config_entries.async_unload(entry.entry_id)
|
||||
assert websocket.close.called
|
||||
assert entry.state == ENTRY_STATE_NOT_LOADED
|
||||
|
||||
|
||||
async def test_setup_with_photo_session(hass):
|
||||
async def test_setup_with_photo_session(hass, entry, mock_websocket, setup_plex_server):
|
||||
"""Test setup component with config."""
|
||||
|
||||
mock_plex_server = MockPlexServer(session_type="photo")
|
||||
|
||||
entry = MockConfigEntry(
|
||||
domain=const.DOMAIN,
|
||||
data=DEFAULT_DATA,
|
||||
options=DEFAULT_OPTIONS,
|
||||
unique_id=DEFAULT_DATA["server_id"],
|
||||
)
|
||||
|
||||
with patch("plexapi.server.PlexServer", return_value=mock_plex_server), patch(
|
||||
"plexapi.myplex.MyPlexAccount", return_value=MockPlexAccount()
|
||||
), 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()
|
||||
mock_plex_server = await setup_plex_server(config_entry=entry, session_type="photo")
|
||||
|
||||
assert len(hass.config_entries.async_entries(const.DOMAIN)) == 1
|
||||
assert entry.state == ENTRY_STATE_LOADED
|
||||
@@ -186,7 +106,7 @@ async def test_setup_with_photo_session(hass):
|
||||
assert sensor.state == str(len(mock_plex_server.accounts))
|
||||
|
||||
|
||||
async def test_setup_when_certificate_changed(hass):
|
||||
async def test_setup_when_certificate_changed(hass, entry):
|
||||
"""Test setup component when the Plex certificate has changed."""
|
||||
|
||||
old_domain = "1-2-3-4.1234567890abcdef1234567890abcdef.plex.direct"
|
||||
@@ -210,8 +130,6 @@ async def test_setup_when_certificate_changed(hass):
|
||||
unique_id=DEFAULT_DATA["server_id"],
|
||||
)
|
||||
|
||||
new_entry = MockConfigEntry(domain=const.DOMAIN, data=DEFAULT_DATA)
|
||||
|
||||
# Test with account failure
|
||||
with patch(
|
||||
"plexapi.server.PlexServer", side_effect=WrongCertHostnameException
|
||||
@@ -247,49 +165,23 @@ async def test_setup_when_certificate_changed(hass):
|
||||
|
||||
assert (
|
||||
old_entry.data[const.PLEX_SERVER_CONFIG][CONF_URL]
|
||||
== new_entry.data[const.PLEX_SERVER_CONFIG][CONF_URL]
|
||||
== entry.data[const.PLEX_SERVER_CONFIG][CONF_URL]
|
||||
)
|
||||
|
||||
|
||||
async def test_tokenless_server(hass):
|
||||
async def test_tokenless_server(hass, entry, mock_websocket, setup_plex_server):
|
||||
"""Test setup with a server with token auth disabled."""
|
||||
mock_plex_server = MockPlexServer()
|
||||
|
||||
TOKENLESS_DATA = copy.deepcopy(DEFAULT_DATA)
|
||||
TOKENLESS_DATA[const.PLEX_SERVER_CONFIG].pop(CONF_TOKEN, None)
|
||||
entry.data = TOKENLESS_DATA
|
||||
|
||||
entry = MockConfigEntry(
|
||||
domain=const.DOMAIN,
|
||||
data=TOKENLESS_DATA,
|
||||
options=DEFAULT_OPTIONS,
|
||||
unique_id=DEFAULT_DATA["server_id"],
|
||||
)
|
||||
|
||||
with patch("plexapi.server.PlexServer", return_value=mock_plex_server), 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()
|
||||
|
||||
await setup_plex_server(config_entry=entry)
|
||||
assert entry.state == ENTRY_STATE_LOADED
|
||||
|
||||
trigger_plex_update(mock_websocket)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def test_bad_token_with_tokenless_server(hass):
|
||||
async def test_bad_token_with_tokenless_server(hass, entry):
|
||||
"""Test setup with a bad token and a server with token auth disabled."""
|
||||
mock_plex_server = MockPlexServer()
|
||||
|
||||
entry = MockConfigEntry(
|
||||
domain=const.DOMAIN,
|
||||
data=DEFAULT_DATA,
|
||||
options=DEFAULT_OPTIONS,
|
||||
unique_id=DEFAULT_DATA["server_id"],
|
||||
)
|
||||
|
||||
with patch("plexapi.server.PlexServer", return_value=mock_plex_server), patch(
|
||||
with patch("plexapi.server.PlexServer", return_value=MockPlexServer()), patch(
|
||||
"plexapi.myplex.MyPlexAccount", side_effect=plexapi.exceptions.Unauthorized
|
||||
), patch(
|
||||
"homeassistant.components.plex.PlexWebsocket", autospec=True
|
||||
@@ -300,5 +192,6 @@ async def test_bad_token_with_tokenless_server(hass):
|
||||
|
||||
assert entry.state == ENTRY_STATE_LOADED
|
||||
|
||||
# Ensure updates that rely on account return nothing
|
||||
trigger_plex_update(mock_websocket)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
Reference in New Issue
Block a user