Skip to content

Commit

Permalink
feat(Config Schema): Schema for Cognito Pool events (#8105)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericbarthelet committed Aug 20, 2020
1 parent 82f6db7 commit 184cb48
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 107 deletions.
62 changes: 18 additions & 44 deletions lib/plugins/aws/package/compile/events/cognitoUserPool/index.js
Expand Up @@ -32,9 +32,15 @@ class AwsCompileCognitoUserPoolEvents {
'after:package:finalize': this.mergeWithCustomResources.bind(this),
};

// TODO: Complete schema, see https://github.com/serverless/serverless/issues/8028
this.serverless.configSchemaHandler.defineFunctionEvent('aws', 'cognitoUserPool', {
type: 'object',
properties: {
pool: { type: 'string', maxLength: 128, pattern: '^[\\w\\s+=,.@-]+$' },
trigger: { enum: validTriggerSources },
existing: { type: 'boolean' },
},
required: ['pool', 'trigger'],
additionalProperties: false,
});
}

Expand Down Expand Up @@ -260,50 +266,18 @@ class AwsCompileCognitoUserPoolEvents {
functionObj.events.forEach(event => {
if (event.cognitoUserPool) {
if (event.cognitoUserPool.existing) return;
// Check event definition for `cognitoUserPool` object
if (typeof event.cognitoUserPool === 'object') {
// Check `cognitoUserPool` object has required properties
if (!event.cognitoUserPool.pool || !event.cognitoUserPool.trigger) {
throw new this.serverless.classes.Error(
[
`Cognito User Pool event of function "${functionName}" is not an object.`,
'The correct syntax is an object with the "pool" and "trigger" properties.',
'Please check the docs for more info.',
].join(' ')
);
}

// Check `cognitoUserPool` trigger is valid
if (!validTriggerSources.includes(event.cognitoUserPool.trigger)) {
throw new this.serverless.classes.Error(
[
'Cognito User Pool trigger source is invalid, must be one of:',
`${validTriggerSources.join(', ')}.`,
'Please check the docs for more info.',
].join(' ')
);
}

// Save trigger functions so we can use them to generate
// IAM permissions later
cognitoUserPoolTriggerFunctions.push({
functionName,
poolName: event.cognitoUserPool.pool,
triggerSource: event.cognitoUserPool.trigger,
});

// Save user pools so we can use them to generate
// CloudFormation resources later
userPools.push(event.cognitoUserPool.pool);
} else {
throw new this.serverless.classes.Error(
[
`Cognito User Pool event of function "${functionName}" is not an object.`,
'The correct syntax is an object with the "pool" and "trigger" properties.',
'Please check the docs for more info.',
].join(' ')
);
}
// Save trigger functions so we can use them to generate
// IAM permissions later
cognitoUserPoolTriggerFunctions.push({
functionName,
poolName: event.cognitoUserPool.pool,
triggerSource: event.cognitoUserPool.trigger,
});

// Save user pools so we can use them to generate
// CloudFormation resources later
userPools.push(event.cognitoUserPool.pool);
}
});
}
Expand Down
Expand Up @@ -37,69 +37,6 @@ describe('AwsCompileCognitoUserPoolEvents', () => {
});

describe('#newCognitoUserPools()', () => {
it('should throw an error if cognitoUserPool event type is not an object', () => {
awsCompileCognitoUserPoolEvents.serverless.service.functions = {
first: {
events: [
{
cognitoUserPool: 42,
},
],
},
};

expect(() => awsCompileCognitoUserPoolEvents.newCognitoUserPools()).to.throw(Error);
});

it('should throw an error if the "pool" property is not given', () => {
awsCompileCognitoUserPoolEvents.serverless.service.functions = {
first: {
events: [
{
cognitoUserPool: {
pool: null,
},
},
],
},
};

expect(() => awsCompileCognitoUserPoolEvents.newCognitoUserPools()).to.throw(Error);
});

it('should throw an error if the "trigger" property is not given', () => {
awsCompileCognitoUserPoolEvents.serverless.service.functions = {
first: {
events: [
{
cognitoUserPool: {
trigger: null,
},
},
],
},
};

expect(() => awsCompileCognitoUserPoolEvents.newCognitoUserPools()).to.throw(Error);
});

it('should throw an error if the "trigger" property is invalid', () => {
awsCompileCognitoUserPoolEvents.serverless.service.functions = {
first: {
events: [
{
cognitoUserPool: {
pool: 'MyUserPool',
trigger: 'invalidTrigger',
},
},
],
},
};

expect(() => awsCompileCognitoUserPoolEvents.newCognitoUserPools()).to.throw(Error);
});

it('should create resources when CUP events are given as separate functions', () => {
awsCompileCognitoUserPoolEvents.serverless.service.functions = {
first: {
Expand Down

0 comments on commit 184cb48

Please sign in to comment.