11"""Define tests for the NextDNS config flow."""
22
3- from unittest .mock import AsyncMock , patch
3+ from unittest .mock import AsyncMock
44
55from nextdns import ApiError , InvalidApiKeyError
66import pytest
@@ -21,6 +21,7 @@ async def test_form_create_entry(
2121 hass : HomeAssistant ,
2222 mock_setup_entry : AsyncMock ,
2323 mock_nextdns_client : AsyncMock ,
24+ mock_nextdns : AsyncMock ,
2425) -> None :
2526 """Test that the user step works."""
2627 result = await hass .config_entries .flow .async_init (
@@ -64,6 +65,7 @@ async def test_form_errors(
6465 hass : HomeAssistant ,
6566 mock_setup_entry : AsyncMock ,
6667 mock_nextdns_client : AsyncMock ,
68+ mock_nextdns : AsyncMock ,
6769 exc : Exception ,
6870 base_error : str ,
6971) -> None :
@@ -74,18 +76,18 @@ async def test_form_errors(
7476 assert result ["type" ] is FlowResultType .FORM
7577 assert result ["errors" ] == {}
7678
77- with patch (
78- "homeassistant.components.nextdns.NextDns.create" ,
79- side_effect = exc ,
80- ):
81- result = await hass .config_entries .flow .async_configure (
82- result ["flow_id" ],
83- {CONF_API_KEY : "fake_api_key" },
84- )
79+ mock_nextdns .create .side_effect = exc
80+
81+ result = await hass .config_entries .flow .async_configure (
82+ result ["flow_id" ],
83+ {CONF_API_KEY : "fake_api_key" },
84+ )
8585
8686 assert result ["type" ] is FlowResultType .FORM
8787 assert result ["errors" ] == {"base" : base_error }
8888
89+ mock_nextdns .create .side_effect = None
90+
8991 result = await hass .config_entries .flow .async_configure (
9092 result ["flow_id" ],
9193 {CONF_API_KEY : "fake_api_key" },
@@ -110,6 +112,7 @@ async def test_form_already_configured(
110112 hass : HomeAssistant ,
111113 mock_config_entry : MockConfigEntry ,
112114 mock_nextdns_client : AsyncMock ,
115+ mock_nextdns : AsyncMock ,
113116) -> None :
114117 """Test that errors are shown when duplicates are added."""
115118 await init_integration (hass , mock_config_entry )
@@ -135,6 +138,7 @@ async def test_reauth_successful(
135138 hass : HomeAssistant ,
136139 mock_config_entry : MockConfigEntry ,
137140 mock_nextdns_client : AsyncMock ,
141+ mock_nextdns : AsyncMock ,
138142) -> None :
139143 """Test starting a reauthentication flow."""
140144 await init_integration (hass , mock_config_entry )
@@ -168,6 +172,7 @@ async def test_reauth_errors(
168172 base_error : str ,
169173 mock_config_entry : MockConfigEntry ,
170174 mock_nextdns_client : AsyncMock ,
175+ mock_nextdns : AsyncMock ,
171176) -> None :
172177 """Test reauthentication flow with errors."""
173178 await init_integration (hass , mock_config_entry )
@@ -176,14 +181,17 @@ async def test_reauth_errors(
176181 assert result ["type" ] is FlowResultType .FORM
177182 assert result ["step_id" ] == "reauth_confirm"
178183
179- with patch ("homeassistant.components.nextdns.NextDns.create" , side_effect = exc ):
180- result = await hass .config_entries .flow .async_configure (
181- result ["flow_id" ],
182- user_input = {CONF_API_KEY : "new_api_key" },
183- )
184+ mock_nextdns .create .side_effect = exc
185+
186+ result = await hass .config_entries .flow .async_configure (
187+ result ["flow_id" ],
188+ user_input = {CONF_API_KEY : "new_api_key" },
189+ )
184190
185191 assert result ["errors" ] == {"base" : base_error }
186192
193+ mock_nextdns .create .side_effect = None
194+
187195 result = await hass .config_entries .flow .async_configure (
188196 result ["flow_id" ],
189197 user_input = {CONF_API_KEY : "new_api_key" },
0 commit comments