Beispiel #1
0
 def _handle_auth_result(self, status_code, json_resp):
     if status_code == 200:
         self._auth_token = json_resp["access_token"]
         self.refresh_token = json_resp[CommunityAuthentication.REFRESH_TOKEN]
         self._expire_at = time.time() + json_resp["expires_in"] - CommunityAuthentication.ALLOWED_TIME_DELAY
         self._refresh_session()
         self._save_login_token(self.refresh_token)
     elif status_code == 400:
         raise authentication.FailedAuthentication("Invalid username or password.")
     else:
         raise authentication.AuthenticationError(f"Error code: {status_code}")
Beispiel #2
0
 def login(self, username, password):
     self._ensure_community_url()
     self._reset_tokens()
     params = {
         "username": username,
         "password": password,
         "grant_type": "password",
     }
     resp = requests.post(self.authentication_url, params=params)
     try:
         self._handle_auth_result(resp.status_code, resp.json())
     except json.JSONDecodeError as e:
         raise authentication.FailedAuthentication(e)
     if self.is_logged_in():
         self.update_supports()
Beispiel #3
0
def test_auto_login(auth):
    with mock.patch.object(auth, "_refresh_auth",
                           mock.Mock()) as _refresh_auth_mock:
        auth._auto_login("1")
        assert auth.refresh_token == "1"
        _refresh_auth_mock.assert_called_once()
    auth.refresh_token = None
    with mock.patch.object(auth, "_refresh_auth", mock.Mock(side_effect=authentication.FailedAuthentication())) as \
            _refresh_auth_mock, \
         mock.patch.object(auth, "logout", mock.Mock()) as logout_mock:
        auth._auto_login("1")
        assert auth.refresh_token == "1"
        _refresh_auth_mock.assert_called_once()
        logout_mock.assert_called_once()
    auth.refresh_token = None
    with mock.patch.object(auth, "_refresh_auth", mock.Mock(side_effect=Exception())) as \
            _refresh_auth_mock, \
         mock.patch.object(auth, "logout", mock.Mock()) as logout_mock, \
         mock.patch.object(auth, "logger", mock.Mock()) as logger_mock:
        auth._auto_login("1")
        assert auth.refresh_token == "1"
        _refresh_auth_mock.assert_called_once()
        logger_mock.exception.assert_called_once()
        logout_mock.assert_not_called()