|
| 1 | +import * as cdk from 'aws-cdk-lib'; |
| 2 | +import { Template, Match } from 'aws-cdk-lib/assertions'; |
| 3 | +import { BlogInfrastructureStack } from '../../../../backend/blog-backend-infrastructure-stack'; |
| 4 | + |
| 5 | +/***************************** |
| 6 | + * List of tests in this file: |
| 7 | + * |
| 8 | + * 1. Stack contains API Gateway RestApi with correct name |
| 9 | + * 2. API Gateway has correct endpoint configuration |
| 10 | + * 3. API Gateway has /api resource |
| 11 | + * 4. API Gateway has POST method for /api/contact |
| 12 | + * 5. Lambda function is created for contact resource |
| 13 | + * 6. Stack contains API Gateway Deployment with Retain policy |
| 14 | + * 7. Stack contains API Gateway Stage of prod that follows best practices |
| 15 | + * 8. API Gateway has POST method for /api/subscription |
| 16 | + * 9. Step Functions is created for subscription resource |
| 17 | + *****************************/ |
| 18 | + |
| 19 | +describe('Validate Api Gateway', () => { |
| 20 | + let app: cdk.App; |
| 21 | + let stack: BlogInfrastructureStack; |
| 22 | + let template: Template; |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + app = new cdk.App(); |
| 26 | + stack = new BlogInfrastructureStack(app, 'blog-backend-infrastructure', { |
| 27 | + env: { |
| 28 | + account: |
| 29 | + process.env.CDK_DEPLOY_ACCOUNT || process.env.CDK_DEFAULT_ACCOUNT, |
| 30 | + region: 'eu-west-1', |
| 31 | + }, |
| 32 | + }); |
| 33 | + template = Template.fromStack(stack); |
| 34 | + }); |
| 35 | + |
| 36 | + test('Stack contains API Gateway RestApi with correct name', () => { |
| 37 | + template.hasResourceProperties('AWS::ApiGateway::RestApi', { |
| 38 | + Name: 'blog-backend-infrastructure', |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + // test('API Gateway has correct endpoint configuration', () => { |
| 43 | + // template.hasResourceProperties('AWS::ApiGateway::RestApi', { |
| 44 | + // EndpointConfiguration: { |
| 45 | + // Types: ['EDGE'], |
| 46 | + // }, |
| 47 | + // }); |
| 48 | + // }); |
| 49 | + |
| 50 | + test('Stack contains API Gateway Deployment with Retain policy', () => { |
| 51 | + template.hasResource('AWS::ApiGateway::Deployment', { |
| 52 | + UpdateReplacePolicy: 'Retain', |
| 53 | + DeletionPolicy: 'Retain', |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + test('Stack contains API Gateway Stage of prod that follows best practices', () => { |
| 58 | + template.hasResourceProperties('AWS::ApiGateway::Stage', { |
| 59 | + StageName: 'prd', |
| 60 | + TracingEnabled: true, |
| 61 | + CacheClusterEnabled: false, // TODO: DISABLED |
| 62 | + MethodSettings: Match.arrayWith([ |
| 63 | + { |
| 64 | + CacheDataEncrypted: false, // TODO: DISABLED |
| 65 | + CachingEnabled: false, // TODO: DISABLED |
| 66 | + DataTraceEnabled: false, |
| 67 | + HttpMethod: '*', |
| 68 | + LoggingLevel: 'ERROR', |
| 69 | + MetricsEnabled: true, |
| 70 | + ResourcePath: '/*', |
| 71 | + }, |
| 72 | + ]), |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + test('API Gateway has /api resource', () => { |
| 77 | + template.hasResourceProperties('AWS::ApiGateway::Resource', { |
| 78 | + PathPart: 'api', |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + test('API Gateway has POST method for /api/contact', () => { |
| 83 | + template.hasResourceProperties('AWS::ApiGateway::Method', { |
| 84 | + HttpMethod: 'POST', |
| 85 | + Integration: { |
| 86 | + IntegrationHttpMethod: 'POST', |
| 87 | + Type: 'AWS_PROXY', |
| 88 | + }, |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + test('API Gateway has POST method for /api/subscription', () => { |
| 93 | + template.hasResourceProperties('AWS::ApiGateway::Method', { |
| 94 | + HttpMethod: 'POST', |
| 95 | + Integration: { |
| 96 | + IntegrationHttpMethod: 'POST', |
| 97 | + Type: 'AWS_PROXY', |
| 98 | + }, |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + test('API Gateway has POST method for /api/feedback', () => { |
| 103 | + template.hasResourceProperties('AWS::ApiGateway::Method', { |
| 104 | + HttpMethod: 'POST', |
| 105 | + Integration: { |
| 106 | + IntegrationHttpMethod: 'POST', |
| 107 | + Type: 'AWS_PROXY', |
| 108 | + }, |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + // test('/api/subscription resource is using a Lambda function', () => { |
| 113 | + // template.hasResourceProperties('AWS::ApiGateway::Method', { |
| 114 | + // HttpMethod: 'POST', |
| 115 | + // Integration: { |
| 116 | + // Type: 'AWS_PROXY', |
| 117 | + // IntegrationHttpMethod: 'POST', |
| 118 | + // Uri: { |
| 119 | + // 'Fn::Join': [ |
| 120 | + // '', |
| 121 | + // [ |
| 122 | + // 'arn:', |
| 123 | + // { Ref: 'AWS::Partition' }, |
| 124 | + // ':apigateway:', |
| 125 | + // { Ref: 'AWS::Region' }, |
| 126 | + // ':lambda:path/2015-03-31/functions/', |
| 127 | + // { |
| 128 | + // 'Fn::GetAtt': [ |
| 129 | + // expect.stringMatching(/^SubscriptionLambda[A-Z0-9]+$/), |
| 130 | + // 'Arn', |
| 131 | + // ], |
| 132 | + // }, |
| 133 | + // '/invocations', |
| 134 | + // ], |
| 135 | + // ], |
| 136 | + // }, |
| 137 | + // }, |
| 138 | + // }); |
| 139 | + // }); |
| 140 | + |
| 141 | + // test('/api/contact resource is using Step Functions', () => { |
| 142 | + // template.hasResourceProperties('AWS::ApiGateway::Method', { |
| 143 | + // HttpMethod: 'POST', |
| 144 | + // Integration: { |
| 145 | + // Type: 'AWS', |
| 146 | + // IntegrationHttpMethod: 'POST', |
| 147 | + // Uri: { |
| 148 | + // 'Fn::Join': [ |
| 149 | + // '', |
| 150 | + // [ |
| 151 | + // 'arn:', |
| 152 | + // { Ref: 'AWS::Partition' }, |
| 153 | + // ':apigateway:', |
| 154 | + // { Ref: 'AWS::Region' }, |
| 155 | + // ':states:action/StartExecution', |
| 156 | + // ], |
| 157 | + // ], |
| 158 | + // }, |
| 159 | + // IntegrationResponses: [ |
| 160 | + // { |
| 161 | + // StatusCode: '200', |
| 162 | + // }, |
| 163 | + // ], |
| 164 | + // }, |
| 165 | + // }); |
| 166 | + // }); |
| 167 | + |
| 168 | + // test('/api/feedback resource is using Step Functions', () => { |
| 169 | + // template.hasResourceProperties('AWS::ApiGateway::Method', { |
| 170 | + // HttpMethod: 'POST', |
| 171 | + // Integration: { |
| 172 | + // Type: 'AWS', |
| 173 | + // IntegrationHttpMethod: 'POST', |
| 174 | + // Uri: { |
| 175 | + // 'Fn::Join': [ |
| 176 | + // '', |
| 177 | + // [ |
| 178 | + // 'arn:', |
| 179 | + // { Ref: 'AWS::Partition' }, |
| 180 | + // ':apigateway:', |
| 181 | + // { Ref: 'AWS::Region' }, |
| 182 | + // ':states:action/StartExecution', |
| 183 | + // ], |
| 184 | + // ], |
| 185 | + // }, |
| 186 | + // IntegrationResponses: [ |
| 187 | + // { |
| 188 | + // StatusCode: '200', |
| 189 | + // }, |
| 190 | + // ], |
| 191 | + // }, |
| 192 | + // }); |
| 193 | + // }); |
| 194 | +}); |
0 commit comments