|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const expect = require('chai').expect; |
| 4 | +const BbPromise = require('bluebird'); |
| 5 | +const sinon = require('sinon'); |
| 6 | +const Serverless = require('serverless/lib/Serverless'); |
| 7 | +const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider'); |
| 8 | +const ServerlessStepFunctions = require('./index'); |
| 9 | + |
| 10 | +describe('dataProsessing', () => { |
| 11 | + let serverless; |
| 12 | + let serverlessStepFunctions; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + serverless = new Serverless(); |
| 16 | + serverless.servicePath = true; |
| 17 | + serverless.service.service = 'step-functions'; |
| 18 | + serverless.service.functions = { |
| 19 | + first: { |
| 20 | + handler: true, |
| 21 | + name: 'first', |
| 22 | + }, |
| 23 | + }; |
| 24 | + |
| 25 | + const options = { |
| 26 | + stage: 'dev', |
| 27 | + region: 'us-east-1', |
| 28 | + function: 'first', |
| 29 | + functionObj: { |
| 30 | + name: 'first', |
| 31 | + }, |
| 32 | + state: 'hellofunc', |
| 33 | + data: 'inputData', |
| 34 | + }; |
| 35 | + |
| 36 | + serverless.init(); |
| 37 | + serverless.setProvider('aws', new AwsProvider(serverless)); |
| 38 | + serverlessStepFunctions = new ServerlessStepFunctions(serverless, options); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('#yamlParse()', () => { |
| 42 | + let yamlParserStub; |
| 43 | + beforeEach(() => { |
| 44 | + yamlParserStub = sinon.stub(serverlessStepFunctions.serverless.yamlParser, 'parse') |
| 45 | + .returns(BbPromise.resolve({ stepFunctions: { stateMachine: 'stepFunctions' } })); |
| 46 | + serverlessStepFunctions.serverless.config.servicePath = 'servicePath'; |
| 47 | + }); |
| 48 | + |
| 49 | + it('should yamlParse with correct params' |
| 50 | + , () => serverlessStepFunctions.yamlParse() |
| 51 | + .then(() => { |
| 52 | + expect(yamlParserStub.calledOnce).to.be.equal(true); |
| 53 | + expect(serverless.service.stepFunctions).to.be.equal('stepFunctions'); |
| 54 | + serverlessStepFunctions.serverless.yamlParser.parse.restore(); |
| 55 | + }) |
| 56 | + ); |
| 57 | + |
| 58 | + it('should return resolve when servicePath does not exists', () => { |
| 59 | + serverlessStepFunctions.serverless.config.servicePath = null; |
| 60 | + serverlessStepFunctions.yamlParse() |
| 61 | + .then(() => { |
| 62 | + expect(yamlParserStub.callCount).to.be.equal(0); |
| 63 | + serverlessStepFunctions.serverless.yamlParser.parse.restore(); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should return resolve when variables exists in the yaml', () => { |
| 68 | + serverlessStepFunctions.serverless.yamlParser.parse.restore(); |
| 69 | + yamlParserStub = sinon.stub(serverlessStepFunctions.serverless.yamlParser, 'parse') |
| 70 | + .returns(BbPromise.resolve({ stepFunctions: { stateMachine: '${self:defaults.region}' } })); |
| 71 | + serverlessStepFunctions.yamlParse() |
| 72 | + .then(() => { |
| 73 | + expect(yamlParserStub.calledOnce).to.be.equal(true); |
| 74 | + expect(serverless.service.stepFunctions).to.be.equal('us-east-1'); |
| 75 | + }); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('#compile()', () => { |
| 80 | + it('should throw error when stepFunction state does not exists', () => { |
| 81 | + expect(() => serverlessStepFunctions.compile()).to.throw(Error); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should throw error when stateMachine name does not exists', () => { |
| 85 | + serverlessStepFunctions.stepFunctions = {}; |
| 86 | + expect(() => serverlessStepFunctions.compile()).to.throw(Error); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should comple with correct params', () => { |
| 90 | + serverless.service.stepFunctions = { |
| 91 | + hellofunc: { |
| 92 | + States: { |
| 93 | + HelloWorld: { |
| 94 | + Resource: 'first', |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + }; |
| 99 | + serverlessStepFunctions.functionArns.first = 'lambdaArn'; |
| 100 | + serverlessStepFunctions.compile().then(() => { |
| 101 | + expect(serverlessStepFunctions.serverless.service.stepFunctions.hellofunc) |
| 102 | + .to.be.equal('{"States":{"HelloWorld":{"Resource":"lambdaArn"}}}'); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should comple with correct params when nested Resource', () => { |
| 107 | + serverlessStepFunctions.serverless.service.stepFunctions = { |
| 108 | + hellofunc: { |
| 109 | + States: { |
| 110 | + HelloWorld: { |
| 111 | + Resource: 'first', |
| 112 | + HelloWorld: { |
| 113 | + Resource: 'first', |
| 114 | + HelloWorld: { |
| 115 | + Resource: 'first', |
| 116 | + }, |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }; |
| 122 | + |
| 123 | + let a = '{"States":{"HelloWorld":{"Resource":"lambdaArn","HelloWorld"'; |
| 124 | + a += ':{"Resource":"lambdaArn","HelloWorld":{"Resource":"lambdaArn"}}}}}'; |
| 125 | + serverlessStepFunctions.functionArns.first = 'lambdaArn'; |
| 126 | + serverlessStepFunctions.compile().then(() => { |
| 127 | + expect(serverlessStepFunctions.serverless.service.stepFunctions.hellofunc).to.be.equal(a); |
| 128 | + }); |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + describe('#parseInputdate()', () => { |
| 133 | + beforeEach(() => { |
| 134 | + serverlessStepFunctions.serverless.config.servicePath = 'servicePath'; |
| 135 | + sinon.stub(serverlessStepFunctions.serverless.utils, 'fileExistsSync').returns(true); |
| 136 | + sinon.stub(serverlessStepFunctions.serverless.utils, 'readFileSync') |
| 137 | + .returns({ foo: 'var' }); |
| 138 | + serverlessStepFunctions.options.data = null; |
| 139 | + serverlessStepFunctions.options.path = 'data.json'; |
| 140 | + }); |
| 141 | + |
| 142 | + it('should throw error if file does not exists', () => { |
| 143 | + serverlessStepFunctions.serverless.utils.fileExistsSync.restore(); |
| 144 | + sinon.stub(serverlessStepFunctions.serverless.utils, 'fileExistsSync').returns(false); |
| 145 | + expect(() => serverlessStepFunctions.parseInputdate()).to.throw(Error); |
| 146 | + serverlessStepFunctions.serverless.utils.readFileSync.restore(); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should parse file if path param is provided' |
| 150 | + , () => serverlessStepFunctions.parseInputdate().then(() => { |
| 151 | + expect(serverlessStepFunctions.options.data).to.deep.equal('{"foo":"var"}'); |
| 152 | + serverlessStepFunctions.serverless.utils.fileExistsSync.restore(); |
| 153 | + serverlessStepFunctions.serverless.utils.readFileSync.restore(); |
| 154 | + }) |
| 155 | + ); |
| 156 | + |
| 157 | + it('should parse file if path param is provided when absolute path', () => { |
| 158 | + serverlessStepFunctions.options.path = '/data.json'; |
| 159 | + serverlessStepFunctions.parseInputdate().then(() => { |
| 160 | + expect(serverlessStepFunctions.options.data).to.deep.equal('{"foo":"var"}'); |
| 161 | + serverlessStepFunctions.serverless.utils.fileExistsSync.restore(); |
| 162 | + serverlessStepFunctions.serverless.utils.readFileSync.restore(); |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + it('should return resolve if path param is not provided', () => { |
| 167 | + serverlessStepFunctions.options.path = null; |
| 168 | + return serverlessStepFunctions.parseInputdate().then(() => { |
| 169 | + expect(serverlessStepFunctions.options.data).to.deep.equal(null); |
| 170 | + serverlessStepFunctions.serverless.utils.fileExistsSync.restore(); |
| 171 | + serverlessStepFunctions.serverless.utils.readFileSync.restore(); |
| 172 | + }); |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + describe('#getFunctionArns()', () => { |
| 177 | + let getCallerIdentityStub; |
| 178 | + beforeEach(() => { |
| 179 | + getCallerIdentityStub = sinon.stub(serverlessStepFunctions.provider, 'request') |
| 180 | + .returns(BbPromise.resolve({ Account: 1234 })); |
| 181 | + }); |
| 182 | + |
| 183 | + it('should getFunctionArns with correct params', () => serverlessStepFunctions.getFunctionArns() |
| 184 | + .then(() => { |
| 185 | + expect(getCallerIdentityStub.calledOnce).to.be.equal(true); |
| 186 | + expect(getCallerIdentityStub.calledWithExactly( |
| 187 | + 'STS', |
| 188 | + 'getCallerIdentity', |
| 189 | + {}, |
| 190 | + serverlessStepFunctions.options.stage, |
| 191 | + serverlessStepFunctions.options.region |
| 192 | + )).to.be.equal(true); |
| 193 | + expect(serverlessStepFunctions.functionArns.first).to.be |
| 194 | + .equal('arn:aws:lambda:us-east-1:1234:function:first'); |
| 195 | + serverlessStepFunctions.provider.request.restore(); |
| 196 | + }) |
| 197 | + ); |
| 198 | + }); |
| 199 | + |
| 200 | + describe('#compileAll()', () => { |
| 201 | + it('should throw error when stepFunction state does not exists', () => { |
| 202 | + expect(() => serverlessStepFunctions.compileAll()).to.throw(Error); |
| 203 | + }); |
| 204 | + |
| 205 | + it('should comple with correct params when nested Resource', () => { |
| 206 | + serverlessStepFunctions.serverless.service.stepFunctions = { |
| 207 | + hellofunc: { |
| 208 | + States: { |
| 209 | + HelloWorld: { |
| 210 | + Resource: 'first', |
| 211 | + HelloWorld: { |
| 212 | + Resource: 'first', |
| 213 | + HelloWorld: { |
| 214 | + Resource: 'first', |
| 215 | + }, |
| 216 | + }, |
| 217 | + }, |
| 218 | + }, |
| 219 | + }, |
| 220 | + }; |
| 221 | + |
| 222 | + let a = '{"States":{"HelloWorld":{"Resource":"lambdaArn","HelloWorld"'; |
| 223 | + a += ':{"Resource":"lambdaArn","HelloWorld":{"Resource":"lambdaArn"}}}}}'; |
| 224 | + serverlessStepFunctions.functionArns.first = 'lambdaArn'; |
| 225 | + serverlessStepFunctions.compileAll().then(() => { |
| 226 | + expect(serverlessStepFunctions.serverless.service.stepFunctions.hellofunc).to.be.equal(a); |
| 227 | + }); |
| 228 | + }); |
| 229 | + }); |
| 230 | +}); |
0 commit comments