require('blanket')({ pattern: function (filename) { return !/node_modules/.test(filename); } }); var proxyquire = require('proxyquire'); var chai = require('chai'); var sinonChai = require("sinon-chai"); var extend = require('lodash').extend; var sinon = require('sinon'); chai.use(sinonChai); describe('AwsHandler', function () { var expect = chai.expect; var sizesConfigs = [ { width: 800, destinationPath: 'large' }, { width: 500, destinationPath: 'medium' }, { width: 200, destinationPath: 'small' }, { width: 45, destinationPath: 'thumbnail'} ]; var baseEvent = { "Records": [ { "s3": { "bucket": { "name": "testbucket" }, "object": { "key": null } } } ] }; describe('reject to process non recognised image file extensions', function () { var event, gmSpy, getObjectSpy, putObjectSpy, contextDoneSpy, testedModule; before(function () { event = extend({}, baseEvent); event.Records[0].s3.object.key = "no-supported.gif"; gmSpy = sinon.spy(); getObjectSpy = sinon.spy(); putObjectSpy = sinon.spy(); contextDoneSpy = sinon.spy(); testedModule = getTestedModule(gmSpy, getObjectSpy, putObjectSpy); testedModule.AwsHandler(event, { done: contextDoneSpy }); }); it('never call s3 getObject', function () { expect(getObjectSpy).has.not.been.called; }); it('never call graphics magick', function () { expect(gmSpy).has.not.been.called; }); it('never call s3 putObject', function () { expect(putObjectSpy).has.not.been.called; }); it('call context done with error', function () { expect(contextDoneSpy).has.been.calledOnce.and.calledWith(new Error()); }); }); describe('process the image when the it has jpg extension', function () { var event, gmStubs, getObjectStub, putObjectStub, contextDoneSpy, testedModule, fakeResponse; before(function (done) { fakeResponse = { Body: 'image content' }; event = extend({}, baseEvent); event.Records[0].s3.object.key = "image.jpg"; gmStubs = getGmStubs(); getObjectStub = sinon.stub().callsArgWith(1, null, fakeResponse); putObjectStub = sinon.stub().callsArgWith(1, null); contextDoneSpy = sinon.spy(); testedModule = getTestedModule(gmStubs.gm, getObjectStub, putObjectStub); testedModule.AwsHandler(event, { done: function () { contextDoneSpy.apply(null, arguments); done(); }}); }); it('call s3 getObject', function () { expect(getObjectStub).has.been.calledOnce; expect(getObjectStub).has.been.calledWith({ Bucket: event.Records[0].s3.bucket.name, Key: event.Records[0].s3.object.key }); }); it('call graphics magick', function () { expect(gmStubs.gm).has.been.callCount(sizesConfigs.length); expect(gmStubs.resize).has.been.callCount(sizesConfigs.length); expect(gmStubs.toBuffer).has.been.callCount(sizesConfigs.length); expect(gmStubs.gm).always.has.been.calledWith(fakeResponse.Body, event.Records[0].s3.object.key); sizesConfigs.forEach(function(s) { expect(gmStubs.resize).has.been.calledWith(s.width); expect(gmStubs.toBuffer).has.been.calledWith('jpg'); }); }); it('call s3 putObject', function () { expect(putObjectStub).has.been.callCount(sizesConfigs.length); sizesConfigs.forEach(function(s) { expect(putObjectStub).has.been.calledWith({ Bucket: event.Records[0].s3.bucket.name, Key: 'dst/' + s.destinationPath + '/' + event.Records[0].s3.object.key, Body: 'data', ContentType: 'image/jpg' }); }); }); it('call context done with no error', function () { expect(contextDoneSpy).has.been.calledOnce.and.calledWith(null); }); }); describe('process the image when the it has jpge extension', function () { var event, gmStubs, getObjectStub, putObjectStub, contextDoneSpy, testedModule, fakeResponse; before(function (done) { fakeResponse = { Body: 'image content' }; event = extend({}, baseEvent); event.Records[0].s3.object.key = "image.jpge"; gmStubs = getGmStubs(); getObjectStub = sinon.stub().callsArgWith(1, null, fakeResponse); putObjectStub = sinon.stub().callsArgWith(1, null); contextDoneSpy = sinon.spy(); testedModule = getTestedModule(gmStubs.gm, getObjectStub, putObjectStub); testedModule.AwsHandler(event, { done: function () { contextDoneSpy.apply(null, arguments); done(); }}); }); it('call s3 getObject', function () { expect(getObjectStub).has.been.calledOnce; expect(getObjectStub).has.been.calledWith({ Bucket: event.Records[0].s3.bucket.name, Key: event.Records[0].s3.object.key }); }); it('call graphics magick', function () { expect(gmStubs.gm).has.been.callCount(sizesConfigs.length); expect(gmStubs.resize).has.been.callCount(sizesConfigs.length); expect(gmStubs.toBuffer).has.been.callCount(sizesConfigs.length); expect(gmStubs.gm).always.has.been.calledWith(fakeResponse.Body, event.Records[0].s3.object.key); sizesConfigs.forEach(function(s) { expect(gmStubs.resize).has.been.calledWith(s.width); expect(gmStubs.toBuffer).has.been.calledWith('jpg'); }); }); it('call s3 putObject', function () { expect(putObjectStub).has.been.callCount(sizesConfigs.length); sizesConfigs.forEach(function(s) { expect(putObjectStub).has.been.calledWith({ Bucket: event.Records[0].s3.bucket.name, Key: 'dst/' + s.destinationPath + '/' + event.Records[0].s3.object.key, Body: 'data', ContentType: 'image/jpg' }); }); }); it('call context done with no error', function () { expect(contextDoneSpy).has.been.calledOnce.and.calledWith(null); }); }); describe('process the image when the it has png extension', function () { var event, gmStubs, getObjectStub, putObjectStub, contextDoneSpy, testedModule, fakeResponse; before(function (done) { fakeResponse = { Body: 'image content' }; event = extend({}, baseEvent); event.Records[0].s3.object.key = "image.png"; gmStubs = getGmStubs(); getObjectStub = sinon.stub().callsArgWith(1, null, fakeResponse); putObjectStub = sinon.stub().callsArgWith(1, null); contextDoneSpy = sinon.spy(); testedModule = getTestedModule(gmStubs.gm, getObjectStub, putObjectStub); testedModule.AwsHandler(event, { done: function () { contextDoneSpy.apply(null, arguments); done(); }}); }); it('call s3 getObject', function () { expect(getObjectStub).has.been.calledOnce; expect(getObjectStub).has.been.calledWith({ Bucket: event.Records[0].s3.bucket.name, Key: event.Records[0].s3.object.key }); }); it('call graphics magick', function () { expect(gmStubs.gm).has.been.callCount(sizesConfigs.length); expect(gmStubs.resize).has.been.callCount(sizesConfigs.length); expect(gmStubs.toBuffer).has.been.callCount(sizesConfigs.length); expect(gmStubs.gm).always.has.been.calledWith(fakeResponse.Body, event.Records[0].s3.object.key); sizesConfigs.forEach(function(s) { expect(gmStubs.resize).has.been.calledWith(s.width); expect(gmStubs.toBuffer).has.been.calledWith('png'); }); }); it('call s3 putObject', function () { expect(putObjectStub).has.been.callCount(sizesConfigs.length); sizesConfigs.forEach(function(s) { expect(putObjectStub).has.been.calledWith({ Bucket: event.Records[0].s3.bucket.name, Key: 'dst/' + s.destinationPath + '/' + event.Records[0].s3.object.key, Body: 'data', ContentType: 'image/png' }); }); }); it('call context done with no error', function () { expect(contextDoneSpy).has.been.calledOnce.and.calledWith(null); }); }); describe('process the image but image magick fails', function () { var event, gmStubs, getObjectStub, putObjectSpy, contextDoneSpy, testedModule, fakeResponse; before(function (done) { fakeResponse = { Body: 'image content' }; event = extend({}, baseEvent); event.Records[0].s3.object.key = "image.png"; var toBufferStub = sinon.stub().callsArgWith(1, new Error('Image resize failed')); gmStubs = getGmStubs(toBufferStub); getObjectStub = sinon.stub().callsArgWith(1, null, fakeResponse); putObjectSpy = sinon.spy(); contextDoneSpy = sinon.spy(); testedModule = getTestedModule(gmStubs.gm, getObjectStub, putObjectSpy); testedModule.AwsHandler(event, { done: function () { contextDoneSpy.apply(null, arguments); done(); }}); }); it('call s3 getObject', function () { expect(getObjectStub).has.been.calledOnce; expect(getObjectStub).has.been.calledWith({ Bucket: event.Records[0].s3.bucket.name, Key: event.Records[0].s3.object.key }); }); it('call graphics magick', function () { expect(gmStubs.gm).has.been.callCount(sizesConfigs.length); expect(gmStubs.resize).has.been.callCount(sizesConfigs.length); expect(gmStubs.toBuffer).has.been.callCount(sizesConfigs.length); expect(gmStubs.gm).always.has.been.calledWith(fakeResponse.Body, event.Records[0].s3.object.key); sizesConfigs.forEach(function(s) { expect(gmStubs.resize).has.been.calledWith(s.width); expect(gmStubs.toBuffer).has.been.calledWith('png'); }); }); it('never call s3 putObject', function () { expect(putObjectSpy).has.been.not.called; }); it('call context done with no error', function () { expect(contextDoneSpy).has.been.calledOnce.and.calledWith(new Error('Image resize failed')); }); }); }); function getGmStubs(toBuffer) { var toBuffer = toBuffer || sinon.stub().callsArgWith(1, null, 'data') var resize = sinon.stub().returns({ toBuffer: toBuffer }); var gm = sinon.stub().returns({ resize: resize }); return { gm: gm, resize: resize, toBuffer: toBuffer }; } function getTestedModule(gm, getObject, putObject) { return proxyquire('../aws-handler.js', { 'gm': { subClass: function() { return gm; } }, 'aws-sdk': { "S3": function () { return { getObject: getObject, putObject: putObject }; } } }); }