From e2fbee465fcf483745024456f26e9df3c545f051 Mon Sep 17 00:00:00 2001 From: Rikard Teodorsson <9367038+hej2010@users.noreply.github.com> Date: Tue, 26 Sep 2023 17:23:53 +0200 Subject: [PATCH 1/4] Failing test case for CloudCode.spec.js Signed-off-by: Rikard Teodorsson <9367038+hej2010@users.noreply.github.com> --- spec/CloudCode.spec.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/spec/CloudCode.spec.js b/spec/CloudCode.spec.js index 37ab6a0c32..738f185dd8 100644 --- a/spec/CloudCode.spec.js +++ b/spec/CloudCode.spec.js @@ -2510,6 +2510,25 @@ describe('beforeFind hooks', () => { expect(res2.get('pointerFieldArray')[0].get('aField')).toBe('aFieldValue'); expect(spy).toHaveBeenCalledTimes(2); }); + + it('should have access to context in include query in beforeFind hook', async () => { + const obj1 = new Parse.Object('TestObject'); + const obj2 = new Parse.Object('TestObject2'); + obj2.set('aField', 'aFieldValue'); + await obj2.save(); + obj1.set('pointerField', obj2); + await obj1.save(); + Parse.Cloud.beforeFind('TestObject', req => { + expect(req.context).toBeDefined(); + expect(req.context.a).toEqual('a'); + }); + Parse.Cloud.beforeFind('TestObject2', req => { + expect(req.context).toBeDefined(); + expect(req.context.a).toEqual('a'); + }); + const query = new Parse.Query('TestObject'); + return query.include('pointerField').find({ context: { a: 'a' } }); + }); }); describe('afterFind hooks', () => { From 6085f285cb5218ff066981851ca9ab34ba12db80 Mon Sep 17 00:00:00 2001 From: Rikard Teodorsson <9367038+hej2010@users.noreply.github.com> Date: Tue, 26 Sep 2023 19:43:49 +0200 Subject: [PATCH 2/4] Update RestQuery.js Signed-off-by: Rikard Teodorsson <9367038+hej2010@users.noreply.github.com> --- src/RestQuery.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/RestQuery.js b/src/RestQuery.js index 96a52ec17a..5af678bb96 100644 --- a/src/RestQuery.js +++ b/src/RestQuery.js @@ -478,6 +478,7 @@ _UnsafeRestQuery.prototype.replaceInQuery = async function () { className: inQueryValue.className, restWhere: inQueryValue.where, restOptions: additionalOptions, + context: this.context, }); return subquery.execute().then(response => { transformInQuery(inQueryObject, subquery.className, response.results); @@ -537,6 +538,7 @@ _UnsafeRestQuery.prototype.replaceNotInQuery = async function () { className: notInQueryValue.className, restWhere: notInQueryValue.where, restOptions: additionalOptions, + context: this.context, }); return subquery.execute().then(response => { @@ -609,6 +611,7 @@ _UnsafeRestQuery.prototype.replaceSelect = async function () { className: selectValue.query.className, restWhere: selectValue.query.where, restOptions: additionalOptions, + context: this.context, }); return subquery.execute().then(response => { @@ -671,6 +674,7 @@ _UnsafeRestQuery.prototype.replaceDontSelect = async function () { className: dontSelectValue.query.className, restWhere: dontSelectValue.query.where, restOptions: additionalOptions, + context: this.context, }); return subquery.execute().then(response => { @@ -860,6 +864,7 @@ _UnsafeRestQuery.prototype.handleInclude = function () { this.auth, this.response, this.include[0], + this.context, this.restOptions ); if (pathResponse.then) { @@ -946,7 +951,7 @@ _UnsafeRestQuery.prototype.handleAuthAdapters = async function () { // Adds included values to the response. // Path is a list of field names. // Returns a promise for an augmented response. -function includePath(config, auth, response, path, restOptions = {}) { +function includePath(config, auth, response, path, context, restOptions = {}) { var pointers = findPointers(response.results, path); if (pointers.length == 0) { return response; @@ -1026,6 +1031,7 @@ function includePath(config, auth, response, path, restOptions = {}) { className, restWhere: where, restOptions: includeRestOptions, + context: context, }); return query.execute({ op: 'get' }).then(results => { results.className = className; From 0c830cef9e4c29490849bb340b70c073333d4c2e Mon Sep 17 00:00:00 2001 From: Rikard Teodorsson <9367038+hej2010@users.noreply.github.com> Date: Tue, 26 Sep 2023 19:44:13 +0200 Subject: [PATCH 3/4] Update CloudCode.spec.js Signed-off-by: Rikard Teodorsson <9367038+hej2010@users.noreply.github.com> --- spec/CloudCode.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/CloudCode.spec.js b/spec/CloudCode.spec.js index 738f185dd8..624d01bd22 100644 --- a/spec/CloudCode.spec.js +++ b/spec/CloudCode.spec.js @@ -2527,7 +2527,7 @@ describe('beforeFind hooks', () => { expect(req.context.a).toEqual('a'); }); const query = new Parse.Query('TestObject'); - return query.include('pointerField').find({ context: { a: 'a' } }); + await query.include('pointerField').find({ context: { a: 'a' } }); }); }); From f657b6cab25d837804ba67d9f7fb61e3fb680fbc Mon Sep 17 00:00:00 2001 From: Manuel <5673677+mtrezza@users.noreply.github.com> Date: Tue, 26 Sep 2023 21:28:13 +0200 Subject: [PATCH 4/4] add test trigger call Signed-off-by: Manuel <5673677+mtrezza@users.noreply.github.com> --- spec/CloudCode.spec.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spec/CloudCode.spec.js b/spec/CloudCode.spec.js index 624d01bd22..a1c8b48bfd 100644 --- a/spec/CloudCode.spec.js +++ b/spec/CloudCode.spec.js @@ -2512,6 +2512,8 @@ describe('beforeFind hooks', () => { }); it('should have access to context in include query in beforeFind hook', async () => { + let beforeFindTestObjectCalled = false; + let beforeFindTestObject2Called = false; const obj1 = new Parse.Object('TestObject'); const obj2 = new Parse.Object('TestObject2'); obj2.set('aField', 'aFieldValue'); @@ -2521,13 +2523,17 @@ describe('beforeFind hooks', () => { Parse.Cloud.beforeFind('TestObject', req => { expect(req.context).toBeDefined(); expect(req.context.a).toEqual('a'); + beforeFindTestObjectCalled = true; }); Parse.Cloud.beforeFind('TestObject2', req => { expect(req.context).toBeDefined(); expect(req.context.a).toEqual('a'); + beforeFindTestObject2Called = true; }); const query = new Parse.Query('TestObject'); await query.include('pointerField').find({ context: { a: 'a' } }); + expect(beforeFindTestObjectCalled).toBeTrue(); + expect(beforeFindTestObject2Called).toBeTrue(); }); });