Ensure SimpliSafe re-auth only looks at SimpliSafe config entries (#71009)

* Ensure SimpliSafe re-auth only looks at SimpliSafe config entries

* Add a test

* Trigger Build

* Linting

* Comment

* Simplify test
This commit is contained in:
Aaron Bach 2022-04-28 12:45:37 -06:00 committed by GitHub
parent ce9d000cd8
commit d0f1168ff0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 3 deletions

View File

@ -126,7 +126,8 @@ class SimpliSafeFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
if existing_entries := [
entry
for entry in self.hass.config_entries.async_entries()
if entry.unique_id in (self._username, user_id)
if entry.domain == DOMAIN
and entry.unique_id in (self._username, user_id)
]:
existing_entry = existing_entries[0]
self.hass.config_entries.async_update_entry(

View File

@ -12,6 +12,8 @@ from homeassistant.const import CONF_CODE, CONF_TOKEN, CONF_USERNAME
from .common import REFRESH_TOKEN, USER_ID, USERNAME
from tests.common import MockConfigEntry
CONF_USER_ID = "user_id"
@ -57,9 +59,15 @@ async def test_options_flow(hass, config_entry):
@pytest.mark.parametrize("unique_id", [USERNAME, USER_ID])
async def test_step_reauth(
hass, config, config_entry, reauth_config, setup_simplisafe, sms_config
hass, config, config_entry, reauth_config, setup_simplisafe, sms_config, unique_id
):
"""Test the re-auth step (testing both username and user ID as unique ID)."""
# Add a second config entry (tied to a random domain, but with the same unique ID
# that could exist in a SimpliSafe entry) to ensure that this reauth process only
# touches the SimpliSafe entry:
entry = MockConfigEntry(domain="random", unique_id=USERNAME, data={"some": "data"})
entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_REAUTH}, data=config
)
@ -78,11 +86,17 @@ async def test_step_reauth(
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "reauth_successful"
assert len(hass.config_entries.async_entries()) == 1
assert len(hass.config_entries.async_entries()) == 2
# Test that the SimpliSafe config flow is updated:
[config_entry] = hass.config_entries.async_entries(DOMAIN)
assert config_entry.unique_id == USER_ID
assert config_entry.data == config
# Test that the non-SimpliSafe config flow remains the same:
[config_entry] = hass.config_entries.async_entries("random")
assert config_entry == entry
@pytest.mark.parametrize(
"exc,error_string",