Skip to content

Commit f596448

Browse files
Extract setting up routes with and without authorizer definition
1 parent b954fa4 commit f596448

File tree

1 file changed

+108
-83
lines changed

1 file changed

+108
-83
lines changed

lib/plugins/aws/package/compile/events/websockets/lib/authorizers.test.js

Lines changed: 108 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -8,102 +8,127 @@ const AwsProvider = require('../../../../../provider/awsProvider');
88
describe('#compileAuthorizers()', () => {
99
let awsCompileWebsocketsEvents;
1010

11-
beforeEach(() => {
12-
const serverless = new Serverless();
13-
serverless.setProvider('aws', new AwsProvider(serverless));
14-
serverless.service.provider.compiledCloudFormationTemplate = { Resources: {} };
11+
describe('for routes with authorizer definition', () => {
12+
beforeEach(() => {
13+
const serverless = new Serverless();
14+
serverless.setProvider('aws', new AwsProvider(serverless));
15+
serverless.service.provider.compiledCloudFormationTemplate = { Resources: {} };
1516

16-
awsCompileWebsocketsEvents = new AwsCompileWebsocketsEvents(serverless);
17+
awsCompileWebsocketsEvents = new AwsCompileWebsocketsEvents(serverless);
1718

18-
awsCompileWebsocketsEvents.websocketsApiLogicalId
19-
= awsCompileWebsocketsEvents.provider.naming.getWebsocketsApiLogicalId();
20-
});
19+
awsCompileWebsocketsEvents.websocketsApiLogicalId
20+
= awsCompileWebsocketsEvents.provider.naming.getWebsocketsApiLogicalId();
2121

22-
it('should create an authorizer resource for routes with authorizer definition', () => {
23-
awsCompileWebsocketsEvents.validated = {
24-
events: [
25-
{
26-
functionName: 'First',
27-
route: '$connect',
28-
authorizer: {
29-
name: 'auth',
30-
uri: {
31-
'Fn::Join': ['',
32-
[
33-
'arn:',
34-
{ Ref: 'AWS::Partition' },
35-
':apigateway:',
36-
{ Ref: 'AWS::Region' },
37-
':lambda:path/2015-03-31/functions/',
38-
{ 'Fn::GetAtt': ['AuthLambdaFunction', 'Arn'] },
39-
'/invocations',
22+
awsCompileWebsocketsEvents.validated = {
23+
events: [
24+
{
25+
functionName: 'First',
26+
route: '$connect',
27+
authorizer: {
28+
name: 'auth',
29+
uri: {
30+
'Fn::Join': ['',
31+
[
32+
'arn:',
33+
{ Ref: 'AWS::Partition' },
34+
':apigateway:',
35+
{ Ref: 'AWS::Region' },
36+
':lambda:path/2015-03-31/functions/',
37+
{ 'Fn::GetAtt': ['AuthLambdaFunction', 'Arn'] },
38+
'/invocations',
39+
],
4040
],
41-
],
41+
},
42+
identitySource: ['route.request.header.Auth'],
4243
},
43-
identitySource: ['route.request.header.Auth'],
4444
},
45-
},
46-
],
47-
};
48-
49-
return awsCompileWebsocketsEvents.compileAuthorizers().then(() => {
50-
const resources = awsCompileWebsocketsEvents.serverless.service.provider
51-
.compiledCloudFormationTemplate.Resources;
52-
53-
expect(resources).to.deep.equal({
54-
AuthWebsocketsAuthorizer: {
55-
Type: 'AWS::ApiGatewayV2::Authorizer',
56-
Properties: {
57-
ApiId: {
58-
Ref: 'WebsocketsApi',
59-
},
60-
Name: 'auth',
61-
AuthorizerType: 'REQUEST',
62-
AuthorizerUri: {
63-
'Fn::Join': [
64-
'',
65-
[
66-
'arn:',
67-
{
68-
Ref: 'AWS::Partition',
69-
},
70-
':apigateway:',
71-
{
72-
Ref: 'AWS::Region',
73-
},
74-
':lambda:path/2015-03-31/functions/',
75-
{
76-
'Fn::GetAtt': [
77-
'AuthLambdaFunction',
78-
'Arn',
79-
],
80-
},
81-
'/invocations',
45+
],
46+
};
47+
});
48+
49+
it('should create an authorizer resource', () => {
50+
return awsCompileWebsocketsEvents.compileAuthorizers().then(() => {
51+
const resources = awsCompileWebsocketsEvents.serverless.service.provider
52+
.compiledCloudFormationTemplate.Resources;
53+
54+
expect(resources).to.deep.equal({
55+
AuthWebsocketsAuthorizer: {
56+
Type: 'AWS::ApiGatewayV2::Authorizer',
57+
Properties: {
58+
ApiId: {
59+
Ref: 'WebsocketsApi',
60+
},
61+
Name: 'auth',
62+
AuthorizerType: 'REQUEST',
63+
AuthorizerUri: {
64+
'Fn::Join': [
65+
'',
66+
[
67+
'arn:',
68+
{
69+
Ref: 'AWS::Partition',
70+
},
71+
':apigateway:',
72+
{
73+
Ref: 'AWS::Region',
74+
},
75+
':lambda:path/2015-03-31/functions/',
76+
{
77+
'Fn::GetAtt': [
78+
'AuthLambdaFunction',
79+
'Arn',
80+
],
81+
},
82+
'/invocations',
83+
],
8284
],
83-
],
85+
},
86+
IdentitySource: ['route.request.header.Auth'],
8487
},
85-
IdentitySource: ['route.request.header.Auth'],
8688
},
87-
},
89+
});
8890
});
8991
});
92+
9093
});
9194

92-
it('should NOT create an authorizer resource for routes with not authorizer definition', () => {
93-
awsCompileWebsocketsEvents.validated = {
94-
events: [
95-
{
96-
functionName: 'First',
97-
route: '$connect',
98-
},
99-
],
100-
};
101-
102-
return awsCompileWebsocketsEvents.compileAuthorizers().then(() => {
103-
const resources = awsCompileWebsocketsEvents.serverless.service.provider
104-
.compiledCloudFormationTemplate.Resources;
105-
106-
expect(resources).to.deep.equal({});
95+
describe('for routes without authorizer definition', () => {
96+
beforeEach(() => {
97+
const serverless = new Serverless();
98+
serverless.setProvider('aws', new AwsProvider(serverless));
99+
serverless.service.provider.compiledCloudFormationTemplate = { Resources: {} };
100+
101+
awsCompileWebsocketsEvents = new AwsCompileWebsocketsEvents(serverless);
102+
103+
awsCompileWebsocketsEvents.websocketsApiLogicalId
104+
= awsCompileWebsocketsEvents.provider.naming.getWebsocketsApiLogicalId();
105+
106+
awsCompileWebsocketsEvents.validated = {
107+
events: [
108+
{
109+
functionName: 'First',
110+
route: '$connect',
111+
},
112+
],
113+
};
114+
});
115+
116+
it('should NOT create an authorizer resource for routes with not authorizer definition', () => {
117+
awsCompileWebsocketsEvents.validated = {
118+
events: [
119+
{
120+
functionName: 'First',
121+
route: '$connect',
122+
},
123+
],
124+
};
125+
126+
return awsCompileWebsocketsEvents.compileAuthorizers().then(() => {
127+
const resources = awsCompileWebsocketsEvents.serverless.service.provider
128+
.compiledCloudFormationTemplate.Resources;
129+
130+
expect(resources).to.deep.equal({});
131+
});
107132
});
108133
});
109134
});

0 commit comments

Comments
 (0)