|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const _ = require('lodash'); |
| 4 | +const expect = require('chai').expect; |
| 5 | +const Serverless = require('serverless/lib/Serverless'); |
| 6 | +const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider'); |
| 7 | +const ServerlessStepFunctions = require('./../../../index'); |
| 8 | + |
| 9 | +describe('#compileHttpLambdaPermissions()', () => { |
| 10 | + let serverless; |
| 11 | + let serverlessStepFunctions; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + serverless = new Serverless(); |
| 15 | + serverless.setProvider('aws', new AwsProvider(serverless)); |
| 16 | + serverless.service.provider.compiledCloudFormationTemplate = { Resources: {} }; |
| 17 | + serverless.service.service = 'new-service'; |
| 18 | + serverless.service.stepfunctions = { |
| 19 | + stateMachines: { |
| 20 | + first: { |
| 21 | + }, |
| 22 | + }, |
| 23 | + }; |
| 24 | + serverlessStepFunctions = new ServerlessStepFunctions(serverless); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should not create a Lambda Permission resource when there are no Lambda authorizers', () => { |
| 28 | + serverlessStepFunctions.compileHttpLambdaPermissions().then(() => { |
| 29 | + const resources = serverlessStepFunctions.serverless.service.provider |
| 30 | + .compiledCloudFormationTemplate.Resources; |
| 31 | + const lambdaPermissions = |
| 32 | + _.values(resources).filter(x => x.Type === 'AWS::Lambda::Permission'); |
| 33 | + expect(lambdaPermissions).to.have.lengthOf(0); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should create a Lambda Permission resource when there is a TOKEN authorizer', () => { |
| 38 | + serverlessStepFunctions.serverless.service.provider |
| 39 | + .compiledCloudFormationTemplate.Resources.HelloApiGatewayAuthorizer = { |
| 40 | + Type: 'AWS::ApiGateway::Authorizer', |
| 41 | + Properties: { |
| 42 | + AuthorizerResultTtlInSeconds: 300, |
| 43 | + IdentitySource: 'method.request.header.Authorization', |
| 44 | + Name: 'hello', |
| 45 | + RestApiId: { |
| 46 | + Ref: 'ApiGatewayRestApi', |
| 47 | + }, |
| 48 | + AuthorizerUri: { |
| 49 | + 'Fn::Join': [ |
| 50 | + '', |
| 51 | + [ |
| 52 | + 'arn:', |
| 53 | + { |
| 54 | + Ref: 'AWS::Partition', |
| 55 | + }, |
| 56 | + ':apigateway:', |
| 57 | + { |
| 58 | + Ref: 'AWS::Region', |
| 59 | + }, |
| 60 | + ':lambda:path/2015-03-31/functions/', |
| 61 | + { |
| 62 | + 'Fn::GetAtt': [ |
| 63 | + 'HelloLambdaFunction', |
| 64 | + 'Arn', |
| 65 | + ], |
| 66 | + }, |
| 67 | + '/invocations', |
| 68 | + ], |
| 69 | + ], |
| 70 | + }, |
| 71 | + Type: 'TOKEN', |
| 72 | + }, |
| 73 | + }; |
| 74 | + |
| 75 | + serverlessStepFunctions.compileHttpLambdaPermissions().then(() => { |
| 76 | + const resources = serverlessStepFunctions.serverless.service.provider |
| 77 | + .compiledCloudFormationTemplate.Resources; |
| 78 | + const lambdaPermissions = |
| 79 | + _.values(resources).filter(x => x.Type === 'AWS::Lambda::Permission'); |
| 80 | + expect(lambdaPermissions).to.have.lengthOf(1); |
| 81 | + expect(lambdaPermissions[0]).to.deep.eq({ |
| 82 | + Type: 'AWS::Lambda::Permission', |
| 83 | + Properties: { |
| 84 | + FunctionName: { |
| 85 | + 'Fn::GetAtt': ['HelloLambdaFunction', 'Arn'], |
| 86 | + }, |
| 87 | + Action: 'lambda:InvokeFunction', |
| 88 | + Principal: { |
| 89 | + 'Fn::Sub': 'apigateway.${AWS::URLSuffix}', |
| 90 | + }, |
| 91 | + }, |
| 92 | + }); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should create a Lambda Permission resource when there is a CUSTOM authorizer', () => { |
| 97 | + serverlessStepFunctions.serverless.service.provider |
| 98 | + .compiledCloudFormationTemplate.Resources.HelloApiGatewayAuthorizer = { |
| 99 | + Type: 'AWS::ApiGateway::Authorizer', |
| 100 | + Properties: { |
| 101 | + AuthorizerResultTtlInSeconds: 300, |
| 102 | + IdentitySource: 'method.request.header.Authorization', |
| 103 | + Name: 'hello', |
| 104 | + RestApiId: { |
| 105 | + Ref: 'ApiGatewayRestApi', |
| 106 | + }, |
| 107 | + AuthorizerUri: { |
| 108 | + 'Fn::Join': [ |
| 109 | + '', |
| 110 | + [ |
| 111 | + 'arn:', |
| 112 | + { |
| 113 | + Ref: 'AWS::Partition', |
| 114 | + }, |
| 115 | + ':apigateway:', |
| 116 | + { |
| 117 | + Ref: 'AWS::Region', |
| 118 | + }, |
| 119 | + ':lambda:path/2015-03-31/functions/', |
| 120 | + { |
| 121 | + 'Fn::GetAtt': [ |
| 122 | + 'HelloLambdaFunction', |
| 123 | + 'Arn', |
| 124 | + ], |
| 125 | + }, |
| 126 | + '/invocations', |
| 127 | + ], |
| 128 | + ], |
| 129 | + }, |
| 130 | + Type: 'CUSTOM', |
| 131 | + }, |
| 132 | + }; |
| 133 | + |
| 134 | + serverlessStepFunctions.compileHttpLambdaPermissions().then(() => { |
| 135 | + const resources = serverlessStepFunctions.serverless.service.provider |
| 136 | + .compiledCloudFormationTemplate.Resources; |
| 137 | + const lambdaPermissions = |
| 138 | + _.values(resources).filter(x => x.Type === 'AWS::Lambda::Permission'); |
| 139 | + expect(lambdaPermissions).to.have.lengthOf(1); |
| 140 | + expect(lambdaPermissions[0]).to.deep.eq({ |
| 141 | + Type: 'AWS::Lambda::Permission', |
| 142 | + Properties: { |
| 143 | + FunctionName: { |
| 144 | + 'Fn::GetAtt': ['HelloLambdaFunction', 'Arn'], |
| 145 | + }, |
| 146 | + Action: 'lambda:InvokeFunction', |
| 147 | + Principal: { |
| 148 | + 'Fn::Sub': 'apigateway.${AWS::URLSuffix}', |
| 149 | + }, |
| 150 | + }, |
| 151 | + }); |
| 152 | + }); |
| 153 | + }); |
| 154 | +}); |
0 commit comments