-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocessing.test.js
99 lines (93 loc) · 3.14 KB
/
processing.test.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
import makeDebug from 'debug'
import feathers from '@feathersjs/feathers'
import chai, { util, expect } from 'chai'
import chailint from 'chai-lint'
import sharp from 'sharp'
import express from '@feathersjs/express'
import fs from 'fs'
import crypto from 'crypto'
import { Service } from '../lib/index.js'
feathers.setDebug(makeDebug)
let app, service, expressServer
const options = {
s3Client: {
credentials: {
accessKeyId: process.env.S3_ACCESS_KEY_ID,
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY
},
endpoint: process.env.S3_ENDPOINT,
region: process.env.S3_REGION,
signatureVersion: 'v4'
},
bucket: process.env.S3_BUCKET,
prefix: crypto.randomUUID()
}
const fileId = 'image.png'
const fileContent = fs.readFileSync('test/data/image.png')
let resizedFileContent
async function resizeImage (hook) {
resizedFileContent = await sharp(hook.data.buffer).resize(128, 48, { fit: 'contain', background: '#00000000' }).toBuffer()
// Write the processed image for further testing
await sharp(resizedFileContent).toFile('test/tmp/resized-image.png')
hook.data.buffer = resizedFileContent
}
describe('feathers-s3-processing', () => {
before(() => {
chailint(chai, util)
app = express(feathers())
app.use(express.json())
app.configure(express.rest())
})
it('configure the service', async () => {
app.use('s3', new Service(options), {
methods: ['create', 'get', 'find', 'remove', 'putObject']
})
service = app.service('s3')
expect(service).toExist()
// Add hook for processing
service.hooks({
before: {
putObject: [resizeImage]
}
})
expressServer = await app.listen(3333)
})
it('upload with processing', async () => {
let eventReceived = false
service.once('object-put', (data) => {
if (data.id === fileId) eventReceived = true
})
const response = await service.putObject({
id: fileId,
buffer: fileContent,
type: 'image/png'
})
expect(response.id).to.equal(fileId)
expect(response.ETag).toExist()
expect(eventReceived).beTrue()
})
it('download processed file', async () => {
const response = await service.get(fileId)
expect(response.id).to.equal(fileId)
expect(response.buffer).toExist()
expect(response.type).to.equal('image/png')
const buffer = service.atob(response.buffer)
expect(buffer.toString()).to.equal(resizedFileContent.toString())
// Write the downloaded image
await sharp(buffer).toFile('test/tmp/downloaded-resized-image.png')
// Ensure it is similar to what we expect
const resizedImage = fs.readFileSync('test/tmp/resized-image.png')
const downloadedImage = fs.readFileSync('test/tmp/downloaded-resized-image.png')
expect(resizedImage.toString()).to.equal(downloadedImage.toString())
})
it('remove uploaded file', async () => {
fs.unlinkSync('test/tmp/resized-image.png')
fs.unlinkSync('test/tmp/downloaded-resized-image.png')
const response = await service.remove(fileId)
expect(response.id).to.equal(fileId)
expect(response.$metadata.httpStatusCode).to.equal(204)
})
after(async () => {
await expressServer.close()
})
})