Adds context object in Cloud Code hooks (#4939)

* wip

* Refactors triggers a bit

- Adds testing for hooks and context

* comment nit

* nits
This commit is contained in:
Florent Vilmart
2018-08-09 11:08:18 -04:00
parent 488b2ff231
commit 457d51a972
4 changed files with 145 additions and 42 deletions

View File

@@ -1809,4 +1809,46 @@ describe('afterFind hooks', () => {
done();
});
});
it('should expose context in before and afterSave', async () => {
let calledBefore = false;
let calledAfter = false;
Parse.Cloud.beforeSave('MyClass', (req) => {
req.context = {
key: 'value',
otherKey: 1,
}
calledBefore = true;
});
Parse.Cloud.afterSave('MyClass', (req) => {
expect(req.context.otherKey).toBe(1);
expect(req.context.key).toBe('value');
calledAfter = true;
});
const object = new Parse.Object('MyClass');
await object.save();
expect(calledBefore).toBe(true);
expect(calledAfter).toBe(true);
});
it('should expose context in before and afterSave and let keys be set individually', async () => {
let calledBefore = false;
let calledAfter = false;
Parse.Cloud.beforeSave('MyClass', (req) => {
req.context.some = 'value';
req.context.yolo = 1;
calledBefore = true;
});
Parse.Cloud.afterSave('MyClass', (req) => {
expect(req.context.yolo).toBe(1);
expect(req.context.some).toBe('value');
calledAfter = true;
});
const object = new Parse.Object('MyClass');
await object.save();
expect(calledBefore).toBe(true);
expect(calledAfter).toBe(true);
});
});

View File

@@ -5,6 +5,9 @@ const triggers = require('../lib/triggers');
const HooksController = require('../lib/Controllers/HooksController').default;
const express = require("express");
const bodyParser = require('body-parser');
const auth = require('../lib/Auth');
const Config = require('../lib/Config');
const port = 12345;
const hookServerURL = "http://localhost:" + port;
@@ -503,3 +506,39 @@ describe('Hooks', () => {
});
});
});
describe('triggers', () => {
it('should produce a proper request object with context in beforeSave', () => {
const config = Config.get('test');
const master = auth.master(config);
const context = {
originalKey: 'original'
};
const req = triggers.getRequestObject(triggers.Types.beforeSave, master, {}, {}, config, context);
expect(req.context.originalKey).toBe('original');
req.context = {
key: 'value'
};
expect(context.key).toBe(undefined);
req.context = {
key: 'newValue'
};
expect(context.key).toBe(undefined);
});
it('should produce a proper request object with context in afterSave', () => {
const config = Config.get('test');
const master = auth.master(config);
const context = {};
const req = triggers.getRequestObject(triggers.Types.afterSave, master, {}, {}, config, context);
expect(req.context).not.toBeUndefined();
});
it('should not set context on beforeFind', () => {
const config = Config.get('test');
const master = auth.master(config);
const context = {};
const req = triggers.getRequestObject(triggers.Types.beforeFind, master, {}, {}, config, context);
expect(req.context).toBeUndefined();
});
});