Skip to content

Commit

Permalink
fixing suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesjamous committed Oct 30, 2018
1 parent cdb82b4 commit defcde6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 45 deletions.
45 changes: 27 additions & 18 deletions spec/CloudCodeReadonlyTriggers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('CloudCode ReadonlyTrigger tests', () => {
expect(sessionObject.get('sessionToken')).toBeDefined();
expect(sessionObject.get('createdWith')).toBeDefined();
expect(sessionObject.get('user')).toBeDefined();
expect(sessionObject.get('user').toEqual(jasmine.any(Parse.User)));
expect(sessionObject.get('user')).toEqual(jasmine.any(Parse.User));
});
try {
// signup a user (internally creates a session)
Expand All @@ -18,7 +18,7 @@ describe('CloudCode ReadonlyTrigger tests', () => {
user.setPassword('password');
await user.signUp();
} catch (error) {
throw 'Should not have failed';
throw error;
}
});
it('readonly-beforeSave should disregard any changes', async () => {
Expand All @@ -45,36 +45,45 @@ describe('CloudCode ReadonlyTrigger tests', () => {
expect(sessionObject.get('user').id).toBe(user.id);
expect(sessionObject.get('sessionToken')).toBeDefined();
});
it('readonly-beforeSave should ignore any thrown errors during signup', async () => {
const name = 'some username we dont like';
it('readonly-beforeSave should not affect user creation flow during signup', async () => {
let user;
Parse.Cloud.beforeSave('_Session', async () => {
// reject the session
throw new Parse.Error(12345678, 'Sorry, we dont like this username');
});
Parse.Cloud.beforeSave('_User', async req => {
// make sure this runs correctly
req.object.set('firstName', 'abcd');
});
Parse.Cloud.afterSave('_User', async req => {
if (req.object.has('lastName')) {
return;
}
// make sure this runs correctly
req.object.set('lastName', '1234');
await req.object.save({}, { useMasterKey: true });
});
try {
user = new Parse.User();
user.setUsername(name);
user.setPassword('password');
user.setUsername('user-name');
user.setPassword('user-password');
await user.signUp();
} catch (error) {
throw 'Should not have failed';
expect(error.code).toBe(12345678);
expect(error.message).toBe('Sorry, we dont like this username');
}
// get the user
const query = new Parse.Query('_User');
query.equalTo('username', name);
const createdUser = await query.first({
useMasterKey: true,
});
expect(createdUser).toBeDefined();
await delay(200); // just so that afterSave has time to run
await user.fetch({ useMasterKey: true });
expect(user.get('username')).toBe('user-name');
expect(user.get('firstName')).toBe('abcd');
expect(user.get('lastName')).toBe('1234');
// get the session
const query2 = new Parse.Query('_Session');
query2.equalTo('user', createdUser);
query2.equalTo('user', user);
const sessionObject = await query2.first({
useMasterKey: true,
});
expect(sessionObject).toBeDefined();
expect(sessionObject.get('sessionToken')).toBeDefined();
expect(sessionObject.get('sessionToken')).toBe(user.getSessionToken());
expect(sessionObject).toBeUndefined();
});
it('readonly-beforeSave should fail and prevent login on throw', async () => {
Parse.Cloud.beforeSave('_Session', async req => {
Expand Down
11 changes: 10 additions & 1 deletion src/RestWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,16 @@ RestWrite.prototype.createSessionToken = function() {
this.response.response.sessionToken = sessionData.sessionToken;
}

return createSession();
return createSession().catch(e => {
// we need to swallow the error during a signup
// this is to make sure the .execute() chain continues normally for the user.
// we dont need to kwow about any errors when signing a user up.
if (!this.storage['authProvider']) {
return Promise.resolve();
}
// on login proppagate errors
throw e;
});
};

// Delete email reset tokens if user is changing password or email.
Expand Down
32 changes: 6 additions & 26 deletions src/triggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ export const Types = {
afterFind: 'afterFind',
};

const ReadOnlyTriggers = Object.freeze(['_Session']);

function isReadonlyTrigger(className) {
return ReadOnlyTriggers.indexOf(className) > -1;
}

const baseStore = function() {
const Validators = {};
const Functions = {};
Expand All @@ -37,10 +31,6 @@ const baseStore = function() {
};

function validateClassNameForTriggers(className, type) {
const restrictedClassNames = [];
if (restrictedClassNames.indexOf(className) != -1) {
throw `Triggers are not supported for ${className} class.`;
}
if (type == Types.beforeSave && className === '_PushStatus') {
// _PushStatus uses undocumented nested key increment ops
// allowing beforeSave would mess up the objects big time
Expand Down Expand Up @@ -253,7 +243,7 @@ export function getRequestQueryObject(
// Any changes made to the object in a beforeSave will be included.
export function getResponseObject(request, resolve, reject) {
const className = request.object ? request.object.className : null;
const isReadOnlyTrigger = isReadonlyTrigger(className);
const isSessionTrigger = className === '_Session';
return {
success: function(response) {
if (request.triggerName === Types.afterFind) {
Expand All @@ -265,8 +255,8 @@ export function getResponseObject(request, resolve, reject) {
});
return resolve(response);
}
// ignore edited object in readonly triggers
if (isReadOnlyTrigger) {
// ignore edited object in session triggers
if (isSessionTrigger) {
return resolve();
}
// Use the JSON response
Expand All @@ -284,19 +274,9 @@ export function getResponseObject(request, resolve, reject) {
return resolve(response);
},
error: function(error) {
// handle special readOnlyTriggers cases
if (isReadOnlyTrigger) {
// Ignore thrown errors in beforeDelete & during login.
// We should prevent login from breaking if an error is thrown during the process,
// this can be accomplished with beforeSave on users.
if (
request.triggerName === Types.beforeDelete ||
(request.triggerName === Types.beforeSave &&
request.object.className === '_Session' &&
request.object.get('createdWith').action === 'signup')
) {
return resolve();
}
// ignore errors thrown in before-delete (during logout for example)
if (isSessionTrigger && request.triggerName === Types.beforeDelete) {
return resolve();
}
if (error instanceof Parse.Error) {
reject(error);
Expand Down

0 comments on commit defcde6

Please sign in to comment.