Skip to content

Commit 3833868

Browse files
dblythydavimacedo
andauthored
New: Validate Cloud Validators (#7154)
* new: validate cloud validators * add otherKey * Update CHANGELOG.md * Update CloudCode.Validator.spec.js * Update CloudCode.Validator.spec.js * new: validate cloud validators * add otherKey * Update CHANGELOG.md * Update CloudCode.Validator.spec.js * Update CloudCode.Validator.spec.js * Update Parse.Cloud.js * Update CHANGELOG.md * Change to throw error Co-authored-by: Antonio Davi Macedo Coelho de Castro <[email protected]>
1 parent cd78f89 commit 3833868

File tree

3 files changed

+244
-24
lines changed

3 files changed

+244
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ ___
2828
- IMPROVE: Added new account lockout policy option `accountLockout.unlockOnPasswordReset` to automatically unlock account on password reset. [#7146](https://github.com/parse-community/parse-server/pull/7146). Thanks to [Manuel Trezza](https://github.com/mtrezza).
2929
- IMPROVE: Parse Server is from now on continuously tested against all recent MongoDB versions that have not reached their end-of-life support date. Added MongoDB compatibility table to Parse Server docs. [7161](https://github.com/parse-community/parse-server/pull/7161). Thanks to [Manuel Trezza](https://github.com/mtrezza).
3030
- IMPROVE: Parse Server is from now on continuously tested against all recent Node.js versions that have not reached their end-of-life support date. [7161](https://github.com/parse-community/parse-server/pull/7177). Thanks to [Manuel Trezza](https://github.com/mtrezza).
31+
- IMPROVE: Throw error on invalid Cloud Function validation configuration. [#7154](https://github.com/parse-community/parse-server/pull/7154). Thanks to [dblythy](https://github.com/dblythy)
3132
- IMPROVE: Allow Cloud Validator `options` to be async [#7155](https://github.com/parse-community/parse-server/pull/7155). Thanks to [dblythy](https://github.com/dblythy)
3233
- IMPROVE: Optimize queries on classes with pointer permissions. [#7061](https://github.com/parse-community/parse-server/pull/7061). Thanks to [Pedro Diaz](https://github.com/pdiaz)
3334
- IMPROVE: Parse Server will from now on be continuously tested against all relevant Postgres versions (minor versions). Added Postgres compatibility table to Parse Server docs. [#7176](https://github.com/parse-community/parse-server/pull/7176). Thanks to [Corey Baker](https://github.com/cbaker6).

spec/CloudCode.Validator.spec.js

Lines changed: 164 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ describe('cloud validator', () => {
9292
},
9393
async () => {
9494
await new Promise(resolve => {
95-
setTimeout(() => {
96-
resolve();
97-
}, 1000);
95+
setTimeout(resolve, 1000);
9896
});
9997
throw 'async error';
10098
}
@@ -132,7 +130,7 @@ describe('cloud validator', () => {
132130
await Parse.Cloud.run('myFunction');
133131
});
134132

135-
it('require user on cloud functions', done => {
133+
it('require user on cloud functions', async done => {
136134
Parse.Cloud.define(
137135
'hello1',
138136
() => {
@@ -142,16 +140,14 @@ describe('cloud validator', () => {
142140
requireUser: true,
143141
}
144142
);
145-
146-
Parse.Cloud.run('hello1', {})
147-
.then(() => {
148-
fail('function should have failed.');
149-
})
150-
.catch(error => {
151-
expect(error.code).toEqual(Parse.Error.VALIDATION_ERROR);
152-
expect(error.message).toEqual('Validation failed. Please login to continue.');
153-
done();
154-
});
143+
try {
144+
await Parse.Cloud.run('hello1', {});
145+
fail('function should have failed.');
146+
} catch (error) {
147+
expect(error.code).toEqual(Parse.Error.VALIDATION_ERROR);
148+
expect(error.message).toEqual('Validation failed. Please login to continue.');
149+
done();
150+
}
155151
});
156152

157153
it('require master on cloud functions', done => {
@@ -605,16 +601,10 @@ describe('cloud validator', () => {
605601
expect(obj.get('foo')).toBe('bar');
606602

607603
const query = new Parse.Query('beforeFind');
608-
try {
609-
const first = await query.first({ useMasterKey: true });
610-
expect(first).toBeDefined();
611-
expect(first.id).toBe(obj.id);
612-
done();
613-
} catch (e) {
614-
console.log(e);
615-
console.log(e.code);
616-
throw e;
617-
}
604+
const first = await query.first({ useMasterKey: true });
605+
expect(first).toBeDefined();
606+
expect(first.id).toBe(obj.id);
607+
done();
618608
});
619609

620610
it('basic beforeDelete skipWithMasterKey', async function (done) {
@@ -1429,6 +1419,156 @@ describe('cloud validator', () => {
14291419
}
14301420
});
14311421

1422+
it('does not log on valid config', () => {
1423+
Parse.Cloud.define('myFunction', () => {}, {
1424+
requireUser: true,
1425+
requireMaster: true,
1426+
validateMasterKey: false,
1427+
skipWithMasterKey: true,
1428+
requireUserKeys: {
1429+
Acc: {
1430+
constant: true,
1431+
options: ['A', 'B'],
1432+
required: true,
1433+
default: 'f',
1434+
error: 'a',
1435+
type: String,
1436+
},
1437+
},
1438+
fields: {
1439+
Acc: {
1440+
constant: true,
1441+
options: ['A', 'B'],
1442+
required: true,
1443+
default: 'f',
1444+
error: 'a',
1445+
type: String,
1446+
},
1447+
},
1448+
});
1449+
});
1450+
it('Logs on invalid config', () => {
1451+
const fields = [
1452+
{
1453+
field: 'requiredUser',
1454+
value: true,
1455+
error: 'requiredUser is not a supported parameter for Cloud Function validations.',
1456+
},
1457+
{
1458+
field: 'requireUser',
1459+
value: [],
1460+
error:
1461+
'Invalid type for Cloud Function validation key requireUser. Expected boolean, actual array',
1462+
},
1463+
{
1464+
field: 'requireMaster',
1465+
value: [],
1466+
error:
1467+
'Invalid type for Cloud Function validation key requireMaster. Expected boolean, actual array',
1468+
},
1469+
{
1470+
field: 'validateMasterKey',
1471+
value: [],
1472+
error:
1473+
'Invalid type for Cloud Function validation key validateMasterKey. Expected boolean, actual array',
1474+
},
1475+
{
1476+
field: 'skipWithMasterKey',
1477+
value: [],
1478+
error:
1479+
'Invalid type for Cloud Function validation key skipWithMasterKey. Expected boolean, actual array',
1480+
},
1481+
{
1482+
field: 'requireAllUserRoles',
1483+
value: true,
1484+
error:
1485+
'Invalid type for Cloud Function validation key requireAllUserRoles. Expected array|function, actual boolean',
1486+
},
1487+
{
1488+
field: 'requireAnyUserRoles',
1489+
value: true,
1490+
error:
1491+
'Invalid type for Cloud Function validation key requireAnyUserRoles. Expected array|function, actual boolean',
1492+
},
1493+
{
1494+
field: 'fields',
1495+
value: true,
1496+
error:
1497+
'Invalid type for Cloud Function validation key fields. Expected array|object, actual boolean',
1498+
},
1499+
{
1500+
field: 'requireUserKeys',
1501+
value: true,
1502+
error:
1503+
'Invalid type for Cloud Function validation key requireUserKeys. Expected array|object, actual boolean',
1504+
},
1505+
];
1506+
for (const field of fields) {
1507+
try {
1508+
Parse.Cloud.define('myFunction', () => {}, {
1509+
[field.field]: field.value,
1510+
});
1511+
fail(`Expected error registering invalid Cloud Function validation ${field.field}.`);
1512+
} catch (e) {
1513+
expect(e).toBe(field.error);
1514+
}
1515+
}
1516+
});
1517+
1518+
it('Logs on invalid config', () => {
1519+
const fields = [
1520+
{
1521+
field: 'otherKey',
1522+
value: true,
1523+
error: 'otherKey is not a supported parameter for Cloud Function validations.',
1524+
},
1525+
{
1526+
field: 'constant',
1527+
value: [],
1528+
error:
1529+
'Invalid type for Cloud Function validation key constant. Expected boolean, actual array',
1530+
},
1531+
{
1532+
field: 'required',
1533+
value: [],
1534+
error:
1535+
'Invalid type for Cloud Function validation key required. Expected boolean, actual array',
1536+
},
1537+
{
1538+
field: 'error',
1539+
value: [],
1540+
error:
1541+
'Invalid type for Cloud Function validation key error. Expected string, actual array',
1542+
},
1543+
];
1544+
for (const field of fields) {
1545+
try {
1546+
Parse.Cloud.define('myFunction', () => {}, {
1547+
fields: {
1548+
name: {
1549+
[field.field]: field.value,
1550+
},
1551+
},
1552+
});
1553+
fail(`Expected error registering invalid Cloud Function validation ${field.field}.`);
1554+
} catch (e) {
1555+
expect(e).toBe(field.error);
1556+
}
1557+
try {
1558+
Parse.Cloud.define('myFunction', () => {}, {
1559+
requireUserKeys: {
1560+
name: {
1561+
[field.field]: field.value,
1562+
},
1563+
},
1564+
});
1565+
fail(`Expected error registering invalid Cloud Function validation ${field.field}.`);
1566+
} catch (e) {
1567+
expect(e).toBe(field.error);
1568+
}
1569+
}
1570+
});
1571+
14321572
it('set params options function async', async () => {
14331573
Parse.Cloud.define(
14341574
'hello',

0 commit comments

Comments
 (0)