UNPKG

6.12 kBJavaScriptView Raw
1const S3NotificationConfig = require('../functions/s3-notification-topic-config');
2const test = require('tape');
3const AWS = require('@mapbox/mock-aws-sdk-js');
4const http = require('http');
5
6test('[s3-notification-topic-config] create handles errors', assert => {
7 AWS.stub('S3', 'getBucketNotificationConfiguration', (cb) => {
8 return cb(null, {
9 TopicConfigurations: [],
10 LambdaConfigurations: [],
11 QueueConfigurations: []
12 });
13 });
14
15 AWS.stub('S3', 'putBucketNotificationConfiguration', (opts, cb) => {
16 assert.ok(opts.hasOwnProperty('NotificationConfiguration'), 'should have NotificationConfiguration passed in call');
17 return cb(new Error('random error'));
18 });
19
20 const notificationConfig = new S3NotificationConfig('test-config', 'arn:aws:sns:us-east-1:special-topic', 'test-bucket', 'us-east-1', ['s3:objectCreated:*']);
21
22 notificationConfig.create((err) => {
23 assert.equal(err.message, 'random error', 'errors');
24 AWS.S3.restore();
25 assert.end();
26 });
27});
28
29test('[s3-notification-topic-config] create handles success', assert => {
30 AWS.stub('S3', 'getBucketNotificationConfiguration', (cb) => {
31 return cb(null, {
32 TopicConfigurations: [],
33 LambdaConfigurations: [],
34 QueueConfigurations: []
35 });
36 });
37
38 AWS.stub('S3', 'putBucketNotificationConfiguration', (opts, cb) => {
39 assert.ok(opts.hasOwnProperty('NotificationConfiguration'), 'should have NotificationConfiguration passed in call');
40 return cb(null, 'putBucket success');
41 });
42
43 const notificationConfig = new S3NotificationConfig('test-config', 'arn:aws:sns:us-east-1:special-topic', 'test-bucket', 'us-east-1', ['s3:objectCreated:*']);
44
45 notificationConfig.create((err, res) => {
46 assert.ifError(err, 'does not error');
47 assert.notOk(res, 'no response is success');
48 AWS.S3.restore();
49 assert.end();
50 });
51});
52
53test('[s3-notification-topic-config] update deletes old topic config and creates the new one', assert => {
54 const getBucket = AWS.stub('S3', 'getBucketNotificationConfiguration');
55 getBucket.onCall(0).yields(null, {
56 TopicConfigurations: [{
57 Id: 'test-config',
58 TopicArn: 'arn:aws:sns:us-east-1:special-topic',
59 Events: [ 's3:ObjectCreated:*' ]
60 }],
61 LambdaConfigurations: [],
62 QueueConfigurations: []
63 });
64 getBucket.onCall(1).yields(null, {
65 TopicConfigurations: [],
66 LambdaConfigurations: [],
67 QueueConfigurations: []
68 });
69
70 const putBucket = AWS.stub('S3', 'putBucketNotificationConfiguration');
71 putBucket.onCall(0).callsFake((opts, cb) => {
72 assert.ok(opts.NotificationConfiguration.TopicConfigurations.length === 0, 'no TopicConfigurations');
73 cb(null, 'putBucket success');
74 });
75 putBucket.onCall(1).callsFake((opts, cb) => {
76 assert.ok(opts.hasOwnProperty('NotificationConfiguration'), 'has Notification configuration passed to function');
77 cb(null, 'putBucket success');
78 });
79
80 const notificationConfig = new S3NotificationConfig(
81 'test-config',
82 'arn:aws:sns:us-east-1:special-topic',
83 'test-bucket',
84 'us-east-1',
85 ['s3:objectCreated:*'],
86 undefined,
87 undefined,
88 {
89 Id: 'test-config',
90 Bucket: 'test-bucket',
91 BucketRegion: 'us-east-1'
92 });
93
94 notificationConfig.update((err, res) => {
95 assert.ifError(err, 'does not error');
96 assert.notOk(res, 'no response is success');
97 getBucket.restore();
98 putBucket.restore();
99 assert.end();
100 });
101});
102
103test('[s3-notification-topic-config] delete removes the config', assert => {
104 AWS.stub('S3', 'getBucketNotificationConfiguration', (cb) => {
105 return cb(null, {
106 TopicConfigurations: [{
107 Id: 'test-config',
108 TopicArn: 'arn:aws:sns:us-east-1:special-topic',
109 Events: [ 's3:ObjectCreated:*' ]
110 }],
111 LambdaConfigurations: [],
112 QueueConfigurations: []
113 });
114 });
115
116 AWS.stub('S3', 'putBucketNotificationConfiguration', (opts, cb) => {
117 assert.ok(opts.hasOwnProperty('NotificationConfiguration'), 'should have NotificationConfiguration passed in call');
118 assert.ok(opts.NotificationConfiguration.TopicConfigurations.length === 0, 'should not have any TopicConfigurations');
119 return cb(null, 'putBucket success');
120 });
121
122 const notificationConfig = new S3NotificationConfig('test-config', 'arn:aws:sns:us-east-1:special-topic', 'test-bucket', 'us-east-1', ['s3:objectCreated:*']);
123
124 notificationConfig.delete((err, res) => {
125 assert.ifError(err, 'does not error');
126 assert.notOk(res, 'no response is success');
127 AWS.S3.restore();
128 assert.end();
129 });
130});
131
132test('[s3-notification-topic-config] manage parses events and relays LatestStreamLabel through Response', assert => {
133 http.request = (options, cb) => {
134 return {
135 on: function() {
136 return this;
137 },
138 write: () => {},
139 end: () => {
140 return cb();
141 }
142 };
143 };
144
145 AWS.stub('S3', 'getBucketNotificationConfiguration', (cb) => {
146 return cb(null, {
147 TopicConfigurations: [],
148 LambdaConfigurations: [],
149 QueueConfigurations: []
150 });
151 });
152
153 AWS.stub('S3', 'putBucketNotificationConfiguration', (opts, cb) => {
154 assert.ok(opts.hasOwnProperty('NotificationConfiguration'), 'should have NotificationConfiguration passed in call');
155 assert.ok(opts.NotificationConfiguration.TopicConfigurations.length === 1, 'should have a TopicConfiguration');
156 return cb(null, 'putBucket success');
157 });
158
159 S3NotificationConfig.manage({
160 ResponseURL: 'https://api.mapbox.com/hello',
161 PhysicalResourceId: 'abc',
162 StackId: 'abc',
163 LogicalResourceId: 'abc',
164 ResourceProperties: {
165 Id: 'test-config',
166 SnsTopicArn: 'arn:aws:sns:us-east-1:special-topic',
167 Bucket: 'test-bucket',
168 BucketRegion: 'us-east-1',
169 EventTypes: [ 's3:ObjectCreated:*' ],
170 PrefixFilter: 'prefix',
171 SuffixFilter: '.jpg'
172 },
173 RequestId: 'abc',
174 RequestType: 'CREATE'
175 }, {
176 done: (err, body) => {
177 assert.ifError(err, 'no error');
178 assert.equals(JSON.parse(body).Status, 'SUCCESS', 'status is success');
179 AWS.S3.restore();
180 assert.end();
181 }
182 });
183});