async def test_controller_site_already_configured(hass): """Test config flow.""" flow = unifi.UnifiFlowHandler() flow.hass = hass entry = MockConfigEntry( domain=unifi.DOMAIN, data={'controller': { 'host': '1.2.3.4', 'site': 'default', }}) entry.add_to_hass(hass) flow.config = { unifi.CONF_HOST: '1.2.3.4', unifi.CONF_USERNAME: '******', unifi.CONF_PASSWORD: '******', } flow.desc = 'site name' flow.sites = { 'site1': { 'name': 'default', 'role': 'admin', 'desc': 'site name' } } result = await flow.async_step_site() assert result['type'] == 'abort'
async def test_controller_multiple_sites(hass): """Test config flow.""" flow = unifi.UnifiFlowHandler() flow.hass = hass flow.config = { unifi.CONF_HOST: '1.2.3.4', unifi.CONF_USERNAME: '******', unifi.CONF_PASSWORD: '******', } flow.sites = { 'site1': { 'name': 'default', 'role': 'admin', 'desc': 'site name' }, 'site2': { 'name': 'site2', 'role': 'admin', 'desc': 'site2 name' } } result = await flow.async_step_site() assert result['type'] == 'form' assert result['step_id'] == 'site' assert result['data_schema']({'site': 'site name'}) assert result['data_schema']({'site': 'site2 name'})
async def test_flow_works(hass, aioclient_mock): """Test config flow.""" flow = unifi.UnifiFlowHandler() flow.hass = hass with patch('aiounifi.Controller') as mock_controller: def mock_constructor(host, username, password, port, site, websession): """Fake the controller constructor.""" mock_controller.host = host mock_controller.username = username mock_controller.password = password mock_controller.port = port mock_controller.site = site return mock_controller mock_controller.side_effect = mock_constructor mock_controller.login.return_value = mock_coro() mock_controller.sites.return_value = mock_coro({ 'site1': { 'name': 'default', 'role': 'admin', 'desc': 'site name' } }) await flow.async_step_user( user_input={ unifi.CONF_HOST: '1.2.3.4', unifi.CONF_USERNAME: '******', unifi.CONF_PASSWORD: '******', unifi.CONF_PORT: 1234, unifi.CONF_VERIFY_SSL: True }) result = await flow.async_step_site(user_input={}) assert mock_controller.host == '1.2.3.4' assert len(mock_controller.login.mock_calls) == 1 assert len(mock_controller.sites.mock_calls) == 1 assert result['type'] == 'create_entry' assert result['title'] == 'site name' assert result['data'] == { unifi.CONF_CONTROLLER: { unifi.CONF_HOST: '1.2.3.4', unifi.CONF_USERNAME: '******', unifi.CONF_PASSWORD: '******', unifi.CONF_PORT: 1234, unifi.CONF_SITE_ID: 'default', unifi.CONF_VERIFY_SSL: True }, unifi.CONF_POE_CONTROL: True }
async def test_controller_unkown_problem(hass, aioclient_mock): """Test config flow.""" flow = unifi.UnifiFlowHandler() flow.hass = hass with patch.object(unifi, 'get_controller', side_effect=Exception): result = await flow.async_step_user({ unifi.CONF_HOST: '1.2.3.4', unifi.CONF_USERNAME: '******', unifi.CONF_PASSWORD: '******', unifi.CONF_SITE_ID: 'default', }) assert result['type'] == 'abort'
async def test_controller_is_unavailable(hass, aioclient_mock): """Test config flow.""" flow = unifi.UnifiFlowHandler() flow.hass = hass with patch.object(unifi, 'get_controller', side_effect=unifi.errors.CannotConnect): result = await flow.async_step_user({ unifi.CONF_HOST: '1.2.3.4', unifi.CONF_USERNAME: '******', unifi.CONF_PASSWORD: '******', unifi.CONF_SITE_ID: 'default', }) assert result['type'] == 'form' assert result['errors'] == {'base': 'service_unavailable'}
async def test_user_credentials_faulty(hass, aioclient_mock): """Test config flow.""" flow = unifi.UnifiFlowHandler() flow.hass = hass with patch.object(unifi, 'get_controller', side_effect=unifi.errors.AuthenticationRequired): result = await flow.async_step_user({ unifi.CONF_HOST: '1.2.3.4', unifi.CONF_USERNAME: '******', unifi.CONF_PASSWORD: '******', unifi.CONF_SITE_ID: 'default', }) assert result['type'] == 'form' assert result['errors'] == {'base': 'faulty_credentials'}
async def test_user_permissions_low(hass, aioclient_mock): """Test config flow.""" flow = unifi.UnifiFlowHandler() flow.hass = hass with patch('aiounifi.Controller') as mock_controller: def mock_constructor(host, username, password, port, site, websession): """Fake the controller constructor.""" mock_controller.host = host mock_controller.username = username mock_controller.password = password mock_controller.port = port mock_controller.site = site return mock_controller mock_controller.side_effect = mock_constructor mock_controller.login.return_value = mock_coro() mock_controller.sites.return_value = mock_coro({ 'site1': { 'name': 'default', 'role': 'viewer', 'desc': 'site name' } }) await flow.async_step_user( user_input={ unifi.CONF_HOST: '1.2.3.4', unifi.CONF_USERNAME: '******', unifi.CONF_PASSWORD: '******', unifi.CONF_PORT: 1234, unifi.CONF_VERIFY_SSL: True }) result = await flow.async_step_site(user_input={}) assert result['type'] == 'abort'