serverless/lib/plugins/aws/deploy/lib/validateTemplate.test.js
2017-05-31 14:08:23 +02:00

86 lines
2.7 KiB
JavaScript

'use strict';
/* eslint-disable no-unused-expressions */
const sinon = require('sinon');
const chai = require('chai');
const AwsProvider = require('../../provider/awsProvider');
const AwsDeploy = require('../index');
const Serverless = require('../../../../Serverless');
// Configure chai
chai.use(require('chai-as-promised'));
chai.use(require('sinon-chai'));
const expect = require('chai').expect;
describe('validateTemplate', () => {
let awsDeploy;
let serverless;
let validateTemplateStub;
beforeEach(() => {
serverless = new Serverless();
serverless.config.servicePath = 'foo';
serverless.setProvider('aws', new AwsProvider(serverless));
const options = {
stage: 'dev',
region: 'us-east-1',
};
awsDeploy = new AwsDeploy(serverless, options);
awsDeploy.bucketName = 'deployment-bucket';
awsDeploy.serverless.service.package.artifactDirectoryName = 'somedir';
awsDeploy.serverless.service.functions = {
first: {
handler: 'foo',
},
};
validateTemplateStub = sinon.stub(awsDeploy.provider, 'request');
awsDeploy.serverless.cli = {
log: sinon.spy(),
};
});
afterEach(() => {
awsDeploy.provider.request.restore();
});
describe('#validateTemplate()', () => {
it('should resolve if the CloudFormation template is valid', () => {
validateTemplateStub.resolves();
return expect(awsDeploy.validateTemplate()).to.be.fulfilled.then(() => {
expect(awsDeploy.serverless.cli.log).to.have.been.called;
expect(validateTemplateStub).to.have.been.calledOnce;
expect(validateTemplateStub).to.have.been.calledWithExactly(
'CloudFormation',
'validateTemplate',
{
TemplateURL: 'https://s3.amazonaws.com/deployment-bucket/somedir/compiled-cloudformation-template.json',
},
awsDeploy.options.stage,
awsDeploy.options.region
);
});
});
it('should throw an error if the CloudFormation template is invalid', () => {
validateTemplateStub.rejects({ message: 'Some error while validating' });
return expect(awsDeploy.validateTemplate()).to.be.rejected.then((error) => {
expect(awsDeploy.serverless.cli.log).to.have.been.called;
expect(validateTemplateStub).to.have.been.calledOnce;
expect(validateTemplateStub).to.have.been.calledWithExactly(
'CloudFormation',
'validateTemplate',
{
TemplateURL: 'https://s3.amazonaws.com/deployment-bucket/somedir/compiled-cloudformation-template.json',
},
awsDeploy.options.stage,
awsDeploy.options.region
);
expect(error.message).to.match(/is invalid: Some error while validating/);
});
});
});
});