diff --git a/spec/AuthenticationAdapters.spec.js b/spec/AuthenticationAdapters.spec.js index 008decd6..1dfd190e 100644 --- a/spec/AuthenticationAdapters.spec.js +++ b/spec/AuthenticationAdapters.spec.js @@ -590,6 +590,41 @@ describe('AuthenticationProviders', function() { }); }); +describe('instagram auth adapter', () => { + const instagram = require('../lib/Adapters/Auth/instagram'); + const httpsRequest = require('../lib/Adapters/Auth/httpsRequest'); + + it('should use default api', async () => { + spyOn(httpsRequest, 'get').and.callFake(() => { + return Promise.resolve({ data: { id: 'userId' } }); + }); + await instagram.validateAuthData( + { id: 'userId', access_token: 'the_token' }, + {} + ); + expect(httpsRequest.get).toHaveBeenCalledWith( + 'https://api.instagram.com/v1/users/self/?access_token=the_token' + ); + }); + + it('should pass in api url', async () => { + spyOn(httpsRequest, 'get').and.callFake(() => { + return Promise.resolve({ data: { id: 'userId' } }); + }); + await instagram.validateAuthData( + { + id: 'userId', + access_token: 'the_token', + apiURL: 'https://new-api.instagram.com/v1/', + }, + {} + ); + expect(httpsRequest.get).toHaveBeenCalledWith( + 'https://new-api.instagram.com/v1/users/self/?access_token=the_token' + ); + }); +}); + describe('google auth adapter', () => { const google = require('../lib/Adapters/Auth/google'); const httpsRequest = require('../lib/Adapters/Auth/httpsRequest'); diff --git a/src/Adapters/Auth/instagram.js b/src/Adapters/Auth/instagram.js index ee6302c1..4cbeba64 100644 --- a/src/Adapters/Auth/instagram.js +++ b/src/Adapters/Auth/instagram.js @@ -1,20 +1,21 @@ // Helper functions for accessing the instagram API. var Parse = require('parse/node').Parse; const httpsRequest = require('./httpsRequest'); +const defaultURL = 'https://api.instagram.com/v1/'; // Returns a promise that fulfills iff this user id is valid. function validateAuthData(authData) { - return request('users/self/?access_token=' + authData.access_token).then( - response => { - if (response && response.data && response.data.id == authData.id) { - return; - } - throw new Parse.Error( - Parse.Error.OBJECT_NOT_FOUND, - 'Instagram auth is invalid for this user.' - ); + const apiURL = authData.apiURL || defaultURL; + const path = `${apiURL}users/self/?access_token=${authData.access_token}`; + return httpsRequest.get(path).then(response => { + if (response && response.data && response.data.id == authData.id) { + return; } - ); + throw new Parse.Error( + Parse.Error.OBJECT_NOT_FOUND, + 'Instagram auth is invalid for this user.' + ); + }); } // Returns a promise that fulfills iff this app id is valid. @@ -22,12 +23,7 @@ function validateAppId() { return Promise.resolve(); } -// A promisey wrapper for api requests -function request(path) { - return httpsRequest.get('https://api.instagram.com/v1/' + path); -} - module.exports = { - validateAppId: validateAppId, - validateAuthData: validateAuthData, + validateAppId, + validateAuthData, };