Skip to content

Instagram: Support passing in API url #6398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions spec/AuthenticationAdapters.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
30 changes: 13 additions & 17 deletions src/Adapters/Auth/instagram.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
// 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.
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,
};