|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const sinon = require('sinon') |
| 4 | +const chai = require('chai') |
| 5 | +const BbPromise = require('bluebird') |
| 6 | +const Serverless = require('serverless/lib/Serverless') |
| 7 | +const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider') |
| 8 | +const ServerlessApigatewayServiceProxy = require('./index') |
| 9 | + |
| 10 | +chai.use(require('chai-as-promised')) |
| 11 | +chai.use(require('sinon-chai')) |
| 12 | + |
| 13 | +const expect = chai.expect |
| 14 | + |
| 15 | +describe('#index()', () => { |
| 16 | + let serverless |
| 17 | + let serverlessApigatewayServiceProxy |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + serverless = new Serverless() |
| 21 | + serverless.servicePath = true |
| 22 | + serverless.service.service = 'apigw-service-proxy' |
| 23 | + const options = { |
| 24 | + stage: 'dev', |
| 25 | + region: 'us-east-1' |
| 26 | + } |
| 27 | + serverless.setProvider('aws', new AwsProvider(serverless)) |
| 28 | + serverless.service.provider.compiledCloudFormationTemplate = { Resources: {} } |
| 29 | + serverlessApigatewayServiceProxy = new ServerlessApigatewayServiceProxy(serverless, options) |
| 30 | + }) |
| 31 | + |
| 32 | + describe('#constructor()', () => { |
| 33 | + it('should have hooks', () => expect(serverlessApigatewayServiceProxy.hooks).to.be.not.empty) |
| 34 | + |
| 35 | + it('should set the provider variable to an instance of AwsProvider', () => |
| 36 | + expect(serverlessApigatewayServiceProxy.provider).to.be.instanceof(AwsProvider)) |
| 37 | + |
| 38 | + it('should have access to the serverless instance', () => |
| 39 | + expect(serverlessApigatewayServiceProxy.serverless).to.deep.equal(serverless)) |
| 40 | + |
| 41 | + it('should set the options variable', () => |
| 42 | + expect(serverlessApigatewayServiceProxy.options).to.deep.equal({ |
| 43 | + stage: 'dev', |
| 44 | + region: 'us-east-1' |
| 45 | + })) |
| 46 | + |
| 47 | + it('should set the region variable', () => |
| 48 | + expect(serverlessApigatewayServiceProxy.region).to.be.equal('us-east-1')) |
| 49 | + |
| 50 | + it('should set the stage variable', () => |
| 51 | + expect(serverlessApigatewayServiceProxy.stage).to.be.equal('dev')) |
| 52 | + |
| 53 | + it('should run package:compileEvents in order', async () => { |
| 54 | + serverlessApigatewayServiceProxy.serverless.service.custom = { |
| 55 | + apiGatewayServiceProxies: [ |
| 56 | + { |
| 57 | + kinesis: { |
| 58 | + path: '/kinesis', |
| 59 | + method: 'post' |
| 60 | + } |
| 61 | + }, |
| 62 | + { |
| 63 | + sqs: { |
| 64 | + path: '/sqs', |
| 65 | + method: 'post' |
| 66 | + } |
| 67 | + } |
| 68 | + ] |
| 69 | + } |
| 70 | + |
| 71 | + const validateServiceProxiesStub = sinon |
| 72 | + .stub(serverlessApigatewayServiceProxy, 'validateServiceProxies') |
| 73 | + .returns(BbPromise.resolve()) |
| 74 | + const compileRestApiStub = sinon |
| 75 | + .stub(serverlessApigatewayServiceProxy, 'compileRestApi') |
| 76 | + .returns(BbPromise.resolve()) |
| 77 | + const compileResourcesStub = sinon |
| 78 | + .stub(serverlessApigatewayServiceProxy, 'compileResources') |
| 79 | + .returns(BbPromise.resolve()) |
| 80 | + const compileCorsStub = sinon |
| 81 | + .stub(serverlessApigatewayServiceProxy, 'compileCors') |
| 82 | + .returns(BbPromise.resolve()) |
| 83 | + const compileKinesisServiceProxyStub = sinon |
| 84 | + .stub(serverlessApigatewayServiceProxy, 'compileKinesisServiceProxy') |
| 85 | + .returns(BbPromise.resolve()) |
| 86 | + const compileSqsServiceProxyStub = sinon |
| 87 | + .stub(serverlessApigatewayServiceProxy, 'compileSqsServiceProxy') |
| 88 | + .returns(BbPromise.resolve()) |
| 89 | + const mergeDeploymentStub = sinon |
| 90 | + .stub(serverlessApigatewayServiceProxy, 'mergeDeployment') |
| 91 | + .returns(BbPromise.resolve()) |
| 92 | + |
| 93 | + await expect( |
| 94 | + serverlessApigatewayServiceProxy.hooks['package:compileEvents']() |
| 95 | + ).to.be.fulfilled.then(() => { |
| 96 | + expect(validateServiceProxiesStub.calledOnce).to.be.equal(true) |
| 97 | + expect(compileRestApiStub.calledOnce).to.be.equal(true) |
| 98 | + expect(compileResourcesStub.calledOnce).to.be.equal(true) |
| 99 | + expect(compileCorsStub.calledOnce).to.be.equal(true) |
| 100 | + expect(compileKinesisServiceProxyStub.calledOnce).to.be.equal(true) |
| 101 | + expect(compileSqsServiceProxyStub.calledOnce).to.be.equal(true) |
| 102 | + expect(mergeDeploymentStub.calledOnce).to.be.equal(true) |
| 103 | + }) |
| 104 | + |
| 105 | + serverlessApigatewayServiceProxy.validateServiceProxies.restore() |
| 106 | + serverlessApigatewayServiceProxy.compileRestApi.restore() |
| 107 | + serverlessApigatewayServiceProxy.compileResources.restore() |
| 108 | + serverlessApigatewayServiceProxy.compileCors.restore() |
| 109 | + serverlessApigatewayServiceProxy.compileKinesisServiceProxy.restore() |
| 110 | + serverlessApigatewayServiceProxy.compileSqsServiceProxy.restore() |
| 111 | + serverlessApigatewayServiceProxy.mergeDeployment.restore() |
| 112 | + }) |
| 113 | + |
| 114 | + it('should not run package:compileEvents if no plugin setting', async () => { |
| 115 | + const validateServiceProxiesStub = sinon |
| 116 | + .stub(serverlessApigatewayServiceProxy, 'validateServiceProxies') |
| 117 | + .returns(BbPromise.resolve()) |
| 118 | + const compileRestApiStub = sinon |
| 119 | + .stub(serverlessApigatewayServiceProxy, 'compileRestApi') |
| 120 | + .returns(BbPromise.resolve()) |
| 121 | + const compileResourcesStub = sinon |
| 122 | + .stub(serverlessApigatewayServiceProxy, 'compileResources') |
| 123 | + .returns(BbPromise.resolve()) |
| 124 | + const compileCorsStub = sinon |
| 125 | + .stub(serverlessApigatewayServiceProxy, 'compileCors') |
| 126 | + .returns(BbPromise.resolve()) |
| 127 | + const compileKinesisServiceProxyStub = sinon |
| 128 | + .stub(serverlessApigatewayServiceProxy, 'compileKinesisServiceProxy') |
| 129 | + .returns(BbPromise.resolve()) |
| 130 | + const compileSqsServiceProxyStub = sinon |
| 131 | + .stub(serverlessApigatewayServiceProxy, 'compileSqsServiceProxy') |
| 132 | + .returns(BbPromise.resolve()) |
| 133 | + const mergeDeploymentStub = sinon |
| 134 | + .stub(serverlessApigatewayServiceProxy, 'mergeDeployment') |
| 135 | + .returns(BbPromise.resolve()) |
| 136 | + |
| 137 | + await expect( |
| 138 | + serverlessApigatewayServiceProxy.hooks['package:compileEvents']() |
| 139 | + ).to.be.fulfilled.then(() => { |
| 140 | + expect(validateServiceProxiesStub.notCalled).to.be.equal(true) |
| 141 | + expect(compileRestApiStub.notCalled).to.be.equal(true) |
| 142 | + expect(compileResourcesStub.notCalled).to.be.equal(true) |
| 143 | + expect(compileCorsStub.notCalled).to.be.equal(true) |
| 144 | + expect(compileKinesisServiceProxyStub.notCalled).to.be.equal(true) |
| 145 | + expect(compileSqsServiceProxyStub.notCalled).to.be.equal(true) |
| 146 | + expect(mergeDeploymentStub.notCalled).to.be.equal(true) |
| 147 | + }) |
| 148 | + |
| 149 | + serverlessApigatewayServiceProxy.validateServiceProxies.restore() |
| 150 | + serverlessApigatewayServiceProxy.compileRestApi.restore() |
| 151 | + serverlessApigatewayServiceProxy.compileResources.restore() |
| 152 | + serverlessApigatewayServiceProxy.compileCors.restore() |
| 153 | + serverlessApigatewayServiceProxy.compileKinesisServiceProxy.restore() |
| 154 | + serverlessApigatewayServiceProxy.compileSqsServiceProxy.restore() |
| 155 | + serverlessApigatewayServiceProxy.mergeDeployment.restore() |
| 156 | + }) |
| 157 | + |
| 158 | + it('should run after:deploy:deploy in order', async () => { |
| 159 | + serverlessApigatewayServiceProxy.serverless.service.custom = { |
| 160 | + apiGatewayServiceProxies: [ |
| 161 | + { |
| 162 | + kinesis: { |
| 163 | + path: '/kinesis', |
| 164 | + method: 'post' |
| 165 | + } |
| 166 | + }, |
| 167 | + { |
| 168 | + sqs: { |
| 169 | + path: '/sqs', |
| 170 | + method: 'post' |
| 171 | + } |
| 172 | + } |
| 173 | + ] |
| 174 | + } |
| 175 | + |
| 176 | + const getStackInfoStub = sinon |
| 177 | + .stub(serverlessApigatewayServiceProxy, 'getStackInfo') |
| 178 | + .returns(BbPromise.resolve()) |
| 179 | + const displayStub = sinon |
| 180 | + .stub(serverlessApigatewayServiceProxy, 'display') |
| 181 | + .returns(BbPromise.resolve()) |
| 182 | + |
| 183 | + await expect( |
| 184 | + serverlessApigatewayServiceProxy.hooks['after:deploy:deploy']() |
| 185 | + ).to.be.fulfilled.then(() => { |
| 186 | + expect(getStackInfoStub.calledOnce).to.be.equal(true) |
| 187 | + expect(displayStub.calledOnce).to.be.equal(true) |
| 188 | + }) |
| 189 | + |
| 190 | + serverlessApigatewayServiceProxy.getStackInfo.restore() |
| 191 | + serverlessApigatewayServiceProxy.display.restore() |
| 192 | + }) |
| 193 | + |
| 194 | + it('should not run after:deploy:deploy if no plugin setting', async () => { |
| 195 | + const getStackInfoStub = sinon |
| 196 | + .stub(serverlessApigatewayServiceProxy, 'getStackInfo') |
| 197 | + .returns(BbPromise.resolve()) |
| 198 | + const displayStub = sinon |
| 199 | + .stub(serverlessApigatewayServiceProxy, 'display') |
| 200 | + .returns(BbPromise.resolve()) |
| 201 | + |
| 202 | + await expect( |
| 203 | + serverlessApigatewayServiceProxy.hooks['after:deploy:deploy']() |
| 204 | + ).to.be.fulfilled.then(() => { |
| 205 | + expect(getStackInfoStub.notCalled).to.be.equal(true) |
| 206 | + expect(displayStub.notCalled).to.be.equal(true) |
| 207 | + }) |
| 208 | + |
| 209 | + serverlessApigatewayServiceProxy.getStackInfo.restore() |
| 210 | + serverlessApigatewayServiceProxy.display.restore() |
| 211 | + }) |
| 212 | + }) |
| 213 | + |
| 214 | + describe('#mergeDeployment()', () => { |
| 215 | + it('should set Dependson resource if core has http events', async () => { |
| 216 | + const compileDeploymentStub = sinon |
| 217 | + .stub(serverlessApigatewayServiceProxy, 'compileDeployment') |
| 218 | + .returns(BbPromise.resolve()) |
| 219 | + serverlessApigatewayServiceProxy.apiGatewayMethodLogicalIds = 'apiResource' |
| 220 | + serverless.service.provider.compiledCloudFormationTemplate.Resources = { |
| 221 | + api: { |
| 222 | + Type: 'AWS::ApiGateway::Deployment', |
| 223 | + DependsOn: ['method-dependency1', 'method-dependency2'] |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + await expect(serverlessApigatewayServiceProxy.mergeDeployment()).to.be.fulfilled.then(() => { |
| 228 | + expect( |
| 229 | + serverless.service.provider.compiledCloudFormationTemplate.Resources.api.DependsOn |
| 230 | + ).to.deep.equal(['method-dependency1', 'method-dependency2', 'apiResource']) |
| 231 | + expect(compileDeploymentStub.notCalled).to.be.equal(true) |
| 232 | + }) |
| 233 | + serverlessApigatewayServiceProxy.compileDeployment.restore() |
| 234 | + }) |
| 235 | + |
| 236 | + it('should create deployment resource if core do not have any http events', async () => { |
| 237 | + serverlessApigatewayServiceProxy.apiGatewayMethodLogicalIds = 'apiResource' |
| 238 | + serverlessApigatewayServiceProxy.serverless.instanceId = 'xxx' |
| 239 | + |
| 240 | + await expect(serverlessApigatewayServiceProxy.mergeDeployment()).to.be.fulfilled.then(() => { |
| 241 | + expect(serverless.service.provider.compiledCloudFormationTemplate.Resources).to.deep.equal({ |
| 242 | + ApiGatewayDeploymentxxx: { |
| 243 | + Type: 'AWS::ApiGateway::Deployment', |
| 244 | + Properties: { |
| 245 | + RestApiId: { Ref: 'ApiGatewayRestApi' }, |
| 246 | + StageName: 'dev', |
| 247 | + Description: undefined |
| 248 | + }, |
| 249 | + DependsOn: 'apiResource' |
| 250 | + } |
| 251 | + }) |
| 252 | + }) |
| 253 | + }) |
| 254 | + }) |
| 255 | +}) |
0 commit comments