def login(self, username=None, password=None): """Explicit Abode login.""" if username is not None: self._username = username if password is not None: self._password = password if self._username is None or not isinstance(self._username, str): raise AbodeAuthenticationException(ERROR.USERNAME) if self._password is None or not isinstance(self._password, str): raise AbodeAuthenticationException(ERROR.PASSWORD) self._token = None login_data = {'id': self._username, 'password': self._password} response = self._session.post(CONST.LOGIN_URL, data=login_data) response_object = json.loads(response.text) if response.status_code != 200: raise AbodeAuthenticationException( (response.status_code, response_object['message'])) _LOGGER.debug("Login Response: %s", response.text) self._token = response_object['token'] self._panel = response_object['panel'] self._user = response_object['user'] _LOGGER.info("Login successful") return True
def logout(self): """Explicit Abode logout.""" if self._token: header_data = {'ABODE-API-KEY': self._token} self._session = requests.session() self._token = None self._panel = None self._user = None self._devices = None self._automations = None try: response = self._session.post(CONST.LOGOUT_URL, headers=header_data) response_object = json.loads(response.text) except OSError as exc: _LOGGER.warning("Caught exception during logout: %s", str(exc)) return False if response.status_code != 200: raise AbodeAuthenticationException( (response.status_code, response_object['message'])) _LOGGER.debug("Logout Response: %s", response.text) _LOGGER.info("Logout successful") return True
def login(self, username=None, password=None): """Explicit Abode login.""" if username is not None: self._cache[CONST.ID] = username if password is not None: self._cache[CONST.PASSWORD] = password if (self._cache[CONST.ID] is None or not isinstance(self._cache[CONST.ID], str)): raise AbodeAuthenticationException(ERROR.USERNAME) if (self._cache[CONST.PASSWORD] is None or not isinstance(self._cache[CONST.PASSWORD], str)): raise AbodeAuthenticationException(ERROR.PASSWORD) self._save_cache() self._token = None login_data = { CONST.ID: self._cache[CONST.ID], CONST.PASSWORD: self._cache[CONST.PASSWORD], CONST.UUID: self._cache[CONST.UUID] } response = self._session.post(CONST.LOGIN_URL, json=login_data) response_object = json.loads(response.text) oauth_token = self._session.get(CONST.OAUTH_TOKEN_URL) oauth_token_object = json.loads(oauth_token.text) if response.status_code != 200: raise AbodeAuthenticationException( (response.status_code, response_object['message'])) _LOGGER.debug("Login Response: %s", response.text) self._token = response_object['token'] self._panel = response_object['panel'] self._user = response_object['user'] self._oauth_token = oauth_token_object['access_token'] _LOGGER.info("Login successful") return True
async def test_invalid_credentials(hass): """Test Abode credentials changing.""" with patch( "homeassistant.components.abode.Abode", side_effect=AbodeAuthenticationException( (HTTP_BAD_REQUEST, "auth error")), ), patch( "homeassistant.components.abode.config_flow.AbodeFlowHandler.async_step_reauth" ) as mock_async_step_reauth: await setup_platform(hass, ALARM_DOMAIN) mock_async_step_reauth.assert_called_once()
async def test_invalid_credentials(hass): """Test that invalid credentials throws an error.""" conf = {CONF_USERNAME: "******", CONF_PASSWORD: "******"} flow = config_flow.AbodeFlowHandler() flow.hass = hass with patch( "homeassistant.components.abode.config_flow.Abode", side_effect=AbodeAuthenticationException((HTTP_BAD_REQUEST, "auth error")), ): result = await flow.async_step_user(user_input=conf) assert result["errors"] == {"base": "invalid_auth"}
async def test_step_mfa(hass): """Test that the MFA step works.""" conf = {CONF_USERNAME: "******", CONF_PASSWORD: "******"} with patch( "homeassistant.components.abode.config_flow.Abode", side_effect=AbodeAuthenticationException(MFA_CODE_REQUIRED), ): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, data=conf ) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "mfa" with patch( "homeassistant.components.abode.config_flow.Abode", side_effect=AbodeAuthenticationException((HTTP_BAD_REQUEST, "invalid mfa")), ): result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={"mfa_code": "123456"} ) assert result["errors"] == {"base": "invalid_mfa_code"} with patch("homeassistant.components.abode.config_flow.Abode"), patch( "abodepy.UTILS" ): result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={"mfa_code": "123456"} ) assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["title"] == "*****@*****.**" assert result["data"] == { CONF_USERNAME: "******", CONF_PASSWORD: "******", CONF_POLLING: False, }
async def test_invalid_credentials(hass: HomeAssistant) -> None: """Test Abode credentials changing.""" with patch( "homeassistant.components.abode.Abode", side_effect=AbodeAuthenticationException( (HTTPStatus.BAD_REQUEST, "auth error")), ), patch( "homeassistant.components.abode.config_flow.AbodeFlowHandler.async_step_reauth", return_value={"type": data_entry_flow.RESULT_TYPE_FORM}, ) as mock_async_step_reauth: await setup_platform(hass, ALARM_DOMAIN) mock_async_step_reauth.assert_called_once()
async def test_connection_error(hass): """Test other than invalid credentials throws an error.""" conf = {CONF_USERNAME: "******", CONF_PASSWORD: "******"} flow = config_flow.AbodeFlowHandler() flow.hass = hass with patch( "homeassistant.components.abode.config_flow.Abode", side_effect=AbodeAuthenticationException( (HTTP_INTERNAL_SERVER_ERROR, "connection error")), ): result = await flow.async_step_user(user_input=conf) assert result["errors"] == {"base": "cannot_connect"}
def login(self, username=None, password=None, mfa_code=None): """Explicit Abode login.""" if username is not None: self._cache[CONST.ID] = username if password is not None: self._cache[CONST.PASSWORD] = password if (self._cache[CONST.ID] is None or not isinstance(self._cache[CONST.ID], str)): raise AbodeAuthenticationException(ERROR.USERNAME) if (self._cache[CONST.PASSWORD] is None or not isinstance(self._cache[CONST.PASSWORD], str)): raise AbodeAuthenticationException(ERROR.PASSWORD) self._save_cache() self._token = None login_data = { CONST.ID: self._cache[CONST.ID], CONST.PASSWORD: self._cache[CONST.PASSWORD], CONST.UUID: self._cache[CONST.UUID] } if mfa_code is not None: login_data[CONST.MFA_CODE] = mfa_code login_data['remember_me'] = 1 response = self._session.post(CONST.LOGIN_URL, json=login_data) if response.status_code != 200: raise AbodeAuthenticationException( (response.status_code, response.text)) response_object = json.loads(response.text) # Check for multi-factor authentication if 'mfa_type' in response_object: if response_object['mfa_type'] == "google_authenticator": raise AbodeAuthenticationException(ERROR.MFA_CODE_REQUIRED) raise AbodeAuthenticationException(ERROR.UNKNOWN_MFA_TYPE) # Persist cookies (which contains the UUID and the session ID) to disk if self._session.cookies.get_dict(): self._cache[CONST.COOKIES] = self._session.cookies self._save_cache() oauth_response = self._session.get(CONST.OAUTH_TOKEN_URL) if oauth_response.status_code != 200: raise AbodeAuthenticationException( (oauth_response.status_code, oauth_response.text)) oauth_response_object = json.loads(oauth_response.text) _LOGGER.debug("Login Response: %s", response.text) self._token = response_object['token'] self._panel = response_object['panel'] self._user = response_object['user'] self._oauth_token = oauth_response_object['access_token'] _LOGGER.info("Login successful") return True