From 7a080478b518f2c9dba94db636af0bf374d52c22 Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Tue, 11 Jun 2019 13:40:34 -0500 Subject: [PATCH] Fix #5654 (#5664) * Fix #5654 * fix tests * throw error instead --- spec/ParseObject.spec.js | 15 +++++++++++++++ spec/rest.spec.js | 22 ++++++++++++++++++++++ src/RestWrite.js | 6 ++++++ 3 files changed, 43 insertions(+) diff --git a/spec/ParseObject.spec.js b/spec/ParseObject.spec.js index a157dd1e..3220f606 100644 --- a/spec/ParseObject.spec.js +++ b/spec/ParseObject.spec.js @@ -2068,4 +2068,19 @@ describe('Parse.Object testing', () => { done(); }); + + it('isNew in cloud code', async () => { + Parse.Cloud.beforeSave('CloudCodeIsNew', req => { + expect(req.object.isNew()).toBeTruthy(); + expect(req.object.id).toBeUndefined(); + }); + + Parse.Cloud.afterSave('CloudCodeIsNew', req => { + expect(req.object.isNew()).toBeFalsy(); + expect(req.object.id).toBeDefined(); + }); + + const object = new Parse.Object('CloudCodeIsNew'); + await object.save(); + }); }); diff --git a/spec/rest.spec.js b/spec/rest.spec.js index ca0294d4..503a4d77 100644 --- a/spec/rest.spec.js +++ b/spec/rest.spec.js @@ -446,6 +446,28 @@ describe('rest create', () => { }); }); + it('cannot set id', done => { + const headers = { + 'Content-Type': 'application/json', + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest', + }; + request({ + headers: headers, + method: 'POST', + url: 'http://localhost:8378/1/classes/TestObject', + body: JSON.stringify({ + foo: 'bar', + id: 'hello', + }), + }).then(fail, response => { + const b = response.data; + expect(b.code).toEqual(105); + expect(b.error).toEqual('id is an invalid field name.'); + done(); + }); + }); + it('test default session length', done => { const user = { username: 'asdf', diff --git a/src/RestWrite.js b/src/RestWrite.js index 998d3576..e3db4853 100644 --- a/src/RestWrite.js +++ b/src/RestWrite.js @@ -52,6 +52,12 @@ function RestWrite( 'objectId is an invalid field name.' ); } + if (!query && data.id) { + throw new Parse.Error( + Parse.Error.INVALID_KEY_NAME, + 'id is an invalid field name.' + ); + } // When the operation is complete, this.response may have several // fields.