diff --git a/homeassistant/components/nyt_games/config_flow.py b/homeassistant/components/nyt_games/config_flow.py index 6676cfad34a464..bfed1f47c41e40 100644 --- a/homeassistant/components/nyt_games/config_flow.py +++ b/homeassistant/components/nyt_games/config_flow.py @@ -22,7 +22,8 @@ async def async_step_user( 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 @@ async def async_step_user( 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}), diff --git a/tests/components/nyt_games/test_config_flow.py b/tests/components/nyt_games/test_config_flow.py index 144b3a3ad1708d..bd17724887e2de 100644 --- a/tests/components/nyt_games/test_config_flow.py +++ b/tests/components/nyt_games/test_config_flow.py @@ -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"), [