Skip to content
This repository has been archived by the owner on Jan 4, 2024. It is now read-only.

Commit

Permalink
use sinon as mockframework
Browse files Browse the repository at this point in the history
  • Loading branch information
ste-xx committed Jun 11, 2018
1 parent 97c3455 commit ae6e6b3
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 147 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
22 changes: 0 additions & 22 deletions test/s3ApiMock.js

This file was deleted.

15 changes: 0 additions & 15 deletions test/snsApiMock.js

This file was deleted.

245 changes: 136 additions & 109 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
});
});
}

});

0 comments on commit ae6e6b3

Please sign in to comment.