async def test_renewal_with_error(self, prepared_api: SpotifyApiClient): invalid_token = prepared_api.spotify_authorization_token invalid_token.access_token = 'invalid' prepared_api.spotify_authorization_token = invalid_token prepared_api.token_renew_instance = FalseRenew() with pytest.raises(TokenExpired): await prepared_api.albums.get_one('03dlqdFWY9gwJxGl3AREVy')
def test_get_hold_authentication(self, api: SpotifyApiClient): api.hold_authentication = False assert False is api.hold_authentication with pytest.raises(SpotifyError): api.spotify_authorization_token = SpotifyAuthorisationToken() with pytest.raises(SpotifyError): token = api.spotify_authorization_token
async def test_renewal(self, prepared_api: SpotifyApiClient): invalid_token = prepared_api.spotify_authorization_token invalid_token.access_token = 'invalid' prepared_api.spotify_authorization_token = invalid_token prepared_api.token_renew_instance = TokenRenewClass() album = await prepared_api.albums.get_one('03dlqdFWY9gwJxGl3AREVy') assert isinstance(album, dict)
async def test_inability_to_get_token_with_client_credentials(self, api: SpotifyApiClient): auth_code_flow = AuthorizationCodeFlow() auth_code_flow.load_from_env() api = SpotifyApiClient(auth_code_flow) with pytest.raises(SpotifyError): await api.get_auth_token_with_client_credentials()
async def test_inability_to_get_token_client_credential(self): client_credentials = ClientCredentialsFlow() client_credentials.load_from_env() client_credentials.scopes = TestDataTransfer.scopes api = SpotifyApiClient(client_credentials) with pytest.raises(SpotifyError): await api.get_code_with_cookie(TestDataTransfer.cookies)
async def test_authorization_code_flow(self, api: SpotifyApiClient): auth_code_flow = AuthorizationCodeFlow() auth_code_flow.load_from_env() auth_code_flow.scopes = TestDataTransfer.scopes api = SpotifyApiClient(auth_code_flow) code = await api.get_code_with_cookie(TestDataTransfer.cookies) await api.get_auth_token_with_code(code)
def test_pass_authentication(self): client = SpotifyApiClient( TestDataTransfer.auth_code_flow, hold_authentication=True, spotify_authorisation_token=SpotifyAuthorisationToken( refresh_token='1')) assert client.spotify_authorization_token.refresh_token is '1'
def test_auth_url(self, api: SpotifyApiClient): url = api.build_authorization_url(show_dialog=False, state="TestState") assert ("show_dialog=False" in url and "state=TestState" in url and TestDataTransfer.auth_code_flow.application_id in url and urlencode({ "redirect_uri": TestDataTransfer.auth_code_flow.redirect_url }) in url)
async def test_wrong_code_url(self): auth_flow = AuthorizationCodeFlow("test-with-wrong-code", "test-with-wrong-code", ["test-with-wrong-code"], "test-with-wrong-code") api = SpotifyApiClient(auth_flow) with pytest.raises(SpotifyError): await api.get_code_with_cookie(TestDataTransfer.cookies)
async def test_expired_token(self, prepared_api: SpotifyApiClient): await prepared_api.create_new_client() token = SpotifyAuthorisationToken(access_token='t', refresh_token='i', activation_time=int(time.time())) prepared_api.spotify_authorization_token = token with pytest.raises(TokenExpired): await prepared_api.albums.get_one('03dlqdFWY9gwJxGl3AREVy')
def add_api(): """ Add api parameter """ auth_code_flow = AuthorizationCodeFlow() auth_code_flow.load_from_env() auth_code_flow.scopes = TestDataTransfer.scopes TestDataTransfer.api = SpotifyApiClient(auth_code_flow, hold_authentication=True)
async def test_client_credentials(self): client_credentials = ClientCredentialsFlow() client_credentials.load_from_env() client_credentials.scopes = TestDataTransfer.scopes api = SpotifyApiClient(client_credentials) await api.get_auth_token_with_client_credentials() await api.create_new_client() resp = await api.albums.get_one('03dlqdFWY9gwJxGl3AREVy') assert isinstance(resp, dict)
def api(): """ Returns: The an api instance """ auth_code_flow = AuthorizationCodeFlow() auth_code_flow.load_from_env() auth_code_flow.scopes = TestDataTransfer.scopes api = SpotifyApiClient(auth_code_flow) return api
def __init__(self, bot): self.bot = bot self.games = {} if not bot.spy_client: auth_flow = AuthorizationCodeFlow(scopes=[]) auth_flow.load_from_env() api_client = SpotifyApiClient( auth_flow, hold_authentication=True, token_renew_instance=TokenRenewClass()) bot.spy_client = api_client bot.loop.create_task(self.connect_to_spotify()) bot.loop.create_task(self.create_tables())
async def prepared_api(): """ Returns: A ready to go api client """ auth_code_flow = AuthorizationCodeFlow() auth_code_flow.load_from_env() auth_code_flow.scopes = TestDataTransfer.scopes api = SpotifyApiClient(auth_code_flow, hold_authentication=True) code = await api.get_code_with_cookie(TestDataTransfer.cookies) await api.get_auth_token_with_code(code) await api.create_new_client() yield api await api.close_client()
def test_getter_and_setter(self, prepared_api: SpotifyApiClient): renew = TokenRenewClass() prepared_api.token_renew_instance = renew assert prepared_api.token_renew_instance is renew
def test_load_wrong_auth_flow(self): auth_flow = AuthorizationCodeFlow() with pytest.raises(SpotifyError): SpotifyApiClient(auth_flow)
def test_open_browser(self, api: SpotifyApiClient): api.open_oauth_dialog_in_browser() assert True
async def test_remove_authentication(self, prepared_api: SpotifyApiClient): prepared_api.hold_authentication = False await prepared_api.create_new_client() with pytest.raises(SpotifyError): await prepared_api.albums.get_one('somerandomstring') await prepared_api.close_client()