Reolink check for admin (#85570)
Co-authored-by: Martin Hjelmare <marhje52@gmail.com> fixes undefined
This commit is contained in:
parent
d35c4f8d82
commit
b2b6ae417d
7 changed files with 171 additions and 27 deletions
|
@ -24,7 +24,7 @@ TEST_NVR_NAME = "test_reolink_name"
|
|||
TEST_USE_HTTPS = True
|
||||
|
||||
|
||||
def get_mock_info(error=None, host_data_return=True):
|
||||
def get_mock_info(error=None, host_data_return=True, user_level="admin"):
|
||||
"""Return a mock gateway info instance."""
|
||||
host_mock = Mock()
|
||||
if error is None:
|
||||
|
@ -40,6 +40,8 @@ def get_mock_info(error=None, host_data_return=True):
|
|||
host_mock.nvr_name = TEST_NVR_NAME
|
||||
host_mock.port = TEST_PORT
|
||||
host_mock.use_https = TEST_USE_HTTPS
|
||||
host_mock.is_admin = user_level == "admin"
|
||||
host_mock.user_level = user_level
|
||||
return host_mock
|
||||
|
||||
|
||||
|
@ -110,7 +112,22 @@ async def test_config_flow_errors(hass):
|
|||
|
||||
assert result["type"] is data_entry_flow.FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {"host": "cannot_connect"}
|
||||
assert result["errors"] == {CONF_HOST: "cannot_connect"}
|
||||
|
||||
host_mock = get_mock_info(user_level="guest")
|
||||
with patch("homeassistant.components.reolink.host.Host", return_value=host_mock):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_USERNAME: TEST_USERNAME,
|
||||
CONF_PASSWORD: TEST_PASSWORD,
|
||||
CONF_HOST: TEST_HOST,
|
||||
},
|
||||
)
|
||||
|
||||
assert result["type"] is data_entry_flow.FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {CONF_USERNAME: "not_admin"}
|
||||
|
||||
host_mock = get_mock_info(error=json.JSONDecodeError("test_error", "test", 1))
|
||||
with patch("homeassistant.components.reolink.host.Host", return_value=host_mock):
|
||||
|
@ -125,7 +142,7 @@ async def test_config_flow_errors(hass):
|
|||
|
||||
assert result["type"] is data_entry_flow.FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {"host": "unknown"}
|
||||
assert result["errors"] == {CONF_HOST: "unknown"}
|
||||
|
||||
host_mock = get_mock_info(error=CredentialsInvalidError("Test error"))
|
||||
with patch("homeassistant.components.reolink.host.Host", return_value=host_mock):
|
||||
|
@ -140,7 +157,7 @@ async def test_config_flow_errors(hass):
|
|||
|
||||
assert result["type"] is data_entry_flow.FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {"host": "invalid_auth"}
|
||||
assert result["errors"] == {CONF_HOST: "invalid_auth"}
|
||||
|
||||
host_mock = get_mock_info(error=ApiError("Test error"))
|
||||
with patch("homeassistant.components.reolink.host.Host", return_value=host_mock):
|
||||
|
@ -155,7 +172,7 @@ async def test_config_flow_errors(hass):
|
|||
|
||||
assert result["type"] is data_entry_flow.FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {"host": "api_error"}
|
||||
assert result["errors"] == {CONF_HOST: "api_error"}
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
|
@ -261,3 +278,64 @@ async def test_change_connection_settings(hass):
|
|||
assert config_entry.data[CONF_HOST] == TEST_HOST2
|
||||
assert config_entry.data[CONF_USERNAME] == TEST_USERNAME2
|
||||
assert config_entry.data[CONF_PASSWORD] == TEST_PASSWORD2
|
||||
|
||||
|
||||
async def test_reauth(hass):
|
||||
"""Test a reauth flow."""
|
||||
config_entry = MockConfigEntry(
|
||||
domain=const.DOMAIN,
|
||||
unique_id=format_mac(TEST_MAC),
|
||||
data={
|
||||
CONF_HOST: TEST_HOST,
|
||||
CONF_USERNAME: TEST_USERNAME,
|
||||
CONF_PASSWORD: TEST_PASSWORD,
|
||||
CONF_PORT: TEST_PORT,
|
||||
const.CONF_USE_HTTPS: TEST_USE_HTTPS,
|
||||
},
|
||||
options={
|
||||
const.CONF_PROTOCOL: const.DEFAULT_PROTOCOL,
|
||||
},
|
||||
title=TEST_NVR_NAME,
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
const.DOMAIN,
|
||||
context={
|
||||
"source": config_entries.SOURCE_REAUTH,
|
||||
"entry_id": config_entry.entry_id,
|
||||
"title_placeholders": {"name": TEST_NVR_NAME},
|
||||
"unique_id": format_mac(TEST_MAC),
|
||||
},
|
||||
data=config_entry.data,
|
||||
)
|
||||
|
||||
assert result["type"] == "form"
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{},
|
||||
)
|
||||
|
||||
assert result["type"] == "form"
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {}
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_HOST: TEST_HOST2,
|
||||
CONF_USERNAME: TEST_USERNAME2,
|
||||
CONF_PASSWORD: TEST_PASSWORD2,
|
||||
},
|
||||
)
|
||||
|
||||
assert result["type"] == "abort"
|
||||
assert result["reason"] == "reauth_successful"
|
||||
assert config_entry.data[CONF_HOST] == TEST_HOST2
|
||||
assert config_entry.data[CONF_USERNAME] == TEST_USERNAME2
|
||||
assert config_entry.data[CONF_PASSWORD] == TEST_PASSWORD2
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue