aws-lambda-helper
Advanced tools
Comparing version 2.16.1 to 2.17.0
@@ -315,5 +315,10 @@ 'use strict'; | ||
function saveRecord (params, callback) { | ||
var countdown = params.items.length; | ||
// console.log(params.items); | ||
params.items.forEach(function (item) { | ||
const filtered = params.items.filter((item) => { | ||
return item.type && item.type.match(/^tile|package$/); | ||
}); | ||
var countdown = filtered.length; | ||
if (countdown === 0) { | ||
return callback(); | ||
} | ||
filtered.forEach(function (item) { | ||
var env = AwsHelper.env; // remember to Initialise this! | ||
@@ -320,0 +325,0 @@ var s3params = { |
{ | ||
"name": "aws-lambda-helper", | ||
"version": "2.16.1", | ||
"version": "2.17.0", | ||
"description": "Collection of helper methods for lambda", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
// this test needs to run *FIRST* for some reason ... any help figuring out why much appreciated. | ||
require('env2')('.env'); | ||
var assert = require('assert'); | ||
var AwsHelper = require('./../../lib/index'); | ||
const assert = require('assert'); | ||
const simple = require('simple-mock'); | ||
const AWS = require('aws-sdk'); | ||
const AwsHelper = require('./../../lib/index'); | ||
@@ -13,4 +15,11 @@ describe('pushResultToClient', function () { | ||
}); | ||
beforeEach(() => { | ||
simple.mock(AWS.S3.prototype, 'upload').callbackWith(); | ||
simple.mock(AWS.SNS.prototype, 'publish').callbackWith(); | ||
}); | ||
afterEach(() => { | ||
simple.restore(); | ||
}); | ||
it('Send result to Client *AND* Save to S3', function (done) { | ||
it('publishes results to SNS', (done) => { | ||
var params = { | ||
@@ -20,11 +29,73 @@ id: 'dummyConnectionId', // the session id from WebSocket Server | ||
userId: 'TESTUSERID', | ||
items: [{ | ||
id: 123, hello: 'world', title: 'amazing holiday', | ||
url: 'userId/connectionId/bucketId/123' | ||
}] | ||
items: [ | ||
{ | ||
type: 'package', id: 123, hello: 'world', title: 'amazing holiday', | ||
url: 'userId/connectionId/bucketId/123' | ||
}, | ||
{ | ||
type: 'package', id: 456, hello: 'world', title: 'not amazing holiday', | ||
url: 'userId/connectionId/bucketId/456' | ||
} | ||
] | ||
}; | ||
const expected = JSON.stringify({ | ||
default: JSON.stringify(params) | ||
}); | ||
AwsHelper.pushResultToClient(params, function (err) { | ||
assert(!err); | ||
assert.equal(AWS.SNS.prototype.publish.callCount, 1); | ||
assert.equal(AWS.SNS.prototype.publish.calls[0].args[0].Message, expected); | ||
done(); | ||
}); | ||
}); | ||
it('saves items to S3', function (done) { | ||
var params = { | ||
id: 'dummyConnectionId', // the session id from WebSocket Server | ||
searchId: 'ABC', | ||
userId: 'TESTUSERID', | ||
items: [ | ||
{ | ||
type: 'package', id: 123, hello: 'world', title: 'amazing holiday', | ||
url: 'userId/connectionId/bucketId/123' | ||
}, | ||
{ | ||
type: 'package', id: 456, hello: 'world', title: 'not amazing holiday', | ||
url: 'userId/connectionId/bucketId/456' | ||
} | ||
] | ||
}; | ||
AwsHelper.pushResultToClient(params, function (err) { | ||
assert(!err); | ||
assert.equal(AWS.S3.prototype.upload.callCount, 2); | ||
assert.equal(AWS.S3.prototype.upload.calls[0].args[0].Body, JSON.stringify(params.items[0])); | ||
assert.equal(AWS.S3.prototype.upload.calls[1].args[0].Body, JSON.stringify(params.items[1])); | ||
done(); | ||
}); | ||
}); | ||
it('does not save items with types other than "packge" or "tile" to S3', function (done) { | ||
var params = { | ||
id: 'dummyConnectionId', // the session id from WebSocket Server | ||
searchId: 'ABC', | ||
userId: 'TESTUSERID', | ||
items: [ | ||
{ | ||
type: 'package', id: 123, hello: 'world', title: 'amazing holiday', | ||
url: 'userId/connectionId/bucketId/123' | ||
}, | ||
{ | ||
type: 'tile', id: 456, hello: 'world', title: 'amazing holiday', | ||
url: 'userId/connectionId/bucketId/123' | ||
}, | ||
{ | ||
type: 'filter', id: 789, hello: 'world', title: 'not amazing holiday' | ||
} | ||
] | ||
}; | ||
AwsHelper.pushResultToClient(params, function (err, res) { | ||
// assert(!err); | ||
console.log(err); | ||
assert.equal(res.Key, 'ci/' + params.items[0].url + '.json'); | ||
assert(!err); | ||
assert.equal(AWS.S3.prototype.upload.callCount, 2); | ||
assert.equal(AWS.S3.prototype.upload.calls[0].args[0].Body, JSON.stringify(params.items[0])); | ||
assert.equal(AWS.S3.prototype.upload.calls[1].args[0].Body, JSON.stringify(params.items[1])); | ||
done(); | ||
@@ -34,3 +105,21 @@ }); | ||
it('Send result to Client *AND* Save to S3 (no SNS Topic)', function (done) { | ||
it('skips saving to s3 if no items have url properties', function (done) { | ||
var params = { | ||
id: 'dummyConnectionId', // the session id from WebSocket Server | ||
searchId: 'ABC', | ||
userId: 'TESTUSERID', | ||
items: [ | ||
{ | ||
id: 456, hello: 'world', title: 'not amazing holiday' | ||
} | ||
] | ||
}; | ||
AwsHelper.pushResultToClient(params, function (err, res) { | ||
assert(!err); | ||
assert.equal(AWS.S3.prototype.upload.callCount, 0); | ||
done(); | ||
}); | ||
}); | ||
it('throws an error if SNS topic is not defined', function (done) { | ||
delete process.env.SEARCH_RESULT_TOPIC; | ||
@@ -37,0 +126,0 @@ var params = { |
@@ -13,4 +13,4 @@ require('env2')('.env'); | ||
items: [ | ||
{id: 0, title: 'my amazing hotel', url: 'test/test/12345'}, | ||
{id: 1, title: 'my lovely resort', url: 'test/test/45678'} | ||
{type: 'package', id: 0, title: 'my amazing hotel', url: 'test/test/12345'}, | ||
{type: 'tile', id: 1, title: 'my lovely resort', url: 'test/test/45678'} | ||
] | ||
@@ -17,0 +17,0 @@ }; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
191550
1606