Skip to content

Fix beforeQuery trigger when getting objects through GET API #3862

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

Merged
merged 1 commit into from
Jun 21, 2017
Merged
Show file tree
Hide file tree
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
29 changes: 29 additions & 0 deletions spec/CloudCode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,7 @@ describe('beforeFind hooks', () => {
expect(jsonQuery.include).toEqual('otherKey,otherValue');
expect(jsonQuery.limit).toEqual(100);
expect(jsonQuery.skip).toBe(undefined);
expect(req.isGet).toEqual(false);
});

const query = new Parse.Query('MyObject');
Expand Down Expand Up @@ -1305,6 +1306,34 @@ describe('beforeFind hooks', () => {
done();
});
});

it('should add beforeFind trigger using get API',(done) => {
const hook = {
method: function(req) {
expect(req.isGet).toEqual(true);
return Promise.resolve();
}
};
spyOn(hook, 'method').and.callThrough();
Parse.Cloud.beforeFind('MyObject', hook.method);
const obj = new Parse.Object('MyObject');
obj.set('secretField', 'SSID');
obj.save().then(function() {
rp({
method: 'GET',
uri: 'http://localhost:8378/1/classes/MyObject/' + obj.id,
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest'
},
json: true,
}).then(body => {
expect(body.secretField).toEqual('SSID');
expect(hook.method).toHaveBeenCalled();
done();
});
});
});
});

describe('afterFind hooks', () => {
Expand Down
9 changes: 7 additions & 2 deletions src/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@ function find(config, auth, className, restWhere, restOptions, clientSDK) {

// get is just like find but only queries an objectId.
const get = (config, auth, className, objectId, restOptions, clientSDK) => {
var restWhere = { objectId };
enforceRoleSecurity('get', className, auth);
const query = new RestQuery(config, auth, className, { objectId }, restOptions, clientSDK);
return query.execute();
return triggers.maybeRunQueryTrigger(triggers.Types.beforeFind, className, restWhere, restOptions, config, auth, true).then((result) => {
restWhere = result.restWhere || restWhere;
restOptions = result.restOptions || restOptions;
const query = new RestQuery(config, auth, className, restWhere, restOptions, clientSDK);
return query.execute();
});
}

// Returns a promise that doesn't resolve to any useful value.
Expand Down
11 changes: 7 additions & 4 deletions src/triggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,16 @@ export function getRequestObject(triggerType, auth, parseObject, originalParseOb
return request;
}

export function getRequestQueryObject(triggerType, auth, query, count, config) {
export function getRequestQueryObject(triggerType, auth, query, count, config, isGet) {
isGet = !!isGet;

var request = {
triggerName: triggerType,
query,
master: false,
count,
log: config.loggerController
log: config.loggerController,
isGet
};

if (!auth) {
Expand Down Expand Up @@ -286,7 +289,7 @@ export function maybeRunAfterFindTrigger(triggerType, auth, className, objects,
});
}

export function maybeRunQueryTrigger(triggerType, className, restWhere, restOptions, config, auth) {
export function maybeRunQueryTrigger(triggerType, className, restWhere, restOptions, config, auth, isGet) {
const trigger = getTrigger(className, triggerType, config.applicationId);
if (!trigger) {
return Promise.resolve({
Expand All @@ -312,7 +315,7 @@ export function maybeRunQueryTrigger(triggerType, className, restWhere, restOpti
}
count = !!restOptions.count;
}
const requestObject = getRequestQueryObject(triggerType, auth, parseQuery, count, config);
const requestObject = getRequestQueryObject(triggerType, auth, parseQuery, count, config, isGet);
return Promise.resolve().then(() => {
return trigger(requestObject);
}).then((result) => {
Expand Down