def test_oauth2_token_exchange__invalid_refresh(self): with self.assertRaises(Exception): data = oauth2_token_exchange( refresh_token=REFRESH_TOKEN_INVALID, client_id=CLIENT_ID_VALID, client_secret=CLIENT_SECRET_VALID, redirect_uri=REDIRECT_URI)
def refresh_oh_token(self, client_id, client_secret): """ Refreshes the applications saved access and refresh tokens for this user. :param client_id: The OpenHumans project client ID. :param client_secret: The OpenHumans project secret. :return: boolean, true if success else fals """ try: new_tokens = oauth2_token_exchange( client_id, client_secret, None, 'https://www.openhumans.org/', refresh_token=self.refresh_token) self.update_local_tokens( new_tokens['access_token'], new_tokens['refresh_token'], self.__calculate_expiry_datetime(new_tokens['expires_in'])) self.save_current_tokens() return True except: print( f"Attempt to refresh OH token for OH member {self.member_code} " f"was un-successful, no data will be fetched") return False
def test_oauth2_token_exchange__valid_refresh(self): data = oauth2_token_exchange( refresh_token=REFRESH_TOKEN_VALID, client_id=CLIENT_ID_VALID, client_secret=CLIENT_SECRET_VALID, redirect_uri=REDIRECT_URI) assert data == { 'access_token': 'newaccesstoken', 'expires_in': 36000, 'refresh_token': 'newrefreshtoken', 'scope': 'american-gut read wildlife open-humans write ' 'pgp go-viral', 'token_type': 'Bearer'}
def test_oauth2_token_exchange__valid_code(self): data = oauth2_token_exchange( code='codegoeshere', **self.TESTING_KWARGS_OAUTH2_TOKEN_EXCHANGE) assert data == { 'access_token': 'returnedaccesstoken', 'expires_in': 36000, 'refresh_token': 'returnedrefreshtoken', 'scope': 'american-gut read wildlife open-humans write ' 'pgp go-viral', 'token_type': 'Bearer' }
def test_oauth2_token_exchange__invalid_refresh(self): data = oauth2_token_exchange(refresh_token=REFRESH_TOKEN_INVALID, client_id=CLIENT_ID_VALID, client_secret=CLIENT_SECRET_VALID, redirect_uri=REDIRECT_URI) assert data == {'error': 'invalid_grant'}
def test_oauth2_token_exchange__invalid_secret(self): data = oauth2_token_exchange(code=CODE_VALID, client_id=CLIENT_ID_VALID, client_secret=CLIENT_SECRET_INVALID, redirect_uri=REDIRECT_URI) assert data == {'error': 'invalid_client'}
def test_oauth2_token_exchange__invalid_secret(self): with self.assertRaises(Exception): data = oauth2_token_exchange(code=CODE_VALID, client_id=CLIENT_ID_VALID, client_secret=CLIENT_SECRET_INVALID, redirect_uri=REDIRECT_URI)
def test_oauth2_token_exchange__invalid_refresh(self): data = oauth2_token_exchange( refresh_token='refreshtokengoeshere', **self.TESTING_KWARGS_OAUTH2_TOKEN_EXCHANGE) assert data == {'error': 'invalid_grant'}
def test_oauth2_token_exchange__invalid_secret(self): data = oauth2_token_exchange( code='codegoeshere', **self.TESTING_KWARGS_OAUTH2_TOKEN_EXCHANGE) assert data == {'error': 'invalid_client'}