-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlambda-pdf-thumbnail.spec.js
361 lines (316 loc) · 10.5 KB
/
lambda-pdf-thumbnail.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
(function () {
'use strict';
var AWS = require('aws-sdk');
var lambdaPdfThumbnail = require('./lambda-pdf-thumbnail');
var async = require('async');
var bufferEqual = require('buffer-equal');
var fs = require('fs');
var rimraf = require('rimraf');
var dynaliteFolder = './mydb';
var s3rverFolder = './s3rver';
var S3rver = require('s3rver');
var dynalite = require('dynalite');
var dynaliteServer = dynalite({path: dynaliteFolder, createTableMs: 0});
var s3rver = new S3rver();
var INPUT_BUFFER = fs.readFileSync('test/pdf/test.pdf');
var EVENT_DATA = require('./test-event.json');
var TABLE_NAME = 'pdf-thumbnail-bucket-mappings';
var INPUT_BUCKET_NAME = 'input-bucket';
var OUT_PUTBUCKET_NAME = 'output-bucket';
var INPUT_KEY = 'test.pdf';
var SUBDIR_INPUT_KEY = 'test/test.pdf';
var OUTPUT_KEY = 'test-thumbnail.png';
var VERIFIER_DATA = [{
s3Bucket: OUT_PUTBUCKET_NAME,
s3Key: OUTPUT_KEY
}];
function setupS3rver(callback) {
s3rver.setHostname('localhost')
.setPort(10001)
.setDirectory('./s3rver')
.setSilent(true)
.run(function(err) {
if (err) {
callback(err);
return;
}
var config = {
s3ForcePathStyle: true,
accessKeyId: 'ACCESS_KEY_ID',
secretAccessKey: 'SECRET_ACCESS_KEY',
endpoint: new AWS.Endpoint('http://localhost:10001')
};
var s3 = new AWS.S3(config);
callback(null, s3);
});
}
function setupDynalite(callback) {
dynaliteServer.listen(10002, function(err){
if (err) {
callback(err);
return;
}
var config = {
s3ForcePathStyle: true,
accessKeyId: 'ACCESS_KEY_ID',
secretAccessKey: 'SECRET_ACCESS_KEY',
region: 'us-west-1',
endpoint: new AWS.Endpoint('http://localhost:10002')
};
var dynamodb = new AWS.DynamoDB(config);
callback(null, dynamodb);
});
}
function createTable(dynamodb, callback) {
var params = {
TableName : TABLE_NAME,
KeySchema: [
{ AttributeName: 'SourceBucket', KeyType: 'HASH' },
],
AttributeDefinitions: [
{ AttributeName: 'SourceBucket', AttributeType: 'S' },
],
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1
}
};
dynamodb.createTable(params, function() {
var params = {
TableName: TABLE_NAME,
Item: {
SourceBucket: {S: INPUT_BUCKET_NAME},
DestinationBucket: {S: OUT_PUTBUCKET_NAME}
}
};
dynamodb.putItem(params, function(err) {
callback(err);
});
});
}
function setupEnvironment(callback) {
var dynamodb;
async.waterfall([
function startDynalite(next) {
setupDynalite(function(err, dynolite){
dynamodb = dynolite;
next(err, dynolite);
});
},
createTable,
function createS3rverFolder(next) {
fs.mkdir(s3rverFolder, function(err) {
next(err);
});
},
setupS3rver,
function createIntputBucket(s3, next) {
s3.createBucket({Bucket: INPUT_BUCKET_NAME}, function(err) {
next(err, s3);
});
},
function createOutputBucket(s3, next) {
s3.createBucket({Bucket: OUT_PUTBUCKET_NAME}, function(err) {
next(err, s3);
});
},
function putTestFile(s3, next) {
var params = {Bucket: INPUT_BUCKET_NAME, Key: INPUT_KEY, Body: INPUT_BUFFER};
s3.putObject(params, function(err) {
next(err, s3);
});
},
function putTestFileSubDir(s3, next) {
var params = {Bucket: INPUT_BUCKET_NAME, Key: SUBDIR_INPUT_KEY, Body: INPUT_BUFFER};
s3.putObject(params, function(err) {
next(err, s3, dynamodb);
});
}
], callback);
}
function thumbnailVerifierHelper (s3, bucketname, key) {
return function (callback) {
s3.getObject(
{Bucket: bucketname, Key: key},
function(downloadError, response) {
if (downloadError) {
callback(downloadError);
return;
}
callback(null, (response.Body.length > 0 && response.ContentType === 'image/png') );
}
);
};
}
function thumbnailVerifier(s3, data, callback) {
var work =[];
data.forEach(function(element) {
work.push(thumbnailVerifierHelper(
s3,
element.s3Bucket,
element.s3Key
));
});
async.parallel(work, callback);
}
function makeLambdaContext(verifierData, s3, expect, jasmineDone) {
return {
fail: function() {
expect(false).toBe(true);
jasmineDone();
},
done: function() {
thumbnailVerifier(s3, verifierData, function(err, correctArray) {
var correct = true;
correctArray.forEach(function(bool){
correct = correct && bool;
});
expect(err).toBeFalsy();
expect(correct).toBe(true);
jasmineDone();
});
}
};
}
describe('lambda-pdf', function() {
var mockS3;
var mockDynamodb;
beforeAll(function(done){
setupEnvironment(function(err, s3, dynamodb) {
if (err) console.log('setting up environment error', err);
mockS3 = s3;
mockDynamodb = dynamodb;
done();
}.bind(this));
});
it ('environment should be set up', function(){
expect(mockS3).toBeDefined();
expect(mockDynamodb).toBeDefined();
});
describe('generateThumbnail', function() {
it('should return an error, when the input file is of unknown type', function (done) {
lambdaPdfThumbnail.generateThumbnail(
mockS3, 72,
INPUT_BUCKET_NAME, 'test',
OUT_PUTBUCKET_NAME, OUTPUT_KEY, function(err) {
expect(function(){throw err;}).toThrowError('unable to infer document type for key test');
done();
});
});
it('should return an error, when the input file is not of type pdf', function (done) {
lambdaPdfThumbnail.generateThumbnail(
mockS3, 72, INPUT_BUCKET_NAME,
'test.jpeg', OUT_PUTBUCKET_NAME,
OUTPUT_KEY, function(err) {
expect(function(){throw err;}).toThrowError('skipping non-pdf test.jpeg');
done();
});
});
it('should make a thumbnail and save it in a bucket', function(done) {
lambdaPdfThumbnail.generateThumbnail(
mockS3, 72,
INPUT_BUCKET_NAME, INPUT_KEY,
OUT_PUTBUCKET_NAME, OUTPUT_KEY, function(thumbnailError) {
expect(thumbnailError).toBeFalsy();
thumbnailVerifier(mockS3, VERIFIER_DATA, function(downloadError, correctArray) {
expect(downloadError).toBeFalsy();
expect(correctArray[0]).toBe(true);
done();
});
});
});
});
describe('s3 event handler', function(){
it('should throw error if tableName or outputBucketName is not specified', function(){
expect(function(){
new lambdaPdfThumbnail.S3EventHandler({});
}).toThrowError('Neither the output bucket or dynamodb table is specified');
});
it('should throw error if tableName or outputBucketName is not specified', function(){
expect(function(){
new lambdaPdfThumbnail.S3EventHandler({outputBucketName:'test'});
}).toThrowError('region is not specified');
});
it('with specified output-bucket should save a thumbnail', function(done){
var verifierContext = makeLambdaContext(VERIFIER_DATA, mockS3, expect, done);
var s3EventHandler = new lambdaPdfThumbnail.S3EventHandler({
region:'eu-west-1',
outputBucketName:'output-bucket',
s3: mockS3
});
expect(s3EventHandler.outputBucketName).toBeDefined();
expect(s3EventHandler.tableName).not.toBeDefined();
s3EventHandler.handler(EVENT_DATA, verifierContext);
});
it('without specified output-bucket should save a thumbnail', function(done){
var verifierData = [{
s3Bucket: OUT_PUTBUCKET_NAME,
s3Key: OUTPUT_KEY
}];
var verifierContext = makeLambdaContext(VERIFIER_DATA, mockS3, expect, done);
var s3EventHandler = new lambdaPdfThumbnail.S3EventHandler({
region:'eu-west-1',
outputBucketName:'output-bucket',
s3: mockS3
});
var eventData = JSON.parse(JSON.stringify(EVENT_DATA));
eventData.Records[0].s3.object.key = 'test/test.pdf';
expect(s3EventHandler.outputBucketName).toBeDefined();
expect(s3EventHandler.tableName).not.toBeDefined();
s3EventHandler.handler(eventData, verifierContext);
});
it('with specified output-bucket and multiple resolution should save mutliple thumbnails', function(done){
var verifierData = [
{
s3Bucket: OUT_PUTBUCKET_NAME,
s3Key: 'test-high.png'
},
{
s3Bucket: OUT_PUTBUCKET_NAME,
s3Key: 'test-medium.png'
},
{
s3Bucket: OUT_PUTBUCKET_NAME,
s3Key: 'test-low.png'
}
];
var context = makeLambdaContext(verifierData, mockS3, expect, done);
var s3EventHandler = new lambdaPdfThumbnail.S3EventHandler({
region:'eu-west-1',
outputBucketName:'output-bucket',
s3: mockS3,
resolution: {
'-low': 26,
'-medium': 52,
'-high': 72
}
});
expect(s3EventHandler.outputBucketName).toBeDefined();
expect(s3EventHandler.tableName).not.toBeDefined();
s3EventHandler.handler(EVENT_DATA, context);
});
it('with specified dynamo database should save a thumbnail', function(done){
var context = makeLambdaContext(VERIFIER_DATA, mockS3, expect, done);
var s3EventHandlerWithDyno = new lambdaPdfThumbnail.S3EventHandler({
region:'eu-west-1',
tableName: TABLE_NAME,
s3: mockS3,
dynamodb: mockDynamodb
});
expect(s3EventHandlerWithDyno.outputBucketName).not.toBeDefined();
expect(s3EventHandlerWithDyno.tableName).toBeDefined();
s3EventHandlerWithDyno.handler(EVENT_DATA, context);
});
});
afterAll(function(done){
rimraf(s3rverFolder, function(err1){
rimraf(dynaliteFolder, function(err2){
if (err1 || err2){
console.log('error deleting folders', err1, err2);
}
done();
});
});
});
});
}());