Skip to content

Commit c338db5

Browse files
committed
Add tests for authorization flows
1 parent 93eaf4d commit c338db5

File tree

1 file changed

+113
-75
lines changed

1 file changed

+113
-75
lines changed

__tests__/spotify-web-api.js

+113-75
Original file line numberDiff line numberDiff line change
@@ -2294,81 +2294,6 @@ describe('Spotify Web API', () => {
22942294
);
22952295
});
22962296

2297-
test.skip('should retrieve an access token using the client credentials flow', function(done) {
2298-
var clientId = 'someClientId',
2299-
clientSecret = 'someClientSecret';
2300-
2301-
var api = new SpotifyWebApi({
2302-
clientId: clientId,
2303-
clientSecret: clientSecret
2304-
});
2305-
2306-
api.clientCredentialsGrant().then(
2307-
function(data) {
2308-
expect('Bearer').toBe(data['token_type']);
2309-
expect(3600).toBe(data['expires_in']);
2310-
expect(data['access_token']).toBeTruthy();
2311-
done();
2312-
},
2313-
function(err) {
2314-
done(err);
2315-
}
2316-
);
2317-
});
2318-
2319-
test.skip('should retrieve an access token with scopes', function(done) {
2320-
var clientId = 'fcecfc79122e4cd299473677a17cbd4d',
2321-
clientSecret = 'f6338737c9bb4bc9a71924cb2940adss';
2322-
2323-
var api = new SpotifyWebApi({
2324-
clientId: clientId,
2325-
clientSecret: clientSecret
2326-
});
2327-
2328-
var scopes = ['playlist-read'];
2329-
2330-
api
2331-
.clientCredentialsGrant({
2332-
scope: scopes
2333-
})
2334-
.then(
2335-
function(data) {
2336-
console.log(data);
2337-
expect('Bearer').toBe(data['token_type']);
2338-
expect(3600).toBe(data['expires_in']);
2339-
expect(data['access_token']).toBeTruthy();
2340-
done();
2341-
},
2342-
function(err) {
2343-
done(err);
2344-
}
2345-
);
2346-
});
2347-
2348-
test.skip('should retrieve an access token using the authorization code flow', function(done) {
2349-
var credentials = {
2350-
clientId: 'someClientId',
2351-
clientSecret: 'someClientSecret',
2352-
redirectUri: 'http://www.michaelthelin.se/test-callback'
2353-
};
2354-
2355-
var api = new SpotifyWebApi(credentials);
2356-
2357-
api.authorizationCodeGrant('mySuperLongCode').then(
2358-
function(data) {
2359-
expect('Bearer').toBe(data['token_type']);
2360-
expect(3600).toBe(data['expires_in']);
2361-
expect(data['access_token']).toBeTruthy();
2362-
expect(data['refresh_token']).toBeTruthy();
2363-
done();
2364-
},
2365-
function(err) {
2366-
console.log(err);
2367-
done(err);
2368-
}
2369-
);
2370-
});
2371-
23722297
test('should refresh an access token', done => {
23732298
sinon.stub(HttpManager, '_makeRequest').callsFake(function(
23742299
method,
@@ -4447,4 +4372,117 @@ describe('Spotify Web API', () => {
44474372
);
44484373
});
44494374

4375+
/**
4376+
* Authentication/Authorization
4377+
*/
4378+
4379+
/* Client credentials */
4380+
test('should retrieve an access token using the client credentials flow', function(done) {
4381+
sinon.stub(HttpManager, '_makeRequest').callsFake(function(
4382+
method,
4383+
options,
4384+
uri,
4385+
callback
4386+
) {
4387+
expect(method).toBe(superagent.post);
4388+
expect(uri).toBe(
4389+
'https://accounts.spotify.com/api/token'
4390+
);
4391+
expect(options.data.grant_type).toBe('client_credentials');
4392+
expect(options.headers).toStrictEqual({ Authorization: "Basic c29tZUNsaWVudElkOnNvbWVDbGllbnRTZWNyZXQ=" });
4393+
callback(null, {
4394+
statusCode: 200
4395+
})
4396+
});
4397+
4398+
var clientId = 'someClientId',
4399+
clientSecret = 'someClientSecret';
4400+
4401+
var api = new SpotifyWebApi({
4402+
clientId: clientId,
4403+
clientSecret: clientSecret
4404+
});
4405+
4406+
api.clientCredentialsGrant().then(
4407+
function(data) {
4408+
done();
4409+
},
4410+
function(err) {
4411+
done(err);
4412+
}
4413+
);
4414+
});
4415+
4416+
test('should retrieve an access token using the authorization code flow', function(done) {
4417+
sinon.stub(HttpManager, '_makeRequest').callsFake(function(
4418+
method,
4419+
options,
4420+
uri,
4421+
callback
4422+
) {
4423+
expect(method).toBe(superagent.post);
4424+
expect(uri).toBe(
4425+
'https://accounts.spotify.com/api/token'
4426+
);
4427+
expect(options.data.grant_type).toBe('authorization_code');
4428+
expect(options.data.redirect_uri).toBe('http://www.michaelthelin.se/test-callback');
4429+
expect(options.data.code).toBe('mySuperLongCode');
4430+
expect(options.data.client_id).toBe('someClientId');
4431+
expect(options.data.client_secret).toBe('someClientSecret');
4432+
callback(null, {
4433+
statusCode: 200
4434+
})
4435+
});
4436+
4437+
var credentials = {
4438+
clientId: 'someClientId',
4439+
clientSecret: 'someClientSecret',
4440+
redirectUri: 'http://www.michaelthelin.se/test-callback'
4441+
};
4442+
4443+
var api = new SpotifyWebApi(credentials);
4444+
4445+
api.authorizationCodeGrant('mySuperLongCode').then(
4446+
function(data) {
4447+
done();
4448+
},
4449+
function(err) {
4450+
console.log(err);
4451+
done(err);
4452+
}
4453+
);
4454+
});
4455+
4456+
test('should refresh token', function(done) {
4457+
sinon.stub(HttpManager, '_makeRequest').callsFake(function(
4458+
method,
4459+
options,
4460+
uri,
4461+
callback
4462+
) {
4463+
expect(method).toBe(superagent.post);
4464+
expect(uri).toBe(
4465+
'https://accounts.spotify.com/api/token'
4466+
);
4467+
expect(options.data.grant_type).toBe('refresh_token');
4468+
expect(options.data.refresh_token).toBe('myRefreshToken');
4469+
callback(null, {
4470+
statusCode: 200
4471+
})
4472+
});
4473+
4474+
var api = new SpotifyWebApi();
4475+
api.setRefreshToken('myRefreshToken');
4476+
4477+
api.refreshAccessToken().then(
4478+
function(data) {
4479+
done();
4480+
},
4481+
function(err) {
4482+
console.log(err);
4483+
done(err);
4484+
}
4485+
);
4486+
});
4487+
44504488
});

0 commit comments

Comments
 (0)