|
| 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 | +}); |
0 commit comments