UNPKG

@incdevco/framework

Version:
1,704 lines (1,106 loc) 42.7 kB
var Expect = require('chai').expect; var Mock = require('../../mock'); var Promise = require('bluebird'); var sinon = require('sinon'); var Lambda = require('./index'); var sandbox = sinon.sandbox.create(); process.on('unhandledRejection', function (reason) { console.log('unhandledRejection', reason); }); describe('lambda cloud-formation-custom-resource', function () { 'use strict'; var config, event, lambda, mock; beforeEach(function () { config = { apiGateway: {}, firehose: {}, waitForDelay: 1, warmUpDelay: 1 }; event = {}; lambda = new Lambda(config); mock = new Mock(); sandbox.stub(lambda, 'log'); }); afterEach(function () { mock.restore(); sandbox.verifyAndRestore(); }); /* describe('ApiGatewayCustomDomainName', function() { var event, expected, options, response; beforeEach(function() { event = {}; options = {}; expected = {}; response = { Data: {} }; }); it('should createDomainName when RequestType is Create and return a successful response', function (done) { event.RequestType = 'Create'; options.domainName = 'domainName'; options.certificateArn = 'certificateArn'; event.ResourceProperties = { Options: options }; mock.mock(lambda.apiGateway) .expect('createDomainName') .with(options) .willReturnAwsPromiseResolve({ distributionDomainName: 'distributionDomainName' }); lambda.ApiGatewayCustomDomainName(event, response) .then(function (result) { Expect(result).to.equal(true, 'result'); Expect(response.Status).to.equal('SUCCESS', 'response.Status'); Expect(response.PhysicalResourceId).to.equal('distributionDomainName', 'response.PhysicalResourceId'); return mock.done(done); }) .catch(done); }); it('should do nothing when RequestType is Update and return a successful response', function (done) { event.PhysicalResourceId = 'PhysicalResourceId'; event.RequestType = 'Update'; event.ResourceProperties = { Options: options }; lambda.ApiGatewayCustomDomainName(event, response) .then(function (result) { Expect(result).to.equal(true, 'result'); Expect(response.Status).to.equal('SUCCESS', 'response.Status'); Expect(response.PhysicalResourceId).to.equal(event.PhysicalResourceId, 'response.PhysicalResourceId'); return mock.done(done); }) .catch(done); }); it('should deleteDomainName when RequestType is Delete and return a successful response', function (done) { event.RequestType = 'Delete'; options.domainName = 'domainName'; event.ResourceProperties = { Options: options }; mock.mock(lambda.apiGateway) .expect('deleteDomainName') .with(options) .willReturnAwsPromiseResolve({}); lambda.ApiGatewayCustomDomainName(event, response) .then(function (result) { Expect(result).to.equal(true, 'result'); Expect(response.Status).to.equal('SUCCESS', 'response.Status'); Expect(response.PhysicalResourceId).to.equal('ApiGatewayCustomDomainName', 'response.PhysicalResourceId'); return mock.done(done); }) .catch(done); }); it('should catch any exception and return a failed response', function (done) { event.RequestType = 'Create'; options.domainName = 'domainName'; options.certificateArn = 'certificateArn'; event.ResourceProperties = { Options: options }; mock.mock(lambda.apiGateway) .expect('createDomainName') .with(options) .willReturnAwsPromiseReject(expected); lambda.ApiGatewayCustomDomainName(event, response) .then(function (result) { Expect(response.Status).to.equal('FAILED', 'response.Status'); return mock.done(done); }) .catch(done); }); }); */ describe('handler', function() { var context, event; beforeEach(function () { context = {}; event = {}; }); }); describe('ApiGatewayCustomDomainName', function() { var event, expected, options, response; beforeEach(function() { event = {}; options = {}; expected = {}; response = { Data: {} }; }); it('should deleteDomainName when Delete and return a successful response', function (done) { event.RequestType = 'Delete'; event.PhysicalResourceId = 'id'; options.domainName = 'domainName'; event.ResourceProperties = { Options: options }; mock.mock(lambda.apiGateway) .expect('deleteDomainName') .with({ domainName: options.domainName }) .willReturnAwsPromiseResolve(expected); lambda.ApiGatewayCustomDomainName(event, response) .then(function (result) { Expect(result).to.equal(true, 'result'); Expect(response.Status).to.equal('SUCCESS', 'response.Status'); Expect(response.PhysicalResourceId).to.equal(event.PhysicalResourceId, 'response.PhysicalResourceId'); return mock.done(done); }) .catch(done); }); it('should catch NotFoundException when Delete and return a successful response', function (done) { event.RequestType = 'Delete'; event.PhysicalResourceId = 'id'; options.domainName = 'domainName'; event.ResourceProperties = { Options: options }; mock.mock(lambda.apiGateway) .expect('deleteDomainName') .with({ domainName: options.domainName }) .willReturnAwsPromiseReject({ code: 'NotFoundException' }); lambda.ApiGatewayCustomDomainName(event, response) .then(function (result) { Expect(result).to.equal(true, 'result'); Expect(response.Status).to.equal('SUCCESS', 'response.Status'); Expect(response.PhysicalResourceId).to.equal(event.PhysicalResourceId, 'response.PhysicalResourceId'); return mock.done(done); }) .catch(done); }); it('should catch any exception and return a failed response', function (done) { event.RequestType = 'Delete'; event.PhysicalResourceId = 'id'; options.domainName = 'domainName'; event.ResourceProperties = { Options: options }; mock.mock(lambda.apiGateway) .expect('deleteDomainName') .with({ domainName: options.domainName }) .willReturnAwsPromiseReject(expected); lambda.ApiGatewayCustomDomainName(event, response) .then(function (result) { Expect(result).to.equal(false, 'result'); Expect(response.Status).to.equal('FAILED', 'response.Status'); return mock.done(done); }) .catch(done); }); }); describe('ApiGatewayRequestValidator', function() { var event, expected, options, response; beforeEach(function() { event = {}; options = {}; expected = {}; response = { Data: {} }; }); it('should createRequestValidator when Create and return a successful response', function (done) { event.RequestType = 'Create'; options.restApiId = 'restApiId'; options.name = 'name'; options.validateRequestBody = 'true'; options.validateRequestParameters = 'false'; event.ResourceProperties = { Options: options }; expected.id = 'id'; mock.mock(lambda.apiGateway) .expect('createRequestValidator') .with({ restApiId: options.restApiId, name: options.name, validateRequestBody: true, validateRequestParameters: false }) .willReturnAwsPromiseResolve(expected); lambda.ApiGatewayRequestValidator(event, response) .then(function (result) { Expect(result).to.equal(true, 'result'); Expect(response.Status).to.equal('SUCCESS', 'response.Status'); Expect(response.PhysicalResourceId).to.equal(expected.id, 'response.PhysicalResourceId'); Expect(response.Data.id).to.equal(expected.id, 'response.Data.id'); return mock.done(done); }) .catch(done); }); it('should deleteRequestValidator when Delete and return a successful response', function (done) { event.RequestType = 'Delete'; event.PhysicalResourceId = 'id'; options.restApiId = 'restApiId'; event.ResourceProperties = { Options: options }; mock.mock(lambda.apiGateway) .expect('deleteRequestValidator') .with({ requestValidatorId: event.PhysicalResourceId, restApiId: options.restApiId }) .willReturnAwsPromiseResolve(expected); lambda.ApiGatewayRequestValidator(event, response) .then(function (result) { Expect(result).to.equal(true, 'result'); Expect(response.Status).to.equal('SUCCESS', 'response.Status'); Expect(response.PhysicalResourceId).to.equal(event.PhysicalResourceId, 'response.PhysicalResourceId'); return mock.done(done); }) .catch(done); }); it('should catch NotFoundException when Delete and return a successful response', function (done) { event.RequestType = 'Delete'; event.PhysicalResourceId = 'id'; options.restApiId = 'restApiId'; event.ResourceProperties = { Options: options }; mock.mock(lambda.apiGateway) .expect('deleteRequestValidator') .with({ requestValidatorId: event.PhysicalResourceId, restApiId: options.restApiId }) .willReturnAwsPromiseReject({ code: 'NotFoundException' }); lambda.ApiGatewayRequestValidator(event, response) .then(function (result) { Expect(result).to.equal(true, 'result'); Expect(response.Status).to.equal('SUCCESS', 'response.Status'); Expect(response.PhysicalResourceId).to.equal(event.PhysicalResourceId, 'response.PhysicalResourceId'); return mock.done(done); }) .catch(done); }); it('should updateRequestValidator when Update and return a successful response', function (done) { event.RequestType = 'Update'; event.PhysicalResourceId = 'id'; options.restApiId = 'restApiId'; options.validateRequestBody = 'false'; options.validateRequestParameters = 'true'; event.ResourceProperties = { Options: options }; mock.mock(lambda.apiGateway) .expect('updateRequestValidator') .with({ requestValidatorId: event.PhysicalResourceId, restApiId: options.restApiId, patchOperations: [ { op: 'replace', path: '/validateRequestBody', value: 'false' }, { op: 'replace', path: '/validateRequestParameters', value: 'true' } ] }) .willReturnAwsPromiseResolve(expected); lambda.ApiGatewayRequestValidator(event, response) .then(function (result) { Expect(result).to.equal(true, 'result'); Expect(response.Status).to.equal('SUCCESS', 'response.Status'); Expect(response.PhysicalResourceId).to.equal(event.PhysicalResourceId, 'response.PhysicalResourceId'); return mock.done(done); }) .catch(done); }); it('should catch any exception and return a failed response', function (done) { event.RequestType = 'Delete'; event.PhysicalResourceId = 'id'; options.restApiId = 'restApiId'; event.ResourceProperties = { Options: options }; mock.mock(lambda.apiGateway) .expect('deleteRequestValidator') .with({ requestValidatorId: event.PhysicalResourceId, restApiId: options.restApiId }) .willReturnAwsPromiseReject(expected); lambda.ApiGatewayRequestValidator(event, response) .then(function (result) { Expect(result).to.equal(false, 'result'); Expect(response.Status).to.equal('FAILED', 'response.Status'); return mock.done(done); }) .catch(done); }); }); describe('ApiGatewayMethodRequestValidator', function() { var event, expected, options, response; beforeEach(function() { event = {}; options = {}; expected = {}; response = { Data: {} }; }); it('should updateMethod when Create and return a successful response', function (done) { event.RequestType = 'Create'; options.restApiId = 'restApiId'; options.requestValidatorId = 'requestValidatorId'; event.ResourceProperties = { Options: options }; expected.id = 'id'; mock.mock(lambda.apiGateway) .expect('updateMethod') .with({ httpMethod: options.httpMethod, resourceId: options.resourceId, restApiId: options.restApiId, patchOperations: [ { op: 'replace', path: '/requestValidatorId', value: options.requestValidatorId } ] }) .willReturnAwsPromiseResolve(expected); lambda.ApiGatewayMethodRequestValidator(event, response) .then(function (result) { Expect(result).to.equal(true, 'result'); Expect(response.Status).to.equal('SUCCESS', 'response.Status'); return mock.done(done); }) .catch(done); }); it('should do nothing when Delete and return a successful response', function (done) { event.RequestType = 'Delete'; options.restApiId = 'restApiId'; options.requestValidatorId = 'requestValidatorId'; event.ResourceProperties = { Options: options }; lambda.ApiGatewayMethodRequestValidator(event, response) .then(function (result) { Expect(result).to.equal(true, 'result'); Expect(response.Status).to.equal('SUCCESS', 'response.Status'); return mock.done(done); }) .catch(done); }); it('should updateMethod when Update and return a successful response', function (done) { event.RequestType = 'Update'; options.restApiId = 'restApiId'; options.requestValidatorId = 'requestValidatorId'; event.ResourceProperties = { Options: options }; expected.id = 'id'; mock.mock(lambda.apiGateway) .expect('updateMethod') .with({ httpMethod: options.httpMethod, resourceId: options.resourceId, restApiId: options.restApiId, patchOperations: [ { op: 'replace', path: '/requestValidatorId', value: options.requestValidatorId } ] }) .willReturnAwsPromiseResolve(expected); lambda.ApiGatewayMethodRequestValidator(event, response) .then(function (result) { Expect(result).to.equal(true, 'result'); Expect(response.Status).to.equal('SUCCESS', 'response.Status'); return mock.done(done); }) .catch(done); }); it('should catch any exception and return a FAILED response', function (done) { event.RequestType = 'Create'; options.restApiId = 'restApiId'; options.requestValidatorId = 'requestValidatorId'; event.ResourceProperties = { Options: options }; expected.id = 'id'; mock.mock(lambda.apiGateway) .expect('updateMethod') .with({ httpMethod: options.httpMethod, resourceId: options.resourceId, restApiId: options.restApiId, patchOperations: [ { op: 'replace', path: '/requestValidatorId', value: options.requestValidatorId } ] }) .willReturnAwsPromiseReject(expected); lambda.ApiGatewayMethodRequestValidator(event, response) .then(function (result) { Expect(result).to.equal(false, 'result'); Expect(response.Status).to.equal('FAILED', 'response.Status'); return mock.done(done); }) .catch(done); }); }); describe('ApiGatewayUsagePlanKey', function() { var event, expected, options, response; beforeEach(function() { event = {}; options = {}; expected = {}; response = { Data: {} }; }); it('should createUsagePlanKey when Create and return a successful response', function (done) { event.RequestType = 'Create'; options.ApiKeyId = 'ApiKeyId'; options.UsagePlanId = 'UsagePlanId'; event.ResourceProperties = { Options: options }; expected.id = 'id'; mock.mock(lambda.apiGateway) .expect('createUsagePlanKey') .with({ keyId: options.ApiKeyId, keyType: 'API_KEY', usagePlanId: options.UsagePlanId }) .willReturnAwsPromiseResolve(expected); lambda.ApiGatewayUsagePlanKey(event, response) .then(function (result) { Expect(result).to.equal(true, 'result'); Expect(response.Status).to.equal('SUCCESS', 'response.Status'); return mock.done(done); }) .catch(done); }); it('should deleteUsagePlanKey when Delete and return a successful response', function (done) { event.RequestType = 'Delete'; options.ApiKeyId = 'ApiKeyId'; options.UsagePlanId = 'UsagePlanId'; event.ResourceProperties = { Options: options }; expected.id = 'id'; mock.mock(lambda.apiGateway) .expect('deleteUsagePlanKey') .with({ keyId: options.ApiKeyId, usagePlanId: options.UsagePlanId }) .willReturnAwsPromiseResolve(expected); lambda.ApiGatewayUsagePlanKey(event, response) .then(function (result) { Expect(result).to.equal(true, 'result'); Expect(response.Status).to.equal('SUCCESS', 'response.Status'); return mock.done(done); }) .catch(done); }); it('should catch NotFoundException when Delete and return a successful response', function (done) { event.RequestType = 'Delete'; options.ApiKeyId = 'ApiKeyId'; options.UsagePlanId = 'UsagePlanId'; event.ResourceProperties = { Options: options }; expected = new Error('Not Found'); expected.code = 'NotFoundException'; mock.mock(lambda.apiGateway) .expect('deleteUsagePlanKey') .with({ keyId: options.ApiKeyId, usagePlanId: options.UsagePlanId }) .willReturnAwsPromiseReject(expected); lambda.ApiGatewayUsagePlanKey(event, response) .then(function (result) { Expect(result).to.equal(true, 'result'); Expect(response.Status).to.equal('SUCCESS', 'response.Status'); return mock.done(done); }) .catch(done); }); it('should catch any exception and return a failed response', function (done) { event.RequestType = 'Create'; options.ApiKeyId = 'ApiKeyId'; options.UsagePlanId = 'UsagePlanId'; event.ResourceProperties = { Options: options }; expected.id = 'id'; mock.mock(lambda.apiGateway) .expect('createUsagePlanKey') .with({ keyId: options.ApiKeyId, keyType: 'API_KEY', usagePlanId: options.UsagePlanId }) .willReturnAwsPromiseReject(expected); lambda.ApiGatewayUsagePlanKey(event, response) .then(function (result) { Expect(response.Status).to.equal('FAILED', 'response.Status'); return mock.done(done); }) .catch(done); }); }); describe('CloudWatchSubscriptionFilter', function() { var event, expected, options, response; beforeEach(function() { event = {}; options = { destinationArn: 'destinationArn' }; expected = {}; response = { Data: {} }; }); it('should putSubscriptionFilter when RequestType is Create and return a successful response', function (done) { event.RequestType = 'Create'; options.filterName = 'filterName'; options.logGroupName = 'logGroupName'; event.ResourceProperties = { Options: options }; mock.mock(lambda.cloudwatchlogs) .expect('putSubscriptionFilter') .with(options) .willReturnAwsPromiseResolve(expected); lambda.CloudWatchSubscriptionFilter(event, response) .then(function (result) { Expect(result).to.equal(true, 'result'); Expect(response.Status).to.equal('SUCCESS', 'response.Status'); Expect(response.PhysicalResourceId).to.equal(options.logGroupName + ':' + options.filterName, 'response.PhysicalResourceId'); return mock.done(done); }) .catch(done); }); it('should wait till firehose is active and return a successful response', function (done) { event.RequestType = 'Create'; options.destinationArn = 'arn:aws:firehose:us-east-1:123456789012:deliverystream/example-stream-name'; options.filterName = 'filterName'; options.logGroupName = 'logGroupName'; event.ResourceProperties = { Options: options }; mock.mock(lambda) .expect('waitForFirehose') .with(options.destinationArn) .willResolve(true); mock.mock(lambda.cloudwatchlogs) .expect('putSubscriptionFilter') .with(options) .willReturnAwsPromiseResolve(expected); lambda.CloudWatchSubscriptionFilter(event, response) .then(function (result) { Expect(result).to.equal(true, 'result'); Expect(response.Status).to.equal('SUCCESS', 'response.Status'); Expect(response.PhysicalResourceId).to.equal(options.logGroupName + ':' + options.filterName, 'response.PhysicalResourceId'); return mock.done(done); }) .catch(done); }); it('should putSubscriptionFilter when RequestType is Update and return a successful response', function (done) { event.RequestType = 'Update'; options.filterName = 'filterName'; options.logGroupName = 'logGroupName'; event.ResourceProperties = { Options: options }; mock.mock(lambda.cloudwatchlogs) .expect('putSubscriptionFilter') .with(options) .willReturnAwsPromiseResolve(expected); lambda.CloudWatchSubscriptionFilter(event, response) .then(function (result) { Expect(result).to.equal(true, 'result'); Expect(response.Status).to.equal('SUCCESS', 'response.Status'); Expect(response.PhysicalResourceId).to.equal(options.logGroupName + ':' + options.filterName, 'response.PhysicalResourceId'); return mock.done(done); }) .catch(done); }); it('should catch any exception and return a failed response', function (done) { expected = new Error('expected error'); event.RequestType = 'Create'; options.filterName = 'filterName'; options.logGroupName = 'logGroupName'; event.ResourceProperties = { Options: options }; mock.mock(lambda.cloudwatchlogs) .expect('putSubscriptionFilter') .with(options) .willReturnAwsPromiseReject(expected); lambda.CloudWatchSubscriptionFilter(event, response) .then(function (result) { Expect(response.Status).to.equal('FAILED', 'response.Status'); return mock.done(done); }) .catch(done); }); }); describe('LambdaEnvironment', function() { var event, expected, options, response; beforeEach(function() { event = {}; options = {}; expected = {}; response = { Data: {} }; }); it('should update function configuration and return a successful response', function (done) { event.RequestType = 'Create'; options.Environment = { Variables: { 'key': 'value' } }; options.FunctionName = 'FunctionName'; event.ResourceProperties = { Options: options }; expected.RuleSetName = options.RuleSetName; mock.mock(lambda.lambda) .expect('updateFunctionConfiguration') .with({ FunctionName: options.FunctionName, Environment: options.Environment }) .willReturnAwsPromiseResolve(expected); lambda.LambdaEnvironment(event, response) .then(function (result) { Expect(result).to.equal(true, 'result'); Expect(response.Status).to.equal('SUCCESS', 'response.Status'); Expect(response.PhysicalResourceId).to.equal('LambdaEnvironment', 'response.PhysicalResourceId'); return mock.done(done); }) .catch(done); }); it('should do nothing and return a successful response', function (done) { event.RequestType = 'Delete'; event.ResourceProperties = { Options: options }; lambda.LambdaEnvironment(event, response) .then(function (result) { Expect(result).to.equal(true, 'result'); Expect(response.Status).to.equal('SUCCESS', 'response.Status'); Expect(response.PhysicalResourceId).to.equal('LambdaEnvironment', 'response.PhysicalResourceId'); return mock.done(done); }) .catch(done); }); it('should catch any exception and return a failed response', function (done) { event.RequestType = 'Create'; options.Environment = { Variables: { 'key': 'value' } }; options.FunctionName = 'FunctionName'; event.ResourceProperties = { Options: options }; expected = new Error('expected'); mock.mock(lambda.lambda) .expect('updateFunctionConfiguration') .with({ FunctionName: options.FunctionName, Environment: options.Environment }) .willReturnAwsPromiseReject(expected); lambda.LambdaEnvironment(event, response) .then(function (result) { Expect(result).to.equal(false, 'result'); Expect(response.Status).to.equal('FAILED', 'response.Status'); Expect(response.Reason).to.equal(expected.message, 'response.Reason'); return mock.done(done); }) .catch(done); }); }); describe('sendCFNResponse', function() { var event, response; beforeEach(function () { event = {}; response = {}; }); it('should', function (done) { var body, httpRequest = {}, httpResponse = {}; event.ResponseURL = 'https://www.amazon.com/response' event.StackId = 'StackId'; event.RequestId = 'RequestId'; event.LogicalResourceId = 'LogicalResourceId'; response.StackId = event.StackId; response.RequestId = event.RequestId; response.LogicalResourceId = event.LogicalResourceId; body = JSON.stringify(response); mock.mock(lambda.https) .expect('request') .with({ protocol: 'https:', hostname: 'www.amazon.com', path: '/response', port: 443, method: 'PUT', headers: { 'Content-Type': '', 'Content-Length': body.length } }) .willReturn(httpRequest); var mocked = mock.mock(httpRequest); mocked.expect('on') .with('error') .willReturn(true); mocked.expect('on') .with('response') .willExecuteCallback(httpResponse); mocked.expect('write') .with(body) .willReturn(true); mocked.expect('end') .willReturn(true); mocked = mock.mock(httpResponse); mocked.expect('on') .with('error') .willReturn(true); mocked.expect('on') .with('data') .willReturn(true); mocked.expect('on') .with('end') .willExecuteCallback(); lambda.sendCFNResponse(event, response) .then(function () { return mock.done(done); }) .catch(done); }); }); describe('S3BucketEmptyBeforeDelete', function() { var event, expected, options, response; beforeEach(function() { event = {}; options = { Bucket: 'Bucket' }; expected = {}; response = { Data: {} }; }); it('should s3DeleteObjectsFromBucket when RequestType is Delete and return a successful response', function (done) { event.RequestType = 'Delete'; event.ResourceProperties = { Options: options }; mock.mock(lambda) .expect('s3DeleteObjectsFromBucket') .with(options.Bucket) .willResolve(expected); lambda.S3BucketEmptyBeforeDelete(event, response) .then(function (result) { Expect(result).to.equal(true, 'result'); Expect(response.Status).to.equal('SUCCESS', 'response.Status'); Expect(response.PhysicalResourceId).to.equal(options.Bucket + '-S3BucketEmptyBeforeDelete', 'response.PhysicalResourceId'); return mock.done(done); }) .catch(done); }); it('should catch "NoSuchBucket" exception and return a successful response', function (done) { event.RequestType = 'Delete'; event.ResourceProperties = { Options: options }; expected = new Error('The specified bucket does not exist'); expected.code = 'NoSuchBucket'; mock.mock(lambda) .expect('s3DeleteObjectsFromBucket') .with(options.Bucket) .willReject(expected); lambda.S3BucketEmptyBeforeDelete(event, response) .then(function (result) { Expect(result).to.equal(true, 'result'); Expect(response.Status).to.equal('SUCCESS', 'response.Status'); Expect(response.PhysicalResourceId).to.equal(options.Bucket + '-S3BucketEmptyBeforeDelete', 'response.PhysicalResourceId'); return mock.done(done); }) .catch(done); }); }); describe('SESRule', function() { var event, expected, options, response; beforeEach(function() { event = {}; options = {}; expected = {}; response = { Data: {} }; }); it('should create the rule and return a successful response', function (done) { event.RequestType = 'Create'; options.Rule = { Name: 'Name' }; options.RuleSetName = 'RuleSetName'; event.ResourceProperties = { Options: options }; expected.RuleSetName = options.RuleSetName; mock.mock(lambda.ses) .expect('createReceiptRule') .with({ Rule: options.Rule, RuleSetName: options.RuleSetName }) .willReturnAwsPromiseResolve(expected); lambda.SESReceiptRule(event, response) .then(function (result) { Expect(result).to.equal(true, 'result'); Expect(response.Status).to.equal('SUCCESS', 'response.Status'); Expect(response.PhysicalResourceId).to.equal(options.Rule.Name, 'response.PhysicalResourceId'); Expect(response.Data.RuleSetName).to.equal(options.RuleSetName, 'response.Data.RuleSetName'); return mock.done(done); }) .catch(done); }); it('should delete the rule and return a successful response', function (done) { event.RequestType = 'Delete'; options.Rule = { Name: 'Name' }; options.RuleSetName = 'RuleSetName'; event.ResourceProperties = { Options: options }; expected.RuleSetName = options.RuleSetName; mock.mock(lambda.ses) .expect('deleteReceiptRule') .with({ RuleName: options.Rule.Name, RuleSetName: options.RuleSetName }) .willReturnAwsPromiseResolve(expected); lambda.SESReceiptRule(event, response) .then(function (result) { Expect(result).to.equal(true, 'result'); Expect(response.Status).to.equal('SUCCESS', 'response.Status'); Expect(response.PhysicalResourceId).to.equal(options.Rule.Name, 'response.PhysicalResourceId'); Expect(response.Data.RuleSetName).to.equal(options.RuleSetName, 'response.Data.RuleSetName'); return mock.done(done); }) .catch(done); }); it('should update the rule and return a successful response', function (done) { event.RequestType = 'Update'; options.Rule = { Name: 'Name' }; options.RuleSetName = 'RuleSetName'; event.ResourceProperties = { Options: options }; expected.RuleSetName = options.RuleSetName; mock.mock(lambda.ses) .expect('updateReceiptRule') .with({ Rule: options.Rule, RuleSetName: options.RuleSetName }) .willReturnAwsPromiseResolve(expected); lambda.SESReceiptRule(event, response) .then(function (result) { Expect(result).to.equal(true, 'result'); Expect(response.Status).to.equal('SUCCESS', 'response.Status'); Expect(response.PhysicalResourceId).to.equal(options.Rule.Name, 'response.PhysicalResourceId'); Expect(response.Data.RuleSetName).to.equal(options.RuleSetName, 'response.Data.RuleSetName'); return mock.done(done); }) .catch(done); }); it('should catch any exception and return a failed response', function (done) { event.RequestType = 'Create'; options.Rule = { Name: 'Name' }; options.RuleSetName = 'RuleSetName'; event.ResourceProperties = { Options: options }; expected = new Error('expected exception'); mock.mock(lambda.ses) .expect('createReceiptRule') .with({ Rule: options.Rule, RuleSetName: options.RuleSetName }) .willReturnAwsPromiseReject(expected); lambda.SESReceiptRule(event, response) .then(function (result) { Expect(result).to.equal(false, 'result'); Expect(response.Status).to.equal('FAILED', 'response.Status'); Expect(response.Reason).to.equal(expected.message, 'response.Reason'); return mock.done(done); }) .catch(done); }); }); describe('SESRuleSet', function() { var event, expected, options, response; beforeEach(function() { event = {}; options = {}; expected = {}; response = { Data: {} }; }); it('should create the rule set and return a successful response', function (done) { event.RequestType = 'Create'; options.RuleSetName = 'RuleSetName'; event.ResourceProperties = { Options: options }; expected.RuleSetName = options.RuleSetName; mock.mock(lambda.ses) .expect('createReceiptRuleSet') .with({ RuleSetName: options.RuleSetName }) .willReturnAwsPromiseResolve(expected); lambda.SESReceiptRuleSet(event, response) .then(function (result) { Expect(result).to.equal(true, 'result'); Expect(response.Status).to.equal('SUCCESS', 'response.Status'); Expect(response.PhysicalResourceId).to.equal(expected.RuleSetName, 'response.PhysicalResourceId'); Expect(response.Data.RuleSetName).to.equal(expected.RuleSetName, 'response.Data.RuleSetName'); return mock.done(done); }) .catch(done); }); it('should delete the rule set and return a successful response', function (done) { event.RequestType = 'Delete'; options.RuleSetName = 'RuleSetName'; event.ResourceProperties = { Options: options }; expected.RuleSetName = options.RuleSetName; mock.mock(lambda.ses) .expect('deleteReceiptRuleSet') .with({ RuleSetName: options.RuleSetName }) .willReturnAwsPromiseResolve(expected); lambda.SESReceiptRuleSet(event, response) .then(function (result) { Expect(result).to.equal(true, 'result'); Expect(response.Status).to.equal('SUCCESS', 'response.Status'); Expect(response.PhysicalResourceId).to.equal(expected.RuleSetName, 'response.PhysicalResourceId'); Expect(response.Data.RuleSetName).to.equal(expected.RuleSetName, 'response.Data.RuleSetName'); return mock.done(done); }) .catch(done); }); it('should do nothing and return a successful response', function (done) { event.RequestType = 'Update'; options.RuleSetName = 'RuleSetName'; event.ResourceProperties = { Options: options }; expected.RuleSetName = options.RuleSetName; lambda.SESReceiptRuleSet(event, response) .then(function (result) { Expect(result).to.equal(true, 'result'); Expect(response.Status).to.equal('SUCCESS', 'response.Status'); Expect(response.PhysicalResourceId).to.equal(expected.RuleSetName, 'response.PhysicalResourceId'); Expect(response.Data.RuleSetName).to.equal(expected.RuleSetName, 'response.Data.RuleSetName'); return mock.done(done); }) .catch(done); }); it('should catch any exception and return a failed response', function (done) { event.RequestType = 'Create'; options.RuleSetName = 'RuleSetName'; event.ResourceProperties = { Options: options }; expected = new Error('expected exception'); mock.mock(lambda.ses) .expect('createReceiptRuleSet') .with({ RuleSetName: options.RuleSetName }) .willReturnAwsPromiseReject(expected); lambda.SESReceiptRuleSet(event, response) .then(function (result) { Expect(result).to.equal(false, 'result'); Expect(response.Status).to.equal('FAILED', 'response.Status'); Expect(response.Reason).to.equal(expected.message, 'response.Reason'); return mock.done(done); }) .catch(done); }); }); describe('waitForFirehose', function () { it('should', function (done) { var arn = 'arn:aws:firehose:us-east-1:123456789012:deliverystream/example-stream-name'; var mocked = mock.mock(lambda.firehose); mocked .expect('describeDeliveryStream') .with({ DeliveryStreamName: 'example-stream-name' }) .willReturnAwsPromiseResolve({ DeliveryStreamDescription: { DeliveryStreamStatus: 'CREATING' } }); mocked .expect('describeDeliveryStream') .with({ DeliveryStreamName: 'example-stream-name' }) .willReturnAwsPromiseResolve({ DeliveryStreamDescription: { DeliveryStreamStatus: 'ACTIVE' } }); lambda.waitForFirehose(arn) .then(function (result) { Expect(result).to.equal(true, 'result'); return mock.done(done); }) .catch(done); }); }); });