11"""Test the SFR Box config flow."""
22
3- import json
43from unittest .mock import AsyncMock , patch
54
65import pytest
1413from homeassistant .core import HomeAssistant
1514from homeassistant .data_entry_flow import FlowResultType
1615
17- from tests .common import async_load_fixture
16+ from tests .common import async_load_json_object_fixture
1817
1918pytestmark = pytest .mark .usefixtures ("mock_setup_entry" )
2019
2120
2221async def test_config_flow_skip_auth (
2322 hass : HomeAssistant , mock_setup_entry : AsyncMock
2423) -> None :
25- """Test we get the form."""
24+ """Test user flow (no authentication)."""
25+ result = await hass .config_entries .flow .async_init (
26+ DOMAIN , context = {"source" : config_entries .SOURCE_USER }
27+ )
28+ assert result ["type" ] is FlowResultType .FORM
29+ assert result ["errors" ] == {}
30+
31+ with patch (
32+ "homeassistant.components.sfr_box.config_flow.SFRBox.system_get_info" ,
33+ return_value = SystemInfo (
34+ ** (
35+ await async_load_json_object_fixture (
36+ hass , "system_getInfo.json" , DOMAIN
37+ )
38+ )
39+ ),
40+ ):
41+ result = await hass .config_entries .flow .async_configure (
42+ result ["flow_id" ],
43+ user_input = {
44+ CONF_HOST : "192.168.0.1" ,
45+ },
46+ )
47+
48+ assert result ["type" ] is FlowResultType .MENU
49+ assert result ["step_id" ] == "choose_auth"
50+
51+ result = await hass .config_entries .flow .async_configure (
52+ result ["flow_id" ],
53+ {"next_step_id" : "skip_auth" },
54+ )
55+
56+ assert result ["type" ] is FlowResultType .CREATE_ENTRY
57+ assert result ["title" ] == "SFR Box"
58+ assert result ["data" ] == {CONF_HOST : "192.168.0.1" }
59+
60+ assert len (mock_setup_entry .mock_calls ) == 1
61+
62+
63+ async def test_config_flow_skip_auth_failure (
64+ hass : HomeAssistant , mock_setup_entry : AsyncMock
65+ ) -> None :
66+ """Test user flow (no authentication) with failure and recovery."""
2667 result = await hass .config_entries .flow .async_init (
2768 DOMAIN , context = {"source" : config_entries .SOURCE_USER }
2869 )
@@ -46,7 +87,11 @@ async def test_config_flow_skip_auth(
4687 with patch (
4788 "homeassistant.components.sfr_box.config_flow.SFRBox.system_get_info" ,
4889 return_value = SystemInfo (
49- ** json .loads (await async_load_fixture (hass , "system_getInfo.json" , DOMAIN ))
90+ ** (
91+ await async_load_json_object_fixture (
92+ hass , "system_getInfo.json" , DOMAIN
93+ )
94+ )
5095 ),
5196 ):
5297 result = await hass .config_entries .flow .async_configure (
@@ -74,7 +119,62 @@ async def test_config_flow_skip_auth(
74119async def test_config_flow_with_auth (
75120 hass : HomeAssistant , mock_setup_entry : AsyncMock
76121) -> None :
77- """Test we get the form."""
122+ """Test user flow (with authentication)."""
123+ result = await hass .config_entries .flow .async_init (
124+ DOMAIN , context = {"source" : config_entries .SOURCE_USER }
125+ )
126+ assert result ["type" ] is FlowResultType .FORM
127+ assert result ["errors" ] == {}
128+
129+ with patch (
130+ "homeassistant.components.sfr_box.config_flow.SFRBox.system_get_info" ,
131+ return_value = SystemInfo (
132+ ** (
133+ await async_load_json_object_fixture (
134+ hass , "system_getInfo.json" , DOMAIN
135+ )
136+ )
137+ ),
138+ ):
139+ result = await hass .config_entries .flow .async_configure (
140+ result ["flow_id" ],
141+ user_input = {
142+ CONF_HOST : "192.168.0.1" ,
143+ },
144+ )
145+
146+ assert result ["type" ] is FlowResultType .MENU
147+ assert result ["step_id" ] == "choose_auth"
148+
149+ result = await hass .config_entries .flow .async_configure (
150+ result ["flow_id" ],
151+ {"next_step_id" : "auth" },
152+ )
153+
154+ with patch ("homeassistant.components.sfr_box.config_flow.SFRBox.authenticate" ):
155+ result = await hass .config_entries .flow .async_configure (
156+ result ["flow_id" ],
157+ user_input = {
158+ CONF_USERNAME : "admin" ,
159+ CONF_PASSWORD : "valid" ,
160+ },
161+ )
162+
163+ assert result ["type" ] is FlowResultType .CREATE_ENTRY
164+ assert result ["title" ] == "SFR Box"
165+ assert result ["data" ] == {
166+ CONF_HOST : "192.168.0.1" ,
167+ CONF_USERNAME : "admin" ,
168+ CONF_PASSWORD : "valid" ,
169+ }
170+
171+ assert len (mock_setup_entry .mock_calls ) == 1
172+
173+
174+ async def test_config_flow_with_auth_failure (
175+ hass : HomeAssistant , mock_setup_entry : AsyncMock
176+ ) -> None :
177+ """Test user flow (with authentication) with failure and recovery."""
78178 result = await hass .config_entries .flow .async_init (
79179 DOMAIN , context = {"source" : config_entries .SOURCE_USER }
80180 )
@@ -84,7 +184,11 @@ async def test_config_flow_with_auth(
84184 with patch (
85185 "homeassistant.components.sfr_box.config_flow.SFRBox.system_get_info" ,
86186 return_value = SystemInfo (
87- ** json .loads (await async_load_fixture (hass , "system_getInfo.json" , DOMAIN ))
187+ ** (
188+ await async_load_json_object_fixture (
189+ hass , "system_getInfo.json" , DOMAIN
190+ )
191+ )
88192 ),
89193 ):
90194 result = await hass .config_entries .flow .async_configure (
@@ -151,7 +255,7 @@ async def test_config_flow_duplicate_host(
151255 assert result ["errors" ] == {}
152256
153257 system_info = SystemInfo (
154- ** json . loads (await async_load_fixture (hass , "system_getInfo.json" , DOMAIN ))
258+ ** (await async_load_json_object_fixture (hass , "system_getInfo.json" , DOMAIN ))
155259 )
156260 # Ensure mac doesn't match existing mock entry
157261 system_info .mac_addr = "aa:bb:cc:dd:ee:ff"
@@ -187,7 +291,7 @@ async def test_config_flow_duplicate_mac(
187291 assert result ["errors" ] == {}
188292
189293 system_info = SystemInfo (
190- ** json . loads (await async_load_fixture (hass , "system_getInfo.json" , DOMAIN ))
294+ ** (await async_load_json_object_fixture (hass , "system_getInfo.json" , DOMAIN ))
191295 )
192296 with patch (
193297 "homeassistant.components.sfr_box.config_flow.SFRBox.system_get_info" ,
0 commit comments