Handle Plex certificate updates (#33230)
* Handle Plex certificate updates * Use exception in place * Add test
This commit is contained in:
1 parent
3c2df7f8f2
commit
312af53935
4 files changed
+97
-5
No files matched your search
@@ -1,6 +1,7 @@
|
||||
"""Tests for Plex setup."""
|
||||
import copy
|
||||
from datetime import timedelta
|
||||
import ssl
|
||||
|
||||
from asynctest import patch
|
||||
import plexapi
|
||||
@@ -19,6 +20,7 @@ from homeassistant.const import (
|
||||
CONF_PORT,
|
||||
CONF_SSL,
|
||||
CONF_TOKEN,
|
||||
CONF_URL,
|
||||
CONF_VERIFY_SSL,
|
||||
)
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
@@ -26,7 +28,7 @@ from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from .const import DEFAULT_DATA, DEFAULT_OPTIONS, MOCK_SERVERS, MOCK_TOKEN
|
||||
from .mock_classes import MockPlexServer
|
||||
from .mock_classes import MockPlexAccount, MockPlexServer
|
||||
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed
|
||||
|
||||
@@ -300,3 +302,45 @@ async def test_setup_with_photo_session(hass):
|
||||
|
||||
sensor = hass.states.get("sensor.plex_plex_server_1")
|
||||
assert sensor.state == str(len(mock_plex_server.accounts))
|
||||
|
||||
|
||||
async def test_setup_when_certificate_changed(hass):
|
||||
"""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
|
||||
|
||||
class WrongCertHostnameException(requests.exceptions.SSLError):
|
||||
"""Mock the exception showing a mismatched hostname."""
|
||||
|
||||
def __init__(self):
|
||||
self.__context__ = ssl.SSLCertVerificationError(
|
||||
f"hostname '{old_domain}' doesn't match"
|
||||
)
|
||||
|
||||
old_entry = MockConfigEntry(
|
||||
domain=const.DOMAIN,
|
||||
data=OLD_HOSTNAME_DATA,
|
||||
options=DEFAULT_OPTIONS,
|
||||
unique_id=DEFAULT_DATA["server_id"],
|
||||
)
|
||||
|
||||
new_entry = MockConfigEntry(domain=const.DOMAIN, data=DEFAULT_DATA)
|
||||
|
||||
with patch(
|
||||
"plexapi.server.PlexServer", side_effect=WrongCertHostnameException
|
||||
), patch("plexapi.myplex.MyPlexAccount", return_value=MockPlexAccount()):
|
||||
old_entry.add_to_hass(hass)
|
||||
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]
|
||||
== new_entry.data[const.PLEX_SERVER_CONFIG][CONF_URL]
|
||||
)
|
||||
Reference in new issue
Block a user