Skip to content

Commit

Permalink
chore!: properly upgrade source and test code to cdk v2
Browse files Browse the repository at this point in the history
  • Loading branch information
seeebiii committed Jun 1, 2022
1 parent 331c147 commit 9cd56c0
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 101 deletions.
13 changes: 7 additions & 6 deletions src/email-forwarding-rule-set.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { RetentionDays } from '@aws-cdk/aws-logs';
import { Bucket } from '@aws-cdk/aws-s3';
import { IReceiptRuleSet, ReceiptRuleSet } from '@aws-cdk/aws-ses';
import { Topic } from '@aws-cdk/aws-sns';
import { CfnOutput, Construct } from '@aws-cdk/core';
import { AwsCustomResource, PhysicalResourceId } from '@aws-cdk/custom-resources';
import { NotificationType, VerifySesDomain, VerifySesEmailAddress } from '@seeebiii/ses-verify-identities';
import { RetentionDays } from 'aws-cdk-lib/aws-logs';
import { Bucket } from 'aws-cdk-lib/aws-s3';
import { IReceiptRuleSet, ReceiptRuleSet } from 'aws-cdk-lib/aws-ses';
import { Topic } from 'aws-cdk-lib/aws-sns';
import { CfnOutput } from 'aws-cdk-lib/core';
import { AwsCustomResource, PhysicalResourceId } from 'aws-cdk-lib/custom-resources';
import { Construct } from 'constructs';
import { EmailForwardingRule, EmailMapping } from './email-forwarding-rule';
import { generateSesPolicyForCustomResource } from './helper';

Expand Down
15 changes: 8 additions & 7 deletions src/email-forwarding-rule.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import * as path from 'path';
import { PolicyStatement } from '@aws-cdk/aws-iam';
import { Code, Function, Runtime } from '@aws-cdk/aws-lambda';
import { Bucket } from '@aws-cdk/aws-s3';
import { ReceiptRule, IReceiptRuleSet, TlsPolicy } from '@aws-cdk/aws-ses';
import * as actions from '@aws-cdk/aws-ses-actions';
import { StringParameter } from '@aws-cdk/aws-ssm';
import { Construct, Duration, RemovalPolicy } from '@aws-cdk/core';
import { PolicyStatement } from 'aws-cdk-lib/aws-iam';
import { Code, Function, Runtime } from 'aws-cdk-lib/aws-lambda';
import { Bucket } from 'aws-cdk-lib/aws-s3';
import { ReceiptRule, IReceiptRuleSet, TlsPolicy } from 'aws-cdk-lib/aws-ses';
import * as actions from 'aws-cdk-lib/aws-ses-actions';
import { StringParameter } from 'aws-cdk-lib/aws-ssm';
import { Duration, RemovalPolicy } from 'aws-cdk-lib/core';
import { Construct } from 'constructs';

export interface EmailMapping {
/**
Expand Down
4 changes: 2 additions & 2 deletions src/helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Effect, PolicyStatement } from '@aws-cdk/aws-iam';
import { AwsCustomResourcePolicy } from '@aws-cdk/custom-resources';
import { Effect, PolicyStatement } from 'aws-cdk-lib/aws-iam';
import { AwsCustomResourcePolicy } from 'aws-cdk-lib/custom-resources';

export function generateSesPolicyForCustomResource(...methods: string[]): AwsCustomResourcePolicy {
// for some reason the default policy is generated as `email:<method>` which does not work -> hence we need to provide our own
Expand Down
69 changes: 34 additions & 35 deletions test/email-forwarding-rule-set.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import {
anything,
countResources,
encodedJson,
expect as expectCDK,
haveOutput,
haveResourceLike,
objectLike,
stringLike,
} from '@aws-cdk/assert';
import * as cdk from '@aws-cdk/core';
import { VerifySesDomain } from '@seeebiii/ses-verify-identities';
import { Match, Template } from 'aws-cdk-lib/assertions';
import * as cdk from 'aws-cdk-lib/core';
import { EmailForwardingRuleSet } from '../src';

VerifySesDomain.prototype.getHostedZone = jest.fn().mockReturnValue({
Expand All @@ -29,16 +20,20 @@ describe('email forwarding rule set', () => {
emailForwardingProps: [],
});

expectCDK(stack).to(countResources('AWS::SES::ReceiptRuleSet'));
expectCDK(stack).to(haveResourceLike('AWS::SES::ReceiptRuleSet', objectLike({
RuleSetName: ruleSetName,
})));
expectCDK(stack).to(haveOutput({
outputName: `${name}ReceiptRuleSetOutput65358D48`,
outputValue: {
Ref: `${name}ReceiptRuleSetC75E5BCA`,
const template = Template.fromStack(stack);
template.resourceCountIs('AWS::SES::ReceiptRuleSet', 1);
template.hasResource('AWS::SES::ReceiptRuleSet', Match.objectLike({
Properties: {
RuleSetName: ruleSetName,
},
}));
template.hasOutput(`${name}ReceiptRuleSetOutput65358D48`,
{
Value: {
Ref: `${name}ReceiptRuleSetC75E5BCA`,
},
},
);
});

it('ensure rule set is enabled by default', () => {
Expand All @@ -52,19 +47,23 @@ describe('email forwarding rule set', () => {
});

// ensure that rule set is activated by default
expectCDK(stack).to(haveResourceLike('Custom::AWS', objectLike({
Create: objectLike({
'Fn::Join': [
'', [
stringLike('{\"service\":\"SES\",\"action\":\"setActiveReceiptRuleSet\"*'), anything(), stringLike('*\"id\":\"enable-rule-set-on-create\"}}'),
Template.fromStack(stack).hasResource('Custom::AWS', Match.objectLike({
Properties: {
Create: Match.objectLike({
'Fn::Join': [
'', [
Match.stringLikeRegexp('{\"service\":\"SES\",\"action\":\"setActiveReceiptRuleSet\".*'),
Match.anyValue(),
Match.stringLikeRegexp('.*\"id\":\"enable-rule-set-on-create\"}}'),
],
],
],
}),
Delete: encodedJson(objectLike({
service: 'SES',
action: 'setActiveReceiptRuleSet',
})),
})));
}),
Delete: Match.serializedJson(Match.objectLike({
service: 'SES',
action: 'setActiveReceiptRuleSet',
})),
},
}));
});

it('ensure rule set is not enabled if property is set to false', () => {
Expand All @@ -79,7 +78,7 @@ describe('email forwarding rule set', () => {
});

// ensure that rule set is activated by default
expectCDK(stack).to(countResources('Custom::AWS', 0));
Template.fromStack(stack).resourceCountIs('Custom::AWS', 0);
});

it('ensure domain and email addresses are not verified by default', () => {
Expand Down Expand Up @@ -122,7 +121,7 @@ describe('email forwarding rule set', () => {
expect(ruleSet.emailForwardingMappings[0].verifySesDomain).toBeTruthy();
expect(ruleSet.emailForwardingMappings[0].verifySesEmailAddresses).toBeFalsy();

expectCDK(stack).to(countResources('Custom::AWS', 4));
Template.fromStack(stack).resourceCountIs('Custom::AWS', 4);
});

it('ensure email address is verified', () => {
Expand All @@ -148,7 +147,7 @@ describe('email forwarding rule set', () => {
expect(ruleSet.emailForwardingMappings[0].verifySesEmailAddresses).toBeTruthy();
expect(ruleSet.emailForwardingMappings[0].verifySesEmailAddresses?.length).toEqual(1);

expectCDK(stack).to(countResources('Custom::AWS', 1));
Template.fromStack(stack).resourceCountIs('Custom::AWS', 1);
});

it('ensure multiple email addresses are verified', () => {
Expand All @@ -174,6 +173,6 @@ describe('email forwarding rule set', () => {
expect(ruleSet.emailForwardingMappings[0].verifySesEmailAddresses).toBeTruthy();
expect(ruleSet.emailForwardingMappings[0].verifySesEmailAddresses?.length).toEqual(2);

expectCDK(stack).to(countResources('Custom::AWS', 2));
Template.fromStack(stack).resourceCountIs('Custom::AWS', 2);
});
});
110 changes: 59 additions & 51 deletions test/email-forwarding-rule.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { anything, arrayWith, countResources, deepObjectLike, expect as expectCDK, haveResourceLike, objectLike } from '@aws-cdk/assert';
import { Bucket } from '@aws-cdk/aws-s3';
import { ReceiptRuleSet } from '@aws-cdk/aws-ses';
import * as cdk from '@aws-cdk/core';
import { Match, Template } from 'aws-cdk-lib/assertions';
import { Bucket } from 'aws-cdk-lib/aws-s3';
import { ReceiptRuleSet } from 'aws-cdk-lib/aws-ses';
import * as cdk from 'aws-cdk-lib/core';
import { EmailForwardingRule } from '../src';

describe('email forwarding rule', () => {
Expand All @@ -20,7 +20,7 @@ describe('email forwarding rule', () => {
ruleSet: receiptRuleSet,
});

expectCDK(stack).to(countResources('AWS::SES::ReceiptRule', 0));
Template.fromStack(stack).resourceCountIs('AWS::SES::ReceiptRule', 0);
});

it('ensure rule is created and contains actions', () => {
Expand All @@ -41,18 +41,20 @@ describe('email forwarding rule', () => {
ruleSet: receiptRuleSet,
});

expectCDK(stack).to(haveResourceLike('AWS::SES::ReceiptRule', objectLike({
Rule: deepObjectLike({
Name: `${ruleId}-rule-set`,
Actions: arrayWith({
S3Action: objectLike({
ObjectKeyPrefix: 'inbox/',
}),
}, {
LambdaAction: anything(),
Template.fromStack(stack).hasResource('AWS::SES::ReceiptRule', Match.objectLike({
Properties: {
Rule: Match.objectLike({
Name: `${ruleId}-rule-set`,
Actions: Match.arrayWith([{
S3Action: Match.objectLike({
ObjectKeyPrefix: 'inbox/',
}),
}, {
LambdaAction: Match.anyValue(),
}]),
}),
}),
})));
},
}));
});

it('S3 action is using custom bucket and prefix', () => {
Expand Down Expand Up @@ -80,19 +82,21 @@ describe('email forwarding rule', () => {
bucketPrefix: bucketPrefix,
});

expectCDK(stack).to(haveResourceLike('AWS::SES::ReceiptRule', objectLike({
Rule: deepObjectLike({
Name: `${ruleId}-rule-set`,
Actions: arrayWith({
S3Action: objectLike({
BucketName: {
Ref: 'examplebucketC9DFA43E',
},
ObjectKeyPrefix: bucketPrefix,
}),
Template.fromStack(stack).hasResource('AWS::SES::ReceiptRule', Match.objectLike({
Properties: {
Rule: Match.objectLike({
Name: `${ruleId}-rule-set`,
Actions: Match.arrayWith([{
S3Action: Match.objectLike({
BucketName: {
Ref: 'examplebucketC9DFA43E',
},
ObjectKeyPrefix: bucketPrefix,
}),
}]),
}),
}),
})));
},
}));
});

it('custom prefix is suffixed with slash', () => {
Expand Down Expand Up @@ -120,19 +124,21 @@ describe('email forwarding rule', () => {
bucketPrefix: bucketPrefix,
});

expectCDK(stack).to(haveResourceLike('AWS::SES::ReceiptRule', objectLike({
Rule: deepObjectLike({
Name: `${ruleId}-rule-set`,
Actions: arrayWith({
S3Action: objectLike({
BucketName: {
Ref: 'examplebucketC9DFA43E',
},
ObjectKeyPrefix: bucketPrefix + '/',
}),
Template.fromStack(stack).hasResource('AWS::SES::ReceiptRule', Match.objectLike({
Properties: {
Rule: Match.objectLike({
Name: `${ruleId}-rule-set`,
Actions: Match.arrayWith([{
S3Action: Match.objectLike({
BucketName: {
Ref: 'examplebucketC9DFA43E',
},
ObjectKeyPrefix: bucketPrefix + '/',
}),
}]),
}),
}),
})));
},
}));
});

it('Lambda action contains appropriate environment variables', () => {
Expand Down Expand Up @@ -162,17 +168,19 @@ describe('email forwarding rule', () => {
bucketPrefix,
});

expectCDK(stack).to(haveResourceLike('AWS::Lambda::Function', objectLike({
Environment: deepObjectLike({
Variables: {
ENABLE_LOGGING: 'false',
FROM_EMAIL: 'noreply123@example.org',
BUCKET_NAME: {
Ref: 'examplebucketC9DFA43E',
Template.fromStack(stack).hasResource('AWS::Lambda::Function', Match.objectLike({
Properties: {
Environment: Match.objectLike({
Variables: {
ENABLE_LOGGING: 'false',
FROM_EMAIL: 'noreply123@example.org',
BUCKET_NAME: {
Ref: 'examplebucketC9DFA43E',
},
BUCKET_PREFIX: bucketPrefix,
},
BUCKET_PREFIX: bucketPrefix,
},
}),
})));
}),
},
}));
});
});

0 comments on commit 9cd56c0

Please sign in to comment.