Skip to content

Commit

Permalink
Add config flow tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ofalvai committed Dec 4, 2021
1 parent 220d810 commit 253e459
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 23 deletions.
23 changes: 0 additions & 23 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,3 @@ def skip_notifications_fixture():
"homeassistant.components.persistent_notification.async_dismiss"
):
yield


# This fixture, when used, will result in calls to async_get_data to return None. To have the call
# return a value, we would add the `return_value=<VALUE_TO_RETURN>` parameter to the patch call.
@pytest.fixture(name="bypass_get_data")
def bypass_get_data_fixture():
"""Skip calls to get data from API."""
with patch(
"custom_components.integration_blueprint.IntegrationBlueprintApiClient.async_get_data"
):
yield


# In this fixture, we are forcing calls to async_get_data to raise an Exception. This is useful
# for exception handling.
@pytest.fixture(name="error_on_get_data")
def error_get_data_fixture():
"""Simulate error when retrieving data from API."""
with patch(
"custom_components.integration_blueprint.IntegrationBlueprintApiClient.async_get_data",
side_effect=Exception,
):
yield
159 changes: 159 additions & 0 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
from unittest.mock import patch

import pytest
from homeassistant import config_entries, data_entry_flow
from homeassistant.const import CONF_IP_ADDRESS, CONF_PASSWORD

from custom_components.candy import DOMAIN, CONF_KEY_USE_ENCRYPTION
from custom_components.candy.client import Encryption


# 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():
"""Prevent setup."""
with patch(
"custom_components.candy.async_setup_entry",
return_value=True,
):
yield


@pytest.fixture(name="detect_no_encryption", autouse=False)
def detect_no_encryption_fixture():
with patch(
"custom_components.candy.config_flow.detect_encryption",
return_value=(Encryption.NO_ENCRYPTION, None)
):
yield


@pytest.fixture(name="detect_encryption_find_key", autouse=False)
def detect_encryption_find_key_fixture():
with patch(
"custom_components.candy.config_flow.detect_encryption",
return_value=(Encryption.ENCRYPTION, "testkey")
):
yield


@pytest.fixture(name="detect_encryption_key_not_found", autouse=False)
def detect_encryption_key_not_found_fixture():
with patch(
"custom_components.candy.config_flow.detect_encryption",
side_effect=ValueError
):
yield


@pytest.fixture(name="detect_encryption_without_key", autouse=False)
def detect_encryption_without_key_fixture():
with patch(
"custom_components.candy.config_flow.detect_encryption",
return_value=(Encryption.ENCRYPTION_WITHOUT_KEY, None)
):
yield


async def test_no_encryption_detected(hass, detect_no_encryption):
"""Test a successful config flow when detected encryption is no encryption."""

# 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"

result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_IP_ADDRESS: "192.168.0.66"}
)

# 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"] == "Candy"
assert result["data"] == {
CONF_IP_ADDRESS: "192.168.0.66",
CONF_KEY_USE_ENCRYPTION: False,
}
assert result["result"]


async def test_detected_encryption_and_key_found(hass, detect_encryption_find_key):
"""Test a successful config flow when encryption is detected and key is found."""

# 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"

result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_IP_ADDRESS: "192.168.0.66"}
)

# 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"] == "Candy"
assert result["data"] == {
CONF_IP_ADDRESS: "192.168.0.66",
CONF_KEY_USE_ENCRYPTION: True,
CONF_PASSWORD: "testkey"
}
assert result["result"]


async def test_detected_encryption_and_key_not_found(hass, detect_encryption_key_not_found):
"""Test a failing config flow when encryption is detected and key is not found."""
# 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"

result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_IP_ADDRESS: "192.168.0.66"}
)

assert result["type"] == data_entry_flow.RESULT_TYPE_FORM

assert result["errors"] == {"base": "detect_encryption"}


async def test_detected_encryption_without_key(hass, detect_encryption_without_key):
"""Test a successful config flow when encryption is detected without using a key."""
# 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"

result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_IP_ADDRESS: "192.168.0.66"}
)

# 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"] == "Candy"
assert result["data"] == {
CONF_IP_ADDRESS: "192.168.0.66",
CONF_KEY_USE_ENCRYPTION: True,
CONF_PASSWORD: ""
}
assert result["result"]

0 comments on commit 253e459

Please sign in to comment.