Skip to content

Commit 1f707a9

Browse files
committed
Merge pull request parse-community#587 from ParsePlatform/nlutsenko.cc.test
Do not pass objectId, updatedAt, createdAt to beforeSave hooks on object create.
2 parents 18d7a7a + be693fb commit 1f707a9

File tree

4 files changed

+31
-18
lines changed

4 files changed

+31
-18
lines changed

spec/ParseAPI.spec.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -390,15 +390,19 @@ describe('miscellaneous', function() {
390390
var object = req.object;
391391
expect(object instanceof Parse.Object).toBeTruthy();
392392
expect(object.get('fooAgain')).toEqual('barAgain');
393-
expect(object.id).not.toBeUndefined();
394-
expect(object.createdAt).not.toBeUndefined();
395-
expect(object.updatedAt).not.toBeUndefined();
396393
if (triggerTime == 0) {
397394
// Create
398395
expect(object.get('foo')).toEqual('bar');
396+
// No objectId/createdAt/updatedAt
397+
expect(object.id).toBeUndefined();
398+
expect(object.createdAt).toBeUndefined();
399+
expect(object.updatedAt).toBeUndefined();
399400
} else if (triggerTime == 1) {
400401
// Update
401402
expect(object.get('foo')).toEqual('baz');
403+
expect(object.id).not.toBeUndefined();
404+
expect(object.createdAt).not.toBeUndefined();
405+
expect(object.updatedAt).not.toBeUndefined();
402406
} else {
403407
res.error();
404408
}
@@ -431,10 +435,10 @@ describe('miscellaneous', function() {
431435
Parse.Cloud.afterSave('GameScore', function(req, res) {
432436
var object = req.object;
433437
expect(object instanceof Parse.Object).toBeTruthy();
434-
expect(object.get('fooAgain')).toEqual('barAgain');
435438
expect(object.id).not.toBeUndefined();
436439
expect(object.createdAt).not.toBeUndefined();
437440
expect(object.updatedAt).not.toBeUndefined();
441+
expect(object.get('fooAgain')).toEqual('barAgain');
438442
if (triggerTime == 0) {
439443
// Create
440444
expect(object.get('foo')).toEqual('bar');
@@ -474,17 +478,21 @@ describe('miscellaneous', function() {
474478
var object = req.object;
475479
expect(object instanceof Parse.Object).toBeTruthy();
476480
expect(object.get('fooAgain')).toEqual('barAgain');
477-
expect(object.id).not.toBeUndefined();
478-
expect(object.createdAt).not.toBeUndefined();
479-
expect(object.updatedAt).not.toBeUndefined();
480481
var originalObject = req.original;
481482
if (triggerTime == 0) {
483+
// No id/createdAt/updatedAt
484+
expect(object.id).toBeUndefined();
485+
expect(object.createdAt).toBeUndefined();
486+
expect(object.updatedAt).toBeUndefined();
482487
// Create
483488
expect(object.get('foo')).toEqual('bar');
484489
// Check the originalObject is undefined
485490
expect(originalObject).toBeUndefined();
486491
} else if (triggerTime == 1) {
487492
// Update
493+
expect(object.id).not.toBeUndefined();
494+
expect(object.createdAt).not.toBeUndefined();
495+
expect(object.updatedAt).not.toBeUndefined();
488496
expect(object.get('foo')).toEqual('baz');
489497
// Check the originalObject
490498
expect(originalObject instanceof Parse.Object).toBeTruthy();
File renamed without changes.

spec/helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var ParseServer = require('../src/index').ParseServer;
1010
var DatabaseAdapter = require('../src/DatabaseAdapter');
1111

1212
var databaseURI = process.env.DATABASE_URI;
13-
var cloudMain = process.env.CLOUD_CODE_MAIN || './cloud/main.js';
13+
var cloudMain = process.env.CLOUD_CODE_MAIN || '../spec/cloud/main.js';
1414
var port = 8378;
1515

1616
// Default server configuration for tests.

src/RestWrite.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,6 @@ function RestWrite(config, auth, className, query, data, originalData) {
5050

5151
// The timestamp we'll use for this whole operation
5252
this.updatedAt = Parse._encode(new Date()).iso;
53-
54-
if (this.data) {
55-
// Add default fields
56-
this.data.updatedAt = this.updatedAt;
57-
if (!this.query) {
58-
this.data.createdAt = this.updatedAt;
59-
this.data.objectId = cryptoUtils.newObjectId();
60-
}
61-
}
6253
}
6354

6455
// A convenient method to perform all the steps of processing the
@@ -76,6 +67,8 @@ RestWrite.prototype.execute = function() {
7667
return this.handleSession();
7768
}).then(() => {
7869
return this.runBeforeTrigger();
70+
}).then(() => {
71+
return this.setRequiredFieldsIfNeeded();
7972
}).then(() => {
8073
return this.validateAuthData();
8174
}).then(() => {
@@ -99,7 +92,7 @@ RestWrite.prototype.getUserAndRoleACL = function() {
9992

10093
this.runOptions.acl = ['*'];
10194

102-
if( this.auth.user ){
95+
if (this.auth.user) {
10396
return this.auth.getUserRoles().then((roles) => {
10497
roles.push(this.auth.user.id);
10598
this.runOptions.acl = this.runOptions.acl.concat(roles);
@@ -146,6 +139,18 @@ RestWrite.prototype.runBeforeTrigger = function() {
146139
});
147140
};
148141

142+
RestWrite.prototype.setRequiredFieldsIfNeeded = function() {
143+
if (this.data) {
144+
// Add default fields
145+
this.data.updatedAt = this.updatedAt;
146+
if (!this.query) {
147+
this.data.createdAt = this.updatedAt;
148+
this.data.objectId = cryptoUtils.newObjectId();
149+
}
150+
}
151+
return Promise.resolve();
152+
};
153+
149154
// Transforms auth data for a user object.
150155
// Does nothing if this isn't a user object.
151156
// Returns a promise for when we're done if it can't finish this tick.

0 commit comments

Comments
 (0)