Skip to content

Commit ba6c448

Browse files
authored
Add test cases for FacebookUtils (#779)
* Add facebook Options * use test credentials * remove fit * Improve docs
1 parent 02366c9 commit ba6c448

File tree

4 files changed

+376
-22
lines changed

4 files changed

+376
-22
lines changed

integration/server.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ const api = new ParseServer({
1818
module: CustomAuth,
1919
option1: 'hello',
2020
option2: 'world',
21+
},
22+
facebook: {
23+
appIds: "test"
2124
}
2225
}
2326
});

integration/test/ParseUserTest.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ const provider = {
3131
};
3232
Parse.User._registerAuthenticationProvider(provider);
3333

34+
const authResponse = {
35+
userID: 'test',
36+
accessToken: 'test',
37+
expiresIn: 'test', // Should be unix timestamp
38+
};
39+
global.FB = {
40+
init: () => {},
41+
login: (cb) => {
42+
cb({ authResponse });
43+
},
44+
getAuthResponse: () => authResponse,
45+
};
46+
3447
describe('Parse User', () => {
3548
beforeAll(() => {
3649
Parse.initialize('integration', null, 'notsosecret');
@@ -673,4 +686,38 @@ describe('Parse User', () => {
673686
await user._unlinkFrom(provider);
674687
expect(user._isLinked(provider)).toBe(false);
675688
});
689+
690+
it('can login with facebook', async () => {
691+
Parse.User.enableUnsafeCurrentUser();
692+
Parse.FacebookUtils.init();
693+
const user = await Parse.FacebookUtils.logIn();
694+
expect(Parse.FacebookUtils.isLinked(user)).toBe(true);
695+
});
696+
697+
it('can link user with facebook', async () => {
698+
Parse.User.enableUnsafeCurrentUser();
699+
Parse.FacebookUtils.init();
700+
const user = new Parse.User();
701+
user.setUsername('Alice');
702+
user.setPassword('sekrit');
703+
await user.signUp();
704+
await Parse.FacebookUtils.link(user);
705+
expect(Parse.FacebookUtils.isLinked(user)).toBe(true);
706+
await Parse.FacebookUtils.unlink(user);
707+
expect(Parse.FacebookUtils.isLinked(user)).toBe(false);
708+
});
709+
710+
it('can link anonymous user with facebook', async () => {
711+
Parse.User.enableUnsafeCurrentUser();
712+
Parse.FacebookUtils.init();
713+
const user = await Parse.AnonymousUtils.logIn();
714+
await Parse.FacebookUtils.link(user);
715+
716+
expect(Parse.FacebookUtils.isLinked(user)).toBe(true);
717+
expect(Parse.AnonymousUtils.isLinked(user)).toBe(true);
718+
await Parse.FacebookUtils.unlink(user);
719+
720+
expect(Parse.FacebookUtils.isLinked(user)).toBe(false);
721+
expect(Parse.AnonymousUtils.isLinked(user)).toBe(true);
722+
});
676723
});

src/FacebookUtils.js

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,23 @@ const FacebookUtils = {
143143
* SDK to authenticate the user, and then automatically logs in (or
144144
* creates, in the case where it is a new user) a Parse.User.
145145
*
146+
* Standard API:
147+
*
148+
* <code>logIn(permission: string, authData: Object);</code>
149+
*
150+
* Advanced API: Used for handling your own oAuth tokens
151+
* {@link https://docs.parseplatform.org/rest/guide/#linking-users}
152+
*
153+
* <code>logIn(authData: Object, options?: Object);</code>
154+
*
146155
* @method logIn
147156
* @name Parse.FacebookUtils.logIn
148157
* @param {(String|Object)} permissions The permissions required for Facebook
149158
* log in. This is a comma-separated string of permissions.
150159
* Alternatively, supply a Facebook authData object as described in our
151160
* REST API docs if you want to handle getting facebook auth tokens
152161
* yourself.
153-
* @param {Object} options Standard options object with success and error
154-
* callbacks.
162+
* @param {Object} options MasterKey / SessionToken. Alternatively can be used for authData if permissions is a string
155163
* @returns {Promise}
156164
*/
157165
logIn(permissions, options) {
@@ -163,23 +171,25 @@ const FacebookUtils = {
163171
}
164172
requestedPermissions = permissions;
165173
return ParseUser._logInWith('facebook', options);
166-
} else {
167-
const newOptions = {};
168-
if (options) {
169-
for (const key in options) {
170-
newOptions[key] = options[key];
171-
}
172-
}
173-
newOptions.authData = permissions;
174-
return ParseUser._logInWith('facebook', newOptions);
175174
}
175+
const authData = { authData: permissions };
176+
return ParseUser._logInWith('facebook', authData, options);
176177
},
177178

178179
/**
179180
* Links Facebook to an existing PFUser. This method delegates to the
180181
* Facebook SDK to authenticate the user, and then automatically links
181182
* the account to the Parse.User.
182183
*
184+
* Standard API:
185+
*
186+
* <code>link(user: Parse.User, permission: string, authData?: Object);</code>
187+
*
188+
* Advanced API: Used for handling your own oAuth tokens
189+
* {@link https://docs.parseplatform.org/rest/guide/#linking-users}
190+
*
191+
* <code>link(user: Parse.User, authData: Object, options?: FullOptions);</code>
192+
*
183193
* @method link
184194
* @name Parse.FacebookUtils.link
185195
* @param {Parse.User} user User to link to Facebook. This must be the
@@ -189,8 +199,7 @@ const FacebookUtils = {
189199
* Alternatively, supply a Facebook authData object as described in our
190200
* REST API docs if you want to handle getting facebook auth tokens
191201
* yourself.
192-
* @param {Object} options Standard options object with success and error
193-
* callbacks.
202+
* @param {Object} options MasterKey / SessionToken. Alternatively can be used for authData if permissions is a string
194203
* @returns {Promise}
195204
*/
196205
link(user, permissions, options) {
@@ -202,16 +211,9 @@ const FacebookUtils = {
202211
}
203212
requestedPermissions = permissions;
204213
return user._linkWith('facebook', options);
205-
} else {
206-
const newOptions = {};
207-
if (options) {
208-
for (const key in options) {
209-
newOptions[key] = options[key];
210-
}
211-
}
212-
newOptions.authData = permissions;
213-
return user._linkWith('facebook', newOptions);
214214
}
215+
const authData = { authData: permissions };
216+
return user._linkWith('facebook', authData, options);
215217
},
216218

217219
/**
@@ -232,6 +234,11 @@ const FacebookUtils = {
232234
);
233235
}
234236
return user._unlinkFrom('facebook', options);
237+
},
238+
239+
// Used for testing purposes
240+
_getAuthProvider() {
241+
return provider;
235242
}
236243
};
237244

0 commit comments

Comments
 (0)