From ae6e6b3f8bd189a11069a1aa49af78c2727e2e22 Mon Sep 17 00:00:00 2001 From: ste-xx Date: Mon, 11 Jun 2018 20:39:28 +0200 Subject: [PATCH] use sinon as mockframework --- package.json | 4 +- test/s3ApiMock.js | 22 ---- test/snsApiMock.js | 15 --- test/test.js | 245 +++++++++++++++++++++++++-------------------- 4 files changed, 139 insertions(+), 147 deletions(-) delete mode 100644 test/s3ApiMock.js delete mode 100644 test/snsApiMock.js diff --git a/package.json b/package.json index 6354648..0ede395 100644 --- a/package.json +++ b/package.json @@ -13,8 +13,10 @@ "license": "ISC", "devDependencies": { "chai": "^4.1.2", + "chai-as-promised": "^7.1.1", "dotenv": "^5.0.1", "mocha": "^5.2.0", - "proxyquire": "^2.0.1" + "proxyquire": "^2.0.1", + "sinon": "^6.0.0" } } diff --git a/test/s3ApiMock.js b/test/s3ApiMock.js deleted file mode 100644 index 7d27c60..0000000 --- a/test/s3ApiMock.js +++ /dev/null @@ -1,22 +0,0 @@ -const expect = require('chai').expect; - -let expectedParams = {}; -let givenResponseHeadObject = {}; - -module.exports = { - expectedHeadObjectParams: (_) => expectedParams = _, - givenResponseHeadObject: (_) => givenResponseHeadObject = _, - reset: () => { - expectedParams = {}; - givenResponseHeadObject = {}; - }, - headObject: async (params) => { - expect(params).to.deep.equal(expectedParams); - console.log(`expected head params: ${JSON.stringify(expectedParams)}`); - console.log(`given head reponse: ${JSON.stringify(givenResponseHeadObject)}`); - - return new Promise((resolve, rejected) => resolve(givenResponseHeadObject)); - - // } - } -}; \ No newline at end of file diff --git a/test/snsApiMock.js b/test/snsApiMock.js deleted file mode 100644 index 74ab8dc..0000000 --- a/test/snsApiMock.js +++ /dev/null @@ -1,15 +0,0 @@ -const expect = require('chai').expect; - -let expectedParams = {}; - -module.exports = { - expectedPublishParams: (_) => expectedParams = _, - reset: () => { - expectedParams = {}; - }, - publish: async (params) => { - expect(params).to.deep.equal(expectedParams); - console.log(`expected publish params: ${JSON.stringify(expectedParams)}`); - return new Promise((resolve, rejected) => resolve()); - } -}; \ No newline at end of file diff --git a/test/test.js b/test/test.js index 7c22f12..6b9bc25 100644 --- a/test/test.js +++ b/test/test.js @@ -5,141 +5,168 @@ require('dotenv').config({ const expect = require('chai').expect; const proxyquire = require('proxyquire').noCallThru(); +const fake = require('sinon').fake; const fs = require('fs'); const event = JSON.parse(fs.readFileSync('test/s3EventData.json', 'utf8')); describe('eventhandler', () => { - let s3ApiMock, snsApiMock; - beforeEach(() => { - s3ApiMock = require('./s3ApiMock'); - snsApiMock = require('./snsApiMock'); - s3ApiMock.reset(); - snsApiMock.reset(); - }); - it('collect head request without meta data', async () => { - s3ApiMock.expectedHeadObjectParams({ - Bucket: "s2t-base-s2tbucket-19xbw73dypb0s", - Key: "gcp/not-transcoded/sugr1km8s6/f423fbfb-6381-11e8-a23f-c7cbebde15f2.ogg" - }); - - snsApiMock.expectedPublishParams( - { - Message: "placeholder", - MessageAttributes: { - "api-key-id": { - DataType: "String", - StringValue: "sugr1km8s6" - }, - bucket: { - DataType: "String", - StringValue: "s2t-base-s2tbucket-19xbw73dypb0s" - }, - key: { - DataType: "String", - StringValue: "gcp/not-transcoded/sugr1km8s6/f423fbfb-6381-11e8-a23f-c7cbebde15f2.ogg" - }, - pid: { - DataType: "String", - StringValue: "f423fbfb-6381-11e8-a23f-c7cbebde15f2" - }, - "transcribe-provider": { - DataType: "String", - StringValue: "gcp" - } - }, - "TopicArn": "given:arn:from:env" - }); - - s3ApiMock.givenResponseHeadObject({ - AcceptRanges: "bytes", - ContentLength: 3191, - ContentType: "image/jpeg", - ETag: "\"6805f2cfc46c0f04559748bb039d69ae\"", - LastModified: '', - Metadata: {}, - }); + let s3HeadObjectFake = fake.resolves(withoutMetadata()); + let snsPublishFake = fake.resolves("success"); let underTest = proxyquire('../index.js', { - './s3Api': s3ApiMock, - './snsApi': snsApiMock + './s3Api': { + headObject: s3HeadObjectFake + }, + './snsApi': { + publish: snsPublishFake + } }); - //let underTest = require('../index.js'); - let snsMessage = await underTest.handler(event); + await underTest.handler(event); + + let [s3HeadParam] = s3HeadObjectFake.firstCall.args; + expectS3Bucket(s3HeadParam); + expectS3Key(s3HeadParam); + + let [snsPublishParam] = snsPublishFake.firstCall.args; + expect(snsPublishParam).to.have.all.keys('TopicArn', 'Message', 'MessageAttributes'); + expectSNSTopicArn(snsPublishParam); + expectSNSMessage(snsPublishParam); + + let messageAttributes = snsPublishParam.MessageAttributes; + expect(messageAttributes).to.have.all.keys('api-key-id', 'bucket', 'key','pid','transcribe-provider'); + expectStringMessageAttribute(messageAttributes['api-key-id'],'sugr1km8s6'); + expectStringMessageAttribute(messageAttributes.bucket,'s2t-base-s2tbucket-19xbw73dypb0s'); + expectStringMessageAttribute(messageAttributes.key,'gcp/not-transcoded/sugr1km8s6/f423fbfb-6381-11e8-a23f-c7cbebde15f2.ogg'); + expectStringMessageAttribute(messageAttributes.pid,'f423fbfb-6381-11e8-a23f-c7cbebde15f2'); + expectStringMessageAttribute(messageAttributes['transcribe-provider'],'gcp'); + + }); + + it('file without ending', async () => { + + }); + + it('file without api key id prefix', async () => { - expect(snsMessage).to.deep.equal({ - bucket: "s2t-base-s2tbucket-19xbw73dypb0s", - key: "gcp/not-transcoded/sugr1km8s6/f423fbfb-6381-11e8-a23f-c7cbebde15f2.ogg", - "transcribe-provider": "gcp", - pid: "f423fbfb-6381-11e8-a23f-c7cbebde15f2", - "api-key-id": "sugr1km8s6" - }); }); + it('fail collect', async () => { + + }); + + + it('fail send', async () => { + + }); it('delegate collected meta data', async () => { + let s3HeadObjectFake = fake.resolves(withMetadata()); + let snsPublishFake = fake.resolves("success"); - s3ApiMock.expectedHeadObjectParams({ - Bucket: "s2t-base-s2tbucket-19xbw73dypb0s", - Key: "gcp/not-transcoded/sugr1km8s6/f423fbfb-6381-11e8-a23f-c7cbebde15f2.ogg" + let underTest = proxyquire('../index.js', { + './s3Api': { + headObject: s3HeadObjectFake + }, + './snsApi': { + publish: snsPublishFake + } }); - snsApiMock.expectedPublishParams( - { - Message: "placeholder", - MessageAttributes: { - "api-key-id": { - DataType: "String", - StringValue: "sugr1km8s6" - }, - bucket: { - DataType: "String", - StringValue: "s2t-base-s2tbucket-19xbw73dypb0s" - }, - key: { - DataType: "String", - StringValue: "gcp/not-transcoded/sugr1km8s6/f423fbfb-6381-11e8-a23f-c7cbebde15f2.ogg" - }, - pid: { - DataType: "String", - StringValue: "f423fbfb-6381-11e8-a23f-c7cbebde15f2" - }, - "transcribe-provider": { - DataType: "String", - StringValue: "gcp" - } - }, - "TopicArn": "given:arn:from:env" - }); - - s3ApiMock.givenResponseHeadObject({ + await underTest.handler(event); + + let [s3HeadParam] = s3HeadObjectFake.firstCall.args; + expectS3Bucket(s3HeadParam); + expectS3Key(s3HeadParam); + + let [snsPublishParam] = snsPublishFake.firstCall.args; + expect(snsPublishParam).to.have.all.keys('TopicArn', 'Message', 'MessageAttributes'); + expectSNSTopicArn(snsPublishParam); + expectSNSMessage(snsPublishParam); + + let messageAttributes = snsPublishParam.MessageAttributes; + expect(messageAttributes).to.have.all.keys('api-key-id', 'bucket', 'key','pid','transcribe-provider'); + expectStringMessageAttribute(messageAttributes['api-key-id'],'another_api_key'); + expectStringMessageAttribute(messageAttributes.bucket,'s2t-base-s2tbucket-19xbw73dypb0s'); + expectStringMessageAttribute(messageAttributes.key,'gcp/not-transcoded/sugr1km8s6/f423fbfb-6381-11e8-a23f-c7cbebde15f2.ogg'); + expectStringMessageAttribute(messageAttributes.pid,'another_pid'); + expectStringMessageAttribute(messageAttributes['transcribe-provider'],'aws'); + }); + + + function withoutMetadata() { + return { AcceptRanges: "bytes", ContentLength: 3191, ContentType: "image/jpeg", ETag: "\"6805f2cfc46c0f04559748bb039d69ae\"", LastModified: '', + //todo remove metadata + Metadata: {} + } + } + + function withMetadata() { + //todo change order after remove metadata from withoutmetadata + return Object.assign({}, withoutMetadata(), { Metadata: { - "api-key-id": "sugr1km8s6", - "pid": "f423fbfb-6381-11e8-a23f-c7cbebde15f2", - "transcribe-provider": "gcp" - }, + "api-key-id": "another_api_key", + "pid": "another_pid", + "transcribe-provider": "aws" + } }); - - let underTest = proxyquire('../index.js', { - './s3Api': s3ApiMock, - './snsApi': snsApiMock + } + + + function expectS3Bucket(param, {expectedBucket = 's2t-base-s2tbucket-19xbw73dypb0s'} = {}) { + expect(param.Bucket).to.equal(expectedBucket); + } + + function expectS3Key(param, {expectedKey = 'gcp/not-transcoded/sugr1km8s6/f423fbfb-6381-11e8-a23f-c7cbebde15f2.ogg'} = {}) { + expect(param.Key).to.equal(expectedKey); + } + + function expectSNSTopicArn(param, {expectedArn = 'given:arn:from:env'} = {}) { + expect(param.TopicArn).to.equal(expectedArn); + } + + let defaultExpectedSNSMessageAttributes = { + "api-key-id": { + DataType: "String", + StringValue: "sugr1km8s6" + }, + bucket: { + DataType: "String", + StringValue: "s2t-base-s2tbucket-19xbw73dypb0s" + }, + key: { + DataType: "String", + StringValue: "gcp/not-transcoded/sugr1km8s6/f423fbfb-6381-11e8-a23f-c7cbebde15f2.ogg" + }, + pid: { + DataType: "String", + StringValue: "f423fbfb-6381-11e8-a23f-c7cbebde15f2" + }, + "transcribe-provider": { + DataType: "String", + StringValue: "gcp" + } + }; + + function expectSNSMessage(param, {expectedMessage = 'placeholder'} = {}) { + expect(param.Message).to.equal(expectedMessage); + } + + function expectStringMessageAttribute(messageAttribute, value) { + + expect(messageAttribute).to.include({ + DataType: 'String', + StringValue: value }); - let snsMessage = await underTest.handler(event); - expect(snsMessage).to.deep.equal({ - bucket: "s2t-base-s2tbucket-19xbw73dypb0s", - key: "gcp/not-transcoded/sugr1km8s6/f423fbfb-6381-11e8-a23f-c7cbebde15f2.ogg", - "transcribe-provider": "gcp", - pid: "f423fbfb-6381-11e8-a23f-c7cbebde15f2", - "api-key-id": "sugr1km8s6" - }); - }); + } + }); \ No newline at end of file