Fix Plex when using local tokenless authentication (#37096)
This commit is contained in:
parent
f42eb0d5ca
commit
fd1a8dd96c
7 changed files with 157 additions and 84 deletions
|
@ -13,7 +13,7 @@ from homeassistant.config_entries import (
|
|||
ENTRY_STATE_SETUP_ERROR,
|
||||
ENTRY_STATE_SETUP_RETRY,
|
||||
)
|
||||
from homeassistant.const import CONF_URL, CONF_VERIFY_SSL
|
||||
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
|
||||
|
||||
|
@ -115,8 +115,8 @@ async def test_set_config_entry_unique_id(hass):
|
|||
)
|
||||
|
||||
with patch("plexapi.server.PlexServer", return_value=mock_plex_server), patch(
|
||||
"homeassistant.components.plex.PlexWebsocket.listen"
|
||||
) as mock_listen:
|
||||
"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()
|
||||
|
@ -181,8 +181,8 @@ async def test_setup_with_insecure_config_entry(hass):
|
|||
)
|
||||
|
||||
with patch("plexapi.server.PlexServer", return_value=mock_plex_server), patch(
|
||||
"homeassistant.components.plex.PlexWebsocket.listen"
|
||||
) as mock_listen:
|
||||
"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()
|
||||
|
@ -210,8 +210,8 @@ async def test_unload_config_entry(hass):
|
|||
assert entry is config_entries[0]
|
||||
|
||||
with patch("plexapi.server.PlexServer", return_value=mock_plex_server), patch(
|
||||
"homeassistant.components.plex.PlexWebsocket.listen"
|
||||
) as mock_listen:
|
||||
"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
|
||||
|
@ -243,8 +243,8 @@ async def test_setup_with_photo_session(hass):
|
|||
)
|
||||
|
||||
with patch("plexapi.server.PlexServer", return_value=mock_plex_server), patch(
|
||||
"homeassistant.components.plex.PlexWebsocket.listen"
|
||||
):
|
||||
"plexapi.myplex.MyPlexAccount", return_value=MockPlexAccount()
|
||||
), 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()
|
||||
|
@ -254,11 +254,8 @@ async def test_setup_with_photo_session(hass):
|
|||
|
||||
server_id = mock_plex_server.machineIdentifier
|
||||
|
||||
with patch("plexapi.myplex.MyPlexAccount", return_value=MockPlexAccount()):
|
||||
async_dispatcher_send(
|
||||
hass, const.PLEX_UPDATE_PLATFORMS_SIGNAL.format(server_id)
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
async_dispatcher_send(hass, const.PLEX_UPDATE_PLATFORMS_SIGNAL.format(server_id))
|
||||
await hass.async_block_till_done()
|
||||
|
||||
media_player = hass.states.get("media_player.plex_product_title")
|
||||
assert media_player.state == "idle"
|
||||
|
@ -293,10 +290,33 @@ async def test_setup_when_certificate_changed(hass):
|
|||
|
||||
new_entry = MockConfigEntry(domain=const.DOMAIN, data=DEFAULT_DATA)
|
||||
|
||||
# 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()
|
||||
|
||||
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()
|
||||
|
||||
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()):
|
||||
old_entry.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(old_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
@ -307,3 +327,32 @@ async def test_setup_when_certificate_changed(hass):
|
|||
old_entry.data[const.PLEX_SERVER_CONFIG][CONF_URL]
|
||||
== new_entry.data[const.PLEX_SERVER_CONFIG][CONF_URL]
|
||||
)
|
||||
|
||||
|
||||
async def test_tokenless_server(hass):
|
||||
"""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 = 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(
|
||||
"plexapi.myplex.MyPlexAccount", side_effect=plexapi.exceptions.Unauthorized
|
||||
), 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
|
||||
|
||||
server_id = mock_plex_server.machineIdentifier
|
||||
|
||||
async_dispatcher_send(hass, const.PLEX_UPDATE_PLATFORMS_SIGNAL.format(server_id))
|
||||
await hass.async_block_till_done()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue