Skip to content

Commit

Permalink
bring in changes from context-manager-mock branch
Browse files Browse the repository at this point in the history
Updated integration tests to mock `_get` and `_post` methods in the Spotify class
  • Loading branch information
508chris committed Jan 20, 2025
1 parent c11aa76 commit 221fbe8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 31 deletions.
4 changes: 4 additions & 0 deletions tests/integration/user_endpoints/test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import unittest
from unittest.mock import patch

from spotipy import CLIENT_CREDS_ENV_VARS as CCEV
from spotipy import (Spotify, SpotifyException, SpotifyImplicitGrant,
Expand Down Expand Up @@ -530,6 +531,7 @@ class SpotifyQueueApiTests(unittest.TestCase):
def setUp(self):
self.spotify = Spotify(auth="test_token")

@patch('spotipy.client.Spotify._get')
def test_get_queue(self, mock_get):
# Mock the response from _get
mock_get.return_value = {'songs': ['song1', 'song2']}
Expand All @@ -543,6 +545,7 @@ def test_get_queue(self, mock_get):
# Check if the response is as expected
self.assertEqual(response, {'songs': ['song1', 'song2']})

@patch('spotipy.client.Spotify._post')
def test_add_to_queue(self, mock_post):
test_uri = 'spotify:track:123'

Expand All @@ -553,6 +556,7 @@ def test_add_to_queue(self, mock_post):
endpoint = f"me/player/queue?uri={test_uri}"
mock_post.assert_called_with(endpoint)

@patch('spotipy.client.Spotify._post')
def test_add_to_queue_with_device_id(self, mock_post):
test_uri = 'spotify:track:123'
device_id = 'device123'
Expand Down
46 changes: 15 additions & 31 deletions tests/unit/test_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import unittest
import unittest.mock as mock
import urllib.parse as urllibparse
from unittest.mock import mock_open

from spotipy import SpotifyImplicitGrant, SpotifyOAuth, SpotifyPKCE
from spotipy.cache_handler import MemoryCacheHandler
Expand Down Expand Up @@ -50,8 +49,7 @@ class OAuthCacheTest(unittest.TestCase):

@patch.multiple(SpotifyOAuth,
is_token_expired=DEFAULT, refresh_access_token=DEFAULT)
@patch('spotipy.cache_handler.open', side_effect=lambda *args, **
kwargs: mock_open(read_data='mock data')(), create=True)
@patch('spotipy.cache_handler.open', create=True)
def test_gets_from_cache_path(self, opener,
is_token_expired, refresh_access_token):
"""Test that the token is retrieved from the cache path."""
Expand All @@ -75,8 +73,7 @@ def test_gets_from_cache_path(self, opener,

@patch.multiple(SpotifyOAuth,
is_token_expired=DEFAULT, refresh_access_token=DEFAULT)
@patch('spotipy.cache_handler.open', side_effect=lambda *args, **
kwargs: mock_open(read_data='mock data')(), create=True)
@patch('spotipy.cache_handler.open', create=True)
def test_expired_token_refreshes(self, opener,
is_token_expired, refresh_access_token):
"""Test that an expired token is refreshed."""
Expand All @@ -99,8 +96,7 @@ def test_expired_token_refreshes(self, opener,

@patch.multiple(SpotifyOAuth,
is_token_expired=DEFAULT, refresh_access_token=DEFAULT)
@patch('spotipy.cache_handler.open', side_effect=lambda *args, **
kwargs: mock_open(read_data='mock data')(), create=True)
@patch('spotipy.cache_handler.open', create=True)
def test_badly_scoped_token_bails(self, opener,
is_token_expired, refresh_access_token):
token_scope = "playlist-modify-public"
Expand All @@ -121,8 +117,7 @@ def test_badly_scoped_token_bails(self, opener,
self.assertIsNone(cached_tok)
self.assertEqual(refresh_access_token.call_count, 0)

@patch('spotipy.cache_handler.open', side_effect=lambda *args, **
kwargs: mock_open(read_data='mock data')(), create=True)
@patch('spotipy.cache_handler.open', create=True)
def test_saves_to_cache_path(self, opener):
"""Test that the token is saved to the cache path."""
scope = "playlist-modify-private"
Expand All @@ -140,8 +135,7 @@ def test_saves_to_cache_path(self, opener):
opener.assert_called_with(path, 'w', encoding='utf-8')
self.assertTrue(fi.write.called)

@patch('spotipy.cache_handler.open', side_effect=lambda *args, **
kwargs: mock_open(read_data='mock data')(), create=True)
@patch('spotipy.cache_handler.open', create=True)
def test_saves_to_cache_path_legacy(self, opener):
scope = "playlist-modify-private"
path = ".cache-username"
Expand Down Expand Up @@ -265,8 +259,7 @@ def test_spotify_client_credentials_get_access_token(self):
class ImplicitGrantCacheTest(unittest.TestCase):

@patch.object(SpotifyImplicitGrant, "is_token_expired", DEFAULT)
@patch('spotipy.cache_handler.open', side_effect=lambda *args, **
kwargs: mock_open(read_data='mock data')(), create=True)
@patch('spotipy.cache_handler.open', create=True)
def test_gets_from_cache_path(self, opener, is_token_expired):
scope = "playlist-modify-private"
path = ".cache-username"
Expand All @@ -287,8 +280,7 @@ def test_gets_from_cache_path(self, opener, is_token_expired):
self.assertIsNotNone(cached_tok_legacy)

@patch.object(SpotifyImplicitGrant, "is_token_expired", DEFAULT)
@patch('spotipy.cache_handler.open', side_effect=lambda *args, **
kwargs: mock_open(read_data='mock data')(), create=True)
@patch('spotipy.cache_handler.open', create=True)
def test_expired_token_returns_none(self, opener, is_token_expired):
"""Test that an expired token returns None."""
scope = "playlist-modify-private"
Expand All @@ -308,8 +300,7 @@ def test_expired_token_returns_none(self, opener, is_token_expired):
self.assertIsNone(cached_tok)

@patch.object(SpotifyImplicitGrant, "is_token_expired", DEFAULT)
@patch('spotipy.cache_handler.open', side_effect=lambda *args, **
kwargs: mock_open(read_data='mock data')(), create=True)
@patch('spotipy.cache_handler.open', create=True)
def test_badly_scoped_token_bails(self, opener, is_token_expired):
token_scope = "playlist-modify-public"
requested_scope = "playlist-modify-private"
Expand All @@ -328,8 +319,7 @@ def test_badly_scoped_token_bails(self, opener, is_token_expired):
opener.assert_called_with(path, encoding='utf-8')
self.assertIsNone(cached_tok)

@patch('spotipy.cache_handler.open', side_effect=lambda *args, **
kwargs: mock_open(read_data='mock data')(), create=True)
@patch('spotipy.cache_handler.open', create=True)
def test_saves_to_cache_path(self, opener):
scope = "playlist-modify-private"
path = ".cache-username"
Expand All @@ -346,8 +336,7 @@ def test_saves_to_cache_path(self, opener):
opener.assert_called_with(path, 'w', encoding='utf-8')
self.assertTrue(fi.write.called)

@patch('spotipy.cache_handler.open', side_effect=lambda *args, **
kwargs: mock_open(read_data='mock data')(), create=True)
@patch('spotipy.cache_handler.open', create=True)
def test_saves_to_cache_path_legacy(self, opener):
scope = "playlist-modify-private"
path = ".cache-username"
Expand Down Expand Up @@ -419,8 +408,7 @@ class SpotifyPKCECacheTest(unittest.TestCase):

@patch.multiple(SpotifyPKCE,
is_token_expired=DEFAULT, refresh_access_token=DEFAULT)
@patch('spotipy.cache_handler.open', side_effect=lambda *args, **
kwargs: mock_open(read_data='mock data')(), create=True)
@patch('spotipy.cache_handler.open', create=True)
def test_gets_from_cache_path(self, opener,
is_token_expired, refresh_access_token):
scope = "playlist-modify-private"
Expand All @@ -444,8 +432,7 @@ def test_gets_from_cache_path(self, opener,

@patch.multiple(SpotifyPKCE,
is_token_expired=DEFAULT, refresh_access_token=DEFAULT)
@patch('spotipy.cache_handler.open', side_effect=lambda *args, **
kwargs: mock_open(read_data='mock data')(), create=True)
@patch('spotipy.cache_handler.open', create=True)
def test_expired_token_refreshes(self, opener,
is_token_expired, refresh_access_token):
scope = "playlist-modify-private"
Expand All @@ -467,8 +454,7 @@ def test_expired_token_refreshes(self, opener,

@patch.multiple(SpotifyPKCE,
is_token_expired=DEFAULT, refresh_access_token=DEFAULT)
@patch('spotipy.cache_handler.open', side_effect=lambda *args, **
kwargs: mock_open(read_data='mock data')(), create=True)
@patch('spotipy.cache_handler.open', create=True)
def test_badly_scoped_token_bails(self, opener,
is_token_expired, refresh_access_token):
token_scope = "playlist-modify-public"
Expand All @@ -489,8 +475,7 @@ def test_badly_scoped_token_bails(self, opener,
self.assertIsNone(cached_tok)
self.assertEqual(refresh_access_token.call_count, 0)

@patch('spotipy.cache_handler.open', side_effect=lambda *args, **
kwargs: mock_open(read_data='mock data')(), create=True)
@patch('spotipy.cache_handler.open', create=True)
def test_saves_to_cache_path(self, opener):
scope = "playlist-modify-private"
path = ".cache-username"
Expand All @@ -506,8 +491,7 @@ def test_saves_to_cache_path(self, opener):
opener.assert_called_with(path, 'w', encoding='utf-8')
self.assertTrue(fi.write.called)

@patch('spotipy.cache_handler.open', side_effect=lambda *args, **
kwargs: mock_open(read_data='mock data')(), create=True)
@patch('spotipy.cache_handler.open', create=True)
def test_saves_to_cache_path_legacy(self, opener):
scope = "playlist-modify-private"
path = ".cache-username"
Expand Down

0 comments on commit 221fbe8

Please sign in to comment.