|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const assert = require('chai').assert |
| 4 | +const path = require('path') |
| 5 | +const aws = require('aws-sdk-mock') |
| 6 | +aws.setSDK(path.resolve('node_modules/aws-sdk')) |
| 7 | +const S3Events = require('../lib/s3_events') |
| 8 | + |
| 9 | +const params = { |
| 10 | + FunctionArn: 'arn:aws:lambda:us-west-2:XXX:function:node-lambda-test-function', |
| 11 | + Bucket: 'node-lambda-test-bucket', |
| 12 | + Events: ['s3:ObjectCreated:*'], |
| 13 | + Filter: null |
| 14 | +} |
| 15 | + |
| 16 | +const mockResponse = { |
| 17 | + addPermission: { |
| 18 | + Statement: JSON.stringify({ |
| 19 | + Sid: 'node-lambda-test-bucket', |
| 20 | + Resource: 'arn:aws:lambda:node-lambda-test-function', |
| 21 | + Effect: 'Allow', |
| 22 | + Principal: { Service: 's3.amazonaws.com' }, |
| 23 | + Action: [ 'lambda:InvokeFunction' ], |
| 24 | + Condition: { ArnLike: { 'AWS:SourceArn': 'arn:aws:s3:::node-lambda-test-bucket' } } |
| 25 | + }) |
| 26 | + }, |
| 27 | + |
| 28 | + putBucketNotificationConfiguration: {} |
| 29 | +} |
| 30 | + |
| 31 | +var s3Events = null |
| 32 | + |
| 33 | +/* global before, after, describe, it */ |
| 34 | +describe('lib/s3_events', () => { |
| 35 | + before(() => { |
| 36 | + aws.mock('Lambda', 'addPermission', (params, callback) => { |
| 37 | + callback(null, mockResponse.addPermission) |
| 38 | + }) |
| 39 | + aws.mock('S3', 'putBucketNotificationConfiguration', (params, callback) => { |
| 40 | + callback(null, mockResponse.putBucketNotificationConfiguration) |
| 41 | + }) |
| 42 | + |
| 43 | + s3Events = new S3Events(require('aws-sdk')) |
| 44 | + }) |
| 45 | + |
| 46 | + after(() => { |
| 47 | + aws.restore('Lambda') |
| 48 | + aws.restore('S3') |
| 49 | + }) |
| 50 | + |
| 51 | + describe('_functionName', () => { |
| 52 | + it('Extract name from FunctionArn', () => { |
| 53 | + assert.equal( |
| 54 | + s3Events._functionName(params), |
| 55 | + 'node-lambda-test-function' |
| 56 | + ) |
| 57 | + }) |
| 58 | + }) |
| 59 | + |
| 60 | + describe('_addPermissionParams', () => { |
| 61 | + it('Return parameters for lambda.addPermission()', () => { |
| 62 | + const expected = { |
| 63 | + Action: 'lambda:InvokeFunction', |
| 64 | + FunctionName: 'node-lambda-test-function', |
| 65 | + Principal: 's3.amazonaws.com', |
| 66 | + SourceArn: 'arn:aws:s3:::node-lambda-test-bucket', |
| 67 | + StatementId: 'node-lambda-test-bucket' |
| 68 | + } |
| 69 | + assert.deepEqual(s3Events._addPermissionParams(params), expected) |
| 70 | + }) |
| 71 | + }) |
| 72 | + |
| 73 | + describe('_putBucketNotificationConfigurationParams', () => { |
| 74 | + it('Return parameters for s3.putBucketNotificationConfiguration(). No Filter', () => { |
| 75 | + const expected = { |
| 76 | + Bucket: 'node-lambda-test-bucket', |
| 77 | + NotificationConfiguration: { |
| 78 | + LambdaFunctionConfigurations: [{ |
| 79 | + Events: ['s3:ObjectCreated:*'], |
| 80 | + LambdaFunctionArn: 'arn:aws:lambda:us-west-2:XXX:function:node-lambda-test-function' |
| 81 | + }] |
| 82 | + } |
| 83 | + } |
| 84 | + assert.deepEqual( |
| 85 | + s3Events._putBucketNotificationConfigurationParams(params), |
| 86 | + expected |
| 87 | + ) |
| 88 | + }) |
| 89 | + |
| 90 | + it('Return parameters for s3.putBucketNotificationConfiguration(). Use Filter', () => { |
| 91 | + const expected = { |
| 92 | + Bucket: 'node-lambda-test-bucket', |
| 93 | + NotificationConfiguration: { |
| 94 | + LambdaFunctionConfigurations: [{ |
| 95 | + Events: ['s3:ObjectCreated:*'], |
| 96 | + LambdaFunctionArn: 'arn:aws:lambda:us-west-2:XXX:function:node-lambda-test-function', |
| 97 | + Filter: { |
| 98 | + Key: { |
| 99 | + FilterRules: [{ |
| 100 | + Name: 'prefix', |
| 101 | + Value: 'test-prefix' |
| 102 | + }] |
| 103 | + } |
| 104 | + } |
| 105 | + }] |
| 106 | + } |
| 107 | + } |
| 108 | + const _params = Object.assign({}, params) |
| 109 | + _params.Filter = { |
| 110 | + Key: { |
| 111 | + FilterRules: [{ |
| 112 | + Name: 'prefix', |
| 113 | + Value: 'test-prefix' |
| 114 | + }] |
| 115 | + } |
| 116 | + } |
| 117 | + assert.deepEqual( |
| 118 | + s3Events._putBucketNotificationConfigurationParams(_params), |
| 119 | + expected |
| 120 | + ) |
| 121 | + }) |
| 122 | + }) |
| 123 | + |
| 124 | + describe('_addPermission', () => { |
| 125 | + it('using mock', () => { |
| 126 | + return s3Events._addPermission(params).then(data => { |
| 127 | + assert.deepEqual(data, mockResponse.addPermission) |
| 128 | + }) |
| 129 | + }) |
| 130 | + }) |
| 131 | + |
| 132 | + describe('_putBucketNotificationConfiguration', () => { |
| 133 | + it('using mock', () => { |
| 134 | + return s3Events._putBucketNotificationConfiguration(params).then(data => { |
| 135 | + assert.deepEqual(data, mockResponse.putBucketNotificationConfiguration) |
| 136 | + }) |
| 137 | + }) |
| 138 | + }) |
| 139 | + |
| 140 | + describe('add', () => { |
| 141 | + it('using mock', () => { |
| 142 | + return s3Events.add(params).then(data => { |
| 143 | + assert.deepEqual(data, mockResponse.putBucketNotificationConfiguration) |
| 144 | + }) |
| 145 | + }) |
| 146 | + }) |
| 147 | +}) |
0 commit comments