|
5 | 5 | from aiohttp import ClientError |
6 | 6 | import pytest |
7 | 7 |
|
8 | | -from homeassistant.components.namecheapdns.const import DOMAIN |
9 | | -from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER |
| 8 | +from homeassistant.components.namecheapdns.const import DOMAIN, UPDATE_URL |
| 9 | +from homeassistant.components.namecheapdns.helpers import AuthFailed |
| 10 | +from homeassistant.config_entries import ( |
| 11 | + SOURCE_IMPORT, |
| 12 | + SOURCE_REAUTH, |
| 13 | + SOURCE_USER, |
| 14 | + ConfigEntryState, |
| 15 | +) |
10 | 16 | from homeassistant.const import CONF_PASSWORD |
11 | 17 | from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant |
12 | 18 | from homeassistant.data_entry_flow import FlowResultType |
|
16 | 22 | from .conftest import TEST_USER_INPUT |
17 | 23 |
|
18 | 24 | from tests.common import MockConfigEntry |
| 25 | +from tests.test_util.aiohttp import AiohttpClientMocker |
19 | 26 |
|
20 | 27 |
|
21 | 28 | @pytest.mark.usefixtures("mock_namecheap") |
@@ -173,6 +180,7 @@ async def test_reconfigure( |
173 | 180 | (ValueError, "unknown"), |
174 | 181 | (False, "update_failed"), |
175 | 182 | (ClientError, "cannot_connect"), |
| 183 | + (AuthFailed, "invalid_auth"), |
176 | 184 | ], |
177 | 185 | ) |
178 | 186 | async def test_reconfigure_errors( |
@@ -208,3 +216,120 @@ async def test_reconfigure_errors( |
208 | 216 | assert result["reason"] == "reconfigure_successful" |
209 | 217 |
|
210 | 218 | assert config_entry.data[CONF_PASSWORD] == "new-password" |
| 219 | + |
| 220 | + |
| 221 | +@pytest.mark.usefixtures("mock_namecheap") |
| 222 | +async def test_reauth( |
| 223 | + hass: HomeAssistant, |
| 224 | + config_entry: MockConfigEntry, |
| 225 | + aioclient_mock: AiohttpClientMocker, |
| 226 | +) -> None: |
| 227 | + """Test reauth flow.""" |
| 228 | + aioclient_mock.get( |
| 229 | + UPDATE_URL, |
| 230 | + params=TEST_USER_INPUT, |
| 231 | + text="<interface-response><ErrCount>0</ErrCount></interface-response>", |
| 232 | + ) |
| 233 | + config_entry.add_to_hass(hass) |
| 234 | + assert await hass.config_entries.async_setup(config_entry.entry_id) |
| 235 | + await hass.async_block_till_done() |
| 236 | + |
| 237 | + assert config_entry.state is ConfigEntryState.LOADED |
| 238 | + |
| 239 | + result = await config_entry.start_reauth_flow(hass) |
| 240 | + |
| 241 | + assert result["type"] is FlowResultType.FORM |
| 242 | + assert result["step_id"] == "reauth_confirm" |
| 243 | + |
| 244 | + result = await hass.config_entries.flow.async_configure( |
| 245 | + result["flow_id"], {CONF_PASSWORD: "new-password"} |
| 246 | + ) |
| 247 | + await hass.async_block_till_done() |
| 248 | + |
| 249 | + assert result["type"] is FlowResultType.ABORT |
| 250 | + assert result["reason"] == "reauth_successful" |
| 251 | + assert config_entry.data[CONF_PASSWORD] == "new-password" |
| 252 | + |
| 253 | + |
| 254 | +@pytest.mark.parametrize( |
| 255 | + ("side_effect", "text_error"), |
| 256 | + [ |
| 257 | + (ValueError, "unknown"), |
| 258 | + (False, "update_failed"), |
| 259 | + (ClientError, "cannot_connect"), |
| 260 | + (AuthFailed, "invalid_auth"), |
| 261 | + ], |
| 262 | +) |
| 263 | +async def test_reauth_errors( |
| 264 | + hass: HomeAssistant, |
| 265 | + config_entry: MockConfigEntry, |
| 266 | + mock_namecheap: AsyncMock, |
| 267 | + side_effect: Exception | bool, |
| 268 | + text_error: str, |
| 269 | + aioclient_mock: AiohttpClientMocker, |
| 270 | +) -> None: |
| 271 | + """Test we handle errors.""" |
| 272 | + aioclient_mock.get( |
| 273 | + UPDATE_URL, |
| 274 | + params=TEST_USER_INPUT, |
| 275 | + text="<interface-response><ErrCount>0</ErrCount></interface-response>", |
| 276 | + ) |
| 277 | + config_entry.add_to_hass(hass) |
| 278 | + assert await hass.config_entries.async_setup(config_entry.entry_id) |
| 279 | + await hass.async_block_till_done() |
| 280 | + |
| 281 | + assert config_entry.state is ConfigEntryState.LOADED |
| 282 | + |
| 283 | + result = await config_entry.start_reauth_flow(hass) |
| 284 | + |
| 285 | + assert result["type"] is FlowResultType.FORM |
| 286 | + assert result["step_id"] == "reauth_confirm" |
| 287 | + |
| 288 | + mock_namecheap.side_effect = [side_effect] |
| 289 | + result = await hass.config_entries.flow.async_configure( |
| 290 | + result["flow_id"], {CONF_PASSWORD: "new-password"} |
| 291 | + ) |
| 292 | + |
| 293 | + assert result["type"] is FlowResultType.FORM |
| 294 | + assert result["errors"] == {"base": text_error} |
| 295 | + |
| 296 | + mock_namecheap.side_effect = None |
| 297 | + |
| 298 | + result = await hass.config_entries.flow.async_configure( |
| 299 | + result["flow_id"], {CONF_PASSWORD: "new-password"} |
| 300 | + ) |
| 301 | + |
| 302 | + assert result["type"] is FlowResultType.ABORT |
| 303 | + assert result["reason"] == "reauth_successful" |
| 304 | + |
| 305 | + assert config_entry.data[CONF_PASSWORD] == "new-password" |
| 306 | + |
| 307 | + |
| 308 | +async def test_initiate_reauth_flow( |
| 309 | + hass: HomeAssistant, |
| 310 | + config_entry: MockConfigEntry, |
| 311 | + aioclient_mock: AiohttpClientMocker, |
| 312 | +) -> None: |
| 313 | + """Test authentication error initiates reauth flow.""" |
| 314 | + |
| 315 | + aioclient_mock.get( |
| 316 | + UPDATE_URL, |
| 317 | + params=TEST_USER_INPUT, |
| 318 | + text="<interface-response><ErrCount>1</ErrCount><errors><Err1>Passwords do not match</Err1></errors></interface-response>", |
| 319 | + ) |
| 320 | + config_entry.add_to_hass(hass) |
| 321 | + await hass.config_entries.async_setup(config_entry.entry_id) |
| 322 | + await hass.async_block_till_done() |
| 323 | + |
| 324 | + assert config_entry.state is ConfigEntryState.SETUP_ERROR |
| 325 | + |
| 326 | + flows = hass.config_entries.flow.async_progress() |
| 327 | + assert len(flows) == 1 |
| 328 | + |
| 329 | + flow = flows[0] |
| 330 | + assert flow.get("step_id") == "reauth_confirm" |
| 331 | + assert flow.get("handler") == DOMAIN |
| 332 | + |
| 333 | + assert "context" in flow |
| 334 | + assert flow["context"].get("source") == SOURCE_REAUTH |
| 335 | + assert flow["context"].get("entry_id") == config_entry.entry_id |
0 commit comments