Skip to content

Commit c2f2281

Browse files
dblythydplewis
andauthored
Validation Handler Update (#6968)
* Initial Commit * Update FunctionsRouter.js * Update FunctionsRouter.js * Change params to fields * Changes requested * Fix failing tests * More tests * More tests * Remove existing functionality * Remove legacy tests * fix array typo * Update triggers.js * Docs * Allow requireUserKeys to be object * validateMasterKey * Improve documentation Co-authored-by: Diamond Lewis <[email protected]>
1 parent e89cf25 commit c2f2281

8 files changed

+1752
-176
lines changed

spec/CloudCode.Validator.spec.js

+1,174
Large diffs are not rendered by default.

spec/CloudCode.spec.js

+43
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,21 @@ describe('Cloud Code', () => {
113113
);
114114
});
115115

116+
it('returns an empty error', done => {
117+
Parse.Cloud.define('cloudCodeWithError', () => {
118+
throw null;
119+
});
120+
121+
Parse.Cloud.run('cloudCodeWithError').then(
122+
() => done.fail('should not succeed'),
123+
e => {
124+
expect(e.code).toEqual(141);
125+
expect(e.message).toEqual('Script failed.');
126+
done();
127+
}
128+
);
129+
});
130+
116131
it('beforeSave rejection with custom error code', function (done) {
117132
Parse.Cloud.beforeSave('BeforeSaveFailWithErrorCode', function () {
118133
throw new Parse.Error(999, 'Nope');
@@ -2675,6 +2690,34 @@ describe('beforeLogin hook', () => {
26752690
expect(result).toBe(file);
26762691
});
26772692

2693+
it('throw custom error from beforeSaveFile', async done => {
2694+
Parse.Cloud.beforeSaveFile(() => {
2695+
throw new Parse.Error(Parse.Error.SCRIPT_FAILED, 'It should fail');
2696+
});
2697+
try {
2698+
const file = new Parse.File('popeye.txt', [1, 2, 3], 'text/plain');
2699+
await file.save({ useMasterKey: true });
2700+
fail('error should have thrown');
2701+
} catch (e) {
2702+
expect(e.code).toBe(Parse.Error.SCRIPT_FAILED);
2703+
done();
2704+
}
2705+
});
2706+
2707+
it('throw empty error from beforeSaveFile', async done => {
2708+
Parse.Cloud.beforeSaveFile(() => {
2709+
throw null;
2710+
});
2711+
try {
2712+
const file = new Parse.File('popeye.txt', [1, 2, 3], 'text/plain');
2713+
await file.save({ useMasterKey: true });
2714+
fail('error should have thrown');
2715+
} catch (e) {
2716+
expect(e.code).toBe(130);
2717+
done();
2718+
}
2719+
});
2720+
26782721
it('beforeSaveFile should return file that is already saved and not save anything to files adapter', async () => {
26792722
await reconfigureServer({ filesAdapter: mockAdapter });
26802723
const createFileSpy = spyOn(mockAdapter, 'createFile').and.callThrough();

spec/ParseAPI.spec.js

+44-74
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const headers = {
2424
};
2525

2626
describe_only_db('mongo')('miscellaneous', () => {
27-
it('test rest_create_app', function(done) {
27+
it('test rest_create_app', function (done) {
2828
let appId;
2929
Parse._request('POST', 'rest_create_app')
3030
.then(res => {
@@ -57,19 +57,19 @@ describe_only_db('mongo')('miscellaneous', () => {
5757
});
5858
});
5959

60-
describe('miscellaneous', function() {
61-
it('create a GameScore object', function(done) {
60+
describe('miscellaneous', function () {
61+
it('create a GameScore object', function (done) {
6262
const obj = new Parse.Object('GameScore');
6363
obj.set('score', 1337);
64-
obj.save().then(function(obj) {
64+
obj.save().then(function (obj) {
6565
expect(typeof obj.id).toBe('string');
6666
expect(typeof obj.createdAt.toGMTString()).toBe('string');
6767
done();
6868
}, done.fail);
6969
});
7070

71-
it('get a TestObject', function(done) {
72-
create({ bloop: 'blarg' }, async function(obj) {
71+
it('get a TestObject', function (done) {
72+
create({ bloop: 'blarg' }, async function (obj) {
7373
const t2 = new TestObject({ objectId: obj.id });
7474
const obj2 = await t2.fetch();
7575
expect(obj2.get('bloop')).toEqual('blarg');
@@ -79,8 +79,8 @@ describe('miscellaneous', function() {
7979
});
8080
});
8181

82-
it('create a valid parse user', function(done) {
83-
createTestUser().then(function(data) {
82+
it('create a valid parse user', function (done) {
83+
createTestUser().then(function (data) {
8484
expect(data.id).not.toBeUndefined();
8585
expect(data.getSessionToken()).not.toBeUndefined();
8686
expect(data.get('password')).toBeUndefined();
@@ -297,8 +297,8 @@ describe('miscellaneous', function() {
297297
});
298298
});
299299

300-
it('succeed in logging in', function(done) {
301-
createTestUser().then(async function(u) {
300+
it('succeed in logging in', function (done) {
301+
createTestUser().then(async function (u) {
302302
expect(typeof u.id).toEqual('string');
303303

304304
const user = await Parse.User.logIn('test', 'moon-y');
@@ -310,7 +310,7 @@ describe('miscellaneous', function() {
310310
}, fail);
311311
});
312312

313-
it('increment with a user object', function(done) {
313+
it('increment with a user object', function (done) {
314314
createTestUser()
315315
.then(user => {
316316
user.increment('foo');
@@ -338,7 +338,7 @@ describe('miscellaneous', function() {
338338
);
339339
});
340340

341-
it('save various data types', function(done) {
341+
it('save various data types', function (done) {
342342
const obj = new TestObject();
343343
obj.set('date', new Date());
344344
obj.set('array', [1, 2, 3]);
@@ -358,7 +358,7 @@ describe('miscellaneous', function() {
358358
});
359359
});
360360

361-
it('query with limit', function(done) {
361+
it('query with limit', function (done) {
362362
const baz = new TestObject({ foo: 'baz' });
363363
const qux = new TestObject({ foo: 'qux' });
364364
baz
@@ -383,7 +383,7 @@ describe('miscellaneous', function() {
383383
);
384384
});
385385

386-
it('query without limit get default 100 records', function(done) {
386+
it('query without limit get default 100 records', function (done) {
387387
const objects = [];
388388
for (let i = 0; i < 150; i++) {
389389
objects.push(new TestObject({ name: 'name' + i }));
@@ -404,7 +404,7 @@ describe('miscellaneous', function() {
404404
);
405405
});
406406

407-
it('basic saveAll', function(done) {
407+
it('basic saveAll', function (done) {
408408
const alpha = new TestObject({ letter: 'alpha' });
409409
const beta = new TestObject({ letter: 'beta' });
410410
Parse.Object.saveAll([alpha, beta])
@@ -425,26 +425,26 @@ describe('miscellaneous', function() {
425425
);
426426
});
427427

428-
it('test beforeSave set object acl success', function(done) {
428+
it('test beforeSave set object acl success', function (done) {
429429
const acl = new Parse.ACL({
430430
'*': { read: true, write: false },
431431
});
432-
Parse.Cloud.beforeSave('BeforeSaveAddACL', function(req) {
432+
Parse.Cloud.beforeSave('BeforeSaveAddACL', function (req) {
433433
req.object.setACL(acl);
434434
});
435435

436436
const obj = new Parse.Object('BeforeSaveAddACL');
437437
obj.set('lol', true);
438438
obj.save().then(
439-
function() {
439+
function () {
440440
const query = new Parse.Query('BeforeSaveAddACL');
441441
query.get(obj.id).then(
442-
function(objAgain) {
442+
function (objAgain) {
443443
expect(objAgain.get('lol')).toBeTruthy();
444444
expect(objAgain.getACL().equals(acl));
445445
done();
446446
},
447-
function(error) {
447+
function (error) {
448448
fail(error);
449449
done();
450450
}
@@ -667,10 +667,10 @@ describe('miscellaneous', function() {
667667
});
668668
});
669669

670-
it('test afterSave get full object on create and update', function(done) {
670+
it('test afterSave get full object on create and update', function (done) {
671671
let triggerTime = 0;
672672
// Register a mock beforeSave hook
673-
Parse.Cloud.afterSave('GameScore', function(req) {
673+
Parse.Cloud.afterSave('GameScore', function (req) {
674674
const object = req.object;
675675
expect(object instanceof Parse.Object).toBeTruthy();
676676
expect(object.id).not.toBeUndefined();
@@ -694,29 +694,29 @@ describe('miscellaneous', function() {
694694
obj.set('fooAgain', 'barAgain');
695695
obj
696696
.save()
697-
.then(function() {
697+
.then(function () {
698698
// We only update foo
699699
obj.set('foo', 'baz');
700700
return obj.save();
701701
})
702702
.then(
703-
function() {
703+
function () {
704704
// Make sure the checking has been triggered
705705
expect(triggerTime).toBe(2);
706706
done();
707707
},
708-
function(error) {
708+
function (error) {
709709
fail(error);
710710
done();
711711
}
712712
);
713713
});
714714

715-
it('test afterSave get original object on update', function(done) {
715+
it('test afterSave get original object on update', function (done) {
716716
let triggerTime = 0;
717717
// Register a mock beforeSave hook
718718

719-
Parse.Cloud.afterSave('GameScore', function(req) {
719+
Parse.Cloud.afterSave('GameScore', function (req) {
720720
const object = req.object;
721721
expect(object instanceof Parse.Object).toBeTruthy();
722722
expect(object.get('fooAgain')).toEqual('barAgain');
@@ -750,18 +750,18 @@ describe('miscellaneous', function() {
750750
obj.set('fooAgain', 'barAgain');
751751
obj
752752
.save()
753-
.then(function() {
753+
.then(function () {
754754
// We only update foo
755755
obj.set('foo', 'baz');
756756
return obj.save();
757757
})
758758
.then(
759-
function() {
759+
function () {
760760
// Make sure the checking has been triggered
761761
expect(triggerTime).toBe(2);
762762
done();
763763
},
764-
function(error) {
764+
function (error) {
765765
jfail(error);
766766
done();
767767
}
@@ -771,7 +771,7 @@ describe('miscellaneous', function() {
771771
it('test afterSave get full original object even req auth can not query it', done => {
772772
let triggerTime = 0;
773773
// Register a mock beforeSave hook
774-
Parse.Cloud.afterSave('GameScore', function(req) {
774+
Parse.Cloud.afterSave('GameScore', function (req) {
775775
const object = req.object;
776776
const originalObject = req.original;
777777
if (triggerTime == 0) {
@@ -802,18 +802,18 @@ describe('miscellaneous', function() {
802802
obj.setACL(acl);
803803
obj
804804
.save()
805-
.then(function() {
805+
.then(function () {
806806
// We only update foo
807807
obj.set('foo', 'baz');
808808
return obj.save();
809809
})
810810
.then(
811-
function() {
811+
function () {
812812
// Make sure the checking has been triggered
813813
expect(triggerTime).toBe(2);
814814
done();
815815
},
816-
function(error) {
816+
function (error) {
817817
jfail(error);
818818
done();
819819
}
@@ -823,7 +823,7 @@ describe('miscellaneous', function() {
823823
it('afterSave flattens custom operations', done => {
824824
let triggerTime = 0;
825825
// Register a mock beforeSave hook
826-
Parse.Cloud.afterSave('GameScore', function(req) {
826+
Parse.Cloud.afterSave('GameScore', function (req) {
827827
const object = req.object;
828828
expect(object instanceof Parse.Object).toBeTruthy();
829829
const originalObject = req.original;
@@ -865,7 +865,7 @@ describe('miscellaneous', function() {
865865
it('beforeSave receives ACL', done => {
866866
let triggerTime = 0;
867867
// Register a mock beforeSave hook
868-
Parse.Cloud.beforeSave('GameScore', function(req) {
868+
Parse.Cloud.beforeSave('GameScore', function (req) {
869869
const object = req.object;
870870
if (triggerTime == 0) {
871871
const acl = object.getACL();
@@ -909,7 +909,7 @@ describe('miscellaneous', function() {
909909
it('afterSave receives ACL', done => {
910910
let triggerTime = 0;
911911
// Register a mock beforeSave hook
912-
Parse.Cloud.afterSave('GameScore', function(req) {
912+
Parse.Cloud.afterSave('GameScore', function (req) {
913913
const object = req.object;
914914
if (triggerTime == 0) {
915915
const acl = object.getACL();
@@ -1057,14 +1057,14 @@ describe('miscellaneous', function() {
10571057
);
10581058
});
10591059

1060-
it('test beforeSave/afterSave get installationId', function(done) {
1060+
it('test beforeSave/afterSave get installationId', function (done) {
10611061
let triggerTime = 0;
1062-
Parse.Cloud.beforeSave('GameScore', function(req) {
1062+
Parse.Cloud.beforeSave('GameScore', function (req) {
10631063
triggerTime++;
10641064
expect(triggerTime).toEqual(1);
10651065
expect(req.installationId).toEqual('yolo');
10661066
});
1067-
Parse.Cloud.afterSave('GameScore', function(req) {
1067+
Parse.Cloud.afterSave('GameScore', function (req) {
10681068
triggerTime++;
10691069
expect(triggerTime).toEqual(2);
10701070
expect(req.installationId).toEqual('yolo');
@@ -1087,14 +1087,14 @@ describe('miscellaneous', function() {
10871087
});
10881088
});
10891089

1090-
it('test beforeDelete/afterDelete get installationId', function(done) {
1090+
it('test beforeDelete/afterDelete get installationId', function (done) {
10911091
let triggerTime = 0;
1092-
Parse.Cloud.beforeDelete('GameScore', function(req) {
1092+
Parse.Cloud.beforeDelete('GameScore', function (req) {
10931093
triggerTime++;
10941094
expect(triggerTime).toEqual(1);
10951095
expect(req.installationId).toEqual('yolo');
10961096
});
1097-
Parse.Cloud.afterDelete('GameScore', function(req) {
1097+
Parse.Cloud.afterDelete('GameScore', function (req) {
10981098
triggerTime++;
10991099
expect(triggerTime).toEqual(2);
11001100
expect(req.installationId).toEqual('yolo');
@@ -1170,33 +1170,6 @@ describe('miscellaneous', function() {
11701170
});
11711171
});
11721172

1173-
it('test cloud function parameter validation', done => {
1174-
// Register a function with validation
1175-
Parse.Cloud.define(
1176-
'functionWithParameterValidationFailure',
1177-
() => {
1178-
return 'noway';
1179-
},
1180-
request => {
1181-
return request.params.success === 100;
1182-
}
1183-
);
1184-
1185-
Parse.Cloud.run('functionWithParameterValidationFailure', {
1186-
success: 500,
1187-
}).then(
1188-
() => {
1189-
fail('Validation should not have succeeded');
1190-
done();
1191-
},
1192-
e => {
1193-
expect(e.code).toEqual(142);
1194-
expect(e.message).toEqual('Validation failed.');
1195-
done();
1196-
}
1197-
);
1198-
});
1199-
12001173
it('can handle null params in cloud functions (regression test for #1742)', done => {
12011174
Parse.Cloud.define('func', request => {
12021175
expect(request.params.nullParam).toEqual(null);
@@ -1715,10 +1688,7 @@ describe('miscellaneous', function() {
17151688

17161689
it('purge empty class', done => {
17171690
const testSchema = new Parse.Schema('UnknownClass');
1718-
testSchema
1719-
.purge()
1720-
.then(done)
1721-
.catch(done.fail);
1691+
testSchema.purge().then(done).catch(done.fail);
17221692
});
17231693

17241694
it('should not update schema beforeSave #2672', done => {

0 commit comments

Comments
 (0)