@@ -115,3 +115,76 @@ async def test_huum_errors(
115115 },
116116 )
117117 assert result ["type" ] is FlowResultType .CREATE_ENTRY
118+
119+
120+ async def test_reauth_flow (
121+ hass : HomeAssistant ,
122+ mock_huum : AsyncMock ,
123+ mock_config_entry : MockConfigEntry ,
124+ ) -> None :
125+ """Test reauthentication flow succeeds with valid credentials."""
126+ mock_config_entry .add_to_hass (hass )
127+
128+ result = await mock_config_entry .start_reauth_flow (hass )
129+
130+ assert result ["type" ] is FlowResultType .FORM
131+ assert result ["step_id" ] == "reauth_confirm"
132+ assert result ["errors" ] == {}
133+
134+ result = await hass .config_entries .flow .async_configure (
135+ result ["flow_id" ],
136+ {CONF_PASSWORD : "new_password" },
137+ )
138+ await hass .async_block_till_done ()
139+
140+ assert result ["type" ] is FlowResultType .ABORT
141+ assert result ["reason" ] == "reauth_successful"
142+ assert mock_config_entry .data [CONF_USERNAME ] == TEST_USERNAME
143+ assert mock_config_entry .data [CONF_PASSWORD ] == "new_password"
144+
145+
146+ @pytest .mark .parametrize (
147+ (
148+ "raises" ,
149+ "error_base" ,
150+ ),
151+ [
152+ (Exception , "unknown" ),
153+ (Forbidden , "invalid_auth" ),
154+ ],
155+ )
156+ async def test_reauth_errors (
157+ hass : HomeAssistant ,
158+ mock_huum : AsyncMock ,
159+ mock_config_entry : MockConfigEntry ,
160+ raises : Exception ,
161+ error_base : str ,
162+ ) -> None :
163+ """Test reauthentication flow handles errors and recovers."""
164+ mock_config_entry .add_to_hass (hass )
165+
166+ result = await mock_config_entry .start_reauth_flow (hass )
167+
168+ with patch (
169+ "homeassistant.components.huum.config_flow.Huum.status" ,
170+ side_effect = raises ,
171+ ):
172+ result = await hass .config_entries .flow .async_configure (
173+ result ["flow_id" ],
174+ {CONF_PASSWORD : "wrong_password" },
175+ )
176+
177+ assert result ["type" ] is FlowResultType .FORM
178+ assert result ["errors" ] == {"base" : error_base }
179+
180+ # Recover with valid credentials
181+ result = await hass .config_entries .flow .async_configure (
182+ result ["flow_id" ],
183+ {CONF_PASSWORD : "new_password" },
184+ )
185+ await hass .async_block_till_done ()
186+
187+ assert result ["type" ] is FlowResultType .ABORT
188+ assert result ["reason" ] == "reauth_successful"
189+ assert mock_config_entry .data [CONF_USERNAME ] == TEST_USERNAME
190+ assert mock_config_entry .data [CONF_PASSWORD ] == "new_password"
0 commit comments