Skip to content

New: Validate Cloud Validators #7154

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 19 commits into from
Mar 1, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ ___
- 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).
- 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).
- 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).
- 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)
- 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)
- 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)
- 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).
Expand Down
188 changes: 164 additions & 24 deletions spec/CloudCode.Validator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ describe('cloud validator', () => {
},
async () => {
await new Promise(resolve => {
setTimeout(() => {
resolve();
}, 1000);
setTimeout(resolve, 1000);
});
throw 'async error';
}
Expand Down Expand Up @@ -132,7 +130,7 @@ describe('cloud validator', () => {
await Parse.Cloud.run('myFunction');
});

it('require user on cloud functions', done => {
it('require user on cloud functions', async done => {
Parse.Cloud.define(
'hello1',
() => {
Expand All @@ -142,16 +140,14 @@ describe('cloud validator', () => {
requireUser: true,
}
);

Parse.Cloud.run('hello1', {})
.then(() => {
fail('function should have failed.');
})
.catch(error => {
expect(error.code).toEqual(Parse.Error.VALIDATION_ERROR);
expect(error.message).toEqual('Validation failed. Please login to continue.');
done();
});
try {
await Parse.Cloud.run('hello1', {});
fail('function should have failed.');
} catch (error) {
expect(error.code).toEqual(Parse.Error.VALIDATION_ERROR);
expect(error.message).toEqual('Validation failed. Please login to continue.');
done();
}
});

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

const query = new Parse.Query('beforeFind');
try {
const first = await query.first({ useMasterKey: true });
expect(first).toBeDefined();
expect(first.id).toBe(obj.id);
done();
} catch (e) {
console.log(e);
console.log(e.code);
throw e;
}
const first = await query.first({ useMasterKey: true });
expect(first).toBeDefined();
expect(first.id).toBe(obj.id);
done();
});

it('basic beforeDelete skipWithMasterKey', async function (done) {
Expand Down Expand Up @@ -1429,6 +1419,156 @@ describe('cloud validator', () => {
}
});

it('does not log on valid config', () => {
Parse.Cloud.define('myFunction', () => {}, {
requireUser: true,
requireMaster: true,
validateMasterKey: false,
skipWithMasterKey: true,
requireUserKeys: {
Acc: {
constant: true,
options: ['A', 'B'],
required: true,
default: 'f',
error: 'a',
type: String,
},
},
fields: {
Acc: {
constant: true,
options: ['A', 'B'],
required: true,
default: 'f',
error: 'a',
type: String,
},
},
});
});
it('Logs on invalid config', () => {
const fields = [
{
field: 'requiredUser',
value: true,
error: 'requiredUser is not a supported parameter for Cloud Function validations.',
},
{
field: 'requireUser',
value: [],
error:
'Invalid type for Cloud Function validation key requireUser. Expected boolean, actual array',
},
{
field: 'requireMaster',
value: [],
error:
'Invalid type for Cloud Function validation key requireMaster. Expected boolean, actual array',
},
{
field: 'validateMasterKey',
value: [],
error:
'Invalid type for Cloud Function validation key validateMasterKey. Expected boolean, actual array',
},
{
field: 'skipWithMasterKey',
value: [],
error:
'Invalid type for Cloud Function validation key skipWithMasterKey. Expected boolean, actual array',
},
{
field: 'requireAllUserRoles',
value: true,
error:
'Invalid type for Cloud Function validation key requireAllUserRoles. Expected array|function, actual boolean',
},
{
field: 'requireAnyUserRoles',
value: true,
error:
'Invalid type for Cloud Function validation key requireAnyUserRoles. Expected array|function, actual boolean',
},
{
field: 'fields',
value: true,
error:
'Invalid type for Cloud Function validation key fields. Expected array|object, actual boolean',
},
{
field: 'requireUserKeys',
value: true,
error:
'Invalid type for Cloud Function validation key requireUserKeys. Expected array|object, actual boolean',
},
];
for (const field of fields) {
try {
Parse.Cloud.define('myFunction', () => {}, {
[field.field]: field.value,
});
fail(`Expected error registering invalid Cloud Function validation ${field.field}.`);
} catch (e) {
expect(e).toBe(field.error);
}
}
});

it('Logs on invalid config', () => {
const fields = [
{
field: 'otherKey',
value: true,
error: 'otherKey is not a supported parameter for Cloud Function validations.',
},
{
field: 'constant',
value: [],
error:
'Invalid type for Cloud Function validation key constant. Expected boolean, actual array',
},
{
field: 'required',
value: [],
error:
'Invalid type for Cloud Function validation key required. Expected boolean, actual array',
},
{
field: 'error',
value: [],
error:
'Invalid type for Cloud Function validation key error. Expected string, actual array',
},
];
for (const field of fields) {
try {
Parse.Cloud.define('myFunction', () => {}, {
fields: {
name: {
[field.field]: field.value,
},
},
});
fail(`Expected error registering invalid Cloud Function validation ${field.field}.`);
} catch (e) {
expect(e).toBe(field.error);
}
try {
Parse.Cloud.define('myFunction', () => {}, {
requireUserKeys: {
name: {
[field.field]: field.value,
},
},
});
fail(`Expected error registering invalid Cloud Function validation ${field.field}.`);
} catch (e) {
expect(e).toBe(field.error);
}
}
});

it('set params options function async', async () => {
Parse.Cloud.define(
'hello',
Expand Down
Loading