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

@@ -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 = {