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 options for AnonymousUtils #860

Merged
merged 2 commits into from
Jul 11, 2019
Merged
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
74 changes: 68 additions & 6 deletions integration/test/ParseUserTest.js
Original file line number Diff line number Diff line change
@@ -536,7 +536,6 @@ describe('Parse User', () => {
Parse.User.enableUnsafeCurrentUser();

await Parse.User.signUp('foobaz', '1234');
await Parse.User.logOut();

const user = await Parse.AnonymousUtils.logIn();
user.set('field', 'hello world');
@@ -552,6 +551,71 @@ describe('Parse User', () => {
}
});

it('anonymous user logIn does not use currentUser sessionToken', async () => {
Parse.User.enableUnsafeCurrentUser();

const user1 = await Parse.User.signUp('anon-not', '1234');
const user2 = await Parse.AnonymousUtils.logIn();
expect(user1.getSessionToken()).toBeDefined();
expect(user2.getSessionToken()).toBeDefined();
expect(user1.getSessionToken()).not.toBe(user2.getSessionToken());
});

it('anonymous user link currentUser', async () => {
Parse.User.enableUnsafeCurrentUser();

const user1 = await Parse.User.signUp('anon-not', '1234');
const user2 = await Parse.AnonymousUtils.link(user1);
expect(user1.getSessionToken()).toBeDefined();
expect(user2.getSessionToken()).toBeDefined();
expect(user1.getSessionToken()).toBe(user2.getSessionToken());
});

it('anonymous user link does not use currentUser sessionToken', async () => {
Parse.User.enableUnsafeCurrentUser();

const user1 = await Parse.User.signUp('anon-not', '1234');
const user2 = new Parse.User();
await Parse.AnonymousUtils.link(user2);
expect(user1.getSessionToken()).toBeDefined();
expect(user2.getSessionToken()).toBeDefined();
expect(user1.getSessionToken()).not.toBe(user2.getSessionToken());
});

it('facebook logIn does not use currentUser sessionToken', async () => {
Parse.User.enableUnsafeCurrentUser();
Parse.FacebookUtils.init();

const user1 = await Parse.User.signUp('facebook-not', '1234');
const user2 = await Parse.FacebookUtils.logIn();
expect(user1.getSessionToken()).toBeDefined();
expect(user2.getSessionToken()).toBeDefined();
expect(user1.getSessionToken()).not.toBe(user2.getSessionToken());
});

it('facebook link currentUser', async () => {
Parse.User.enableUnsafeCurrentUser();
Parse.FacebookUtils.init();

const user1 = await Parse.User.signUp('facebook-not', '1234');
const user2 = await Parse.FacebookUtils.link(user1);
expect(user1.getSessionToken()).toBeDefined();
expect(user2.getSessionToken()).toBeDefined();
expect(user1.getSessionToken()).toBe(user2.getSessionToken());
});

it('facebook link does not use currentUser sessionToken', async () => {
Parse.User.enableUnsafeCurrentUser();
Parse.FacebookUtils.init();

const user1 = await Parse.User.signUp('facebook-not', '1234');
const user2 = new Parse.User();
await Parse.FacebookUtils.link(user2);
expect(user1.getSessionToken()).toBeDefined();
expect(user2.getSessionToken()).toBeDefined();
expect(user1.getSessionToken()).not.toBe(user2.getSessionToken());
});

it('can signUp user with subclass', async () => {
Parse.User.enableUnsafeCurrentUser();

@@ -649,15 +713,13 @@ describe('Parse User', () => {
expect(loggedIn.authenticated()).toBeTruthy();
});

it('linking un-authenticated user without master key will throw', async (done) => {
it('can linking un-authenticated user without master key', async () => {
const user = new Parse.User();
user.setUsername('Alice');
user.setPassword('sekrit');
await user.save(null, { useMasterKey: true });
user._linkWith(provider.getAuthType(), provider.getAuthData())
.then(() => done.fail('should fail'))
.catch(e => expect(e.message).toBe(`Cannot modify user ${user.id}.`))
.then(done);
await user._linkWith(provider.getAuthType(), provider.getAuthData());
expect(user.getSessionToken()).toBeDefined();
});

it('can link with custom auth', async () => {
11 changes: 7 additions & 4 deletions src/AnonymousUtils.js
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
*/
import ParseUser from './ParseUser';
const uuidv4 = require('uuid/v4');
import type { RequestOptions } from './RESTController';

let registered = false;

@@ -62,12 +63,13 @@ const AnonymousUtils = {
*
* @method logIn
* @name Parse.AnonymousUtils.logIn
* @param {Object} options MasterKey / SessionToken.
* @returns {Promise}
* @static
*/
logIn() {
logIn(options?: RequestOptions) {
const provider = this._getAuthProvider();
return ParseUser._logInWith(provider.getAuthType(), provider.getAuthData());
return ParseUser._logInWith(provider.getAuthType(), provider.getAuthData(), options);
},

/**
@@ -76,12 +78,13 @@ const AnonymousUtils = {
* @method link
* @name Parse.AnonymousUtils.link
* @param {Parse.User} user User to link. This must be the current user.
* @param {Object} options MasterKey / SessionToken.
* @returns {Promise}
* @static
*/
link(user: ParseUser) {
link(user: ParseUser, options?: RequestOptions) {
const provider = this._getAuthProvider();
return user._linkWith(provider.getAuthType(), provider.getAuthData());
return user._linkWith(provider.getAuthType(), provider.getAuthData(), options);
},

_getAuthProvider() {
11 changes: 6 additions & 5 deletions src/ParseUser.js
Original file line number Diff line number Diff line change
@@ -77,7 +77,8 @@ class ParseUser extends ParseObject {
* Unlike in the Android/iOS SDKs, logInWith is unnecessary, since you can
* call linkWith on the user (even if it doesn't exist yet on the server).
*/
_linkWith(provider: any, options: { authData?: AuthData }, saveOpts?: FullOptions): Promise<ParseUser> {
_linkWith(provider: any, options: { authData?: AuthData }, saveOpts?: FullOptions = {}): Promise<ParseUser> {
saveOpts.sessionToken = saveOpts.sessionToken || this.getSessionToken() || '';
let authType;
if (typeof provider === 'string') {
authType = provider;
@@ -685,8 +686,8 @@ class ParseUser extends ParseObject {
return controller.hydrate(userJSON);
}

static logInWith(provider: any, options?: RequestOptions) {
return ParseUser._logInWith(provider, options);
static logInWith(provider: any, options: { authData?: AuthData }, saveOpts?: FullOptions) {
return ParseUser._logInWith(provider, options, saveOpts);
}

/**
@@ -804,9 +805,9 @@ class ParseUser extends ParseObject {
});
}

static _logInWith(provider: any, options?: RequestOptions) {
static _logInWith(provider: any, options: { authData?: AuthData }, saveOpts?: FullOptions) {
const user = new ParseUser();
return user._linkWith(provider, options);
return user._linkWith(provider, options, saveOpts);
}

static _clearCache() {
4 changes: 2 additions & 2 deletions src/__tests__/AnonymousUtils-test.js
Original file line number Diff line number Diff line change
@@ -74,15 +74,15 @@ describe('AnonymousUtils', () => {
jest.spyOn(user, '_linkWith');
AnonymousUtils.link(user);
expect(user._linkWith).toHaveBeenCalledTimes(1);
expect(user._linkWith).toHaveBeenCalledWith('anonymous', mockProvider.getAuthData());
expect(user._linkWith).toHaveBeenCalledWith('anonymous', mockProvider.getAuthData(), undefined);
expect(AnonymousUtils._getAuthProvider).toHaveBeenCalledTimes(1);
});

it('can login user', () => {
jest.spyOn(MockUser, '_logInWith');
AnonymousUtils.logIn();
expect(MockUser._logInWith).toHaveBeenCalledTimes(1);
expect(MockUser._logInWith).toHaveBeenCalledWith('anonymous', mockProvider.getAuthData());
expect(MockUser._logInWith).toHaveBeenCalledWith('anonymous', mockProvider.getAuthData(), undefined);
expect(AnonymousUtils._getAuthProvider).toHaveBeenCalledTimes(1);
});
});