hass-core/tests/components/nextcloud/test_config_flow.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

223 lines
7.2 KiB
Python
Raw Normal View History

"""Tests for the Nextcloud config flow."""
from unittest.mock import Mock, patch
2023-03-29 21:46:08 +02:00
from nextcloudmonitor import (
NextcloudMonitorAuthorizationError,
NextcloudMonitorConnectionError,
NextcloudMonitorRequestError,
)
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.nextcloud import DOMAIN
from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER
from homeassistant.const import CONF_PASSWORD, CONF_URL, CONF_USERNAME, CONF_VERIFY_SSL
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from tests.common import MockConfigEntry
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
VALID_CONFIG = {
CONF_URL: "nc_url",
CONF_USERNAME: "nc_user",
CONF_PASSWORD: "nc_pass",
CONF_VERIFY_SSL: True,
}
async def test_user_create_entry(
hass: HomeAssistant, mock_nextcloud_monitor: Mock, snapshot: SnapshotAssertion
) -> None:
"""Test that the user step works."""
2023-03-29 21:46:08 +02:00
# start user flow
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
assert result["errors"] == {}
2023-03-29 21:46:08 +02:00
# test NextcloudMonitorAuthorizationError
with patch(
"homeassistant.components.nextcloud.config_flow.NextcloudMonitor",
2023-03-29 21:46:08 +02:00
side_effect=NextcloudMonitorAuthorizationError,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
VALID_CONFIG,
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.FORM
2023-03-29 21:46:08 +02:00
assert result["step_id"] == "user"
assert result["errors"] == {"base": "invalid_auth"}
# test NextcloudMonitorConnectionError
with patch(
"homeassistant.components.nextcloud.config_flow.NextcloudMonitor",
side_effect=NextcloudMonitorConnectionError,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
VALID_CONFIG,
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.FORM
2023-03-29 21:46:08 +02:00
assert result["step_id"] == "user"
assert result["errors"] == {"base": "connection_error"}
# test NextcloudMonitorRequestError
with patch(
"homeassistant.components.nextcloud.config_flow.NextcloudMonitor",
side_effect=NextcloudMonitorRequestError,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
VALID_CONFIG,
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
assert result["errors"] == {"base": "connection_error"}
2023-03-29 21:46:08 +02:00
# test success
with patch(
"homeassistant.components.nextcloud.config_flow.NextcloudMonitor",
return_value=mock_nextcloud_monitor,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
VALID_CONFIG,
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "nc_url"
assert result["data"] == snapshot
async def test_user_already_configured(
hass: HomeAssistant, mock_nextcloud_monitor: Mock
) -> None:
"""Test that errors are shown when duplicates are added."""
entry = MockConfigEntry(
domain=DOMAIN,
title="nc_url",
unique_id="nc_url",
data=VALID_CONFIG,
)
entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
assert result["errors"] == {}
with patch(
"homeassistant.components.nextcloud.config_flow.NextcloudMonitor",
return_value=mock_nextcloud_monitor,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
VALID_CONFIG,
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
2023-03-29 21:46:08 +02:00
async def test_reauth(
hass: HomeAssistant, mock_nextcloud_monitor: Mock, snapshot: SnapshotAssertion
) -> None:
"""Test that the re-auth flow works."""
entry = MockConfigEntry(
domain=DOMAIN,
title="nc_url",
unique_id="nc_url",
data=VALID_CONFIG,
)
entry.add_to_hass(hass)
# start reauth flow
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_REAUTH, "entry_id": entry.entry_id},
data=entry.data,
)
assert result["type"] is FlowResultType.FORM
2023-03-29 21:46:08 +02:00
assert result["step_id"] == "reauth_confirm"
# test NextcloudMonitorAuthorizationError
with patch(
"homeassistant.components.nextcloud.config_flow.NextcloudMonitor",
side_effect=NextcloudMonitorAuthorizationError,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_USERNAME: "other_user",
CONF_PASSWORD: "other_password",
},
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.FORM
2023-03-29 21:46:08 +02:00
assert result["step_id"] == "reauth_confirm"
assert result["errors"] == {"base": "invalid_auth"}
# test NextcloudMonitorConnectionError
with patch(
"homeassistant.components.nextcloud.config_flow.NextcloudMonitor",
side_effect=NextcloudMonitorConnectionError,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_USERNAME: "other_user",
CONF_PASSWORD: "other_password",
},
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.FORM
2023-03-29 21:46:08 +02:00
assert result["step_id"] == "reauth_confirm"
assert result["errors"] == {"base": "connection_error"}
# test NextcloudMonitorRequestError
with patch(
"homeassistant.components.nextcloud.config_flow.NextcloudMonitor",
side_effect=NextcloudMonitorRequestError,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_USERNAME: "other_user",
CONF_PASSWORD: "other_password",
},
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.FORM
2023-03-29 21:46:08 +02:00
assert result["step_id"] == "reauth_confirm"
assert result["errors"] == {"base": "connection_error"}
# test success
with patch(
"homeassistant.components.nextcloud.config_flow.NextcloudMonitor",
return_value=mock_nextcloud_monitor,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_USERNAME: "other_user",
CONF_PASSWORD: "other_password",
},
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.ABORT
2023-03-29 21:46:08 +02:00
assert result["reason"] == "reauth_successful"
assert entry.data == snapshot