Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
describe('get and set', function () {
it('should be able to set and get a property', function () {
var value;
data.set('testProperty', 'testValue');
value = data.get('testProperty');
expect(value).to.equal('testValue');
});
it('should replace value if property exists', function () {
var value;
// set initial value
data.set('testProperty', 'testValue');
// replace value
data.set('testProperty', 'newValue');
value = data.get('testProperty');
expect(value).to.equal('newValue');
});
"key": "Screen+Shot+2016-01-19+at+7.24.37+PM.png",
"size": 519,
"eTag": "aa1e5c8a6a07217c25f55aa8e96ea37a",
"sequencer": "00560DC1B62F962FCD"
}
}
}
]
}, {}, (error, result) => {
"use strict";
assert(error === null, "there should be no errors");
assert(result.statusCode === 200, "should return the file id");
});
});
it('should fail to process a s3 event missing the object key', async () => {
nock('https://api.scanii.com')
.post('/v2.1/files/fetch')
.reply(202, Buffer.from("{\"id\":\"12356789\"}"), {"Location": "https://api.scanii.com/v2.1/files/1234"});
await handler({
"Records": [
{
"eventVersion": "2.0",
"eventSource": "aws:s3",
"awsRegion": "us-west-2",
"eventTime": "2015-10-01T23:28:54.280Z",
"eventName": "ObjectCreated:Put",
"userIdentity": {
"principalId": "AWS:principal"
},
AWS.mock('S3', 'putObjectTagging', async () => {
tagCounter++;
});
AWS.mock('S3', 'getObjectTagging', async (params, callback) => {
callback(null, {
TagSet: []
});
});
});
afterEach(function () {
AWS.restore();
});
it("if all actions are disabled, no action should be taken", async () => {
CONFIG.ACTION_DELETE_OBJECT = false;
CONFIG.ACTION_TAG_OBJECT = false;
await actions.onFindings("bucket1", "key1", result);
assert.strictEqual(0, deleteCounter);
assert.strictEqual(0, tagCounter);
});
it("if a single action is enabled, a single action should be taken", async () => {
CONFIG.ACTION_DELETE_OBJECT = true;
CONFIG.ACTION_TAG_OBJECT = false;
await actions.onFindings("bucket1", "key1", result);
assert.strictEqual(1, deleteCounter);
assert.strictEqual(0, tagCounter);
});
it("if a single action is enabled, a single action should be taken - 2", async () => {
callback();
putObjectTagCallCount++;
return true;
});
AWS.mock('S3', 'getObjectTagging', async (params, callback) => {
callback(null, {
TagSet: []
});
});
await actions.tagObject(result.metadata.bucket, result.metadata.key, result);
assert(putObjectTagCallCount === 1);
});
it('should truncate tag values', async () => {
const result = {
"id": "2e4612793298b1d691202e75dc125f6e",
"checksum": "30d3007d8fa7e76f2741805fbaf1c8bba9a00051",
"content_length": "1251174",
"findings": ["a".repeat(300)],
"creation_date": "2016-01-24T15:05:53.260Z",
"content_type": "image/jpeg",
"metadata": {
"signature": "abc",
"bucket": "test-bucket",
"key": "test-key"
}
};
AWS.mock('S3', 'putObjectTagging', async (params, callback) => {
assert.ok(params.Tagging.TagSet[0].Key === "ScaniiFindings");
"size": 519,
"eTag": "aa1e5c8a6a07217c25f55aa8e96ea37a",
"sequencer": "00560DC1B62F962FCD"
}
}
}
]
}, {}, (error, result) => {
"use strict";
assert(error === null, "there should be no errors");
assert(result.statusCode === 500, "should return the file id");
assert(result.body.includes("key not present"));
});
});
it('should fail to process a s3 event missing the object bucket', async () => {
nock('https://api.scanii.com')
.post('/v2.1/files/fetch')
.reply(202, Buffer.from("{\"id\":\"12356789\"}"), {"Location": "https://api.scanii.com/v2.1/files/1234"});
await handler({
"Records": [
{
"eventVersion": "2.0",
"eventSource": "aws:s3",
"awsRegion": "us-west-2",
"eventTime": "2015-10-01T23:28:54.280Z",
"eventName": "ObjectCreated:Put",
"userIdentity": {
"principalId": "AWS:principal"
assert(result.statusCode === 200);
done();
});
});
it('should handle a bogus callback', done => {
handler({"hello": "world"},
{}, (error) => {
"use strict";
assert(error !== null, "it should throw an error");
assert(error.message.includes("no id provided"));
done();
});
});
it('should require bucket/key in callback metadata', done => {
handler({
"id": "2e4612793298b1d691202e75dc125f6e",
"checksum": "30d3007d8fa7e76f2741805fbaf1c8bba9a00051",
"content_length": "1251174",
"findings": [],
"creation_date": "2016-01-24T15:05:53.260Z",
"content_type": "image/jpeg",
"metadata": {
"signature": generateSignature("test-bucket", "test-key"),
}
},
{}, (error) => {
"use strict";
assert(error !== null, "it should throw an error");
assert(error.message.includes("no bucket supplied in metadata"));
done();
"metadata": {
"signature": "abc",
"bucket": "test-bucket",
"key": "test-key"
}
};
try {
const signature = utils.generateSignature(result.metadata.bucket, result.metadata.key);
assert.ok(result.metadata.signature === signature);
} catch (error) {
assert(error.code === 'ERR_ASSERTION')
}
});
it('should format tag value #1', async () => {
const value = utils.formatTagValue(['content.malicious.porcupine-malware-36555-unofficial', 'content.malicious.trojan-agent-bwqq'
]);
assert.deepStrictEqual(value, "content.malicious.porcupine-malware-36555-unofficial content.malicious.trojan-agent-bwqq");
});
});
"findings": ['finding1', 'finding2'],
"creation_date": "2016-01-24T15:05:53.260Z",
"content_type": "image/jpeg",
"metadata": {
"signature": utils.generateSignature("test-bucket", "wrong-key"),
"bucket": "test-bucket",
"key": "test-key"
}
}), {}, (error, result) => {
"use strict";
assert(error === null, "there should be no errors");
assert(result.statusCode === 500);
});
});
it('should enforce signatures in callbacks', async () => {
await handler(hydrateEvent({
"id": "2e4612793298b1d691202e75dc125f6e",
"checksum": "30d3007d8fa7e76f2741805fbaf1c8bba9a00051",
"content_length": "1251174",
"findings": ['finding1', 'finding2'],
"creation_date": "2016-01-24T15:05:53.260Z",
"content_type": "image/jpeg",
"metadata": {
"signature": "1234",
"bucket": "test-bucket",
"key": "test-key"
}
}), {}, (error, result) => {
"use strict";
"use strict";
assert(error === null, "there should be no errors");
"content_length": "1251174",
"findings": ['finding1', 'finding2'],
"creation_date": "2016-01-24T15:05:53.260Z",
"content_type": "image/jpeg",
"metadata": {
"signature": utils.generateSignature("test-bucket", "test-key"),
"bucket": "test-bucket",
"key": "test-key"
}
}), {}, (error, result) => {
"use strict";
assert(error === null, "there should be no errors");
assert(result.statusCode === 200);
});
});
it('should ensure callback signatures match - negative', async () => {
await handler(hydrateEvent({
"id": "2e4612793298b1d691202e75dc125f6e",
"checksum": "30d3007d8fa7e76f2741805fbaf1c8bba9a00051",
"content_length": "1251174",
"findings": ['finding1', 'finding2'],
"creation_date": "2016-01-24T15:05:53.260Z",
"content_type": "image/jpeg",
"metadata": {
"signature": utils.generateSignature("test-bucket", "wrong-key"),
"bucket": "test-bucket",
"key": "test-key"
}
}), {}, (error, result) => {
"use strict";
assert(error === null, "there should be no errors");
describe('Util tests', () => {
beforeEach(() => {
});
it('should throw error if config secret is not set', async () => {
CONFIG.SECRET = false;
const result = {
"id": "2e4612793298b1d691202e75dc125f6e",
"checksum": "30d3007d8fa7e76f2741805fbaf1c8bba9a00051",
"content_length": "1251174",
"findings": [],
"creation_date": "2016-01-24T15:05:53.260Z",
"content_type": "image/jpeg",
"metadata": {
"signature": "abc",
"bucket": "test-bucket",
"key": "test-key"
}
};
try {
const signature = utils.generateSignature(result.metadata.bucket, result.metadata.key);