Add reauth flow to the Traccar Server integration (#148236)

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Josef Zweck <josef@zweck.dev>
This commit is contained in:
Joakim Sørensen
2025-07-06 08:09:59 +02:00
committed by GitHub
co-authored by Copilot Josef Zweck
parent 26de1ea37b
commit 70e9c4e2d0
4 changed files with 179 additions and 4 deletions
@@ -4,7 +4,7 @@ from collections.abc import Generator
from unittest.mock import AsyncMock
import pytest
from pytraccar import TraccarException
from pytraccar import TraccarAuthenticationException, TraccarException
from homeassistant import config_entries
from homeassistant.components.traccar_server.const import (
@@ -175,3 +175,98 @@ async def test_abort_already_configured(
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
async def test_reauth_flow(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_traccar_api_client: Generator[AsyncMock],
) -> None:
"""Test reauth flow."""
mock_config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={
"source": config_entries.SOURCE_REAUTH,
"entry_id": mock_config_entry.entry_id,
},
data=mock_config_entry.data,
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reauth_confirm"
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_USERNAME: "new-username",
CONF_PASSWORD: "new-password",
},
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reauth_successful"
# Verify the config entry was updated
assert mock_config_entry.data[CONF_USERNAME] == "new-username"
assert mock_config_entry.data[CONF_PASSWORD] == "new-password"
@pytest.mark.parametrize(
("side_effect", "error"),
[
(TraccarAuthenticationException, "invalid_auth"),
(TraccarException, "cannot_connect"),
(Exception, "unknown"),
],
)
async def test_reauth_flow_errors(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_traccar_api_client: Generator[AsyncMock],
side_effect: Exception,
error: str,
) -> None:
"""Test reauth flow with errors."""
mock_config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={
"source": config_entries.SOURCE_REAUTH,
"entry_id": mock_config_entry.entry_id,
},
data=mock_config_entry.data,
)
mock_traccar_api_client.get_server.side_effect = side_effect
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_USERNAME: "new-username",
CONF_PASSWORD: "new-password",
},
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": error}
# Test recovery after error
mock_traccar_api_client.get_server.side_effect = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_USERNAME: "new-username",
CONF_PASSWORD: "new-password",
},
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reauth_successful"