Skip to content
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

Add test cases for FacebookUtils #779

Merged
merged 6 commits into from
Apr 2, 2019
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
3 changes: 3 additions & 0 deletions integration/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const api = new ParseServer({
module: CustomAuth,
option1: 'hello',
option2: 'world',
},
facebook: {
appIds: "test"
}
}
});
Expand Down
47 changes: 47 additions & 0 deletions integration/test/ParseUserTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ const provider = {
};
Parse.User._registerAuthenticationProvider(provider);

const authResponse = {
userID: 'test',
accessToken: 'test',
expiresIn: 'test', // Should be unix timestamp
};
global.FB = {
init: () => {},
login: (cb) => {
cb({ authResponse });
},
getAuthResponse: () => authResponse,
};

describe('Parse User', () => {
beforeAll(() => {
Parse.initialize('integration', null, 'notsosecret');
Expand Down Expand Up @@ -673,4 +686,38 @@ describe('Parse User', () => {
await user._unlinkFrom(provider);
expect(user._isLinked(provider)).toBe(false);
});

it('can login with facebook', async () => {
Parse.User.enableUnsafeCurrentUser();
Parse.FacebookUtils.init();
const user = await Parse.FacebookUtils.logIn();
expect(Parse.FacebookUtils.isLinked(user)).toBe(true);
});

it('can link user with facebook', async () => {
Parse.User.enableUnsafeCurrentUser();
Parse.FacebookUtils.init();
const user = new Parse.User();
user.setUsername('Alice');
user.setPassword('sekrit');
await user.signUp();
await Parse.FacebookUtils.link(user);
expect(Parse.FacebookUtils.isLinked(user)).toBe(true);
await Parse.FacebookUtils.unlink(user);
expect(Parse.FacebookUtils.isLinked(user)).toBe(false);
});

it('can link anonymous user with facebook', async () => {
Parse.User.enableUnsafeCurrentUser();
Parse.FacebookUtils.init();
const user = await Parse.AnonymousUtils.logIn();
await Parse.FacebookUtils.link(user);

expect(Parse.FacebookUtils.isLinked(user)).toBe(true);
expect(Parse.AnonymousUtils.isLinked(user)).toBe(true);
await Parse.FacebookUtils.unlink(user);

expect(Parse.FacebookUtils.isLinked(user)).toBe(false);
expect(Parse.AnonymousUtils.isLinked(user)).toBe(true);
});
});
51 changes: 29 additions & 22 deletions src/FacebookUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,23 @@ const FacebookUtils = {
* SDK to authenticate the user, and then automatically logs in (or
* creates, in the case where it is a new user) a Parse.User.
*
* Standard API:
*
* <code>logIn(permission: string, authData: Object);</code>
*
* Advanced API: Used for handling your own oAuth tokens
* {@link https://docs.parseplatform.org/rest/guide/#linking-users}
*
* <code>logIn(authData: Object, options?: Object);</code>
*
* @method logIn
* @name Parse.FacebookUtils.logIn
* @param {(String|Object)} permissions The permissions required for Facebook
* log in. This is a comma-separated string of permissions.
* Alternatively, supply a Facebook authData object as described in our
* REST API docs if you want to handle getting facebook auth tokens
* yourself.
* @param {Object} options Standard options object with success and error
* callbacks.
* @param {Object} options MasterKey / SessionToken. Alternatively can be used for authData if permissions is a string
* @returns {Promise}
*/
logIn(permissions, options) {
Expand All @@ -163,23 +171,25 @@ const FacebookUtils = {
}
requestedPermissions = permissions;
return ParseUser._logInWith('facebook', options);
} else {
const newOptions = {};
if (options) {
for (const key in options) {
newOptions[key] = options[key];
}
}
newOptions.authData = permissions;
return ParseUser._logInWith('facebook', newOptions);
}
const authData = { authData: permissions };
return ParseUser._logInWith('facebook', authData, options);
},

/**
* Links Facebook to an existing PFUser. This method delegates to the
* Facebook SDK to authenticate the user, and then automatically links
* the account to the Parse.User.
*
* Standard API:
*
* <code>link(user: Parse.User, permission: string, authData?: Object);</code>
*
* Advanced API: Used for handling your own oAuth tokens
* {@link https://docs.parseplatform.org/rest/guide/#linking-users}
*
* <code>link(user: Parse.User, authData: Object, options?: FullOptions);</code>
*
* @method link
* @name Parse.FacebookUtils.link
* @param {Parse.User} user User to link to Facebook. This must be the
Expand All @@ -189,8 +199,7 @@ const FacebookUtils = {
* Alternatively, supply a Facebook authData object as described in our
* REST API docs if you want to handle getting facebook auth tokens
* yourself.
* @param {Object} options Standard options object with success and error
* callbacks.
* @param {Object} options MasterKey / SessionToken. Alternatively can be used for authData if permissions is a string
* @returns {Promise}
*/
link(user, permissions, options) {
Expand All @@ -202,16 +211,9 @@ const FacebookUtils = {
}
requestedPermissions = permissions;
return user._linkWith('facebook', options);
} else {
const newOptions = {};
if (options) {
for (const key in options) {
newOptions[key] = options[key];
}
}
newOptions.authData = permissions;
return user._linkWith('facebook', newOptions);
}
const authData = { authData: permissions };
return user._linkWith('facebook', authData, options);
},

/**
Expand All @@ -232,6 +234,11 @@ const FacebookUtils = {
);
}
return user._unlinkFrom('facebook', options);
},

// Used for testing purposes
_getAuthProvider() {
return provider;
}
};

Expand Down
Loading