Plex tests cleanup and additions (#37117)

This commit is contained in:
jjlawren authored and GitHub committed 2020-06-25 23:24:58 -07:00
1 parent 56907392d3
commit 40573bf393
5 files changed
+102 -196

No files matched your search

+27 -88
View File
@@ -14,96 +14,15 @@ from homeassistant.config_entries import (
ENTRY_STATE_SETUP_RETRY,
)
from homeassistant.const import CONF_TOKEN, CONF_URL, CONF_VERIFY_SSL
from homeassistant.helpers.dispatcher import async_dispatcher_send
import homeassistant.util.dt as dt_util
from .const import DEFAULT_DATA, DEFAULT_OPTIONS
from .helpers import trigger_plex_update
from .mock_classes import MockPlexAccount, MockPlexServer
from tests.async_mock import patch
from tests.common import MockConfigEntry, async_fire_time_changed
# class TestClockedPlex(ClockedTestCase):
# """Create clock-controlled tests.async_mock class."""
# @pytest.fixture(autouse=True)
# def inject_fixture(self, caplog, hass_storage):
# """Inject pytest fixtures as instance attributes."""
# self.caplog = caplog
# async def setUp(self):
# """Initialize this test class."""
# self.hass = await async_test_home_assistant(self.loop)
# async def tearDown(self):
# """Clean up the HomeAssistant instance."""
# await self.hass.async_stop()
# async def test_setup_with_config_entry(self):
# """Test setup component with config."""
# hass = self.hass
# 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(
# "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
# server_id = mock_plex_server.machineIdentifier
# loaded_server = hass.data[const.DOMAIN][const.SERVERS][server_id]
# assert loaded_server.plex_server == mock_plex_server
# async_dispatcher_send(
# hass, const.PLEX_UPDATE_PLATFORMS_SIGNAL.format(server_id)
# )
# await hass.async_block_till_done()
# sensor = hass.states.get("sensor.plex_plex_server_1")
# assert sensor.state == str(len(mock_plex_server.accounts))
# # Ensure existing entities refresh
# await self.advance(const.DEBOUNCE_TIMEOUT)
# async_dispatcher_send(
# hass, const.PLEX_UPDATE_PLATFORMS_SIGNAL.format(server_id)
# )
# await hass.async_block_till_done()
# for test_exception in (
# plexapi.exceptions.BadRequest,
# requests.exceptions.RequestException,
# ):
# with patch.object(
# mock_plex_server, "clients", side_effect=test_exception
# ) as patched_clients_bad_request:
# await self.advance(const.DEBOUNCE_TIMEOUT)
# async_dispatcher_send(
# hass, const.PLEX_UPDATE_PLATFORMS_SIGNAL.format(server_id)
# )
# await hass.async_block_till_done()
# assert patched_clients_bad_request.called
# assert (
# f"Could not connect to Plex server: {mock_plex_server.friendlyName}"
# in self.caplog.text
# )
# self.caplog.clear()
async def test_set_config_entry_unique_id(hass):
"""Test updating missing unique_id from config entry."""
@@ -252,9 +171,7 @@ async def test_setup_with_photo_session(hass):
assert len(hass.config_entries.async_entries(const.DOMAIN)) == 1
assert entry.state == ENTRY_STATE_LOADED
server_id = mock_plex_server.machineIdentifier
async_dispatcher_send(hass, const.PLEX_UPDATE_PLATFORMS_SIGNAL.format(server_id))
trigger_plex_update(hass, mock_plex_server)
await hass.async_block_till_done()
media_player = hass.states.get("media_player.plex_product_title")
@@ -343,6 +260,30 @@ async def test_tokenless_server(hass):
unique_id=DEFAULT_DATA["server_id"],
)
with patch("plexapi.server.PlexServer", return_value=mock_plex_server), patch(
"homeassistant.components.plex.PlexWebsocket.listen"
):
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert entry.state == ENTRY_STATE_LOADED
trigger_plex_update(hass, mock_plex_server)
await hass.async_block_till_done()
async def test_bad_token_with_tokenless_server(hass):
"""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(
"plexapi.myplex.MyPlexAccount", side_effect=plexapi.exceptions.Unauthorized
), patch("homeassistant.components.plex.PlexWebsocket.listen"):
@@ -352,7 +293,5 @@ async def test_tokenless_server(hass):
assert entry.state == ENTRY_STATE_LOADED
server_id = mock_plex_server.machineIdentifier
async_dispatcher_send(hass, const.PLEX_UPDATE_PLATFORMS_SIGNAL.format(server_id))
trigger_plex_update(hass, mock_plex_server)
await hass.async_block_till_done()