Skip to content

Commit

Permalink
refactor: Use async in compile/events/websockets (serverless#8874)
Browse files Browse the repository at this point in the history
Covered the following paths:
* `lib/plugins/aws/package/compile/events/websockets`

Made changes to tests where necessary.
  • Loading branch information
ifitzsimmons committed Feb 3, 2021
1 parent 3c93e2a commit 61dd3bd
Show file tree
Hide file tree
Showing 14 changed files with 358 additions and 389 deletions.
2 changes: 1 addition & 1 deletion lib/plugins/aws/package/compile/events/websockets/index.js
Expand Up @@ -32,7 +32,7 @@ class AwsCompileWebsockets {
);

this.hooks = {
'package:compileEvents': () => {
'package:compileEvents': async () => {
this.validated = this.validate();

if (this.validated.events.length === 0) {
Expand Down
5 changes: 1 addition & 4 deletions lib/plugins/aws/package/compile/events/websockets/lib/api.js
@@ -1,15 +1,14 @@
'use strict';

const _ = require('lodash');
const BbPromise = require('bluebird');

module.exports = {
compileApi() {
const apiGateway = this.serverless.service.provider.apiGateway || {};

// immediately return if we're using an external websocket API id
if (apiGateway.websocketApiId) {
return BbPromise.resolve();
return;
}

this.websocketsApiLogicalId = this.provider.naming.getWebsocketsApiLogicalId();
Expand Down Expand Up @@ -46,7 +45,5 @@ module.exports = {
this.provider.naming.getRoleLogicalId()
].Properties.Policies[0].PolicyDocument.Statement.push(websocketsPolicy);
}

return BbPromise.resolve();
},
};
@@ -1,7 +1,6 @@
'use strict';

const _ = require('lodash');
const BbPromise = require('bluebird');

module.exports = {
compileAuthorizers() {
Expand All @@ -27,6 +26,6 @@ module.exports = {
});
});

return BbPromise.resolve();
return;
},
};
Expand Up @@ -2,7 +2,6 @@

const _ = require('lodash');
const crypto = require('crypto');
const BbPromise = require('bluebird');
const pickWebsocketsTemplatePart = require('./pickWebsocketsTemplatePart');

module.exports = {
Expand Down Expand Up @@ -71,7 +70,5 @@ module.exports = {
Ref: this.websocketsDeploymentLogicalId,
};
}

return BbPromise.resolve();
},
};
@@ -1,7 +1,6 @@
'use strict';

const _ = require('lodash');
const BbPromise = require('bluebird');

module.exports = {
compileIntegrations() {
Expand Down Expand Up @@ -35,7 +34,5 @@ module.exports = {
},
});
});

return BbPromise.resolve();
},
};
@@ -1,7 +1,6 @@
'use strict';

const _ = require('lodash');
const BbPromise = require('bluebird');

module.exports = {
compilePermissions() {
Expand Down Expand Up @@ -65,7 +64,5 @@ module.exports = {
);
}
});

return BbPromise.resolve();
},
};
@@ -1,7 +1,6 @@
'use strict';

const _ = require('lodash');
const BbPromise = require('bluebird');

module.exports = {
compileRoutes() {
Expand Down Expand Up @@ -44,7 +43,5 @@ module.exports = {
routeTemplate
);
});

return BbPromise.resolve();
},
};
Expand Up @@ -8,7 +8,7 @@ const defaultLogLevel = 'INFO';
const validLogLevels = new Set(['INFO', 'ERROR']);

module.exports = {
compileStage() {
async compileStage() {
return BbPromise.try(() => {
const { service, provider } = this.serverless.service;
const stage = this.options.stage;
Expand Down
Expand Up @@ -33,72 +33,70 @@ describe('#compileApi()', () => {
};
});

it('should create a websocket api resource', () =>
awsCompileWebsocketsEvents.compileApi().then(() => {
const resources =
awsCompileWebsocketsEvents.serverless.service.provider.compiledCloudFormationTemplate
.Resources;
it('should create a websocket api resource', () => {
awsCompileWebsocketsEvents.compileApi();
const resources =
awsCompileWebsocketsEvents.serverless.service.provider.compiledCloudFormationTemplate
.Resources;

expect(resources.WebsocketsApi).to.deep.equal({
Type: 'AWS::ApiGatewayV2::Api',
Properties: {
Name: 'dev-my-service-websockets',
RouteSelectionExpression: '$request.body.action',
Description: 'Serverless Websockets',
ProtocolType: 'WEBSOCKET',
},
});
}));
expect(resources.WebsocketsApi).to.deep.equal({
Type: 'AWS::ApiGatewayV2::Api',
Properties: {
Name: 'dev-my-service-websockets',
RouteSelectionExpression: '$request.body.action',
Description: 'Serverless Websockets',
ProtocolType: 'WEBSOCKET',
},
});
});

it('should ignore API resource creation if there is predefined websocketApi config', () => {
awsCompileWebsocketsEvents.serverless.service.provider.apiGateway = {
websocketApiId: '5ezys3sght',
};
return awsCompileWebsocketsEvents.compileApi().then(() => {
const resources =
awsCompileWebsocketsEvents.serverless.service.provider.compiledCloudFormationTemplate
.Resources;
awsCompileWebsocketsEvents.compileApi();
const resources =
awsCompileWebsocketsEvents.serverless.service.provider.compiledCloudFormationTemplate
.Resources;

expect(resources).to.not.have.property('WebsocketsApi');
});
expect(resources).to.not.have.property('WebsocketsApi');
});

it('should add the websockets policy', () =>
awsCompileWebsocketsEvents.compileApi().then(() => {
const resources =
awsCompileWebsocketsEvents.serverless.service.provider.compiledCloudFormationTemplate
.Resources;
it('should add the websockets policy', () => {
awsCompileWebsocketsEvents.compileApi();
const resources =
awsCompileWebsocketsEvents.serverless.service.provider.compiledCloudFormationTemplate
.Resources;

expect(resources[roleLogicalId]).to.deep.equal({
Properties: {
Policies: [
{
PolicyDocument: {
Statement: [
{
Action: ['execute-api:ManageConnections'],
Effect: 'Allow',
Resource: [
{ 'Fn::Sub': 'arn:${AWS::Partition}:execute-api:*:*:*/@connections/*' },
],
},
],
},
expect(resources[roleLogicalId]).to.deep.equal({
Properties: {
Policies: [
{
PolicyDocument: {
Statement: [
{
Action: ['execute-api:ManageConnections'],
Effect: 'Allow',
Resource: [
{ 'Fn::Sub': 'arn:${AWS::Partition}:execute-api:*:*:*/@connections/*' },
],
},
],
},
],
},
});
}));
},
],
},
});
});

it('should NOT add the websockets policy if role resource does not exist', () => {
awsCompileWebsocketsEvents.serverless.service.provider.compiledCloudFormationTemplate.Resources = {};

return awsCompileWebsocketsEvents.compileApi().then(() => {
const resources =
awsCompileWebsocketsEvents.serverless.service.provider.compiledCloudFormationTemplate
.Resources;
awsCompileWebsocketsEvents.compileApi();
const resources =
awsCompileWebsocketsEvents.serverless.service.provider.compiledCloudFormationTemplate
.Resources;

expect(resources[roleLogicalId]).to.deep.equal(undefined);
});
expect(resources[roleLogicalId]).to.deep.equal(undefined);
});
});
Expand Up @@ -47,44 +47,43 @@ describe('#compileAuthorizers()', () => {
});

it('should create an authorizer resource', () => {
return awsCompileWebsocketsEvents.compileAuthorizers().then(() => {
const resources =
awsCompileWebsocketsEvents.serverless.service.provider.compiledCloudFormationTemplate
.Resources;

expect(resources).to.deep.equal({
AuthWebsocketsAuthorizer: {
Type: 'AWS::ApiGatewayV2::Authorizer',
Properties: {
ApiId: {
Ref: 'WebsocketsApi',
},
Name: 'auth',
AuthorizerType: 'REQUEST',
AuthorizerUri: {
'Fn::Join': [
'',
[
'arn:',
{
Ref: 'AWS::Partition',
},
':apigateway:',
{
Ref: 'AWS::Region',
},
':lambda:path/2015-03-31/functions/',
{
'Fn::GetAtt': ['AuthLambdaFunction', 'Arn'],
},
'/invocations',
],
awsCompileWebsocketsEvents.compileAuthorizers();
const resources =
awsCompileWebsocketsEvents.serverless.service.provider.compiledCloudFormationTemplate
.Resources;

expect(resources).to.deep.equal({
AuthWebsocketsAuthorizer: {
Type: 'AWS::ApiGatewayV2::Authorizer',
Properties: {
ApiId: {
Ref: 'WebsocketsApi',
},
Name: 'auth',
AuthorizerType: 'REQUEST',
AuthorizerUri: {
'Fn::Join': [
'',
[
'arn:',
{
Ref: 'AWS::Partition',
},
':apigateway:',
{
Ref: 'AWS::Region',
},
':lambda:path/2015-03-31/functions/',
{
'Fn::GetAtt': ['AuthLambdaFunction', 'Arn'],
},
'/invocations',
],
},
IdentitySource: ['route.request.header.Auth'],
],
},
IdentitySource: ['route.request.header.Auth'],
},
});
},
});
});

Expand All @@ -93,14 +92,13 @@ describe('#compileAuthorizers()', () => {
websocketApiId: '5ezys3sght',
};

return awsCompileWebsocketsEvents.compileAuthorizers().then(() => {
const resources =
awsCompileWebsocketsEvents.serverless.service.provider.compiledCloudFormationTemplate
.Resources;
awsCompileWebsocketsEvents.compileAuthorizers();
const resources =
awsCompileWebsocketsEvents.serverless.service.provider.compiledCloudFormationTemplate
.Resources;

expect(resources.AuthWebsocketsAuthorizer.Properties).to.contain({
ApiId: '5ezys3sght',
});
expect(resources.AuthWebsocketsAuthorizer.Properties).to.contain({
ApiId: '5ezys3sght',
});
});
});
Expand Down Expand Up @@ -135,13 +133,12 @@ describe('#compileAuthorizers()', () => {
],
};

return awsCompileWebsocketsEvents.compileAuthorizers().then(() => {
const resources =
awsCompileWebsocketsEvents.serverless.service.provider.compiledCloudFormationTemplate
.Resources;
awsCompileWebsocketsEvents.compileAuthorizers();
const resources =
awsCompileWebsocketsEvents.serverless.service.provider.compiledCloudFormationTemplate
.Resources;

expect(resources).to.deep.equal({});
});
expect(resources).to.deep.equal({});
});
});
});

0 comments on commit 61dd3bd

Please sign in to comment.