From 129c0ee0a561be458ef2e82c13de3ffe2c8f558d Mon Sep 17 00:00:00 2001 From: Elin Date: Fri, 7 Jan 2022 15:10:15 +0100 Subject: [PATCH] test(AWS Websocket): Refactor API tests (#10466) --- .../compile/events/websockets/index.test.js | 45 +++++--- .../compile/events/websockets/lib/api.test.js | 104 ------------------ 2 files changed, 31 insertions(+), 118 deletions(-) delete mode 100644 test/unit/lib/plugins/aws/package/compile/events/websockets/lib/api.test.js diff --git a/test/unit/lib/plugins/aws/package/compile/events/websockets/index.test.js b/test/unit/lib/plugins/aws/package/compile/events/websockets/index.test.js index c4de3c06708..719aada0685 100644 --- a/test/unit/lib/plugins/aws/package/compile/events/websockets/index.test.js +++ b/test/unit/lib/plugins/aws/package/compile/events/websockets/index.test.js @@ -114,9 +114,11 @@ describe('AwsCompileWebsocketsEvents', () => { }); describe('test/unit/lib/plugins/aws/package/compile/events/websockets/index.test.js', () => { - describe.skip('TODO: regular configuration', () => { + describe('regular configuration', () => { + let cfTemplate; + let awsNaming; before(async () => { - await runServerless({ + ({ cfTemplate, awsNaming } = await runServerless({ fixture: 'function', command: 'package', @@ -131,23 +133,39 @@ describe('test/unit/lib/plugins/aws/package/compile/events/websockets/index.test }, }, }, - }); + })); }); it('should create a websocket api resource', () => { - // Replaces - // https://github.com/serverless/serverless/blob/f64f7c68abb1d6837ecaa6173f4b605cf3975acf/test/unit/lib/plugins/aws/package/compile/events/websockets/lib/api.test.js#L37-L52 + const websocketsApiName = awsNaming.getWebsocketsApiName(); + expect(cfTemplate.Resources.WebsocketsApi).to.deep.equal({ + Type: 'AWS::ApiGatewayV2::Api', + Properties: { + Name: websocketsApiName, + RouteSelectionExpression: '$request.body.action', + Description: 'Serverless Websockets', + ProtocolType: 'WEBSOCKET', + }, + }); }); it('should configure expected IAM', () => { - // Replaces - // https://github.com/serverless/serverless/blob/f64f7c68abb1d6837ecaa6173f4b605cf3975acf/test/unit/lib/plugins/aws/package/compile/events/websockets/lib/api.test.js#L66-L91 + const id = awsNaming.getRoleLogicalId(); + expect( + cfTemplate.Resources[id].Properties.Policies[0].PolicyDocument.Statement + ).to.deep.include({ + Effect: 'Allow', + Action: ['execute-api:ManageConnections'], + Resource: [{ 'Fn::Sub': 'arn:${AWS::Partition}:execute-api:*:*:*/@connections/*' }], + }); }); }); - describe.skip('TODO: external websocket API', () => { + describe('external websocket API', () => { + let cfTemplate; + let awsNaming; before(async () => { - await runServerless({ + ({ cfTemplate, awsNaming } = await runServerless({ fixture: 'function', command: 'package', @@ -170,17 +188,16 @@ describe('test/unit/lib/plugins/aws/package/compile/events/websockets/index.test }, }, }, - }); + })); }); it('should not create a websocket api resource', () => { - // Replaces - // https://github.com/serverless/serverless/blob/f64f7c68abb1d6837ecaa6173f4b605cf3975acf/test/unit/lib/plugins/aws/package/compile/events/websockets/lib/api.test.js#L54-L64 + expect(cfTemplate.Resources.WebsocketsApi).to.equal(undefined); }); it('should not configure IAM policies with custom roles', () => { - // Replaces - // https://github.com/serverless/serverless/blob/f64f7c68abb1d6837ecaa6173f4b605cf3975acf/test/unit/lib/plugins/aws/package/compile/events/websockets/lib/api.test.js#L93-L103 + const id = awsNaming.getRoleLogicalId(); + expect(cfTemplate.Resources[id]).to.equal(undefined); }); }); }); diff --git a/test/unit/lib/plugins/aws/package/compile/events/websockets/lib/api.test.js b/test/unit/lib/plugins/aws/package/compile/events/websockets/lib/api.test.js deleted file mode 100644 index c11588e8bd5..00000000000 --- a/test/unit/lib/plugins/aws/package/compile/events/websockets/lib/api.test.js +++ /dev/null @@ -1,104 +0,0 @@ -'use strict'; - -const expect = require('chai').expect; -const AwsCompileWebsocketsEvents = require('../../../../../../../../../../lib/plugins/aws/package/compile/events/websockets/index'); -const Serverless = require('../../../../../../../../../../lib/Serverless'); -const AwsProvider = require('../../../../../../../../../../lib/plugins/aws/provider'); - -describe('#compileApi()', () => { - let awsCompileWebsocketsEvents; - let roleLogicalId; - - beforeEach(() => { - const serverless = new Serverless(); - serverless.setProvider('aws', new AwsProvider(serverless)); - serverless.service.service = 'my-service'; - serverless.service.provider.compiledCloudFormationTemplate = { Resources: {} }; - - awsCompileWebsocketsEvents = new AwsCompileWebsocketsEvents(serverless); - - roleLogicalId = awsCompileWebsocketsEvents.provider.naming.getRoleLogicalId(); - awsCompileWebsocketsEvents.serverless.service.provider.compiledCloudFormationTemplate.Resources = - { - [roleLogicalId]: { - Properties: { - Policies: [ - { - PolicyDocument: { - Statement: [], - }, - }, - ], - }, - }, - }; - }); - - 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', - }, - }); - }); - - it('should ignore API resource creation if there is predefined websocketApi config', () => { - awsCompileWebsocketsEvents.serverless.service.provider.apiGateway = { - websocketApiId: '5ezys3sght', - }; - awsCompileWebsocketsEvents.compileApi(); - const resources = - awsCompileWebsocketsEvents.serverless.service.provider.compiledCloudFormationTemplate - .Resources; - - expect(resources).to.not.have.property('WebsocketsApi'); - }); - - 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/*' }, - ], - }, - ], - }, - }, - ], - }, - }); - }); - - it('should NOT add the websockets policy if role resource does not exist', () => { - awsCompileWebsocketsEvents.serverless.service.provider.compiledCloudFormationTemplate.Resources = - {}; - - awsCompileWebsocketsEvents.compileApi(); - const resources = - awsCompileWebsocketsEvents.serverless.service.provider.compiledCloudFormationTemplate - .Resources; - - expect(resources[roleLogicalId]).to.deep.equal(undefined); - }); -});