UNPKG

@kalarrs/serverless-shared-api-gateway

Version:

Share a single AWS Api Gatewy between multiple serverless projects

156 lines (134 loc) 5.21 kB
'use strict' /* global describe it before beforeEach afterEach */ const fs = require('fs') const path = require('path') const chai = require('chai') const sinon = require('sinon') const Serverless = require('serverless') const ServerlessSharedApiGateway = require('../src') const AWS = require('aws-sdk-mock') const assert = chai.assert const serverlessFolder = path.join(__dirname, '.serverless') describe('index.js', () => { let sandbox let serverless let serverlessSharedApiGateway const createServerless = async () => { serverless = new Serverless({servicePath: __dirname}) await serverless.init() serverless.pluginManager.addPlugin(ServerlessSharedApiGateway) serverlessSharedApiGateway = serverless.pluginManager.plugins.find(p => p instanceof ServerlessSharedApiGateway) } const runServerlessPackage = () => { serverless.processedInput = { commands: ['package'], options: {stage: undefined, region: undefined} } return serverless.run() } beforeEach((done) => { sandbox = sinon.sandbox.create() done() }) afterEach((done) => { sandbox.restore() done() }) describe('when package command is ran', () => { afterEach((done) => { AWS.restore('APIGateway') done() }) describe('when an api gateway with matching id already exits', () => { let apiId = 'abcdef12gh' let apiName = 'test' let resourceId = 'abcde1fghi' before(async () => { AWS.mock('APIGateway', 'getRestApis', { items: [{ id: apiId, name: apiName, description: 'Existing Api', createdDate: '2018-01-01T00:00:00.000Z', apiKeySource: 'HEADER', endpointConfiguration: {types: ['EDGE']} }] }) AWS.mock('APIGateway', 'getResources', {items: [{id: resourceId, path: '/'}]}) await createServerless() serverless.service.provider.apiGatewayRestApiId = apiId await runServerlessPackage() }) it('it should set the existing api gateway on the plugin', () => { assert.equal(serverlessSharedApiGateway.restApiId, apiId) assert.equal(serverlessSharedApiGateway.restApiName, apiName) }) it('it should find the / resource and set it on the plugin', () => { assert.equal(serverlessSharedApiGateway.restApiResourceId, resourceId) }) it('update the cloud formation template', () => { assert.isTrue(fs.existsSync(`${serverlessFolder}/cloudformation-template-update-stack.json`)) // const cloudformation = require(`${serverlessFolder}/cloudformation-template-update-stack.json`); }) }) describe('when an api gateway with matching name already exits', () => { let apiId = 'abcdef12gh' let apiName = 'custom-name' let resourceId = 'abcde1fghi' before(async () => { AWS.mock('APIGateway', 'getRestApis', { items: [{ id: apiId, name: apiName, description: 'Generated by the shared Serverless - AWS Api Gateway plugin', createdDate: '2018-01-01T00:00:00.000Z', apiKeySource: 'HEADER', endpointConfiguration: {types: ['EDGE']} }] }) AWS.mock('APIGateway', 'getResources', {items: [{id: resourceId, path: '/'}]}) await createServerless() serverless.service.provider.apiGatewayRestApiName = apiName await runServerlessPackage() }) it('it should set the existing api gateway on the plugin', () => { assert.equal(serverlessSharedApiGateway.restApiId, apiId) assert.equal(serverlessSharedApiGateway.restApiName, apiName) }) it('it should find the / resource and set it on the plugin', () => { assert.equal(serverlessSharedApiGateway.restApiResourceId, resourceId) }) }) describe('when no api gateway exits and an apiGateway name is supplied', () => { let apiId = 'ijklmn12op' let apiName = 'generated' let resourceId = 'abcde1fghi' before(async () => { AWS.mock('APIGateway', 'getRestApis', {items: []}) AWS.mock('APIGateway', 'createRestApi', (params, cb) => { cb(null, { id: apiId, name: apiName, description: 'Generated by the shared Serverless - AWS Api Gateway plugin', createdDate: '2018-01-01T00:00:00.000Z', apiKeySource: 'HEADER', endpointConfiguration: {types: ['EDGE']} }) }) AWS.mock('APIGateway', 'getResources', { items: [{id: resourceId, path: '/'}] }) await createServerless() serverless.service.provider.apiGatewayRestApiName = apiName await runServerlessPackage() }) it('should create an api gateway and set it on the plugin', () => { assert.equal(serverlessSharedApiGateway.restApiId, apiId) assert.equal(serverlessSharedApiGateway.restApiName, apiName); }) it('it should find the / resource and set it on the plugin', () => { assert.equal(serverlessSharedApiGateway.restApiResourceId, resourceId) }) }) }) })