Nick Chisiu 16aa6581ab fix #1854 - update npm devDependencies packages (#1879)
* fix #1854 - added latest version of eslint-config-airbnb-base package

* fix #1854 - added latest npm shrinkwrap scheme

* fix #1854 - added latest npm coveralls package

* fix #1854 - added latest npm eslint package

* fix #1854 - added latest npm eslint-config-airbnb package

* fix #1854 - added latest npm eslint-plugin-import package

* fix #1854 - added latest npm eslint-plugin-jsx-a11y package

* fix #1854 - added latest npm eslint-plugin-react package

* fix #1854 - fixed estlint new-parens errors for integration test

* fix #1854 - fixed estlint new-parens errors for yaml parsers tests

* fix #1854 - fixed estlint max-len errors for util tests

* fix #1854 - disabled no-extraneous-dependencies on eslintrc for NodeJS 4 incapability on this feature

* fix #1854 - fixed eslint new-parens errors for Service tests

* fix #1854 - fixed eslint new-parens errors for Serverless tests

* fix #1854 - fixed eslint new-parens errors for plugin manager tests

* fix #1854 - fixed eslint new-parens errors for plugin tracking tests

* fix #1854 - fixed eslint new-parens errors for plugin package zipService lib

* fix #1854 - fixed eslint new-parens errors for plugin package zipService tests

* fix #1854 - fixed eslint trailing spaces errors for plugin package zipService lib

* fix #1854 - fixed eslint new-parens errors for plugin package cleanup  tests

* fix #1854 - fixed eslint new-parens errors for plugin create tests

* fix #1854 - fixed eslint dot same line expectation error on plugin aws logs index

* fix #1854 - fixed eslint operator assignment error on plugin aws logs index

* fix #1854 - fixed eslint dot location error on plugin aws invoke tests

* fix #1854 - fixed eslint new-parens error on plugin aws invoke tests

* fix #1854 - fixed eslint new-parens error on plugin aws deployFunction tests

* fix #1854 - fixed eslint new-parens error on plugin aws deploy uploadDeploymentPackage tests

* fix #1854 - fixed eslint new-parens error on plugin aws deploy updateStack tests

* fix #1854 - fixed eslint new-parens error on plugin aws deploy createStack tests

* fix #1854 - fixed eslint new-parens error on plugin aws deploy apiGateway lib deployment

* fix #1854 - fixed eslint unary typeof whitespace req error on Serverless Service class

* fix #1854 - fixed eslint unary typeof whitespace req error on Serverless Service class ( second fix )

* fix #1854 - fixed eslint no-lonely-if req error on Serverless Service class

* fix #1854 - disabled react/require-extension on eslintrc because it's deprecated

* fix #1854 - AwsCompileApigEvents #constructor() should resolve if no functions are given:
                 Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.

* fix #1854 - createStack #postCreate() should resolve:
                 Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.

* fix #1854 - emptyS3Bucket #deleteObjects() should resolve if objectsInBucket is empty:
                 Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.

* fix #1854 - AwsInvoke #extendedValidate() should resolve if path is not given:
                 Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.

* fix #1854 - #cleanup() should resolve if the .serverless directory is not present:
                 Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.

* fix #1854 -#validate() should resolve if servicePath is given:
                Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.

* fix #1854 -  Service #load() should resolve if no servicePath is found:
                 Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.

* fix #1854 - added latest mocha package

* fix #1854 - added latest sinon npm package

* fix #1854 - serverless/lib/plugins/aws/deploy/tests/createStack.js 136:48  error  Missing semicolon  semi

* fix #1854 - serverless/lib/plugins/package/tests/cleanup.js 35:7  error  Missing semicolon  semi

* fix #1854 - serverless/lib/plugins/package/tests/validate.js 22:49  error  Missing semicolon  semi

* fix #1854 - added latest npm shrinkwrap

* fix #1854 - fixed no-extra-boolean-cast eslint error on aws deploy apiGateway methods

* fix #1854 - fixed  new-parens eslint error on serverless tests for Service class
2016-08-18 11:51:09 +02:00

189 lines
6.2 KiB
JavaScript

'use strict';
const expect = require('chai').expect;
const sinon = require('sinon');
const path = require('path');
const os = require('os');
const AwsInvoke = require('../');
const Serverless = require('../../../../Serverless');
const BbPromise = require('bluebird');
describe('AwsInvoke', () => {
const serverless = new Serverless();
const options = {
stage: 'dev',
region: 'us-east-1',
function: 'first',
};
const awsInvoke = new AwsInvoke(serverless, options);
describe('#constructor()', () => {
it('should have hooks', () => expect(awsInvoke.hooks).to.be.not.empty);
it('should set the provider variable to "aws"', () => expect(awsInvoke.provider)
.to.equal('aws'));
it('should run promise chain in order', () => {
const validateStub = sinon
.stub(awsInvoke, 'extendedValidate').returns(BbPromise.resolve());
const invokeStub = sinon
.stub(awsInvoke, 'invoke').returns(BbPromise.resolve());
const logStub = sinon
.stub(awsInvoke, 'log').returns(BbPromise.resolve());
return awsInvoke.hooks['invoke:invoke']().then(() => {
expect(validateStub.calledOnce).to.be.equal(true);
expect(invokeStub.calledAfter(validateStub)).to.be.equal(true);
expect(logStub.calledAfter(invokeStub)).to.be.equal(true);
awsInvoke.extendedValidate.restore();
awsInvoke.invoke.restore();
awsInvoke.log.restore();
});
});
it('should set an empty options object if no options are given', () => {
const awsInvokeWithEmptyOptions = new AwsInvoke(serverless);
expect(awsInvokeWithEmptyOptions.options).to.deep.equal({});
});
});
describe('#extendedValidate()', () => {
beforeEach(() => {
serverless.config.servicePath = true;
serverless.service.environment = {
vars: {},
stages: {
dev: {
vars: {},
regions: {
'us-east-1': {
vars: {},
},
},
},
},
};
serverless.service.functions = {
first: {
handler: true,
},
};
});
it('it should throw error if function is not provided', () => {
serverless.service.functions = null;
expect(() => awsInvoke.extendedValidate()).to.throw(Error);
});
it('it should parse file if file path is provided', () => {
serverless.config.servicePath = path.join(os.tmpdir(), (new Date()).getTime().toString());
const data = {
testProp: 'testValue',
};
serverless.utils.writeFileSync(path
.join(serverless.config.servicePath, 'data.json'), JSON.stringify(data));
awsInvoke.options.path = 'data.json';
return awsInvoke.extendedValidate().then(() => {
expect(awsInvoke.options.data).to.deep.equal(data);
awsInvoke.options.path = false;
serverless.config.servicePath = true;
});
});
it('it should throw error if service path is not set', () => {
serverless.config.servicePath = false;
expect(() => awsInvoke.extendedValidate()).to.throw(Error);
serverless.config.servicePath = true;
});
it('it should throw error if file path does not exist', () => {
serverless.config.servicePath = path.join(os.tmpdir(), (new Date()).getTime().toString());
awsInvoke.options.path = 'some/path';
expect(() => awsInvoke.extendedValidate()).to.throw(Error);
awsInvoke.options.path = false;
serverless.config.servicePath = true;
});
it('should resolve if path is not given', (done) => {
awsInvoke.options.path = false;
awsInvoke.extendedValidate().then(() => done());
});
});
describe('#invoke()', () => {
let invokeStub;
beforeEach(() => {
invokeStub = sinon.stub(awsInvoke.sdk, 'request').returns(BbPromise.resolve());
awsInvoke.serverless.service.service = 'new-service';
awsInvoke.options = {
stage: 'dev',
function: 'first',
functionObj: {
name: 'customName',
},
};
});
it('should invoke with correct params', () => awsInvoke.invoke()
.then(() => {
expect(invokeStub.calledOnce).to.be.equal(true);
expect(invokeStub.calledWith(awsInvoke.options.stage, awsInvoke.options.region));
expect(invokeStub.args[0][2].FunctionName).to.be.equal('customName');
expect(invokeStub.args[0][2].InvocationType).to.be.equal('RequestResponse');
expect(invokeStub.args[0][2].LogType).to.be.equal('None');
expect(typeof invokeStub.args[0][2].Payload).to.not.be.equal('undefined');
awsInvoke.sdk.request.restore();
})
);
it('should invoke and log', () => {
awsInvoke.options.log = true;
return awsInvoke.invoke().then(() => {
expect(invokeStub.calledOnce).to.be.equal(true);
expect(invokeStub.calledWith(awsInvoke.options.stage, awsInvoke.options.region));
expect(invokeStub.args[0][2].FunctionName).to.be.equal('customName');
expect(invokeStub.args[0][2].InvocationType).to.be.equal('RequestResponse');
expect(invokeStub.args[0][2].LogType).to.be.equal('Tail');
expect(typeof invokeStub.args[0][2].Payload).to.not.be.equal('undefined');
awsInvoke.sdk.request.restore();
});
});
it('should invoke with other invocation type', () => {
awsInvoke.options.type = 'OtherType';
return awsInvoke.invoke().then(() => {
expect(invokeStub.calledOnce).to.be.equal(true);
expect(invokeStub.calledWith(awsInvoke.options.stage, awsInvoke.options.region));
expect(invokeStub.args[0][2].FunctionName).to.be.equal('customName');
expect(invokeStub.args[0][2].InvocationType).to.be.equal('OtherType');
expect(invokeStub.args[0][2].LogType).to.be.equal('None');
expect(typeof invokeStub.args[0][2].Payload).to.not.be.equal('undefined');
awsInvoke.sdk.request.restore();
});
});
});
describe('#log()', () => {
it('should log payload', () => {
const invocationReplyMock = {
Payload: `
{
"testProp": "testValue"
}
`,
LogResult: 'test',
};
return awsInvoke.log(invocationReplyMock);
});
});
});