spotipy/tests/unit/test_scopes.py
Peter-Schorn bd80181816 Renamed the auth parameter of Spotify.__init__ to access_token for better clarity.
Removed the `client_credentials_manager` and `oauth_manager` parameters because they are redundant.

Replaced the `set_auth` and `auth_manager` properties with standard attributes.

Removed the following deprecated methods from `Spotify`:
* `playlist_tracks`
* `user_playlist`
* `user_playlist_tracks`
* `user_playlist_change_details`
* `user_playlist_unfollow`
* `user_playlist_add_tracks`
* `user_playlist_replace_tracks`
* `user_playlist_reorder_tracks`
* `user_playlist_remove_all_occurrences_of_tracks`
* `user_playlist_remove_specific_occurrences_of_tracks`
* `user_playlist_follow_playlist`
* `user_playlist_is_following`

Removed the deprecated `as_dict` parameter from the `get_access_token` method of `SpotifyOAuth` and `SpotifyPKCE`.

Removed the deprecated `get_cached_token` and `_save_token_info` methods of `SpotifyOAuth` and `SpotifyPKCE`.

Removed `SpotifyImplicitGrant`.

Removed `prompt_for_user_token`.
2021-04-14 12:40:08 -05:00

116 lines
4.1 KiB
Python

from unittest import TestCase
from spotipy.scope import Scope
from spotipy.oauth2 import SpotifyAuthBase
class SpotipyScopeTest(TestCase):
@classmethod
def setUpClass(cls):
cls.auth_manager = SpotifyAuthBase(requests_session=True)
def normalize_scope(self, scope):
return self.auth_manager._normalize_scope(scope)
def test_empty_scope(self):
scopes = set()
scope_string = Scope.make_string(scopes)
normalized_scope_string = self.normalize_scope(scopes)
normalized_scope_string_2 = self.normalize_scope(scope_string)
self.assertEqual(scope_string, "")
self.assertEqual(normalized_scope_string, "")
self.assertEqual(normalized_scope_string_2, "")
converted_scopes = Scope.from_string(scope_string)
self.assertEqual(converted_scopes, set())
def test_scopes(self):
scopes = {
Scope.playlist_modify_public,
Scope.playlist_read_collaborative,
Scope.user_read_playback_state,
Scope.ugc_image_upload
}
normalized_scope_string = self.normalize_scope(scopes)
scope_string = Scope.make_string(scopes)
self.assertEqual(scope_string, normalized_scope_string)
normalized_scope_string_2 = self.normalize_scope(scope_string)
converted_scopes = Scope.from_string(scope_string)
normalized_converted_scope = Scope.from_string(normalized_scope_string)
normalized_converted_scope_2 = Scope.from_string(normalized_scope_string_2)
self.assertEqual(scopes, converted_scopes)
self.assertEqual(scopes, normalized_converted_scope)
self.assertEqual(scopes, normalized_converted_scope_2)
def test_single_scope(self):
scope_string = "user-modify-playback-state"
scope = Scope(scope_string)
self.assertEqual(scope, Scope.user_modify_playback_state)
self.assertEqual(scope_string, scope.value)
def test_scope_string(self):
scope_string = (
"user-read-currently-playing playlist-read-collaborative,user-library-read "
"playlist-read-private user-read-email"
)
expected_scopes = {
Scope.user_read_currently_playing,
Scope.playlist_read_collaborative,
Scope.user_library_read,
Scope.playlist_read_private,
Scope.user_read_email
}
parsed_scopes = Scope.from_string(scope_string)
normalized_scope_string = self.normalize_scope(scope_string)
normalized_parsed_scopes = Scope.from_string(normalized_scope_string)
self.assertEqual(parsed_scopes, expected_scopes)
self.assertEqual(normalized_parsed_scopes, expected_scopes)
def test_invalid_types(self):
numbers = [1, 2, 3]
with self.assertRaises(TypeError):
self.normalize_scope(numbers)
with self.assertRaises(TypeError):
self.normalize_scope(True)
def test_normalize_scope(self):
normalized_scope_string = self.normalize_scope([])
self.assertEqual(normalized_scope_string, "")
normalized_scope_string_2 = self.normalize_scope(())
self.assertEqual(normalized_scope_string_2, "")
self.assertIsNone(self.normalize_scope(None))
def test_all_scopes(self):
expected_scopes = {
Scope.user_read_currently_playing,
Scope.playlist_read_collaborative,
Scope.playlist_modify_private,
Scope.user_read_playback_position,
Scope.user_library_modify,
Scope.user_top_read,
Scope.user_read_playback_state,
Scope.user_read_email,
Scope.ugc_image_upload,
Scope.user_read_private,
Scope.playlist_modify_public,
Scope.user_library_read,
Scope.streaming,
Scope.user_read_recently_played,
Scope.user_follow_read,
Scope.user_follow_modify,
Scope.app_remote_control,
Scope.playlist_read_private,
Scope.user_modify_playback_state,
}
self.assertEqual(expected_scopes, Scope.all())