/
permissions.test.js
106 lines (97 loc) · 3.36 KB
/
permissions.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
'use strict';
const expect = require('chai').expect;
const AwsCompileAlbEvents = require('../../../../../../../../../../lib/plugins/aws/package/compile/events/alb/index');
const Serverless = require('../../../../../../../../../../lib/Serverless');
const AwsProvider = require('../../../../../../../../../../lib/plugins/aws/provider');
describe('#compilePermissions()', () => {
let awsCompileAlbEvents;
beforeEach(() => {
const serverless = new Serverless({ commands: [], options: {} });
serverless.setProvider('aws', new AwsProvider(serverless));
serverless.service.service = 'some-service';
serverless.service.provider.compiledCloudFormationTemplate = { Resources: {} };
serverless.service.functions.first = {};
serverless.service.functions.second = {};
awsCompileAlbEvents = new AwsCompileAlbEvents(serverless);
});
it('should create Lambda permission resources', () => {
awsCompileAlbEvents.validated = {
events: [
{
functionName: 'first',
listenerArn:
'arn:aws:elasticloadbalancing:' +
'us-east-1:123456789012:listener/app/my-load-balancer/' +
'50dc6c495c0c9188/f2f7dc8efc522ab2',
priority: 1,
conditions: {
host: 'example.com',
path: '/hello',
},
albId: '50dc6c495c0c9188',
},
{
functionName: 'second',
listenerArn:
'arn:aws:elasticloadbalancing:' +
'us-east-1:123456789012:listener/app/my-load-balancer/' +
'50dc6c495c0c9188/f2f7dc8efc522ab2',
priority: 2,
conditions: {
path: '/world',
},
albId: '50dc6c495c0c9188',
},
],
};
awsCompileAlbEvents.compilePermissions();
const resources =
awsCompileAlbEvents.serverless.service.provider.compiledCloudFormationTemplate.Resources;
expect(resources.FirstLambdaPermissionAlb).to.deep.equal({
Type: 'AWS::Lambda::Permission',
Properties: {
Action: 'lambda:InvokeFunction',
FunctionName: {
'Fn::GetAtt': ['FirstLambdaFunction', 'Arn'],
},
Principal: 'elasticloadbalancing.amazonaws.com',
SourceArn: {
Ref: 'FirstAlbTargetGroup50dc6c495c0c9188',
},
},
});
expect(resources.FirstLambdaPermissionRegisterTarget).to.deep.equal({
Type: 'AWS::Lambda::Permission',
Properties: {
Action: 'lambda:InvokeFunction',
FunctionName: {
'Fn::GetAtt': ['FirstLambdaFunction', 'Arn'],
},
Principal: 'elasticloadbalancing.amazonaws.com',
},
});
expect(resources.SecondLambdaPermissionAlb).to.deep.equal({
Type: 'AWS::Lambda::Permission',
Properties: {
Action: 'lambda:InvokeFunction',
FunctionName: {
'Fn::GetAtt': ['SecondLambdaFunction', 'Arn'],
},
Principal: 'elasticloadbalancing.amazonaws.com',
SourceArn: {
Ref: 'SecondAlbTargetGroup50dc6c495c0c9188',
},
},
});
expect(resources.SecondLambdaPermissionRegisterTarget).to.deep.equal({
Type: 'AWS::Lambda::Permission',
Properties: {
Action: 'lambda:InvokeFunction',
FunctionName: {
'Fn::GetAtt': ['SecondLambdaFunction', 'Arn'],
},
Principal: 'elasticloadbalancing.amazonaws.com',
},
});
});
});