"""Tests for the Nextcloud config flow.""" from unittest.mock import Mock, patch 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.""" # start user flow result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER} ) assert result["type"] == FlowResultType.FORM assert result["step_id"] == "user" assert result["errors"] == {} # test NextcloudMonitorAuthorizationError with patch( "homeassistant.components.nextcloud.config_flow.NextcloudMonitor", 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"] == FlowResultType.FORM 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"] == FlowResultType.FORM 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"] == FlowResultType.FORM assert result["step_id"] == "user" 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"], VALID_CONFIG, ) await hass.async_block_till_done() assert result["type"] == 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"] == 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"] == FlowResultType.ABORT assert result["reason"] == "already_configured" 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"] == FlowResultType.FORM 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"] == FlowResultType.FORM 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"] == FlowResultType.FORM 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"] == FlowResultType.FORM 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"] == FlowResultType.ABORT assert result["reason"] == "reauth_successful" assert entry.data == snapshot