From 225732d00e2f59ae23fce88975b0fb87d5f57bc4 Mon Sep 17 00:00:00 2001 From: jjlawren Date: Mon, 19 Jul 2021 11:50:04 -0500 Subject: [PATCH] Remove I/O in Plex tests (#53196) --- tests/components/plex/test_config_flow.py | 14 +++++++++++--- tests/components/plex/test_init.py | 13 +++++++------ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/tests/components/plex/test_config_flow.py b/tests/components/plex/test_config_flow.py index 716864c1cb1..72958fc10c0 100644 --- a/tests/components/plex/test_config_flow.py +++ b/tests/components/plex/test_config_flow.py @@ -624,7 +624,9 @@ async def test_manual_config(hass, mock_plex_calls, current_request_with_host): assert result["data"][PLEX_SERVER_CONFIG][CONF_TOKEN] == MOCK_TOKEN -async def test_manual_config_with_token(hass, mock_plex_calls): +async def test_manual_config_with_token( + hass, mock_plex_calls, requests_mock, empty_library, empty_payload +): """Test creating via manual configuration with only token.""" result = await hass.config_entries.flow.async_init( @@ -653,13 +655,19 @@ async def test_manual_config_with_token(hass, mock_plex_calls): server_id = result["data"][CONF_SERVER_IDENTIFIER] mock_plex_server = hass.data[DOMAIN][SERVERS][server_id] + mock_url = mock_plex_server.url_in_use - assert result["title"] == mock_plex_server.url_in_use + assert result["title"] == mock_url assert result["data"][CONF_SERVER] == mock_plex_server.friendly_name assert result["data"][CONF_SERVER_IDENTIFIER] == mock_plex_server.machine_identifier - assert result["data"][PLEX_SERVER_CONFIG][CONF_URL] == mock_plex_server.url_in_use + assert result["data"][PLEX_SERVER_CONFIG][CONF_URL] == mock_url assert result["data"][PLEX_SERVER_CONFIG][CONF_TOKEN] == MOCK_TOKEN + # Complete Plex integration setup before teardown + requests_mock.get(f"{mock_url}/library", text=empty_library) + requests_mock.get(f"{mock_url}/library/sections", text=empty_payload) + await hass.async_block_till_done() + async def test_setup_with_limited_credentials(hass, entry, setup_plex_server): """Test setup with a user with limited permissions.""" diff --git a/tests/components/plex/test_init.py b/tests/components/plex/test_init.py index 530f265f3f0..081adb845f4 100644 --- a/tests/components/plex/test_init.py +++ b/tests/components/plex/test_init.py @@ -177,6 +177,7 @@ async def test_setup_with_unknown_session(hass, entry, setup_plex_server): async def test_setup_when_certificate_changed( hass, requests_mock, + empty_library, empty_payload, plex_server_accounts, plex_server_default, @@ -210,13 +211,10 @@ async def test_setup_when_certificate_changed( requests_mock.get("https://plex.tv/api/users/", text=plextv_shared_users) requests_mock.get("https://plex.tv/api/invites/requested", text=empty_payload) - - 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 - requests_mock.get(f"{old_url}/accounts", status_code=401) + requests_mock.get("https://plex.tv/users/account", 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() @@ -225,7 +223,7 @@ async def test_setup_when_certificate_changed( await hass.config_entries.async_unload(old_entry.entry_id) # Test with no servers found - requests_mock.get(f"{old_url}/accounts", text=plex_server_accounts) + requests_mock.get("https://plex.tv/users/account", text=plextv_account) requests_mock.get("https://plex.tv/api/resources", text=empty_payload) assert await hass.config_entries.async_setup(old_entry.entry_id) is False @@ -237,8 +235,11 @@ async def test_setup_when_certificate_changed( # Test with success 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) + for resource_url in [new_url, "http://1.2.3.4:32400"]: + requests_mock.get(resource_url, text=plex_server_default) requests_mock.get(f"{new_url}/accounts", text=plex_server_accounts) + requests_mock.get(f"{new_url}/library", text=empty_library) + requests_mock.get(f"{new_url}/library/sections", text=empty_payload) assert await hass.config_entries.async_setup(old_entry.entry_id) await hass.async_block_till_done()