forked from spotipy-dev/spotipy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
551 lines (448 loc) · 20.5 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
import os
from spotipy import (
CLIENT_CREDS_ENV_VARS as CCEV,
prompt_for_user_token,
Spotify,
SpotifyException,
SpotifyImplicitGrant,
SpotifyPKCE
)
import unittest
from tests import helpers
class SpotipyPlaylistApiTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.four_tracks = ["spotify:track:6RtPijgfPKROxEzTHNRiDp",
"spotify:track:7IHOIqZUUInxjVkko181PB",
"4VrWlk8IQxevMvERoX08iC",
"http://open.spotify.com/track/3cySlItpiPiIAzU3NyHCJf"]
cls.other_tracks = ["spotify:track:2wySlB6vMzCbQrRnNGOYKa",
"spotify:track:29xKs5BAHlmlX1u4gzQAbJ",
"spotify:track:1PB7gRWcvefzu7t3LJLUlf"]
cls.username = os.getenv(CCEV['client_username'])
# be wary here, episodes sometimes go away forever
# which could cause tests that rely on four_episodes
# to fail
cls.four_episodes = [
"spotify:episode:7f9e73vfXKRqR6uCggK2Xy",
"spotify:episode:4wA1RLFNOWCJ8iprngXmM0",
"spotify:episode:32vhLjJjT7m3f9DFCJUCVZ",
"spotify:episode:7cRcsGYYRUFo1OF3RgRzdx",
]
scope = (
'playlist-modify-public '
'user-library-read '
'user-follow-read '
'user-library-modify '
'user-read-private '
'user-top-read '
'user-follow-modify '
'user-read-recently-played '
'ugc-image-upload '
'user-read-playback-state'
)
token = prompt_for_user_token(cls.username, scope=scope)
cls.spotify = Spotify(auth=token)
cls.spotify_no_retry = Spotify(auth=token, retries=0)
cls.new_playlist_name = 'spotipy-playlist-test'
cls.new_playlist = helpers.get_spotify_playlist(
cls.spotify, cls.new_playlist_name, cls.username) or \
cls.spotify.user_playlist_create(cls.username, cls.new_playlist_name)
cls.new_playlist_uri = cls.new_playlist['uri']
@classmethod
def tearDownClass(cls):
cls.spotify.current_user_unfollow_playlist(cls.new_playlist['id'])
def test_user_playlists(self):
playlists = self.spotify.user_playlists(self.username, limit=5)
self.assertTrue('items' in playlists)
self.assertGreaterEqual(len(playlists['items']), 1)
def test_playlist_items(self):
playlists = self.spotify.user_playlists(self.username, limit=5)
self.assertTrue('items' in playlists)
for playlist in playlists['items']:
if playlist['uri'] != self.new_playlist_uri:
continue
pid = playlist['id']
results = self.spotify.playlist_items(pid)
self.assertEqual(len(results['items']), 0)
def test_current_user_playlists(self):
playlists = self.spotify.current_user_playlists(limit=10)
self.assertTrue('items' in playlists)
self.assertGreaterEqual(len(playlists['items']), 1)
self.assertLessEqual(len(playlists['items']), 10)
def test_current_user_follow_playlist(self):
playlist_to_follow_id = '4erXB04MxwRAVqcUEpu30O'
self.spotify.current_user_follow_playlist(playlist_to_follow_id)
follows = self.spotify.playlist_is_following(
playlist_to_follow_id, [self.username])
self.assertTrue(len(follows) == 1, 'proper follows length')
self.assertTrue(follows[0], 'is following')
self.spotify.current_user_unfollow_playlist(playlist_to_follow_id)
follows = self.spotify.playlist_is_following(
playlist_to_follow_id, [self.username])
self.assertTrue(len(follows) == 1, 'proper follows length')
self.assertFalse(follows[0], 'is no longer following')
def test_playlist_replace_items(self):
# add tracks to playlist
self.spotify.playlist_add_items(
self.new_playlist['id'], self.four_tracks)
playlist = self.spotify.playlist(self.new_playlist['id'])
self.assertEqual(playlist['tracks']['total'], 4)
self.assertEqual(len(playlist['tracks']['items']), 4)
# replace with 3 other tracks
self.spotify.playlist_replace_items(self.new_playlist['id'],
self.other_tracks)
playlist = self.spotify.playlist(self.new_playlist['id'])
self.assertEqual(playlist['tracks']['total'], 3)
self.assertEqual(len(playlist['tracks']['items']), 3)
self.spotify.playlist_remove_all_occurrences_of_items(
playlist['id'], self.other_tracks)
playlist = self.spotify.playlist(self.new_playlist['id'])
self.assertEqual(playlist["tracks"]["total"], 0)
def test_get_playlist_by_id(self):
pl = self.spotify.playlist(self.new_playlist['id'])
self.assertEqual(pl["tracks"]["total"], 0)
def test_max_retries_reached_post(self):
import concurrent.futures
max_workers = 100
total_requests = 500
def do():
self.spotify_no_retry.playlist_change_details(
self.new_playlist['id'], description="test")
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
future_to_post = (executor.submit(do) for _i in range(1, total_requests))
for future in concurrent.futures.as_completed(future_to_post):
try:
future.result()
except Exception as exc:
# Test success
self.assertIsInstance(exc, SpotifyException)
self.assertEqual(exc.http_status, 429)
return
self.fail()
def test_playlist_add_items(self):
# add tracks to playlist
self.spotify.playlist_add_items(
self.new_playlist['id'], self.other_tracks)
playlist = self.spotify.playlist_items(self.new_playlist['id'])
self.assertEqual(playlist['total'], 3)
self.assertEqual(len(playlist['items']), 3)
pl = self.spotify.playlist_items(self.new_playlist['id'], limit=2)
self.assertEqual(len(pl["items"]), 2)
self.spotify.playlist_remove_all_occurrences_of_items(
self.new_playlist['id'], self.other_tracks)
playlist = self.spotify.playlist_items(self.new_playlist['id'])
self.assertEqual(playlist["total"], 0)
def test_playlist_add_episodes(self):
# add episodes to playlist
self.spotify.playlist_add_items(
self.new_playlist['id'], self.four_episodes)
playlist = self.spotify.playlist_items(self.new_playlist['id'])
self.assertEqual(playlist['total'], 4)
self.assertEqual(len(playlist['items']), 4)
pl = self.spotify.playlist_items(self.new_playlist['id'], limit=2)
self.assertEqual(len(pl["items"]), 2)
self.spotify.playlist_remove_all_occurrences_of_items(
self.new_playlist['id'], self.four_episodes)
playlist = self.spotify.playlist_items(self.new_playlist['id'])
self.assertEqual(playlist["total"], 0)
def test_playlist_cover_image(self):
# From https://dog.ceo/api/breeds/image/random
small_image = "https://images.dog.ceo/breeds/poodle-toy/n02113624_8936.jpg"
dog_base64 = helpers.get_as_base64(small_image)
self.spotify.playlist_upload_cover_image(self.new_playlist_uri, dog_base64)
res = self.spotify.playlist_cover_image(self.new_playlist_uri)
self.assertEqual(len(res), 1)
first_image = res[0]
self.assertIn('width', first_image)
self.assertIn('height', first_image)
self.assertIn('url', first_image)
def test_large_playlist_cover_image(self):
# From https://dog.ceo/api/breeds/image/random
large_image = "https://images.dog.ceo/breeds/pointer-germanlonghair/hans2.jpg"
dog_base64 = helpers.get_as_base64(large_image)
try:
self.spotify.playlist_upload_cover_image(self.new_playlist_uri, dog_base64)
except Exception as e:
self.assertIsInstance(e, SpotifyException)
self.assertEqual(e.http_status, 413)
return
self.fail()
def test_deprecated_starred(self):
pl = self.spotify.user_playlist(self.username)
self.assertTrue(pl["tracks"] is None)
self.assertTrue(pl["owner"] is None)
def test_deprecated_user_playlist(self):
# Test without user due to change from
# https://developer.spotify.com/community/news/2018/06/12/changes-to-playlist-uris/
pl = self.spotify.user_playlist(None, self.new_playlist['id'])
self.assertEqual(pl["tracks"]["total"], 0)
class SpotipyLibraryApiTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.four_tracks = ["spotify:track:6RtPijgfPKROxEzTHNRiDp",
"spotify:track:7IHOIqZUUInxjVkko181PB",
"4VrWlk8IQxevMvERoX08iC",
"http://open.spotify.com/track/3cySlItpiPiIAzU3NyHCJf"]
cls.album_ids = ["spotify:album:6kL09DaURb7rAoqqaA51KU",
"spotify:album:6RTzC0rDbvagTSJLlY7AKl"]
cls.episode_ids = [
"spotify:episode:3OEdPEYB69pfXoBrhvQYeC",
"spotify:episode:5LEFdZ9pYh99wSz7Go2D0g"
]
cls.username = os.getenv(CCEV['client_username'])
scope = (
'playlist-modify-public '
'user-library-read '
'user-follow-read '
'user-library-modify '
'user-read-private '
'user-top-read '
'user-follow-modify '
'user-read-recently-played '
'ugc-image-upload '
'user-read-playback-state'
)
token = prompt_for_user_token(cls.username, scope=scope)
cls.spotify = Spotify(auth=token)
def test_track_bad_id(self):
with self.assertRaises(SpotifyException):
self.spotify.track('BadID123')
def test_current_user_saved_tracks(self):
tracks = self.spotify.current_user_saved_tracks()
self.assertGreaterEqual(len(tracks['items']), 0)
def test_current_user_save_and_unsave_tracks(self):
tracks = self.spotify.current_user_saved_tracks()
total = tracks['total']
self.spotify.current_user_saved_tracks_add(self.four_tracks)
tracks = self.spotify.current_user_saved_tracks()
new_total = tracks['total']
self.assertEqual(new_total - total, len(self.four_tracks))
self.spotify.current_user_saved_tracks_delete(
self.four_tracks)
tracks = self.spotify.current_user_saved_tracks()
new_total = tracks['total']
self.assertEqual(new_total, total)
def test_current_user_saved_albums(self):
# Add
self.spotify.current_user_saved_albums_add(self.album_ids)
albums = self.spotify.current_user_saved_albums()
self.assertGreaterEqual(len(albums['items']), 2)
# Contains
resp = self.spotify.current_user_saved_albums_contains(self.album_ids)
self.assertEqual(resp, [True, True])
# Remove
self.spotify.current_user_saved_albums_delete(self.album_ids)
resp = self.spotify.current_user_saved_albums_contains(self.album_ids)
self.assertEqual(resp, [False, False])
def test_current_user_saved_episodes(self):
# Add
self.spotify.current_user_saved_episodes_add(self.episode_ids)
episodes = self.spotify.current_user_saved_episodes(market="US")
self.assertGreaterEqual(len(episodes['items']), 2)
# Contains
resp = self.spotify.current_user_saved_episodes_contains(self.episode_ids)
self.assertEqual(resp, [True, True])
# Remove
self.spotify.current_user_saved_episodes_delete(self.episode_ids)
resp = self.spotify.current_user_saved_episodes_contains(self.episode_ids)
self.assertEqual(resp, [False, False])
class SpotipyUserApiTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.username = os.getenv(CCEV['client_username'])
scope = (
'playlist-modify-public '
'user-library-read '
'user-follow-read '
'user-library-modify '
'user-read-private '
'user-top-read '
'user-follow-modify '
'user-read-recently-played '
'ugc-image-upload '
'user-read-playback-state'
)
token = prompt_for_user_token(cls.username, scope=scope)
cls.spotify = Spotify(auth=token)
def test_basic_user_profile(self):
user = self.spotify.user(self.username)
self.assertEqual(user['id'], self.username.lower())
def test_current_user(self):
user = self.spotify.current_user()
self.assertEqual(user['id'], self.username.lower())
def test_me(self):
user = self.spotify.me()
self.assertTrue(user['id'] == self.username.lower())
def test_current_user_top_tracks(self):
response = self.spotify.current_user_top_tracks()
items = response['items']
self.assertGreaterEqual(len(items), 0)
def test_current_user_top_artists(self):
response = self.spotify.current_user_top_artists()
items = response['items']
self.assertGreaterEqual(len(items), 0)
class SpotipyBrowseApiTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
username = os.getenv(CCEV['client_username'])
token = prompt_for_user_token(username)
cls.spotify = Spotify(auth=token)
def test_category(self):
rock_cat_id = '0JQ5DAqbMKFDXXwE9BDJAr'
response = self.spotify.category(rock_cat_id)
self.assertEqual(response['name'], 'Rock')
def test_categories(self):
response = self.spotify.categories()
self.assertGreater(len(response['categories']), 0)
def test_categories_country(self):
response = self.spotify.categories(country='US')
self.assertGreater(len(response['categories']), 0)
def test_categories_global(self):
response = self.spotify.categories()
self.assertGreater(len(response['categories']), 0)
def test_categories_locale(self):
response = self.spotify.categories(locale='en_US')
self.assertGreater(len(response['categories']), 0)
def test_categories_locale_global(self):
response = self.spotify.categories()
self.assertGreater(len(response['categories']), 0)
def test_categories_limit_low(self):
response = self.spotify.categories(limit=1)
self.assertEqual(len(response['categories']['items']), 1)
def test_categories_limit_high(self):
response = self.spotify.categories(limit=50)
self.assertLessEqual(len(response['categories']['items']), 50)
def test_category_playlists(self):
response = self.spotify.categories()
category = 'rock'
for cat in response['categories']['items']:
cat_id = cat['id']
if cat_id == category:
response = self.spotify.category_playlists(category_id=cat_id)
self.assertGreater(len(response['playlists']["items"]), 0)
def test_category_playlists_limit_low(self):
response = self.spotify.categories()
category = 'rock'
for cat in response['categories']['items']:
cat_id = cat['id']
if cat_id == category:
response = self.spotify.category_playlists(category_id=cat_id, limit=1)
self.assertEqual(len(response['categories']['items']), 1)
def test_category_playlists_limit_high(self):
response = self.spotify.categories()
category = 'rock'
for cat in response['categories']['items']:
cat_id = cat['id']
if cat_id == category:
response = self.spotify.category_playlists(category_id=cat_id, limit=50)
self.assertLessEqual(len(response['categories']['items']), 50)
def test_new_releases(self):
response = self.spotify.new_releases()
self.assertGreater(len(response['albums']['items']), 0)
def test_new_releases_limit_low(self):
response = self.spotify.new_releases(limit=1)
self.assertEqual(len(response['albums']['items']), 1)
def test_new_releases_limit_high(self):
response = self.spotify.new_releases(limit=50)
self.assertLessEqual(len(response['albums']['items']), 50)
def test_featured_releases(self):
response = self.spotify.featured_playlists()
self.assertGreater(len(response['playlists']), 0)
class SpotipyFollowApiTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.username = os.getenv(CCEV['client_username'])
scope = (
'playlist-modify-public '
'user-library-read '
'user-follow-read '
'user-library-modify '
'user-read-private '
'user-top-read '
'user-follow-modify '
'user-read-recently-played '
'ugc-image-upload '
'user-read-playback-state'
)
token = prompt_for_user_token(cls.username, scope=scope)
cls.spotify = Spotify(auth=token)
def test_current_user_follows(self):
response = self.spotify.current_user_followed_artists()
artists = response['artists']
self.assertGreaterEqual(len(artists['items']), 0)
def test_user_follows_and_unfollows_artist(self):
# Initially follows 1 artist
current_user_followed_artists = self.spotify.current_user_followed_artists()[
'artists']['total']
# Follow 2 more artists
artists = ["6DPYiyq5kWVQS4RGwxzPC7", "0NbfKEOTQCcwd6o7wSDOHI"]
self.spotify.user_follow_artists(artists)
self.assertTrue(all(self.spotify.current_user_following_artists(artists)))
# Unfollow these 2 artists
self.spotify.user_unfollow_artists(artists)
self.assertFalse(any(self.spotify.current_user_following_artists(artists)))
res = self.spotify.current_user_followed_artists()
self.assertEqual(res['artists']['total'], current_user_followed_artists)
def test_user_follows_and_unfollows_user(self):
users = ["11111204", "xlqeojt6n7on0j7coh9go8ifd"]
# Follow 2 more users
self.spotify.user_follow_users(users)
self.assertTrue(all(self.spotify.current_user_following_users(users)))
# Unfollow these 2 users
self.spotify.user_unfollow_users(users)
self.assertFalse(any(self.spotify.current_user_following_users(users)))
class SpotipyPlayerApiTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.username = os.getenv(CCEV['client_username'])
scope = (
'playlist-modify-public '
'user-library-read '
'user-follow-read '
'user-library-modify '
'user-read-private '
'user-top-read '
'user-follow-modify '
'user-read-recently-played '
'ugc-image-upload '
'user-read-playback-state'
)
token = prompt_for_user_token(cls.username, scope=scope)
cls.spotify = Spotify(auth=token)
def test_devices(self):
# No devices playing by default
res = self.spotify.devices()
self.assertGreaterEqual(len(res["devices"]), 0)
def test_current_user_recently_played(self):
# No cursor
res = self.spotify.current_user_recently_played()
self.assertLessEqual(len(res['items']), 50)
# not much more to test if account is inactive and has no recently played tracks
class SpotipyImplicitGrantTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
scope = (
'user-follow-read '
'user-follow-modify '
)
auth_manager = SpotifyImplicitGrant(scope=scope,
cache_path=".cache-implicittest")
cls.spotify = Spotify(auth_manager=auth_manager)
def test_current_user(self):
c_user = self.spotify.current_user()
user = self.spotify.user(c_user['id'])
self.assertEqual(c_user['display_name'], user['display_name'])
class SpotifyPKCETests(unittest.TestCase):
@classmethod
def setUpClass(cls):
scope = (
'user-follow-read '
'user-follow-modify '
)
auth_manager = SpotifyPKCE(scope=scope, cache_path=".cache-pkcetest")
cls.spotify = Spotify(auth_manager=auth_manager)
def test_current_user(self):
c_user = self.spotify.current_user()
user = self.spotify.user(c_user['id'])
self.assertEqual(c_user['display_name'], user['display_name'])