UNPKG

groove-music-js

Version:
1,255 lines (1,080 loc) 107 kB
var superagent = require('superagent'), HttpManager = require('../src/http-wrapper'), sinon = require('sinon'), GrooveMusicWebApi = require('../src/app'), WebApiError = require('../src/http-error'), should = require('should'); 'use strict'; describe('Groove Music Web API', function () { beforeEach(function (done) { done(); }); afterEach(function (done) { if (typeof HttpManager._makeRequest.restore == 'function') { HttpManager._makeRequest.restore(); } done(); }); this.timeout(5000); it('should set clientId, clientSecret and redirectUri', function () { var credentials = { clientId: 'myClientID', clientSecret: 'myClientSecret', redirectUri: 'myRedirectUri', accessToken: 'mySuperNiceAccessToken', refreshToken: 'iCanEvenSaveMyAccessToken' }; var api = new GrooveMusicWebApi(credentials); api.getCredentials().clientId.should.equal(credentials.clientId); api.getCredentials().clientSecret.should.equal(credentials.clientSecret); api.getCredentials().redirectUri.should.equal(credentials.redirectUri); api.getCredentials().accessToken.should.equal(credentials.accessToken); api.getCredentials().refreshToken.should.equal(credentials.refreshToken); }); it("should retrieve track metadata", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.media.microsoft.com/1/content/music.A83EB907-0100-11DB-89CA-0019B92A3933/lookup'); should.not.exist(options.data); callback(null, { body: { "uri": "spotify:track:3Qm86XLflmIXVm1wcwkgDK" }, headers: { "cache-control": "public, max-age=7200" }, statusCode: 200 }); }); var api = new GrooveMusicWebApi(); api.getTrack('music.A83EB907-0100-11DB-89CA-0019B92A3933') .then(function (data) { data.body.uri.should.equal('spotify:track:3Qm86XLflmIXVm1wcwkgDK'); (data.statusCode).should.equal(200); (data.headers['cache-control']).should.equal('public, max-age=7200'); done(); }, function (err) { done(err); }); }); it("should retrieve error when retrieving track metadata", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.media.microsoft.com/1/content/music.A83EB907-0100-11DB-89CA-0019B92A39K/lookup'); should.not.exist(options.data); callback(new WebApiError("Do NOT do that again!", 400)); }); var api = new GrooveMusicWebApi(); api.getTrack('music.A83EB907-0100-11DB-89CA-0019B92A39K') .then(function (data) { done(new Error("Test failed!")); }, function (err) { (err.statusCode).should.equal(400); err.message.should.equal("Do NOT do that again!"); done(); }); }); it("should retrieve track metadata using callback", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.media.microsoft.com/1/content/music.A83EB907-0100-11DB-89CA-0019B92A39K/lookup'); should.not.exist(options.data); callback(); }); var api = new GrooveMusicWebApi(); api.getTrack('music.A83EB907-0100-11DB-89CA-0019B92A39K', function (err, data) { should.not.exist(err); done(err); }); }); it("should fail for non existing track id", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); callback(new WebApiError('non existing id', 400)); }); var api = new GrooveMusicWebApi(); api.getTrack('idontexist') .then(function (data) { done(new Error('Should have failed')); }, function (err) { 'non existing id'.should.equal(err.message); done(); }); }); it("should fail for non existing track id using callback", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); callback(new WebApiError('non existing id', 400), null); }); var api = new GrooveMusicWebApi(); api.getTrack('idontexist', function (err, data) { should.not.exist(data); should.exist(err); 'non existing id'.should.equal(err.message); done(); }); }); it('should fail for empty track id', function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); callback(new WebApiError('Fail', 400), null); }); var api = new GrooveMusicWebApi(); api.getTrack() .then(function (data) { done(new Error('Should have failed')); }, function (err) { should.exist(err); done(); }); }); it("should retrieve metadata for several tracks", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.media.microsoft.com/1/content/music.A83EB907-0100-11DB-89CA-0019B92A3933+music.B13EB907-0100-11DB-89CA-0019B92A3933/lookup'); should.not.exist(options.data); callback(); }); var api = new GrooveMusicWebApi(); api.getTracks(['music.A83EB907-0100-11DB-89CA-0019B92A3933', 'music.B13EB907-0100-11DB-89CA-0019B92A3933']) .then(function (data) { done(); }, function (err) { done(err); }); }); it("should retrieve metadata for several tracks using callback", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.media.microsoft.com/1/content/music.A83EB907-0100-11DB-89CA-0019B92A3933+music.B13EB907-0100-11DB-89CA-0019B92A3933/lookup'); should.not.exist(options.data); callback(); }); var api = new GrooveMusicWebApi(); api.getTracks(['music.A83EB907-0100-11DB-89CA-0019B92A3933', 'music.B13EB907-0100-11DB-89CA-0019B92A3933'], function (err, data) { should.not.exist(err); done(); }); }); it("should retrieve metadata for an album", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/albums/0sNOF9WDwhWunNAHPD3Baj'); should.not.exist(options.data); callback(null, { body: { uri: 'spotify:album:0sNOF9WDwhWunNAHPD3Baj' }, statusCode: 200 }); }); var api = new GrooveMusicWebApi(); api.getAlbum('0sNOF9WDwhWunNAHPD3Baj') .then(function (data) { ('spotify:album:0sNOF9WDwhWunNAHPD3Baj').should.equal(data.body.uri); (200).should.equal(data.statusCode); done(); }, function (err) { done(err); }); }); it("should retrieve metadata for an album for a market ", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/albums/0sNOF9WDwhWunNAHPD3Baj'); should.not.exist(options.data); options.query.market.should.equal('SE'); callback(null, { body: { uri: 'spotify:album:0sNOF9WDwhWunNAHPD3Baj' }, statusCode: 200 }); }); var api = new GrooveMusicWebApi(); api.getAlbum('0sNOF9WDwhWunNAHPD3Baj', { market: 'SE' }) .then(function (data) { ('spotify:album:0sNOF9WDwhWunNAHPD3Baj').should.equal(data.body.uri); (200).should.equal(data.statusCode); done(); }, function (err) { done(err); }); }); it("should retrieve metadata for an album using callback", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/albums/0sNOF9WDwhWunNAHPD3Baj'); should.not.exist(options.data); callback(null, { body: { uri: 'spotify:album:0sNOF9WDwhWunNAHPD3Baj' }, statusCode: 200 }); }); var api = new GrooveMusicWebApi(); api.getAlbum('0sNOF9WDwhWunNAHPD3Baj', function (err, data) { should.not.exist(err); ('spotify:album:0sNOF9WDwhWunNAHPD3Baj').should.equal(data.body.uri); (200).should.equal(data.statusCode); done(); }); }); it("should retrieve metadata for several albums", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/albums'); options.query.ids.should.equal('41MnTivkwTO3UUJ8DrqEJJ,6JWc4iAiJ9FjyK0B59ABb4'); should.not.exist(options.data); callback(null, { body: { albums: [ { uri: 'spotify:album:41MnTivkwTO3UUJ8DrqEJJ' }, { uri: 'spotify:album:6JWc4iAiJ9FjyK0B59ABb4' } ] }, statusCode: 200 }); }); var api = new GrooveMusicWebApi(); api.getAlbums(['41MnTivkwTO3UUJ8DrqEJJ', '6JWc4iAiJ9FjyK0B59ABb4']) .then(function (data) { 'spotify:album:41MnTivkwTO3UUJ8DrqEJJ'.should.equal(data.body.albums[0].uri); 'spotify:album:6JWc4iAiJ9FjyK0B59ABb4'.should.equal(data.body.albums[1].uri); (200).should.equal(data.statusCode); done(); }, function (err) { done(err); }); }); it("should retrieve metadata for several albums using callback", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/albums'); options.query.ids.should.equal('41MnTivkwTO3UUJ8DrqEJJ,6JWc4iAiJ9FjyK0B59ABb4'); should.not.exist(options.data); callback(null, { body: { albums: [ { uri: 'spotify:album:41MnTivkwTO3UUJ8DrqEJJ' }, { uri: 'spotify:album:6JWc4iAiJ9FjyK0B59ABb4' } ] }, statusCode: 200 }); }); var api = new GrooveMusicWebApi(); api.getAlbums(['41MnTivkwTO3UUJ8DrqEJJ', '6JWc4iAiJ9FjyK0B59ABb4'], function (err, data) { should.not.exist(err); 'spotify:album:41MnTivkwTO3UUJ8DrqEJJ'.should.equal(data.body.albums[0].uri); 'spotify:album:6JWc4iAiJ9FjyK0B59ABb4'.should.equal(data.body.albums[1].uri); (200).should.equal(data.statusCode); done(); }); }); it("should retrive metadata for an artist", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/artists/0LcJLqbBmaGUft1e9Mm8HV'); should.not.exist(options.data); callback(null, { body: { uri: 'spotify:artist:0LcJLqbBmaGUft1e9Mm8HV' } }); }); var api = new GrooveMusicWebApi(); api.getArtist('0LcJLqbBmaGUft1e9Mm8HV') .then(function (data) { ('spotify:artist:0LcJLqbBmaGUft1e9Mm8HV').should.equal(data.body.uri); done(); }, function (err) { done(err); }); }); it("should retrieve metadata for an artist using callback", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/artists/0LcJLqbBmaGUft1e9Mm8HV'); should.not.exist(options.data); callback(null, { body: { uri: 'spotify:artist:0LcJLqbBmaGUft1e9Mm8HV' } }); }); var api = new GrooveMusicWebApi(); api.getArtist('0LcJLqbBmaGUft1e9Mm8HV', function (err, data) { should.not.exist(err); ('spotify:artist:0LcJLqbBmaGUft1e9Mm8HV').should.equal(data.body.uri); done(); }); }); it("should retrieve metadata for several artists", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/artists'); options.query.ids.should.equal('0oSGxfWSnnOXhD2fKuz2Gy,3dBVyJ7JuOMt4GE9607Qin'); should.not.exist(options.data); callback(null, { body: { artists: [ { uri: 'spotify:artist:0oSGxfWSnnOXhD2fKuz2Gy' }, { uri: 'spotify:artist:3dBVyJ7JuOMt4GE9607Qin' } ] }, statusCode: 200 }); }); var api = new GrooveMusicWebApi(); api.getArtists(['0oSGxfWSnnOXhD2fKuz2Gy', '3dBVyJ7JuOMt4GE9607Qin']) .then(function (data) { 'spotify:artist:0oSGxfWSnnOXhD2fKuz2Gy'.should.equal(data.body.artists[0].uri); 'spotify:artist:3dBVyJ7JuOMt4GE9607Qin'.should.equal(data.body.artists[1].uri); (200).should.equal(data.statusCode); done(); }, function (err) { done(err); }); }); it("should retrieve metadata for several artists using callback", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/artists'); options.query.ids.should.equal('0oSGxfWSnnOXhD2fKuz2Gy,3dBVyJ7JuOMt4GE9607Qin'); should.not.exist(options.data); callback(null, { body: { artists: [ { uri: 'spotify:artist:0oSGxfWSnnOXhD2fKuz2Gy' }, { uri: 'spotify:artist:3dBVyJ7JuOMt4GE9607Qin' } ] }, statusCode: 200 }); }); var api = new GrooveMusicWebApi(); api.getArtists(['0oSGxfWSnnOXhD2fKuz2Gy', '3dBVyJ7JuOMt4GE9607Qin'], function (err, data) { should.not.exist(err); 'spotify:artist:0oSGxfWSnnOXhD2fKuz2Gy'.should.equal(data.body.artists[0].uri); 'spotify:artist:3dBVyJ7JuOMt4GE9607Qin'.should.equal(data.body.artists[1].uri); (200).should.equal(data.statusCode); done(); }); }); it("should search for an album using limit and offset", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/search/'); options.query.should.eql({ limit: 3, offset: 2, q: 'The Best of Keane', type: 'album' }); should.not.exist(options.data); callback(null, { body: { albums: { href: 'https://api.spotify.com/v1/search?query=The+Best+of+Keane&offset=2&limit=3&type=album' } }, headers: { 'test': 'value' }, statusCode: 200 }); }); var api = new GrooveMusicWebApi(); api.searchAlbums('The Best of Keane', { limit: 3, offset: 2 }) .then(function (data) { 'https://api.spotify.com/v1/search?query=The+Best+of+Keane&offset=2&limit=3&type=album'.should.equal(data.body.albums.href); (200).should.equal(data.statusCode); 'value'.should.equal(data.headers.test); done(); }, function (err) { console.log(err); done(err); }); }); it("should search for an album using limit and offset using callback", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/search/'); options.query.should.eql({ limit: 3, offset: 2, q: 'The Best of Keane', type: 'album' }); should.not.exist(options.data); callback(null, { body: { albums: { href: 'https://api.spotify.com/v1/search?query=The+Best+of+Keane&offset=2&limit=3&type=album' } } }); }); var api = new GrooveMusicWebApi(); api.searchAlbums('The Best of Keane', { limit: 3, offset: 2 }, function (err, data) { should.not.exist(err); 'https://api.spotify.com/v1/search?query=The+Best+of+Keane&offset=2&limit=3&type=album'.should.equal(data.body.albums.href); done(); }); }); it("should search for playlists", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/search/'); options.query.should.eql({ limit: 1, offset: 0, q: 'workout', type: 'playlist' }); should.not.exist(options.data); callback(null, { body: { playlists: { href: 'https://api.spotify.com/v1/search?query=workout&offset=0&limit=1&type=playlist' } } }); }); var api = new GrooveMusicWebApi(); api.searchPlaylists('workout', { limit: 1, offset: 0 }) .then(function (data) { 'https://api.spotify.com/v1/search?query=workout&offset=0&limit=1&type=playlist'.should.equal(data.body.playlists.href); done(); }, function (err) { console.log(err); done(err); }); }); it("should search for an artist using limit and offset", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/search/'); options.query.should.eql({ limit: 5, offset: 1, q: 'David Bowie', type: 'artist' }); should.not.exist(options.data); callback(null, { body: { artists: { href: 'https://api.spotify.com/v1/search?query=David+Bowie&offset=1&limit=5&type=artist' } } }); }); var api = new GrooveMusicWebApi(); api.searchArtists('David Bowie', { limit: 5, offset: 1 }) .then(function (data) { 'https://api.spotify.com/v1/search?query=David+Bowie&offset=1&limit=5&type=artist'.should.equal(data.body.artists.href); done(); }, function (err) { done(err); }); }); it("should search for an artist using limit and offset using callback", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/search/'); options.query.should.eql({ limit: 5, offset: 1, q: 'David Bowie', type: 'artist' }); should.not.exist(options.data); callback(null, { body: { artists: { href: 'https://api.spotify.com/v1/search?query=David+Bowie&offset=1&limit=5&type=artist' } } }); }); var api = new GrooveMusicWebApi(); api.searchArtists('David Bowie', { limit: 5, offset: 1 }, function (err, data) { should.not.exist(err); 'https://api.spotify.com/v1/search?query=David+Bowie&offset=1&limit=5&type=artist'.should.equal(data.body.artists.href); done(); }); }); it("should search for a track using limit and offset", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/search/'); options.query.should.eql({ limit: 3, offset: 2, q: 'Mr. Brightside', type: 'track' }); should.not.exist(options.data); callback(null, { body: { tracks: { href: 'https://api.spotify.com/v1/search?query=Mr.+Brightside&offset=2&limit=3&type=track' } } }); }); var api = new GrooveMusicWebApi(); api.searchTracks('Mr. Brightside', { limit: 3, offset: 2 }) .then(function (data) { 'https://api.spotify.com/v1/search?query=Mr.+Brightside&offset=2&limit=3&type=track'.should.equal(data.body.tracks.href); done(); }, function (err) { console.log(err); done(err); }); }); it("should search for a track using limit and offset using callback", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/search/'); options.query.should.eql({ limit: 3, offset: 2, q: 'Mr. Brightside', type: 'track' }); should.not.exist(options.data); callback(null, { body: { tracks: { href: 'https://api.spotify.com/v1/search?query=Mr.+Brightside&offset=2&limit=3&type=track' } } }); }); var api = new GrooveMusicWebApi(); api.searchTracks('Mr. Brightside', { limit: 3, offset: 2 }, function (err, data) { should.not.exist(err); 'https://api.spotify.com/v1/search?query=Mr.+Brightside&offset=2&limit=3&type=track'.should.equal(data.body.tracks.href); done(); }); }); it("should search for several types using callback", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/search/'); options.query.should.eql({ limit: 3, offset: 2, q: 'Mr. Brightside', type: 'track,album' }); should.not.exist(options.data); callback(null, { body: { tracks: { href: 'https://api.spotify.com/v1/search?query=Mr.+Brightside&offset=2&limit=3&type=track,album' } } }); }); var api = new GrooveMusicWebApi(); api.search('Mr. Brightside', ['track', 'album'], { limit: 3, offset: 2 }, function (err, data) { should.not.exist(err); 'https://api.spotify.com/v1/search?query=Mr.+Brightside&offset=2&limit=3&type=track,album'.should.equal(data.body.tracks.href); done(); }); }); it("should get artists albums", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/artists/0oSGxfWSnnOXhD2fKuz2Gy/albums'); options.query.should.eql({ album_type: 'album', country: 'GB', limit: 2, offset: 5 }); should.not.exist(options.data); callback(null, { body: { href: 'https://api.spotify.com/v1/artists/0oSGxfWSnnOXhD2fKuz2Gy/albums?offset=5&limit=2&album_type=album&market=GB' } }); }); var api = new GrooveMusicWebApi(); api.getArtistAlbums('0oSGxfWSnnOXhD2fKuz2Gy', { album_type: 'album', country: 'GB', limit: 2, offset: 5 }) .then(function (data) { 'https://api.spotify.com/v1/artists/0oSGxfWSnnOXhD2fKuz2Gy/albums?offset=5&limit=2&album_type=album&market=GB'.should.equal(data.body.href); done(); }, function (err) { console.log(err); done(err); }); }); it("should get artists albums using callback", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/artists/0oSGxfWSnnOXhD2fKuz2Gy/albums'); options.query.should.eql({ album_type: 'album', country: 'GB', limit: 2, offset: 5 }); should.not.exist(options.data); callback(null, { body: { href: 'https://api.spotify.com/v1/artists/0oSGxfWSnnOXhD2fKuz2Gy/albums?offset=5&limit=2&album_type=album&market=GB' } }); }); var api = new GrooveMusicWebApi(); api.getArtistAlbums('0oSGxfWSnnOXhD2fKuz2Gy', { album_type: 'album', country: 'GB', limit: 2, offset: 5 }, function (err, data) { should.not.exist(err); 'https://api.spotify.com/v1/artists/0oSGxfWSnnOXhD2fKuz2Gy/albums?offset=5&limit=2&album_type=album&market=GB'.should.equal(data.body.href); done(); }); }); it("should get tracks from album", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/albums/41MnTivkwTO3UUJ8DrqEJJ/tracks'); options.query.should.eql({ offset: 1, limit: 5 }); should.not.exist(options.data); callback(null, { body: { href: 'https://api.spotify.com/v1/albums/41MnTivkwTO3UUJ8DrqEJJ/tracks?offset=1&limit=5' } }); }); var api = new GrooveMusicWebApi(); api.getAlbumTracks('41MnTivkwTO3UUJ8DrqEJJ', { limit: 5, offset: 1 }) .then(function (data) { 'https://api.spotify.com/v1/albums/41MnTivkwTO3UUJ8DrqEJJ/tracks?offset=1&limit=5'.should.equal(data.body.href); done(); }, function (err) { done(err); }); }); it("should get tracks from album using callback", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/albums/41MnTivkwTO3UUJ8DrqEJJ/tracks'); options.query.should.eql({ offset: 1, limit: 5 }); should.not.exist(options.data); callback(null, { body: { href: 'https://api.spotify.com/v1/albums/41MnTivkwTO3UUJ8DrqEJJ/tracks?offset=1&limit=5' } }); }); var api = new GrooveMusicWebApi(); api.getAlbumTracks('41MnTivkwTO3UUJ8DrqEJJ', { limit: 5, offset: 1 }, function (err, data) { should.not.exist(err); 'https://api.spotify.com/v1/albums/41MnTivkwTO3UUJ8DrqEJJ/tracks?offset=1&limit=5'.should.equal(data.body.href); done(); }); }); it("should get top tracks for artist", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/artists/0oSGxfWSnnOXhD2fKuz2Gy/top-tracks'); options.query.should.eql({ country: 'GB' }); should.not.exist(options.data); callback(); }); var api = new GrooveMusicWebApi(); api.getArtistTopTracks('0oSGxfWSnnOXhD2fKuz2Gy', 'GB') .then(function (data) { done(); }, function (err) { done(err); }); }); it("should get top tracks for artist", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/artists/0oSGxfWSnnOXhD2fKuz2Gy/top-tracks'); options.query.should.eql({ country: 'GB' }); should.not.exist(options.data); callback(); }); var api = new GrooveMusicWebApi(); api.getArtistTopTracks('0oSGxfWSnnOXhD2fKuz2Gy', 'GB', function (err, data) { should.not.exist(err); done(); }); }); it("should get similar artists", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/artists/0qeei9KQnptjwb8MgkqEoy/related-artists'); should.not.exist(options.data); callback(null, { body: { artists: [{}] } }); }); var api = new GrooveMusicWebApi(); api.getArtistRelatedArtists('0qeei9KQnptjwb8MgkqEoy') .then(function (data) { should.exist(data.body.artists); done(); }, function (err) { done(err); }); }); it("should get similar artists using callback", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/artists/0qeei9KQnptjwb8MgkqEoy/related-artists'); should.not.exist(options.data); callback(null, { body: { artists: [{}] } }); }); var api = new GrooveMusicWebApi(); api.getArtistRelatedArtists('0qeei9KQnptjwb8MgkqEoy', function (err, data) { should.exist(data.body.artists); done(); }); }); it("should get a user", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/users/petteralexis'); should.not.exist(options.data); callback(null, { body: { uri: 'spotify:user:petteralexis' } }); }); var api = new GrooveMusicWebApi(); api.getUser('petteralexis') .then(function (data) { 'spotify:user:petteralexis'.should.equal(data.body.uri); done(); }, function (err) { done(err); }); }); it("should get a user with a '#' character and encode it properly", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/users/%23matze23'); should.not.exist(options.data); callback(null, { body: { uri: 'spotify:user:%23matze23' } }); }); var api = new GrooveMusicWebApi(); api.getUser('#matze23') .then(function (data) { 'spotify:user:%23matze23'.should.equal(data.body.uri); done(); }, function (err) { done(err); }); }); it("should get a user using callback", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/users/petteralexis'); should.not.exist(options.data); callback(null, { body: { uri: 'spotify:user:petteralexis' } }); }); var api = new GrooveMusicWebApi(); api.getUser('petteralexis', function (err, data) { 'spotify:user:petteralexis'.should.equal(data.body.uri); done(); }); }); it("should get the authenticated user's information", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/me'); options.headers.should.eql({ Authorization: 'Bearer someAccessToken' }); callback(null, { body: { uri: 'spotify:user:thelinmichael' } }); }); var api = new GrooveMusicWebApi({ accessToken: 'someAccessToken' }); api.getMe() .then(function (data) { 'spotify:user:thelinmichael'.should.equal(data.body.uri); done(); }); }); it("should get the authenticated user's information with accesstoken set on the api object", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/me'); options.headers.should.eql({ Authorization: 'Bearer someAccessToken' }); callback(null, { body: { uri: 'spotify:user:thelinmichael' } }); }); var api = new GrooveMusicWebApi(); api.setAccessToken('someAccessToken'); api.getMe() .then(function (data) { 'spotify:user:thelinmichael'.should.equal(data.body.uri); done(); }); }); it('should fail if no token is provided for a request that requires an access token', function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/me'); if (!options.headers || !options.headers.Authorization) { callback(new WebApiError('No token', 401), null); } }); var api = new GrooveMusicWebApi(); api.getMe() .then(function (data) { done(new Error('Should have failed!')); }, function (err) { 'No token'.should.equal(err.message); (401).should.equal(err.statusCode); done(); }); }); it('should fail if no token is provided for a request that requires an access token using callback', function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/me'); if (!options.headers || !options.headers.Authorization) { callback(new WebApiError('No token', 401), null); } }); var api = new GrooveMusicWebApi(); api.getMe(function (err) { 'No token'.should.equal(err.message); (401).should.equal(err.statusCode); should.exist(err); done(); }); }); it('should get a users playlists', function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/users/thelinmichael/playlists'); should.not.exist(options.query); callback(null, { body: { items: [ { uri: 'spotify:user:thelinmichael:playlist:5ieJqeLJjjI8iJWaxeBLuK' }, { uri: 'spotify:user:thelinmichael:playlist:3EsfV6XzCHU8SPNdbnFogK' } ] }, statusCode: 200 }); }); var api = new GrooveMusicWebApi(); api.setAccessToken('myVeryLongAccessToken'); api.getUserPlaylists('thelinmichael') .then(function (data) { (2).should.equal(data.body.items.length); (200).should.equal(data.statusCode); done(); }); }); it('should get the current users playlists', function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/me/playlists'); should.not.exist(options.query); callback(null, { body: { items: [ { uri: 'spotify:user:thelinmichael:playlist:5ieJqeLJjjI8iJWaxeBLuK' }, { uri: 'spotify:user:thelinmichael:playlist:3EsfV6XzCHU8SPNdbnFogK' } ] }, statusCode: 200 }); }); var api = new GrooveMusicWebApi(); api.setAccessToken('myVeryLongAccessToken'); api.getUserPlaylists() .then(function (data) { (2).should.equal(data.body.items.length); (200).should.equal(data.statusCode); done(); }); }); it('should get a playlist', function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/users/thelinmichael/playlists/5ieJqeLJjjI8iJWaxeBLuK'); should.not.exist(options.query); callback(null, { body: { uri: 'spotify:user:thelinmichael:playlist:5ieJqeLJjjI8iJWaxeBLuK' }, statusCode: 200 }); }); var api = new GrooveMusicWebApi(); api.setAccessToken('myVeryVeryLongAccessToken'); api.getPlaylist('thelinmichael', '5ieJqeLJjjI8iJWaxeBLuK', {}, function (err, data) { 'spotify:user:thelinmichael:playlist:5ieJqeLJjjI8iJWaxeBLuK'.should.equal(data.body.uri); (200).should.equal(data.statusCode); done(); }); }); it.skip('should create a playlist', function (done) { var api = new GrooveMusicWebApi(); api.setAccessToken('long-access-token'); api.createPlaylist('thelinmichael', 'My Cool Playlist', { 'public': true }) .then(function (data) { done(); }, function (err) { console.log(err.error); done(err); }); }); it('should create a private playlist using callback', function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.post); uri.should.equal('https://api.spotify.com/v1/users/thelinmichael/playlists'); JSON.parse(options.data).should.eql({ name: 'My Cool Playlist', 'public': false }); should.not.exist(options.query); callback(null, { body: { name: 'My Cool Playlist', 'public': false }, statusCode: 200 }); }); var api = new GrooveMusicWebApi(); api.createPlaylist('thelinmichael', 'My Cool Playlist', { 'public': false }, function (err, data) { 'My Cool Playlist'.should.equal(data.body.name); (200).should.equal(data.statusCode); done(); }); }); it('should create a playlist using callback without options', function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.post); uri.should.equal('https://api.spotify.com/v1/users/thelinmichael/playlists'); JSON.parse(options.data).should.eql({ name: 'My Cool Playlist' }) callback(null, { body: { name: 'My Cool Playlist' } }); should.not.exist(options.query); }); var api = new GrooveMusicWebApi(); api.createPlaylist('thelinmichael', 'My Cool Playlist', function (err, data) { done(); }); }); it('should change playlist details', function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.put); uri.should.equal('https://api.spotify.com/v1/users/thelinmichael/playlists/5ieJqeLJjjI8iJWaxeBLuK'); JSON.parse(options.data).should.eql({ name: 'This is a new name for my Cool Playlist, and will become private', 'public': false }) callback(null, { statusCode: 200 }); should.not.exist(options.query); }); var api = new GrooveMusicWebApi(); api.setAccessToken('long-access-token'); api.changePlaylistDetails('thelinmichael', '5ieJqeLJjjI8iJWaxeBLuK', { name: 'This is a new name for my Cool Playlist, and will become private', 'public': false }).then(function (data) { (200).should.equal(data.statusCode); done(); }); }); it('should add tracks to playlist', function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.post); uri.should.equal('https://api.spotify.com/v1/users/thelinmichael/playlists/5ieJqeLJjjI8iJWaxeBLuK/tracks'); should.not.exist(options.query); JSON.parse(options.data)["uris"].should.be.an.instanceOf(Array).and.have.lengthOf(2); callback(null, { body: { snapshot_id: 'aSnapshotId' }, statusCode: 201 }); }); var api = new GrooveMusicWebApi(); api.setAccessToken('long-access-token'); api.addTracksToPlaylist('thelinmichael', '5ieJqeLJjjI8iJWaxeBLuK', ['spotify:track:4iV5W9uYEdYUVa79Axb7Rh', 'spotify:track:1301WleyT98MSxVHPZCA6M']) .then(function (data) { (201).should.equal(data.statusCode); done(); }); }); it('should add tracks to playlist with specified index', function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.post); JSON.parse(options.data)["uris"].should.be.an.instanceOf(Array).and.have.lengthOf(2); options.query.should.eql({ position: 10 }); callback(null, { body: { snapshot_id: 'aSnapshotId' }, statusCode: 201 }); }); var api = new GrooveMusicWebApi(); api.setAccessToken('long-access-token'); api.addTracksToPlaylist('thelinmichael', '5ieJqeLJjjI8iJWaxeBLuK', ["spotify:track:4iV5W9uYEdYUVa79Axb7Rh", "spotify:track:1301WleyT98MSxVHPZCA6M"], { position: 10 }) .then(function (data) { done(); }); }); it("should get user's top artists", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/me/top/artists'); options.query.should.eql({ limit: 5 }); options.headers.should.eql({ Authorization: 'Bearer someAccessToken' }); callback(null, { body: { items: [] } }); }); var api = new GrooveMusicWebApi({ accessToken: 'someAccessToken' }); api.getMyTopArtists({ limit: 5 }) .then(function (data) { should.exist(data.body.items); done(); }); }); it("should get user's top tracks", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/me/top/tracks'); options.query.should.eql({ limit: 5 }); options.headers.should.eql({ Authorization: 'Bearer someAccessToken' }); callback(null, { body: { items: [] } }); }); var api = new GrooveMusicWebApi({ accessToken: 'someAccessToken' }); api.getMyTopTracks({ limit: 5 }) .then(function (data) { should.exist(data.body.items); done(); }); }); it("should get user's recently played tracks:", function (done) { sinon.stub(HttpManager, '_makeRequest', function (method, options, uri, callback) { method.should.equal(superagent.get); uri.should.equal('https://api.spotify.com/v1/me/player/recently-played'); options.query.should.eql({ limit: 5 }); options.headers.should.eql({ Authorization: 'Bearer someAccessToken' }); callback(null, { body: { items: [] } }); }); var api = new GrooveMusicWebApi({ accessToken: 'someAccessToken' }); api.getMyRecentlyPlayedTracks({ limit: 5 }) .then(function (data) { should.exist(data.body.items); done();