-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.js
209 lines (147 loc) · 5.79 KB
/
tests.js
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
var util = require('util'),
fs = require('fs'),
sys = require('util'),
Jamendo = require('./index.js'),
assert = require('assert');
function load_or(filename, defaults) {
try { return require(filename); } catch (e) { return defaults; }
}
// try to load my own credentials
var client_id = load_or('./.client_id.js', 'f919df7d');
var client_secret = load_or('./.client_secret.js', null);
console.log('using API client_id:', client_id);
console.log('using API client_secret:', client_secret);
// get an API client
var jamendo = new Jamendo({
debug: true,
retry: true,
protocol: 'https',
client_id: client_id,
client_secret: client_secret,
rejectUnauthorized: false
});
var tests = [
// test tracks method
jamendo.tracks({ id: 245 }, function(error, data){
assert(typeof data.results !== 'undefined');
assert(data.results.length === 1);
assert(data.results[0].id === '245');
assert(data.results[0].artist_id === '5');
assert(data.results[0].album_id === '33');
assert(data.results[0].artist_name === 'Both');
assert(data.results[0].album_name === 'Simple Exercice');
}),
// test tracks MP3 file redirection
jamendo.tracks_file({ id: 245 }, function(error, mp3_data){
assert(mp3_data.length > 0);
assert(mp3_data.substr(0, 3) === 'ID3');
}),
// test tracks MP3 file redirection, with pipes
jamendo.tracks_file({ id: 245 }).pipe(fs.createWriteStream('245.mp3')),
// test albums method
jamendo.albums({ id: 33 }, function(error, data){
assert(typeof data.results !== 'undefined');
assert(data.results.length === 1);
assert(data.results[0].id === '33');
assert(data.results[0].artist_id === '5');
assert(data.results[0].artist_name === 'Both');
assert(data.results[0].name === 'Simple Exercice');
}),
// test album MP3 file redirection
jamendo.albums_file({ id: 33, audioformat: 'mp32' }, function(error, zip_data){
assert(typeof zip_data !== 'undefined');
assert(zip_data.length > 0);
assert(zip_data.substr(0, 2) === 'PK');
}),
// test artists method
jamendo.artists({ id: 5 }, function(error, data){
assert(typeof data.results !== 'undefined');
assert(data.results.length === 1);
assert(data.results[0].id === '5');
assert(data.results[0].name === 'Both');
}),
/* // TODO
album_tracks
artist_albums
artist_tracks
playlists
playlists_tracks
reviews
reviews_albums
radios
*/
// test users_favorites_artists method
jamendo.users_favorites_artists({ id: 257235 }, function(error, data){
assert(data.results !== 'undefined');
assert(data.results.length === 1);
assert(data.results[0].artists.length > 1);
}),
// test concerts method
jamendo.concerts({ }, function(error, data){
assert(typeof data.results !== 'undefined');
assert(data.results.length === 10);
}),
// test array params
jamendo.artists({ id: [ 5, 888 ] }, function(error, data){
assert(typeof data.results !== 'undefined');
assert(data.results.length === 2);
}),
// test default params
jamendo.artists({ }, function(error, data){
assert(typeof data.results !== 'undefined');
assert(data.results.length === 10);
}),
// test datebetween params, as strings and timestamps
jamendo.tracks({ datebetween: [ 449921044 * 1000, '2011-10-10' ], limit: 10 }, function(error, data){
assert(typeof data.results !== 'undefined');
assert(data.results.length === 10);
}),
// test datebetween params, as Date objects
jamendo.tracks({ datebetween: [ new Date('1984-04-04'), new Date('2011-10-10') ], limit: 10 }, function(error, data){
assert(typeof data.results !== 'undefined');
assert(data.results.length === 10);
})
];
for (var i = 0; i < tests.length; i++) {
setTimeout(function(){ tests[i]; }, 1000);
}
// test authorize
var test_redirect_uri = 'http://localhost/DAT_CODE';
jamendo.authorize({ redirect_uri: test_redirect_uri }, function(error, login_url){
assert(typeof login_url !== 'undefined');
var authorization_code = process.env.AUTHORIZATION_CODE;
// dont have any AUTHORIZATION_CODE to play with ,*__*
if (!authorization_code) {
console.log('Open your browser at', login_url, '. Log you in, accept app, and get the auth code');
console.log('then, in max 30 seconds from now, run me with that string in the AUTHORIZATION_CODE environement variable, like');
console.log('$> AUTHORIZATION_CODE=mybrandnewauthcode npm test');
console.log('');
// much better !
} else {
// get granted
jamendo.grant({ redirect_uri: test_redirect_uri, code: authorization_code }, function(error, oauth_data){
assert(typeof oauth_data !== 'undefined', 'Cannot get oauth data, verify your credentials');
var refresh_token = oauth_data.refresh_token;
var access_token = oauth_data.access_token;
var expires_in = oauth_data.expires_in;
var token_type = oauth_data.token_type;
var scope = oauth_data.scope;
// test setuser_fan
jamendo.setuser_fan({ access_token: access_token, artist_id: 5 }, function(error, error_message, warnings){
assert.ok(!error, 'Cannot get be fan of artist Both: ' + error_message);
});
// test setuser_favorite
jamendo.setuser_favorite({ access_token: access_token, track_id: 245 }, function(error, error_message, warnings){
assert.ok(!error, 'Cannot get be fan of track: ' + error_message);
});
// test setuser_like
jamendo.setuser_like({ access_token: access_token, track_id: 245 }, function(error, error_message, warnings){
assert.ok(!error, 'Cannot like track: ' + error_message);
});
// test setuser_dislike
jamendo.setuser_dislike({ access_token: access_token, track_id: 245 }, function(error, error_message, warnings){
assert.ok(!error, 'Cannot dislike track', error_message);
});
});
}
});