forked from spotipy-dev/spotipy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthtests.py
177 lines (133 loc) · 5.91 KB
/
authtests.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
# -*- coding: latin-1 -*-
import spotipy
from spotipy import util
import unittest
import pprint
import sys
'''
Since these tests require authentication they are maintained
separately from the other tests.
These tests try to be benign and leave your collection and
playlists in a relatively stable state.
'''
class AuthTestSpotipy(unittest.TestCase):
'''
These tests require user authentication
'''
playlist = "spotify:user:plamere:playlist:2oCEWyyAPbZp9xhVSxZavx"
four_tracks = ["spotify:track:6RtPijgfPKROxEzTHNRiDp",
"spotify:track:7IHOIqZUUInxjVkko181PB",
"4VrWlk8IQxevMvERoX08iC",
"http://open.spotify.com/track/3cySlItpiPiIAzU3NyHCJf"]
two_tracks = ["spotify:track:6RtPijgfPKROxEzTHNRiDp",
"spotify:track:7IHOIqZUUInxjVkko181PB"]
other_tracks=["spotify:track:2wySlB6vMzCbQrRnNGOYKa",
"spotify:track:29xKs5BAHlmlX1u4gzQAbJ",
"spotify:track:1PB7gRWcvefzu7t3LJLUlf"]
bad_id = 'BAD_ID'
def test_track_bad_id(self):
try:
track = spotify.track(self.bad_id)
self.assertTrue(False)
except spotipy.SpotifyException:
self.assertTrue(True)
def test_basic_user_profile(self):
user = spotify.user(username)
self.assertTrue(user['id'] == username)
def test_current_user(self):
user = spotify.current_user()
self.assertTrue(user['id'] == username)
def test_me(self):
user = spotify.me()
self.assertTrue(user['id'] == username)
def test_user_playlists(self):
playlists = spotify.user_playlists(username, limit=5)
self.assertTrue('items' in playlists)
# known API issue currently causes this test to fail
# the issue is that the API doesn't currently respect the
# limit paramter
self.assertTrue(len(playlists['items']) == 5)
def test_user_playlist_tracks(self):
playlists = spotify.user_playlists(username, limit=5)
self.assertTrue('items' in playlists)
for playlist in playlists['items']:
user = playlist['owner']['id']
pid = playlist['id']
results = spotify.user_playlist_tracks(user, pid)
self.assertTrue(len(results['items']) > 0)
def user_playlist_tracks(self, user, playlist_id = None, fields=None,
limit=100, offset=0):
# known API issue currently causes this test to fail
# the issue is that the API doesn't currently respect the
# limit paramter
self.assertTrue(len(playlists['items']) == 5)
def test_current_user_saved_tracks(self):
tracks = spotify.current_user_saved_tracks()
self.assertTrue(len(tracks['items']) > 0)
def test_current_user_save_and_unsave_tracks(self):
tracks = spotify.current_user_saved_tracks()
total = tracks['total']
spotify.current_user_saved_tracks_add(self.four_tracks)
tracks = spotify.current_user_saved_tracks()
new_total = tracks['total']
self.assertTrue(new_total - total == len(self.four_tracks))
tracks = spotify.current_user_saved_tracks_delete(self.four_tracks)
tracks = spotify.current_user_saved_tracks()
new_total = tracks['total']
self.assertTrue(new_total == total)
def test_new_releases(self):
response = spotify.new_releases()
self.assertTrue(len(response['albums']) > 0)
def test_featured_releases(self):
response = spotify.featured_playlists()
self.assertTrue(len(response['playlists']) > 0)
def get_or_create_spotify_playlist(self, username, playlist_name):
playlists = spotify.user_playlists(username)
while playlists:
for item in playlists['items']:
if item['name'] == playlist_name:
return item['id']
playlists = spotify.next(playlists)
playlist = spotify.user_playlist_create(username, playlist_name)
playlist_id = playlist['uri']
return playlist_id
def test_user_playlist_ops(self):
# create empty playlist
playlist_id = self.get_or_create_spotify_playlist(username,
'spotipy-testing-playlist-1')
# remove all tracks from it
spotify.user_playlist_replace_tracks(username, playlist_id,[])
playlist = spotify.user_playlist(username, playlist_id)
self.assertTrue(playlist['tracks']['total'] == 0)
self.assertTrue(len(playlist['tracks']['items']) == 0)
# add tracks to it
spotify.user_playlist_add_tracks(username, playlist_id, self.four_tracks)
playlist = spotify.user_playlist(username, playlist_id)
self.assertTrue(playlist['tracks']['total'] == 4)
self.assertTrue(len(playlist['tracks']['items']) == 4)
# remove two tracks from it
spotify.user_playlist_remove_all_occurrences_of_tracks (username,
playlist_id, self.two_tracks)
playlist = spotify.user_playlist(username, playlist_id)
self.assertTrue(playlist['tracks']['total'] == 2)
self.assertTrue(len(playlist['tracks']['items']) == 2)
# replace with 3 other tracks
spotify.user_playlist_replace_tracks(username,
playlist_id, self.other_tracks)
playlist = spotify.user_playlist(username, playlist_id)
self.assertTrue(playlist['tracks']['total'] == 3)
self.assertTrue(len(playlist['tracks']['items']) == 3)
if __name__ == '__main__':
if len(sys.argv) > 1:
username = sys.argv[1]
del sys.argv[1]
scope = 'playlist-modify-public '
scope += 'user-library-read '
scope += 'user-library-modify '
scope += 'user-read-private'
token = util.prompt_for_user_token(username, scope)
spotify = spotipy.Spotify(auth=token)
spotify.trace = False
unittest.main()
else:
print "Usage: %s username" % (sys.argv[0],)