mirror of
https://github.com/serverless/serverless.git
synced 2026-01-18 14:58:43 +00:00
test(AWS Websocket): Refactor API tests (#10466)
This commit is contained in:
parent
46d090a302
commit
129c0ee0a5
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -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);
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user