Skip to content

Commit 90a664d

Browse files
committed
Add test for audio analysis and move around auth URL method
1 parent 43090b2 commit 90a664d

File tree

3 files changed

+93
-62
lines changed

3 files changed

+93
-62
lines changed

__tests__/spotify-web-api.js

Lines changed: 69 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3601,6 +3601,37 @@ describe('Spotify Web API', () => {
36013601
);
36023602
});
36033603

3604+
test('should get the audio analysis for a track', done => {
3605+
sinon.stub(HttpManager, '_makeRequest').callsFake(function(
3606+
method,
3607+
options,
3608+
uri,
3609+
callback
3610+
) {
3611+
expect(method).toBe(superagent.get);
3612+
expect(uri).toBe(
3613+
'https://api.spotify.com/v1/audio-analysis/3Qm86XLflmIXVm1wcwkgDK'
3614+
);
3615+
expect(options.query).toBeFalsy();
3616+
expect(options.data).toBeFalsy();
3617+
callback(null, {
3618+
body: {
3619+
}
3620+
});
3621+
});
3622+
3623+
var api = new SpotifyWebApi();
3624+
3625+
api.getAudioAnalysisForTrack('3Qm86XLflmIXVm1wcwkgDK').then(
3626+
function(data) {
3627+
done();
3628+
},
3629+
function(err) {
3630+
done(err);
3631+
}
3632+
);
3633+
});
3634+
36043635
test('should get the audio features for a track', done => {
36053636
sinon.stub(HttpManager, '_makeRequest').callsFake(function(
36063637
method,
@@ -4080,6 +4111,44 @@ describe('Spotify Web API', () => {
40804111
'https://accounts.spotify.com/authorize?client_id=5fe01282e44241328a84e7c5cc169165&response_type=code&redirect_uri=https://example.com/callback&scope=user-read-private%20user-read-email&state=some-state-of-my-choice'
40814112
);
40824113
});
4114+
4115+
test('should create authorization URL with code based authentication', () => {
4116+
var scopes = ['user-read-private', 'user-read-email'],
4117+
redirectUri = 'https://example.com/callback',
4118+
clientId = '5fe01282e44241328a84e7c5cc169165',
4119+
state = 'some-state-of-my-choice',
4120+
showDialog = true;
4121+
4122+
var api = new SpotifyWebApi({
4123+
clientId: clientId,
4124+
redirectUri: redirectUri
4125+
});
4126+
4127+
var authorizeURL = api.createAuthorizeURL(scopes, state, showDialog);
4128+
expect(authorizeURL).toBe(
4129+
'https://accounts.spotify.com/authorize?client_id=5fe01282e44241328a84e7c5cc169165&response_type=code&redirect_uri=https://example.com/callback&scope=user-read-private%20user-read-email&state=some-state-of-my-choice&show_dialog=true'
4130+
);
4131+
});
4132+
4133+
test('should create authorization URL with token based authentication', () => {
4134+
var scopes = ['user-read-private', 'user-read-email'],
4135+
redirectUri = 'https://example.com/callback',
4136+
clientId = '5fe01282e44241328a84e7c5cc169165',
4137+
state = 'some-state-of-my-choice',
4138+
showDialog = true,
4139+
responseType = 'token'
4140+
4141+
var api = new SpotifyWebApi({
4142+
clientId: clientId,
4143+
redirectUri: redirectUri
4144+
});
4145+
4146+
var authorizeURL = api.createAuthorizeURL(scopes, state, showDialog, responseType);
4147+
4148+
expect(authorizeURL).toBe(
4149+
'https://accounts.spotify.com/authorize?client_id=5fe01282e44241328a84e7c5cc169165&response_type=token&redirect_uri=https://example.com/callback&scope=user-read-private%20user-read-email&state=some-state-of-my-choice&show_dialog=true'
4150+
);
4151+
});
40834152

40844153
/* Client credentials */
40854154
test('should retrieve an access token using the client credentials flow', function(done) {
@@ -4238,44 +4307,6 @@ describe('Spotify Web API', () => {
42384307
});
42394308
});
42404309

4241-
test('should create authorization URL with code based authentication', () => {
4242-
var scopes = ['user-read-private', 'user-read-email'],
4243-
redirectUri = 'https://example.com/callback',
4244-
clientId = '5fe01282e44241328a84e7c5cc169165',
4245-
state = 'some-state-of-my-choice',
4246-
showDialog = true;
4247-
4248-
var api = new SpotifyWebApi({
4249-
clientId: clientId,
4250-
redirectUri: redirectUri
4251-
});
4252-
4253-
var authorizeURL = api.createAuthorizeURL(scopes, state, showDialog);
4254-
expect(authorizeURL).toBe(
4255-
'https://accounts.spotify.com/authorize?client_id=5fe01282e44241328a84e7c5cc169165&response_type=code&redirect_uri=https://example.com/callback&scope=user-read-private%20user-read-email&state=some-state-of-my-choice&show_dialog=true'
4256-
);
4257-
});
4258-
4259-
test('should create authorization URL with token based authentication', () => {
4260-
var scopes = ['user-read-private', 'user-read-email'],
4261-
redirectUri = 'https://example.com/callback',
4262-
clientId = '5fe01282e44241328a84e7c5cc169165',
4263-
state = 'some-state-of-my-choice',
4264-
showDialog = true,
4265-
responseType = 'token'
4266-
4267-
var api = new SpotifyWebApi({
4268-
clientId: clientId,
4269-
redirectUri: redirectUri
4270-
});
4271-
4272-
var authorizeURL = api.createAuthorizeURL(scopes, state, showDialog, responseType);
4273-
4274-
expect(authorizeURL).toBe(
4275-
'https://accounts.spotify.com/authorize?client_id=5fe01282e44241328a84e7c5cc169165&response_type=token&redirect_uri=https://example.com/callback&scope=user-read-private%20user-read-email&state=some-state-of-my-choice&show_dialog=true'
4276-
);
4277-
});
4278-
42794310
test('should set, get and reset credentials', function(done) {
42804311
var api = new SpotifyWebApi();
42814312

src/server-methods.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,30 @@ var AuthenticationRequest = require('./authentication-request');
44
var HttpManager = require('./http-manager');
55

66
module.exports = {
7+
8+
/**
9+
* Retrieve a URL where the user can give the application permissions.
10+
* @param {string[]} scopes The scopes corresponding to the permissions the application needs.
11+
* @param {string} state A parameter that you can use to maintain a value between the request and the callback to redirect_uri.It is useful to prevent CSRF exploits.
12+
* @param {boolean} showDialog A parameter that you can use to force the user to approve the app on each login rather than being automatically redirected.
13+
* @param {string} responseType An optional parameter that you can use to specify the code response based on the authentication type - can be set to 'code' or 'token'. Default 'code' to ensure backwards compatability.
14+
* @returns {string} The URL where the user can give application permissions.
15+
*/
16+
createAuthorizeURL: function(scopes, state, showDialog, responseType = 'code') {
17+
return AuthenticationRequest.builder()
18+
.withPath('/authorize')
19+
.withQueryParameters({
20+
client_id: this.getClientId(),
21+
response_type: responseType,
22+
redirect_uri: this.getRedirectURI(),
23+
scope: scopes.join('%20'),
24+
state: state,
25+
show_dialog: showDialog && !!showDialog
26+
})
27+
.build()
28+
.getURL();
29+
},
30+
731
/**
832
* Request an access token using the Client Credentials flow.
933
* Requires that client ID and client secret has been set previous to the call.

src/spotify-web-api.js

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -773,29 +773,6 @@ SpotifyWebApi.prototype = {
773773
.execute(HttpManager.get, callback);
774774
},
775775

776-
/**
777-
* Retrieve a URL where the user can give the application permissions.
778-
* @param {string[]} scopes The scopes corresponding to the permissions the application needs.
779-
* @param {string} state A parameter that you can use to maintain a value between the request and the callback to redirect_uri.It is useful to prevent CSRF exploits.
780-
* @param {boolean} showDialog A parameter that you can use to force the user to approve the app on each login rather than being automatically redirected.
781-
* @param {string} responseType An optional parameter that you can use to specify the code response based on the authentication type - can be set to 'code' or 'token'. Default 'code' to ensure backwards compatability.
782-
* @returns {string} The URL where the user can give application permissions.
783-
*/
784-
createAuthorizeURL: function(scopes, state, showDialog, responseType = 'code') {
785-
return AuthenticationRequest.builder()
786-
.withPath('/authorize')
787-
.withQueryParameters({
788-
client_id: this.getClientId(),
789-
response_type: responseType,
790-
redirect_uri: this.getRedirectURI(),
791-
scope: scopes.join('%20'),
792-
state: state,
793-
show_dialog: showDialog && !!showDialog
794-
})
795-
.build()
796-
.getURL();
797-
},
798-
799776
/**
800777
* Retrieve the tracks that are saved to the authenticated users Your Music library.
801778
* @param {Object} [options] Options, being market, limit, and/or offset.
@@ -1672,7 +1649,6 @@ SpotifyWebApi.prototype = {
16721649
},
16731650
};
16741651

1675-
16761652
SpotifyWebApi._addMethods = function(methods) {
16771653
for (var i in methods) {
16781654
if (methods.hasOwnProperty(i)) {

0 commit comments

Comments
 (0)