feat: Add Cloud Code triggers Parse.Cloud.beforeSave and Parse.Cloud.afterSave for Parse Config (#9232)

This commit is contained in:
Diamond Lewis
2024-07-20 13:35:41 -05:00
committed by GitHub
parent 4d86ace2cc
commit 90a1e4a200
5 changed files with 308 additions and 5 deletions

View File

@@ -6,6 +6,9 @@ const validatorFail = () => {
const validatorSuccess = () => {
return true;
};
function testConfig() {
return Parse.Config.save({ internal: 'i', string: 's', number: 12 }, { internal: true });
}
describe('cloud validator', () => {
it('complete validator', async done => {
@@ -731,6 +734,38 @@ describe('cloud validator', () => {
done();
});
it('basic beforeSave Parse.Config skipWithMasterKey', async () => {
Parse.Cloud.beforeSave(
Parse.Config,
() => {
throw 'beforeSaveFile should have resolved using master key.';
},
{
skipWithMasterKey: true,
}
);
const config = await testConfig();
expect(config.get('internal')).toBe('i');
expect(config.get('string')).toBe('s');
expect(config.get('number')).toBe(12);
});
it('basic afterSave Parse.Config skipWithMasterKey', async () => {
Parse.Cloud.afterSave(
Parse.Config,
() => {
throw 'beforeSaveFile should have resolved using master key.';
},
{
skipWithMasterKey: true,
}
);
const config = await testConfig();
expect(config.get('internal')).toBe('i');
expect(config.get('string')).toBe('s');
expect(config.get('number')).toBe(12);
});
it('beforeSave validateMasterKey and skipWithMasterKey fail', async function (done) {
Parse.Cloud.beforeSave(
'BeforeSave',
@@ -1441,7 +1476,7 @@ describe('cloud validator', () => {
});
it('validate afterSaveFile fail', async done => {
Parse.Cloud.beforeSave(Parse.File, () => {}, validatorFail);
Parse.Cloud.afterSave(Parse.File, () => {}, validatorFail);
try {
const file = new Parse.File('popeye.txt', [1, 2, 3], 'text/plain');
await file.save({ useMasterKey: true });
@@ -1496,6 +1531,42 @@ describe('cloud validator', () => {
}
});
it('validate beforeSave Parse.Config', async () => {
Parse.Cloud.beforeSave(Parse.Config, () => {}, validatorSuccess);
const config = await testConfig();
expect(config.get('internal')).toBe('i');
expect(config.get('string')).toBe('s');
expect(config.get('number')).toBe(12);
});
it('validate beforeSave Parse.Config fail', async () => {
Parse.Cloud.beforeSave(Parse.Config, () => {}, validatorFail);
try {
await testConfig();
fail('cloud function should have failed.');
} catch (e) {
expect(e.code).toBe(Parse.Error.VALIDATION_ERROR);
}
});
it('validate afterSave Parse.Config', async () => {
Parse.Cloud.afterSave(Parse.Config, () => {}, validatorSuccess);
const config = await testConfig();
expect(config.get('internal')).toBe('i');
expect(config.get('string')).toBe('s');
expect(config.get('number')).toBe(12);
});
it('validate afterSave Parse.Config fail', async () => {
Parse.Cloud.afterSave(Parse.Config, () => {}, validatorFail);
try {
await testConfig();
fail('cloud function should have failed.');
} catch (e) {
expect(e.code).toBe(Parse.Error.VALIDATION_ERROR);
}
});
it('Should have validator', async done => {
Parse.Cloud.define(
'myFunction',

View File

@@ -3921,6 +3921,162 @@ describe('saveFile hooks', () => {
});
});
describe('Cloud Config hooks', () => {
function testConfig() {
return Parse.Config.save({ internal: 'i', string: 's', number: 12 }, { internal: true });
}
it('beforeSave(Parse.Config) can run hook with new config', async () => {
let count = 0;
Parse.Cloud.beforeSave(Parse.Config, (req) => {
expect(req.object).toBeDefined();
expect(req.original).toBeUndefined();
expect(req.user).toBeUndefined();
expect(req.headers).toBeDefined();
expect(req.ip).toBeDefined();
expect(req.installationId).toBeDefined();
expect(req.context).toBeDefined();
const config = req.object;
expect(config.get('internal')).toBe('i');
expect(config.get('string')).toBe('s');
expect(config.get('number')).toBe(12);
count += 1;
});
await testConfig();
const config = await Parse.Config.get({ useMasterKey: true });
expect(config.get('internal')).toBe('i');
expect(config.get('string')).toBe('s');
expect(config.get('number')).toBe(12);
expect(count).toBe(1);
});
it('beforeSave(Parse.Config) can run hook with existing config', async () => {
let count = 0;
Parse.Cloud.beforeSave(Parse.Config, (req) => {
if (count === 0) {
expect(req.object.get('number')).toBe(12);
expect(req.original).toBeUndefined();
}
if (count === 1) {
expect(req.object.get('number')).toBe(13);
expect(req.original.get('number')).toBe(12);
}
count += 1;
});
await testConfig();
await Parse.Config.save({ number: 13 });
expect(count).toBe(2);
});
it('beforeSave(Parse.Config) should not change config if nothing is returned', async () => {
let count = 0;
Parse.Cloud.beforeSave(Parse.Config, () => {
count += 1;
return;
});
await testConfig();
const config = await Parse.Config.get({ useMasterKey: true });
expect(config.get('internal')).toBe('i');
expect(config.get('string')).toBe('s');
expect(config.get('number')).toBe(12);
expect(count).toBe(1);
});
it('beforeSave(Parse.Config) throw custom error', async () => {
Parse.Cloud.beforeSave(Parse.Config, () => {
throw new Parse.Error(Parse.Error.SCRIPT_FAILED, 'It should fail');
});
try {
await testConfig();
fail('error should have thrown');
} catch (e) {
expect(e.code).toBe(Parse.Error.SCRIPT_FAILED);
expect(e.message).toBe('It should fail');
}
});
it('beforeSave(Parse.Config) throw string error', async () => {
Parse.Cloud.beforeSave(Parse.Config, () => {
throw 'before save failed';
});
try {
await testConfig();
fail('error should have thrown');
} catch (e) {
expect(e.code).toBe(Parse.Error.SCRIPT_FAILED);
expect(e.message).toBe('before save failed');
}
});
it('beforeSave(Parse.Config) throw empty error', async () => {
Parse.Cloud.beforeSave(Parse.Config, () => {
throw null;
});
try {
await testConfig();
fail('error should have thrown');
} catch (e) {
expect(e.code).toBe(Parse.Error.SCRIPT_FAILED);
expect(e.message).toBe('Script failed. Unknown error.');
}
});
it('afterSave(Parse.Config) can run hook with new config', async () => {
let count = 0;
Parse.Cloud.afterSave(Parse.Config, (req) => {
expect(req.object).toBeDefined();
expect(req.original).toBeUndefined();
expect(req.user).toBeUndefined();
expect(req.headers).toBeDefined();
expect(req.ip).toBeDefined();
expect(req.installationId).toBeDefined();
expect(req.context).toBeDefined();
const config = req.object;
expect(config.get('internal')).toBe('i');
expect(config.get('string')).toBe('s');
expect(config.get('number')).toBe(12);
count += 1;
});
await testConfig();
const config = await Parse.Config.get({ useMasterKey: true });
expect(config.get('internal')).toBe('i');
expect(config.get('string')).toBe('s');
expect(config.get('number')).toBe(12);
expect(count).toBe(1);
});
it('afterSave(Parse.Config) can run hook with existing config', async () => {
let count = 0;
Parse.Cloud.afterSave(Parse.Config, (req) => {
if (count === 0) {
expect(req.object.get('number')).toBe(12);
expect(req.original).toBeUndefined();
}
if (count === 1) {
expect(req.object.get('number')).toBe(13);
expect(req.original.get('number')).toBe(12);
}
count += 1;
});
await testConfig();
await Parse.Config.save({ number: 13 });
expect(count).toBe(2);
});
it('afterSave(Parse.Config) should throw error', async () => {
Parse.Cloud.afterSave(Parse.Config, () => {
throw new Parse.Error(400, 'It should fail');
});
try {
await testConfig();
fail('error should have thrown');
} catch (e) {
expect(e.code).toBe(400);
expect(e.message).toBe('It should fail');
}
});
});
describe('sendEmail', () => {
it('can send email via Parse.Cloud', async done => {
const emailAdapter = {