-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: added tests for config flow and refactored some api and init stuff
- Loading branch information
Jadon
committed
Aug 12, 2023
1 parent
cc07f87
commit 52c145d
Showing
8 changed files
with
218 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,3 +162,5 @@ cython_debug/ | |
.DS_Store | ||
|
||
test | ||
|
||
test_creds.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,5 @@ | |
|
||
CONF_ACCESS_TOKEN = "access_token" | ||
CONF_HOST = "host" | ||
|
||
PLATFORMS = ["binary_sensor", "sensor"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
"""Test Audiobookshelf config flow.""" | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
from _pytest.fixtures import FixtureRequest | ||
from homeassistant import config_entries, data_entry_flow | ||
from homeassistant.core import HomeAssistant | ||
from pytest_homeassistant_custom_component.common import MockConfigEntry | ||
from pytest_homeassistant_custom_component.test_util.aiohttp import AiohttpClientMocker | ||
|
||
from custom_components.audiobookshelf.const import ( | ||
DOMAIN, | ||
PLATFORMS, | ||
) | ||
|
||
from .const import MOCK_CONFIG | ||
|
||
|
||
# This fixture bypasses the actual setup of the integration | ||
# since we only want to test the config flow. We test the | ||
# actual functionality of the integration in other test modules. | ||
@pytest.fixture(autouse=True) | ||
def bypass_setup_fixture() -> None: | ||
"""Prevent setup.""" | ||
with patch("custom_components.audiobookshelf.async_setup", return_value=True), patch( | ||
"custom_components.audiobookshelf.async_setup_entry", | ||
return_value=True, | ||
): | ||
yield | ||
|
||
# Here we simiulate a successful config flow from the backend. | ||
# Note that we use the `bypass_get_data` fixture here because | ||
# we want the config flow validation to succeed during the test. | ||
async def test_successful_config_flow(hass:HomeAssistant, aioclient_mock: AiohttpClientMocker)-> None: | ||
"""Test a successful config flow.""" | ||
|
||
aioclient_mock.get("some_host/ping", json={"success": True}) | ||
aioclient_mock.get("some_host/api/users", json={"users": []}) | ||
aioclient_mock.get("some_host/api/users/online", json={"openSessions": []}) | ||
|
||
|
||
# Initialize a config flow | ||
result = await hass.config_entries.flow.async_init( | ||
DOMAIN, context={"source": config_entries.SOURCE_USER}, | ||
) | ||
|
||
# Check that the config flow shows the user form as the first step | ||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM | ||
assert result["step_id"] == "user" | ||
|
||
# If a user were to enter `some_host` for username and `test_password` | ||
# for password, it would result in this function call | ||
result = await hass.config_entries.flow.async_configure( | ||
result["flow_id"], user_input=MOCK_CONFIG, | ||
) | ||
|
||
# Check that the config flow is complete and a new entry is created with | ||
# the input data | ||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY | ||
assert result["title"] == "some_host" | ||
assert result["data"] == MOCK_CONFIG | ||
assert result["result"] | ||
|
||
aioclient_mock.clear_requests() | ||
|
||
|
||
# In this case, we want to simulate a failure during the config flow. | ||
# We use the `error_on_get_data` mock instead of `bypass_get_data` | ||
# (note the function parameters) to raise an Exception during | ||
# validation of the input config. | ||
async def test_failed_config_flow(hass:HomeAssistant, aioclient_mock: AiohttpClientMocker)-> None: | ||
"""Test a failed config flow due to credential validation failure.""" | ||
aioclient_mock.get("some_host/ping", json={"success": True}) | ||
aioclient_mock.get("some_host/api/users", status=404) | ||
aioclient_mock.get("some_host/api/users/online", status=404) | ||
|
||
|
||
result = await hass.config_entries.flow.async_init( | ||
DOMAIN, context={"source": config_entries.SOURCE_USER}, | ||
) | ||
|
||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM | ||
assert result["step_id"] == "user" | ||
|
||
result = await hass.config_entries.flow.async_configure( | ||
result["flow_id"], user_input=MOCK_CONFIG, | ||
) | ||
|
||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM | ||
assert result["errors"] == {"base": "auth"} | ||
|
||
aioclient_mock.clear_requests() | ||
|
||
|
||
async def test_timeout_error_config_flow(hass: HomeAssistant, timeout_error_on_get_data: FixtureRequest)-> None: | ||
"""Test a failed config flow due to credential validation failure.""" | ||
|
||
result = await hass.config_entries.flow.async_init( | ||
DOMAIN, context={"source": config_entries.SOURCE_USER}, | ||
) | ||
|
||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM | ||
assert result["step_id"] == "user" | ||
|
||
result = await hass.config_entries.flow.async_configure( | ||
result["flow_id"], user_input=MOCK_CONFIG, | ||
) | ||
|
||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM | ||
assert result["errors"] == {"base": "auth"} | ||
|
||
async def test_connectivity_error_config_flow(hass: HomeAssistant, connectivity_error_on_get_data:FixtureRequest)-> None: | ||
"""Test a failed config flow due to credential validation failure.""" | ||
|
||
result = await hass.config_entries.flow.async_init( | ||
DOMAIN, context={"source": config_entries.SOURCE_USER}, | ||
) | ||
|
||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM | ||
assert result["step_id"] == "user" | ||
|
||
result = await hass.config_entries.flow.async_configure( | ||
result["flow_id"], user_input=MOCK_CONFIG, | ||
) | ||
|
||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM | ||
assert result["errors"] == {"base": "auth"} | ||
|
||
async def test_client_error_config_flow(hass:HomeAssistant, client_error_on_get_data:FixtureRequest)-> None: | ||
"""Test a failed config flow due to credential validation failure.""" | ||
|
||
result = await hass.config_entries.flow.async_init( | ||
DOMAIN, context={"source": config_entries.SOURCE_USER}, | ||
) | ||
|
||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM | ||
assert result["step_id"] == "user" | ||
|
||
result = await hass.config_entries.flow.async_configure( | ||
result["flow_id"], user_input=MOCK_CONFIG, | ||
) | ||
|
||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM | ||
assert result["errors"] == {"base": "auth"} | ||
|
||
# Our config flow also has an options flow, so we must test it as well. | ||
async def test_options_flow(hass:HomeAssistant)-> None: | ||
"""Test an options flow.""" | ||
# Create a new MockConfigEntry and add to HASS (we're bypassing config | ||
# flow entirely) | ||
entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG, entry_id="test") | ||
entry.add_to_hass(hass) | ||
|
||
# Initialize an options flow | ||
await hass.config_entries.async_setup(entry.entry_id) | ||
result = await hass.config_entries.options.async_init(entry.entry_id) | ||
|
||
# Verify that the first options step is a user form | ||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM | ||
assert result["step_id"] == "user" | ||
|
||
# Enter some fake data into the form | ||
result = await hass.config_entries.options.async_configure( | ||
result["flow_id"], | ||
user_input={platform: platform != "sensor" for platform in PLATFORMS}, | ||
) | ||
|
||
# Verify that the flow finishes | ||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY | ||
assert result["title"] == "some_host" | ||
|
||
# Verify that the options were updated | ||
assert entry.options == {"binary_sensor": True, "sensor": False} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters