Strip the NYT Games token (#127548)

This commit is contained in:
Joost Lekkerkerker 2024-10-04 12:53:35 +02:00 committed by GitHub
parent ae8219dc97
commit ebfa2fb1d0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 26 additions and 2 deletions

View file

@ -22,7 +22,8 @@ class NYTGamesConfigFlow(ConfigFlow, domain=DOMAIN):
errors: dict[str, str] = {}
if user_input:
session = async_create_clientsession(self.hass)
client = NYTGamesClient(user_input[CONF_TOKEN], session=session)
token = user_input[CONF_TOKEN].strip()
client = NYTGamesClient(token, session=session)
try:
user_id = await client.get_user_id()
except NYTGamesAuthenticationError:
@ -35,7 +36,9 @@ class NYTGamesConfigFlow(ConfigFlow, domain=DOMAIN):
else:
await self.async_set_unique_id(str(user_id))
self._abort_if_unique_id_configured()
return self.async_create_entry(title="NYT Games", data=user_input)
return self.async_create_entry(
title="NYT Games", data={CONF_TOKEN: token}
)
return self.async_show_form(
step_id="user",
data_schema=vol.Schema({vol.Required(CONF_TOKEN): str}),

View file

@ -37,6 +37,27 @@ async def test_full_flow(
assert result["result"].unique_id == "218886794"
async def test_stripping_token(
hass: HomeAssistant,
mock_nyt_games_client: AsyncMock,
mock_setup_entry: AsyncMock,
) -> None:
"""Test stripping token."""
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_USER},
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_TOKEN: " token "},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["data"] == {CONF_TOKEN: "token"}
@pytest.mark.parametrize(
("exception", "error"),
[