Improve Tractive (#54532)
* Tractive, code improve Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net> * Tractive, code improve Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net> * Tractive, code improve Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net> * Update homeassistant/components/tractive/config_flow.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/tractive/const.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Tractive, comments Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net> * Update homeassistant/components/tractive/config_flow.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/tractive/config_flow.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Tractive Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net> * Reauth Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net> * Reauth Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net> * add tests Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net> * add tests Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
75275254f9
commit
d11c58dac8
6 changed files with 237 additions and 36 deletions
|
@ -7,6 +7,8 @@ from homeassistant import config_entries, setup
|
|||
from homeassistant.components.tractive.const import DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
USER_INPUT = {
|
||||
"email": "test-email@example.com",
|
||||
"password": "test-password",
|
||||
|
@ -76,3 +78,165 @@ async def test_form_unknown_error(hass: HomeAssistant) -> None:
|
|||
|
||||
assert result2["type"] == "form"
|
||||
assert result2["errors"] == {"base": "unknown"}
|
||||
|
||||
|
||||
async def test_flow_entry_already_exists(hass: HomeAssistant) -> None:
|
||||
"""Test user input for config_entry that already exists."""
|
||||
first_entry = MockConfigEntry(
|
||||
domain="tractive",
|
||||
data=USER_INPUT,
|
||||
unique_id="USERID",
|
||||
)
|
||||
first_entry.add_to_hass(hass)
|
||||
|
||||
with patch("aiotractive.api.API.user_id", return_value="USERID"):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}, data=USER_INPUT
|
||||
)
|
||||
|
||||
assert result["type"] == "abort"
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_reauthentication(hass):
|
||||
"""Test Tractive reauthentication."""
|
||||
old_entry = MockConfigEntry(
|
||||
domain="tractive",
|
||||
data=USER_INPUT,
|
||||
unique_id="USERID",
|
||||
)
|
||||
old_entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={
|
||||
"source": config_entries.SOURCE_REAUTH,
|
||||
"unique_id": old_entry.unique_id,
|
||||
"entry_id": old_entry.entry_id,
|
||||
},
|
||||
data=old_entry.data,
|
||||
)
|
||||
|
||||
assert result["type"] == "form"
|
||||
assert result["errors"] == {}
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
|
||||
with patch("aiotractive.api.API.user_id", return_value="USERID"):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
USER_INPUT,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result2["type"] == "abort"
|
||||
assert result2["reason"] == "reauth_successful"
|
||||
|
||||
|
||||
async def test_reauthentication_failure(hass):
|
||||
"""Test Tractive reauthentication failure."""
|
||||
old_entry = MockConfigEntry(
|
||||
domain="tractive",
|
||||
data=USER_INPUT,
|
||||
unique_id="USERID",
|
||||
)
|
||||
old_entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={
|
||||
"source": config_entries.SOURCE_REAUTH,
|
||||
"unique_id": old_entry.unique_id,
|
||||
"entry_id": old_entry.entry_id,
|
||||
},
|
||||
data=old_entry.data,
|
||||
)
|
||||
|
||||
assert result["type"] == "form"
|
||||
assert result["errors"] == {}
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
|
||||
with patch(
|
||||
"aiotractive.api.API.user_id",
|
||||
side_effect=aiotractive.exceptions.UnauthorizedError,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
USER_INPUT,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result2["step_id"] == "reauth_confirm"
|
||||
assert result["type"] == "form"
|
||||
assert result2["errors"]["base"] == "invalid_auth"
|
||||
|
||||
|
||||
async def test_reauthentication_unknown_failure(hass):
|
||||
"""Test Tractive reauthentication failure."""
|
||||
old_entry = MockConfigEntry(
|
||||
domain="tractive",
|
||||
data=USER_INPUT,
|
||||
unique_id="USERID",
|
||||
)
|
||||
old_entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={
|
||||
"source": config_entries.SOURCE_REAUTH,
|
||||
"unique_id": old_entry.unique_id,
|
||||
"entry_id": old_entry.entry_id,
|
||||
},
|
||||
data=old_entry.data,
|
||||
)
|
||||
|
||||
assert result["type"] == "form"
|
||||
assert result["errors"] == {}
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
|
||||
with patch(
|
||||
"aiotractive.api.API.user_id",
|
||||
side_effect=Exception,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
USER_INPUT,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result2["step_id"] == "reauth_confirm"
|
||||
assert result["type"] == "form"
|
||||
assert result2["errors"]["base"] == "unknown"
|
||||
|
||||
|
||||
async def test_reauthentication_failure_no_existing_entry(hass):
|
||||
"""Test Tractive reauthentication with no existing entry."""
|
||||
old_entry = MockConfigEntry(
|
||||
domain="tractive",
|
||||
data=USER_INPUT,
|
||||
unique_id="USERID",
|
||||
)
|
||||
old_entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={
|
||||
"source": config_entries.SOURCE_REAUTH,
|
||||
"unique_id": old_entry.unique_id,
|
||||
"entry_id": old_entry.entry_id,
|
||||
},
|
||||
data=old_entry.data,
|
||||
)
|
||||
|
||||
assert result["type"] == "form"
|
||||
assert result["errors"] == {}
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
|
||||
with patch("aiotractive.api.API.user_id", return_value="USERID_DIFFERENT"):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
USER_INPUT,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result2["type"] == "abort"
|
||||
assert result2["reason"] == "reauth_failed_existing"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue