|
1 | | -// import * as cdk from 'aws-cdk-lib/core'; |
2 | | -// import { Template } from 'aws-cdk-lib/assertions'; |
3 | | -// import * as MeshV2 from '../lib/mesh-v2-stack'; |
4 | | - |
5 | | -// example test. To run these tests, uncomment this file along with the |
6 | | -// example resource in lib/mesh-v2-stack.ts |
7 | | -test('SQS Queue Created', () => { |
8 | | -// const app = new cdk.App(); |
9 | | -// // WHEN |
10 | | -// const stack = new MeshV2.MeshV2Stack(app, 'MyTestStack'); |
11 | | -// // THEN |
12 | | -// const template = Template.fromStack(stack); |
13 | | - |
14 | | -// template.hasResourceProperties('AWS::SQS::Queue', { |
15 | | -// VisibilityTimeout: 300 |
16 | | -// }); |
| 1 | +import * as cdk from 'aws-cdk-lib/core'; |
| 2 | +import { Template } from 'aws-cdk-lib/assertions'; |
| 3 | +import * as MeshV2 from '../lib/mesh-v2-stack'; |
| 4 | + |
| 5 | +describe('MeshV2Stack', () => { |
| 6 | + test('AppSync API and DynamoDB Table Created', () => { |
| 7 | + const app = new cdk.App(); |
| 8 | + const stack = new MeshV2.MeshV2Stack(app, 'MyTestStack', { |
| 9 | + env: { account: '123456789012', region: 'us-east-1' } |
| 10 | + }); |
| 11 | + const template = Template.fromStack(stack); |
| 12 | + |
| 13 | + template.hasResourceProperties('AWS::DynamoDB::Table', { |
| 14 | + TableName: 'MeshV2Table-stg' |
| 15 | + }); |
| 16 | + |
| 17 | + template.hasResourceProperties('AWS::AppSync::GraphQLApi', { |
| 18 | + Name: 'MeshV2Api-stg' |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + test('WAF is created when stage is prod', () => { |
| 23 | + const app = new cdk.App({ |
| 24 | + context: { |
| 25 | + stage: 'prod' |
| 26 | + } |
| 27 | + }); |
| 28 | + const stack = new MeshV2.MeshV2Stack(app, 'MyProdTestStack', { |
| 29 | + env: { account: '123456789012', region: 'us-east-1' } |
| 30 | + }); |
| 31 | + const template = Template.fromStack(stack); |
| 32 | + |
| 33 | + template.resourceCountIs('AWS::WAFv2::WebACL', 1); |
| 34 | + template.hasResourceProperties('AWS::WAFv2::WebACL', { |
| 35 | + DefaultAction: { Block: {} }, |
| 36 | + Scope: 'REGIONAL', |
| 37 | + Rules: [ |
| 38 | + { |
| 39 | + Name: 'AllowSpecificOrigins', |
| 40 | + Priority: 1, |
| 41 | + Action: { Allow: {} }, |
| 42 | + Statement: { |
| 43 | + OrStatement: { |
| 44 | + Statements: [ |
| 45 | + { |
| 46 | + ByteMatchStatement: { |
| 47 | + FieldToMatch: { |
| 48 | + SingleHeader: { name: 'origin' } |
| 49 | + }, |
| 50 | + PositionalConstraint: 'EXACTLY', |
| 51 | + SearchString: 'https://smalruby.app', |
| 52 | + TextTransformations: [ |
| 53 | + { |
| 54 | + Priority: 0, |
| 55 | + Type: 'LOWERCASE' |
| 56 | + } |
| 57 | + ] |
| 58 | + } |
| 59 | + }, |
| 60 | + { |
| 61 | + ByteMatchStatement: { |
| 62 | + FieldToMatch: { |
| 63 | + SingleHeader: { name: 'origin' } |
| 64 | + }, |
| 65 | + PositionalConstraint: 'EXACTLY', |
| 66 | + SearchString: 'https://smalruby.jp', |
| 67 | + TextTransformations: [ |
| 68 | + { |
| 69 | + Priority: 0, |
| 70 | + Type: 'LOWERCASE' |
| 71 | + } |
| 72 | + ] |
| 73 | + } |
| 74 | + } |
| 75 | + ] |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + ] |
| 80 | + }); |
| 81 | + |
| 82 | + template.resourceCountIs('AWS::WAFv2::WebACLAssociation', 1); |
| 83 | + }); |
| 84 | + |
| 85 | + test('WAF is not created when stage is stg', () => { |
| 86 | + const app = new cdk.App({ |
| 87 | + context: { |
| 88 | + stage: 'stg' |
| 89 | + } |
| 90 | + }); |
| 91 | + const stack = new MeshV2.MeshV2Stack(app, 'MyStgTestStack', { |
| 92 | + env: { account: '123456789012', region: 'us-east-1' } |
| 93 | + }); |
| 94 | + const template = Template.fromStack(stack); |
| 95 | + |
| 96 | + template.resourceCountIs('AWS::WAFv2::WebACL', 0); |
| 97 | + template.resourceCountIs('AWS::WAFv2::WebACLAssociation', 0); |
| 98 | + }); |
17 | 99 | }); |
0 commit comments