Skip to content

Commit 99ee35e

Browse files
committed
add some tests
1 parent 43d0e32 commit 99ee35e

File tree

3 files changed

+215
-0
lines changed

3 files changed

+215
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
'use strict';
2+
3+
const expect = require('chai').expect;
4+
const sinon = require('sinon');
5+
const BbPromise = require('bluebird');
6+
const Serverless = require('serverless/lib/Serverless');
7+
const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider');
8+
const ServerlessStepFunctions = require('./../../../index');
9+
10+
11+
describe('#getEndpointInfo()', () => {
12+
let serverless;
13+
let serverlessStepFunctions;
14+
let describeStacksStub;
15+
16+
beforeEach(() => {
17+
serverless = new Serverless();
18+
serverless.setProvider('aws', new AwsProvider(serverless));
19+
serverless.service.provider.compiledCloudFormationTemplate = { Resources: {} };
20+
serverless.service.service = 'new-service';
21+
serverless.service.stepfunctions = {
22+
stateMachines: {
23+
first: {
24+
events: [
25+
{
26+
http: {
27+
path: 'foo/bar',
28+
method: 'POST',
29+
},
30+
},
31+
],
32+
},
33+
},
34+
};
35+
serverlessStepFunctions = new ServerlessStepFunctions(serverless);
36+
});
37+
38+
it('should return endpointInfo', () => {
39+
const describeStacksResponse = {
40+
Stacks: [
41+
{
42+
StackId: 'arn:aws:cloudformation:us-east-1:123456789012:' +
43+
'stack/myteststack/466df9e0-0dff-08e3-8e2f-5088487c4896',
44+
Description: 'AWS CloudFormation Sample Template S3_Bucket: ' +
45+
'Sample template showing how to create a publicly accessible S3 bucket.',
46+
Tags: [],
47+
Outputs: [
48+
{
49+
Description: 'URL of the service endpoint',
50+
OutputKey: 'ServiceEndpoint',
51+
OutputValue: 'ab12cd34ef.execute-api.us-east-1.amazonaws.com/dev',
52+
},
53+
{
54+
Description: 'first',
55+
OutputKey: 'ApiGatewayApiKey1Value',
56+
OutputValue: 'xxx',
57+
},
58+
{
59+
Description: 'second',
60+
OutputKey: 'ApiGatewayApiKey2Value',
61+
OutputValue: 'yyy',
62+
},
63+
],
64+
StackStatusReason: null,
65+
CreationTime: '2013-08-23T01:02:15.422Z',
66+
Capabilities: [],
67+
StackName: 'myteststack',
68+
StackStatus: 'CREATE_COMPLETE',
69+
DisableRollback: false,
70+
},
71+
],
72+
};
73+
74+
describeStacksStub = sinon.stub(serverlessStepFunctions.provider, 'request')
75+
.returns(BbPromise.resolve(describeStacksResponse));
76+
77+
return serverlessStepFunctions.getEndpointInfo().then(() => {
78+
expect(describeStacksStub.calledOnce).to.equal(true);
79+
expect(describeStacksStub.calledWithExactly(
80+
'CloudFormation',
81+
'describeStacks',
82+
{
83+
StackName: serverlessStepFunctions.provider.naming.getStackName(),
84+
},
85+
serverlessStepFunctions.options.stage,
86+
serverlessStepFunctions.options.region
87+
)).to.equal(true);
88+
89+
expect(serverlessStepFunctions.endpointInfo)
90+
.to.equal('ab12cd34ef.execute-api.us-east-1.amazonaws.com/dev');
91+
serverlessStepFunctions.provider.request.restore();
92+
});
93+
});
94+
95+
it('should resolve if result is empty', () => {
96+
const describeStacksResponse = null;
97+
98+
describeStacksStub = sinon.stub(serverlessStepFunctions.provider, 'request')
99+
.returns(BbPromise.resolve(describeStacksResponse));
100+
101+
return serverlessStepFunctions.getEndpointInfo().then(() => {
102+
expect(describeStacksStub.calledOnce).to.equal(true);
103+
expect(describeStacksStub.calledWithExactly(
104+
'CloudFormation',
105+
'describeStacks',
106+
{
107+
StackName: serverlessStepFunctions.provider.naming.getStackName(),
108+
},
109+
serverlessStepFunctions.options.stage,
110+
serverlessStepFunctions.options.region
111+
)).to.equal(true);
112+
113+
expect(serverlessStepFunctions.endpointInfo).to.equal(undefined);
114+
serverlessStepFunctions.provider.request.restore();
115+
});
116+
});
117+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
3+
const expect = require('chai').expect;
4+
const Serverless = require('serverless/lib/Serverless');
5+
const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider');
6+
const ServerlessStepFunctions = require('./../../../index');
7+
const path = require('path');
8+
9+
describe('#compileHttpIamRole()', () => {
10+
let serverless;
11+
let serverlessStepFunctions;
12+
13+
beforeEach(() => {
14+
serverless = new Serverless();
15+
serverless.setProvider('aws', new AwsProvider(serverless));
16+
serverless.service.provider.compiledCloudFormationTemplate = { Resources: {} };
17+
serverless.service.service = 'new-service';
18+
serverless.service.stepfunctions = {
19+
stateMachines: {
20+
first: {
21+
events: [
22+
{
23+
http: {
24+
path: 'foo/bar',
25+
method: 'POST',
26+
},
27+
},
28+
],
29+
},
30+
},
31+
};
32+
serverlessStepFunctions = new ServerlessStepFunctions(serverless);
33+
});
34+
35+
it('should create a IAM Role resource', () => serverlessStepFunctions
36+
.compileHttpIamRole().then(() => {
37+
expect(
38+
serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
39+
.Resources.ApigatewayToStepFunctionsRole.Type
40+
).to.equal('AWS::IAM::Role');
41+
})
42+
);
43+
});
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
3+
const expect = require('chai').expect;
4+
const Serverless = require('serverless/lib/Serverless');
5+
const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider');
6+
const ServerlessStepFunctions = require('./../../../index');
7+
8+
describe('#compileRestApi()', () => {
9+
let serverless;
10+
let serverlessStepFunctions;
11+
12+
const serviceResourcesAwsResourcesObjectMock = {
13+
Resources: {
14+
ApiGatewayRestApi: {
15+
Type: 'AWS::ApiGateway::RestApi',
16+
Properties: {
17+
Name: 'dev-new-service',
18+
},
19+
},
20+
},
21+
};
22+
23+
beforeEach(() => {
24+
serverless = new Serverless();
25+
serverless.setProvider('aws', new AwsProvider(serverless));
26+
serverless.service.provider.compiledCloudFormationTemplate = { Resources: {} };
27+
serverless.service.service = 'new-service';
28+
serverless.service.stepfunctions = {
29+
stateMachines: {
30+
first: {
31+
events: [
32+
{
33+
http: {
34+
path: 'foo/bar',
35+
method: 'POST',
36+
},
37+
},
38+
],
39+
},
40+
},
41+
};
42+
serverlessStepFunctions = new ServerlessStepFunctions(serverless);
43+
});
44+
45+
it('should create a REST API resource', () => serverlessStepFunctions
46+
.compileRestApi().then(() => {
47+
expect(
48+
serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate
49+
.Resources
50+
).to.deep.equal(
51+
serviceResourcesAwsResourcesObjectMock.Resources
52+
);
53+
})
54+
);
55+
});

0 commit comments

Comments
 (0)