New: allow options to be async on Cloud Validator (#7155)

* new: allow options to be async on Cloud Validator

* Update CHANGELOG.md

* Ensure pushStatus is properly running (#7213)

* Ensure pushStatus is properly running

* remove duplicate test

* new: allow options to be async on Cloud Validator

* Update CHANGELOG.md

* Update CloudCode.Validator.spec.js

Co-authored-by: Diamond Lewis <findlewis@gmail.com>
This commit is contained in:
dblythy
2021-02-23 19:56:54 +11:00
committed by GitHub
parent 047683219d
commit 91a0108084
3 changed files with 86 additions and 4 deletions

View File

@@ -1428,4 +1428,80 @@ describe('cloud validator', () => {
done();
}
});
it('set params options function async', async () => {
Parse.Cloud.define(
'hello',
() => {
return 'Hello world!';
},
{
fields: {
data: {
type: String,
required: true,
options: async val => {
await new Promise(resolve => {
setTimeout(resolve, 500);
});
return val === 'f';
},
error: 'Validation failed.',
},
},
}
);
try {
await Parse.Cloud.run('hello', { data: 'd' });
fail('validation should have failed');
} catch (error) {
expect(error.code).toEqual(Parse.Error.VALIDATION_ERROR);
expect(error.message).toEqual('Validation failed.');
}
const result = await Parse.Cloud.run('hello', { data: 'f' });
expect(result).toBe('Hello world!');
});
it('basic beforeSave requireUserKey as custom async function', async () => {
Parse.Cloud.beforeSave(Parse.User, () => {}, {
fields: {
accType: {
default: 'normal',
constant: true,
},
},
});
Parse.Cloud.define(
'secureFunction',
() => {
return "Here's all the secure data!";
},
{
requireUserKeys: {
accType: {
options: async val => {
await new Promise(resolve => {
setTimeout(resolve, 500);
});
return ['admin', 'admin2'].includes(val);
},
error: 'Unauthorized.',
},
},
}
);
const user = new Parse.User();
user.set('username', 'testuser');
user.set('password', 'p@ssword');
user.set('accType', 'admin');
await user.signUp();
expect(user.get('accType')).toBe('normal');
try {
await Parse.Cloud.run('secureFunction');
fail('function should only be available to admin users');
} catch (error) {
expect(error.code).toEqual(Parse.Error.VALIDATION_ERROR);
expect(error.message).toEqual('Unauthorized.');
}
});
});