monastery 1.36.1 → 1.37.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +2 -1
- package/.travis.yml +1 -1
- package/changelog.md +28 -0
- package/docs/{schema/rules.md → definition/field-rules.md} +6 -4
- package/docs/definition/index.md +338 -0
- package/docs/image-plugin.md +20 -19
- package/docs/manager/index.md +12 -10
- package/docs/manager/model.md +4 -3
- package/docs/manager/models.md +1 -1
- package/docs/model/find.md +31 -29
- package/docs/model/findOne.md +3 -4
- package/docs/model/findOneAndUpdate.md +42 -0
- package/docs/model/index.md +2 -2
- package/docs/model/insert.md +22 -20
- package/docs/model/remove.md +3 -3
- package/docs/model/update.md +11 -10
- package/docs/model/validate.md +22 -25
- package/docs/readme.md +9 -8
- package/lib/index.js +12 -11
- package/lib/model-crud.js +1 -1
- package/lib/model-validate.js +4 -3
- package/lib/model.js +1 -1
- package/lib/util.js +13 -1
- package/package.json +2 -2
- package/plugins/images/index.js +79 -45
- package/test/assets/house.jpg +0 -0
- package/test/crud.js +94 -23
- package/test/model.js +1 -2
- package/test/plugin-images.js +637 -384
- package/test/test.js +2 -0
- package/test/validate.js +35 -3
- package/docs/schema/index.md +0 -313
package/test/plugin-images.js
CHANGED
|
@@ -142,419 +142,327 @@ module.exports = function(monastery, opendb) {
|
|
|
142
142
|
db.close()
|
|
143
143
|
})
|
|
144
144
|
|
|
145
|
-
test('images addImages', (
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
145
|
+
test('images addImages', async () => {
|
|
146
|
+
let db = (await opendb(null, {
|
|
147
|
+
timestamps: false,
|
|
148
|
+
serverSelectionTimeoutMS: 2000,
|
|
149
|
+
imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
|
|
150
|
+
})).db
|
|
151
|
+
|
|
152
|
+
let user = db.model('user', { fields: {
|
|
153
|
+
logo: { type: 'image' },
|
|
154
|
+
logos: [{ type: 'image' }],
|
|
155
|
+
users: [{ logo: { type: 'image' } }]
|
|
156
|
+
}})
|
|
157
|
+
|
|
158
|
+
let plugin = db.imagePluginFile
|
|
159
|
+
let supertest = require('supertest')
|
|
160
|
+
let express = require('express')
|
|
161
|
+
let upload = require('express-fileupload')
|
|
162
|
+
let app = express()
|
|
163
|
+
app.use(upload({ limits: { fileSize: 1 * 1000 * 1000, files: 10 }}))
|
|
164
|
+
|
|
165
|
+
app.post('/', async function(req, res) {
|
|
166
|
+
try {
|
|
167
167
|
// Files exist
|
|
168
168
|
expect(req.files.logo).toEqual(expect.any(Object))
|
|
169
|
-
plugin._findValidImages(req.files, user)
|
|
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
|
-
|
|
169
|
+
let validFiles = await plugin._findValidImages(req.files, user)
|
|
170
|
+
// Valid file count
|
|
171
|
+
expect(validFiles).toEqual([
|
|
172
|
+
expect.any(Array),
|
|
173
|
+
expect.any(Array),
|
|
174
|
+
expect.any(Array),
|
|
175
|
+
expect.any(Array)
|
|
176
|
+
])
|
|
177
|
+
// Valid imageField
|
|
178
|
+
expect(validFiles[0].imageField).toEqual(expect.any(Object))
|
|
179
|
+
expect(validFiles[1].imageField).toEqual(expect.any(Object))
|
|
180
|
+
expect(validFiles[2].imageField).toEqual(expect.any(Object))
|
|
181
|
+
expect(validFiles[3].imageField).toEqual(expect.any(Object))
|
|
182
|
+
// Valid inputPath
|
|
183
|
+
expect(validFiles[0].inputPath).toEqual('logo')
|
|
184
|
+
expect(validFiles[1].inputPath).toEqual('logos.0')
|
|
185
|
+
expect(validFiles[2].inputPath).toEqual('users.0.logo')
|
|
186
|
+
expect(validFiles[3].inputPath).toEqual('users.2.logo')
|
|
187
|
+
// Valid type
|
|
188
|
+
expect(validFiles[0][0].format).toEqual('png')
|
|
189
|
+
expect(validFiles[1][0].format).toEqual('png')
|
|
190
|
+
expect(validFiles[2][0].format).toEqual('png')
|
|
191
|
+
expect(validFiles[3][0].format).toEqual('png')
|
|
192
|
+
|
|
193
|
+
let response = await plugin.addImages(
|
|
194
|
+
{ model: user, files: req.files, query: { _id: 1234 }},
|
|
195
|
+
req.body,
|
|
196
|
+
true
|
|
197
|
+
)
|
|
198
|
+
expect(response[0]).toEqual({
|
|
199
|
+
name: 'my awesome avatar',
|
|
200
|
+
logo: {
|
|
201
|
+
bucket: 'fake',
|
|
202
|
+
date: expect.any(Number),
|
|
203
|
+
filename: 'logo.png',
|
|
204
|
+
filesize: expect.any(Number),
|
|
205
|
+
path: expect.any(String),
|
|
206
|
+
uid: expect.any(String)
|
|
207
|
+
},
|
|
208
|
+
logos: [ expect.any(Object) ],
|
|
209
|
+
users: [
|
|
210
|
+
{
|
|
199
211
|
logo: {
|
|
200
212
|
bucket: 'fake',
|
|
201
213
|
date: expect.any(Number),
|
|
202
|
-
filename: '
|
|
214
|
+
filename: 'logo2.png',
|
|
203
215
|
filesize: expect.any(Number),
|
|
204
216
|
path: expect.any(String),
|
|
205
217
|
uid: expect.any(String)
|
|
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
|
-
})
|
|
218
|
+
}
|
|
219
|
+
},
|
|
220
|
+
undefined, // !this will be converted to null on insert/update
|
|
221
|
+
{
|
|
222
|
+
logo: {
|
|
223
|
+
bucket: 'fake',
|
|
224
|
+
date: expect.any(Number),
|
|
225
|
+
filename: 'logo2.png',
|
|
226
|
+
filesize: expect.any(Number),
|
|
227
|
+
path: expect.any(String),
|
|
228
|
+
uid: expect.any(String)
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
]
|
|
232
|
+
})
|
|
233
|
+
res.send()
|
|
234
|
+
} catch (e) {
|
|
235
|
+
console.log(e.message || e)
|
|
236
|
+
res.status(500).send()
|
|
237
|
+
}
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
// Start tests
|
|
241
|
+
await supertest(app)
|
|
242
|
+
.post('/')
|
|
243
|
+
.field('name', 'my awesome avatar')
|
|
244
|
+
.attach('logo', `${__dirname}/assets/logo.png`)
|
|
245
|
+
.attach('logos.0', `${__dirname}/assets/logo2.png`)
|
|
246
|
+
.attach('users.0.logo', `${__dirname}/assets/logo2.png`)
|
|
247
|
+
.attach('users.2.logo', `${__dirname}/assets/logo2.png`)
|
|
248
|
+
.expect(200)
|
|
239
249
|
|
|
240
|
-
|
|
241
|
-
supertest(app)
|
|
242
|
-
.post('/')
|
|
243
|
-
.field('name', 'my awesome avatar')
|
|
244
|
-
.attach('logo', `${__dirname}/assets/logo.png`)
|
|
245
|
-
.attach('logos.0', `${__dirname}/assets/logo2.png`)
|
|
246
|
-
.attach('users.0.logo', `${__dirname}/assets/logo2.png`)
|
|
247
|
-
.attach('users.2.logo', `${__dirname}/assets/logo2.png`)
|
|
248
|
-
.expect(200)
|
|
249
|
-
.end((err, res) => { if (err) console.log(err) })
|
|
250
|
-
})()
|
|
250
|
+
db.close()
|
|
251
251
|
})
|
|
252
252
|
|
|
253
|
-
test('images removeImages', (
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
})).db
|
|
253
|
+
test('images removeImages', async () => {
|
|
254
|
+
let db = (await opendb(null, {
|
|
255
|
+
timestamps: false,
|
|
256
|
+
serverSelectionTimeoutMS: 2000,
|
|
257
|
+
imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
|
|
258
|
+
})).db
|
|
260
259
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
260
|
+
let user = db.model('user', { fields: {
|
|
261
|
+
logo: { type: 'image' },
|
|
262
|
+
logos: [{ type: 'image' }],
|
|
263
|
+
users: [{ userlogo: { type: 'image' } }],
|
|
264
|
+
deep: { logo: { type: 'image' }}
|
|
265
|
+
}})
|
|
267
266
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
267
|
+
let image = {
|
|
268
|
+
bucket: 'test',
|
|
269
|
+
date: 1234,
|
|
270
|
+
filename: 'test.png',
|
|
271
|
+
filesize: 1234,
|
|
272
|
+
path: 'test/test123'
|
|
273
|
+
}
|
|
274
|
+
let user1 = await db.user._insert({
|
|
275
|
+
logo: { ...image, uid: 'test1', path: 'dir/test1.png' },
|
|
276
|
+
logos: [
|
|
277
|
+
{ ...image, uid: 'test2', path: 'dir/test2.png' },
|
|
278
|
+
{ ...image, uid: 'test3', path: 'dir/test3.png' }
|
|
279
|
+
],
|
|
280
|
+
users: [
|
|
281
|
+
{ userlogo: { ...image, uid: 'test4', path: 'dir/test4.png' }},
|
|
282
|
+
null,
|
|
283
|
+
{ userlogo: { ...image, uid: 'test4', path: 'dir/test4.png' }},
|
|
284
|
+
{ userlogo: { ...image, uid: 'test4', path: 'dir/test4.png' }}
|
|
285
|
+
],
|
|
286
|
+
deep: {}
|
|
287
|
+
})
|
|
289
288
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
289
|
+
let plugin = db.imagePluginFile
|
|
290
|
+
let supertest = require('supertest')
|
|
291
|
+
let express = require('express')
|
|
292
|
+
let upload = require('express-fileupload')
|
|
293
|
+
let app = express()
|
|
294
|
+
app.use(upload({ limits: { fileSize: 1 * 1000 * 1000, files: 10 }}))
|
|
296
295
|
|
|
297
|
-
|
|
296
|
+
app.post('/', async function(req, res) {
|
|
297
|
+
try {
|
|
298
298
|
req.body.logos = JSON.parse(req.body.logos)
|
|
299
299
|
req.body.users = JSON.parse(req.body.users)
|
|
300
300
|
let options = { files: req.files, model: user, query: { _id: user1._id }}
|
|
301
|
+
let response = await plugin.removeImages(options, req.body, true)
|
|
302
|
+
expect(response[0]).toEqual({ test1: 1, test2: 0, test3: 1, test4: 0 })
|
|
303
|
+
expect(response[1]).toEqual([
|
|
304
|
+
{ Key: 'dir/test2.png' },
|
|
305
|
+
{ Key: 'small/test2.jpg' },
|
|
306
|
+
{ Key: 'medium/test2.jpg' },
|
|
307
|
+
{ Key: 'large/test2.jpg' },
|
|
308
|
+
|
|
309
|
+
{ Key: 'dir/test4.png' },
|
|
310
|
+
{ Key: 'small/test4.jpg' },
|
|
311
|
+
{ Key: 'medium/test4.jpg' },
|
|
312
|
+
{ Key: 'large/test4.jpg' }
|
|
313
|
+
])
|
|
314
|
+
res.send()
|
|
315
|
+
} catch (e) {
|
|
316
|
+
console.log(e.message || e)
|
|
317
|
+
res.status(500).send()
|
|
318
|
+
}
|
|
319
|
+
})
|
|
301
320
|
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
])
|
|
316
|
-
})
|
|
317
|
-
.finally(() => {
|
|
318
|
-
res.json()
|
|
319
|
-
db.close()
|
|
320
|
-
done()
|
|
321
|
-
})
|
|
322
|
-
})
|
|
321
|
+
await supertest(app)
|
|
322
|
+
.post('/')
|
|
323
|
+
.field('name', 'my awesome avatar')
|
|
324
|
+
.field('logos', JSON.stringify([ null, { ...image, uid: 'test3', path: 'dir/test3.png' } ]))
|
|
325
|
+
.field('users', JSON.stringify([
|
|
326
|
+
{ userlogo: { ...image, uid: 'test1', path: 'dir/test1.png' }},
|
|
327
|
+
null,
|
|
328
|
+
null,
|
|
329
|
+
//null // undefined
|
|
330
|
+
]))
|
|
331
|
+
.attach('logo', `${__dirname}/assets/logo.png`)
|
|
332
|
+
.attach('logos.0', `${__dirname}/assets/logo2.png`)
|
|
333
|
+
.expect(200)
|
|
323
334
|
|
|
324
|
-
|
|
325
|
-
supertest(app)
|
|
326
|
-
.post('/')
|
|
327
|
-
.field('name', 'my awesome avatar')
|
|
328
|
-
.field('logos', JSON.stringify([ null, { ...image, uid: 'test3', path: 'dir/test3.png' } ]))
|
|
329
|
-
.field('users', JSON.stringify([
|
|
330
|
-
{ userlogo: { ...image, uid: 'test1', path: 'dir/test1.png' }},
|
|
331
|
-
null,
|
|
332
|
-
null,
|
|
333
|
-
//null // undefined
|
|
334
|
-
]))
|
|
335
|
-
.attach('logo', `${__dirname}/assets/logo.png`)
|
|
336
|
-
.attach('logos.0', `${__dirname}/assets/logo2.png`)
|
|
337
|
-
.expect(200)
|
|
338
|
-
.end((err, res) => { if (err) console.log(err) })
|
|
339
|
-
})()
|
|
335
|
+
db.close()
|
|
340
336
|
})
|
|
341
337
|
|
|
342
|
-
test('images removeImages with no data', (
|
|
343
|
-
(
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
})).db
|
|
338
|
+
test('images removeImages with no data', async () => {
|
|
339
|
+
// NOTE: Redundent, leaving for now (test was needed to fix a project issue)
|
|
340
|
+
let db = (await opendb(null, {
|
|
341
|
+
timestamps: false,
|
|
342
|
+
serverSelectionTimeoutMS: 2000,
|
|
343
|
+
imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
|
|
344
|
+
})).db
|
|
350
345
|
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
// let image = {
|
|
356
|
-
// bucket: 'test',
|
|
357
|
-
// date: 1234,
|
|
358
|
-
// filename: 'test.png',
|
|
359
|
-
// filesize: 1234,
|
|
360
|
-
// path: 'test/test123'
|
|
361
|
-
// }
|
|
362
|
-
let user1 = await db.user._insert({
|
|
363
|
-
logo: null
|
|
364
|
-
})
|
|
346
|
+
let user = db.model('user', { fields: {
|
|
347
|
+
logo: { type: 'image' }
|
|
348
|
+
}})
|
|
365
349
|
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
plugin.removeImages(options, req.body, true)
|
|
377
|
-
.then(res => {
|
|
378
|
-
expect(res[0]).toEqual({})
|
|
379
|
-
expect(res[1]).toEqual([])
|
|
380
|
-
})
|
|
381
|
-
.finally(() => {
|
|
382
|
-
res.json()
|
|
383
|
-
db.close()
|
|
384
|
-
done()
|
|
385
|
-
})
|
|
386
|
-
} catch(e) {
|
|
387
|
-
console.log(e)
|
|
388
|
-
res.error(e)
|
|
389
|
-
}
|
|
390
|
-
})
|
|
350
|
+
// let image = {
|
|
351
|
+
// bucket: 'test',
|
|
352
|
+
// date: 1234,
|
|
353
|
+
// filename: 'test.png',
|
|
354
|
+
// filesize: 1234,
|
|
355
|
+
// path: 'test/test123'
|
|
356
|
+
// }
|
|
357
|
+
let user1 = await db.user._insert({
|
|
358
|
+
logo: null
|
|
359
|
+
})
|
|
391
360
|
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
})()
|
|
399
|
-
})
|
|
361
|
+
let plugin = db.imagePluginFile
|
|
362
|
+
let supertest = require('supertest')
|
|
363
|
+
let express = require('express')
|
|
364
|
+
let upload = require('express-fileupload')
|
|
365
|
+
let app = express()
|
|
366
|
+
app.use(upload({ limits: { fileSize: 1 * 1000 * 1000, files: 10 }}))
|
|
400
367
|
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
let user = db.model('user', { fields: {
|
|
415
|
-
imageIco: { type: 'image' },
|
|
416
|
-
imageWebp: { type: 'image', formats: ['webp'] },
|
|
417
|
-
imageSvgBad: { type: 'image' },
|
|
418
|
-
imageSvgGood: { type: 'image', formats: ['svg'] },
|
|
419
|
-
imageSvgAny: { type: 'image', formats: ['any'] },
|
|
420
|
-
imageSize1: { type: 'image', filesize: 1000 * 100 },
|
|
421
|
-
imageSize2: { type: 'image' },
|
|
422
|
-
}})
|
|
423
|
-
|
|
424
|
-
let plugin = db.imagePluginFile
|
|
425
|
-
let supertest = require('supertest')
|
|
426
|
-
let express = require('express')
|
|
427
|
-
let upload = require('express-fileupload')
|
|
428
|
-
let app = express()
|
|
429
|
-
app.use(upload({ limits: { fileSize: 1000 * 200, files: 10 }}))
|
|
430
|
-
|
|
431
|
-
app.post('/', async (req, res) => {
|
|
432
|
-
let imageSvgBad = { imageSvgBad: req.files.imageSvgBad }
|
|
433
|
-
let imageSize1 = { imageSize1: req.files.imageSize1 }
|
|
434
|
-
let imageSize2 = { imageSize2: req.files.imageSize2 }
|
|
435
|
-
delete req.files.imageSvgBad
|
|
436
|
-
delete req.files.imageSize1
|
|
437
|
-
delete req.files.imageSize2
|
|
438
|
-
// Ico, Webp, and imageSvgGood will throw an error first if it's not a valid type
|
|
439
|
-
await expect(plugin._findValidImages(req.files, user)).resolves.toEqual(expect.any(Array))
|
|
440
|
-
await expect(plugin._findValidImages(imageSvgBad, user)).rejects.toEqual({
|
|
441
|
-
title: 'imageSvgBad',
|
|
442
|
-
detail: 'The file format \'svg\' for \'bad.svg\' is not supported'
|
|
443
|
-
})
|
|
444
|
-
await expect(plugin._findValidImages(imageSize1, user)).rejects.toEqual({
|
|
445
|
-
title: 'imageSize1',
|
|
446
|
-
detail: 'The file size for \'lion1.png\' is bigger than 0.1MB.'
|
|
447
|
-
})
|
|
448
|
-
await expect(plugin._findValidImages(imageSize2, user)).rejects.toEqual({
|
|
449
|
-
title: 'imageSize2',
|
|
450
|
-
detail: 'The file size for \'lion2.jpg\' is too big.'
|
|
451
|
-
})
|
|
452
|
-
res.json()
|
|
453
|
-
})
|
|
368
|
+
app.post('/', async function(req, res) {
|
|
369
|
+
try {
|
|
370
|
+
let options = { files: req.files, model: user, query: { _id: user1._id }}
|
|
371
|
+
let response = await plugin.removeImages(options, req.body, true)
|
|
372
|
+
expect(response[0]).toEqual({})
|
|
373
|
+
expect(response[1]).toEqual([])
|
|
374
|
+
res.send()
|
|
375
|
+
} catch(e) {
|
|
376
|
+
console.log(e.message || e)
|
|
377
|
+
res.status(500).send()
|
|
378
|
+
}
|
|
379
|
+
})
|
|
454
380
|
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
.attach('imageSvgAny', `${__dirname}/assets/bad.svg`)
|
|
463
|
-
.attach('imageSize1', `${__dirname}/assets/lion1.png`)
|
|
464
|
-
.attach('imageSize2', `${__dirname}/assets/lion2.jpg`)
|
|
465
|
-
.expect(200)
|
|
466
|
-
.end((err, res) => {
|
|
467
|
-
if (err) console.log(err)
|
|
468
|
-
db.close()
|
|
469
|
-
done()
|
|
470
|
-
})
|
|
471
|
-
})()
|
|
381
|
+
// Start tests
|
|
382
|
+
await supertest(app)
|
|
383
|
+
.post('/')
|
|
384
|
+
.attach('logo', `${__dirname}/assets/logo.png`)
|
|
385
|
+
.expect(200)
|
|
386
|
+
|
|
387
|
+
db.close()
|
|
472
388
|
})
|
|
473
389
|
|
|
474
|
-
test('images addImages bad file objects', (
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
|
|
481
|
-
})).db
|
|
390
|
+
test('images addImages bad file objects', async () => {
|
|
391
|
+
let db = (await opendb(null, {
|
|
392
|
+
timestamps: false,
|
|
393
|
+
serverSelectionTimeoutMS: 2000,
|
|
394
|
+
imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
|
|
395
|
+
})).db
|
|
482
396
|
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
397
|
+
db.model('user', { fields: {
|
|
398
|
+
logo: { type: 'image' },
|
|
399
|
+
logos: [{ type: 'image' }],
|
|
400
|
+
users: [{ logo: { type: 'image' } }]
|
|
401
|
+
}})
|
|
488
402
|
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
403
|
+
let imageObjectMock = {
|
|
404
|
+
bucket: 'temp',
|
|
405
|
+
date: 1234,
|
|
406
|
+
filename: 'temp.png',
|
|
407
|
+
filesize: 1234,
|
|
408
|
+
path: 'temp',
|
|
409
|
+
uid: 'temp'
|
|
410
|
+
}
|
|
411
|
+
let detailLongMock = 'Image fields need to either be null, undefined, file, or an object containing the '
|
|
412
|
+
+ 'following fields \'{ bucket, date, filename, filesize, path, uid }\''
|
|
413
|
+
|
|
414
|
+
let supertest = require('supertest')
|
|
415
|
+
let express = require('express')
|
|
416
|
+
let bodyParser = require('body-parser')
|
|
417
|
+
let app = express()
|
|
418
|
+
app.use(bodyParser.json())
|
|
419
|
+
app.use(bodyParser.urlencoded({ extended: true }))
|
|
420
|
+
|
|
421
|
+
app.post('/', async function(req, res) {
|
|
422
|
+
try {
|
|
423
|
+
await db.user.validate(req.body)
|
|
424
|
+
res.send()
|
|
425
|
+
} catch (e) {
|
|
426
|
+
res.status(500).send(e)
|
|
496
427
|
}
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
428
|
+
})
|
|
429
|
+
|
|
430
|
+
await supertest(app)
|
|
431
|
+
.post('/')
|
|
432
|
+
.send({
|
|
433
|
+
logo: null,//ok
|
|
434
|
+
logos: [undefined, imageObjectMock, {}],//0,1=ok,3=bad
|
|
435
|
+
users: [
|
|
436
|
+
{ logo: {} },//bad
|
|
437
|
+
{ logo: { bucket: '' }},//bad
|
|
438
|
+
{ logo: imageObjectMock },//ok
|
|
439
|
+
{ logo: null },//ok
|
|
440
|
+
]
|
|
441
|
+
})
|
|
442
|
+
.expect(500)
|
|
443
|
+
.then(res => {
|
|
444
|
+
// console.log(res.body)
|
|
445
|
+
expect(res.body).toEqual([
|
|
446
|
+
{
|
|
447
|
+
detail: 'Invalid image value',
|
|
448
|
+
meta: { rule: 'isImageObject', model: 'user', field: '2', detailLong: detailLongMock },
|
|
449
|
+
status: '400',
|
|
450
|
+
title: 'logos.2'
|
|
451
|
+
}, {
|
|
452
|
+
detail: 'Invalid image value',
|
|
453
|
+
meta: { rule: 'isImageObject', model: 'user', field: 'logo', detailLong: detailLongMock },
|
|
454
|
+
status: '400',
|
|
455
|
+
title: 'users.0.logo'
|
|
456
|
+
}, {
|
|
457
|
+
detail: 'Invalid image value',
|
|
458
|
+
meta: { rule: 'isImageObject', model: 'user', field: 'logo', detailLong: detailLongMock },
|
|
459
|
+
status: '400',
|
|
460
|
+
title: 'users.1.logo'
|
|
461
|
+
}
|
|
462
|
+
])
|
|
514
463
|
})
|
|
515
464
|
|
|
516
|
-
|
|
517
|
-
supertest(app)
|
|
518
|
-
.post('/')
|
|
519
|
-
.send({
|
|
520
|
-
logo: null,//ok
|
|
521
|
-
logos: [undefined, imageObjectMock, {}],//0,1=ok
|
|
522
|
-
users: [
|
|
523
|
-
{ logo: {} },
|
|
524
|
-
{ logo: { bucket: '' }},
|
|
525
|
-
{ logo: imageObjectMock },//ok
|
|
526
|
-
{ logo: null },//ok
|
|
527
|
-
]
|
|
528
|
-
})
|
|
529
|
-
.expect(500)
|
|
530
|
-
.then(res => {
|
|
531
|
-
// console.log(res.body)
|
|
532
|
-
expect(res.body).toEqual([
|
|
533
|
-
{
|
|
534
|
-
detail: 'Invalid image value',
|
|
535
|
-
meta: { rule: 'isImageObject', model: 'user', field: '2', detailLong: detailLongMock },
|
|
536
|
-
status: '400',
|
|
537
|
-
title: 'logos.2'
|
|
538
|
-
}, {
|
|
539
|
-
detail: 'Invalid image value',
|
|
540
|
-
meta: { rule: 'isImageObject', model: 'user', field: 'logo', detailLong: detailLongMock },
|
|
541
|
-
status: '400',
|
|
542
|
-
title: 'users.0.logo'
|
|
543
|
-
}, {
|
|
544
|
-
detail: 'Invalid image value',
|
|
545
|
-
meta: { rule: 'isImageObject', model: 'user', field: 'logo', detailLong: detailLongMock },
|
|
546
|
-
status: '400',
|
|
547
|
-
title: 'users.1.logo'
|
|
548
|
-
}
|
|
549
|
-
])
|
|
550
|
-
db.close()
|
|
551
|
-
done()
|
|
552
|
-
})
|
|
553
|
-
.catch(err => {
|
|
554
|
-
db.close()
|
|
555
|
-
done(err)
|
|
556
|
-
})
|
|
557
|
-
})()
|
|
465
|
+
db.close()
|
|
558
466
|
})
|
|
559
467
|
|
|
560
468
|
test('images reorder', async () => {
|
|
@@ -707,17 +615,199 @@ module.exports = function(monastery, opendb) {
|
|
|
707
615
|
db.close()
|
|
708
616
|
})
|
|
709
617
|
|
|
710
|
-
test('images
|
|
618
|
+
test('images option defaults', async () => {
|
|
619
|
+
// testing (awsAcl filesize formats getSignedUrl path params)
|
|
620
|
+
let db = (await opendb(null, {
|
|
621
|
+
timestamps: false,
|
|
622
|
+
serverSelectionTimeoutMS: 2000,
|
|
623
|
+
imagePlugin: {
|
|
624
|
+
awsBucket: 'fake',
|
|
625
|
+
awsAccessKeyId: 'fake',
|
|
626
|
+
awsSecretAccessKey: 'fake',
|
|
627
|
+
}
|
|
628
|
+
})).db
|
|
629
|
+
|
|
630
|
+
let user = db.model('user', {
|
|
631
|
+
fields: {
|
|
632
|
+
logo: { type: 'image' },
|
|
633
|
+
}
|
|
634
|
+
})
|
|
635
|
+
|
|
636
|
+
let plugin = db.imagePluginFile
|
|
637
|
+
let supertest = require('supertest')
|
|
638
|
+
let express = require('express')
|
|
639
|
+
let upload = require('express-fileupload')
|
|
640
|
+
let app = express()
|
|
641
|
+
app.use(upload({ limits: { fileSize: 1000 * 480, files: 10 }}))
|
|
642
|
+
|
|
643
|
+
// Basic tests
|
|
644
|
+
expect(plugin.awsAcl).toEqual('public-read')
|
|
645
|
+
expect(plugin.filesize).toEqual(undefined)
|
|
646
|
+
expect(plugin.formats).toEqual(['bmp', 'gif', 'jpg', 'jpeg', 'png', 'tiff'])
|
|
647
|
+
expect(plugin.getSignedUrl).toEqual(undefined)
|
|
648
|
+
expect(plugin.path).toEqual(expect.any(Function))
|
|
649
|
+
expect(plugin.params).toEqual({})
|
|
650
|
+
|
|
651
|
+
// Images not signed
|
|
652
|
+
let image
|
|
653
|
+
let userInserted = await db.user._insert({
|
|
654
|
+
logo: (image = {
|
|
655
|
+
bucket: 'fake',
|
|
656
|
+
date: 1234,
|
|
657
|
+
filename: 'lion1.png',
|
|
658
|
+
filesize: 1234,
|
|
659
|
+
path: 'test/lion1.png',
|
|
660
|
+
uid: '1234'
|
|
661
|
+
})
|
|
662
|
+
})
|
|
663
|
+
await expect(db.user.findOne({ query: userInserted._id })).resolves.toEqual({
|
|
664
|
+
_id: expect.any(Object),
|
|
665
|
+
logo: image,
|
|
666
|
+
})
|
|
667
|
+
|
|
668
|
+
app.post('/', async (req, res) => {
|
|
669
|
+
try {
|
|
670
|
+
// Files exist
|
|
671
|
+
expect(req.files.logo).toEqual(expect.any(Object))
|
|
672
|
+
let response = await plugin.addImages(
|
|
673
|
+
{ model: user, files: req.files, query: { _id: 1234 }},
|
|
674
|
+
req.body || {},
|
|
675
|
+
true,
|
|
676
|
+
)
|
|
677
|
+
// Updated data object
|
|
678
|
+
expect(response[0]).toEqual({
|
|
679
|
+
logo: {
|
|
680
|
+
bucket: 'fake',
|
|
681
|
+
date: expect.any(Number),
|
|
682
|
+
filename: 'logo.png',
|
|
683
|
+
filesize: expect.any(Number),
|
|
684
|
+
path: expect.stringMatching(/^full\/.*png$/),
|
|
685
|
+
uid: expect.any(String),
|
|
686
|
+
},
|
|
687
|
+
})
|
|
688
|
+
// S3 options
|
|
689
|
+
expect(response[1]).toEqual([
|
|
690
|
+
[{
|
|
691
|
+
ACL: 'public-read',
|
|
692
|
+
Body: expect.any(Object),
|
|
693
|
+
Bucket: 'fake',
|
|
694
|
+
Key: expect.stringMatching(/^full\/.*png$/),
|
|
695
|
+
}],
|
|
696
|
+
])
|
|
697
|
+
res.send()
|
|
698
|
+
} catch (e) {
|
|
699
|
+
console.log(e.message || e)
|
|
700
|
+
res.status(500).send()
|
|
701
|
+
}
|
|
702
|
+
})
|
|
703
|
+
|
|
704
|
+
// Start tests
|
|
705
|
+
await supertest(app)
|
|
706
|
+
.post('/')
|
|
707
|
+
.attach('logo', `${__dirname}/assets/logo.png`)
|
|
708
|
+
.expect(200)
|
|
709
|
+
|
|
710
|
+
db.close()
|
|
711
|
+
})
|
|
712
|
+
|
|
713
|
+
test('images options formats & filesizes', async () => {
|
|
714
|
+
let db = (await opendb(null, {
|
|
715
|
+
timestamps: false,
|
|
716
|
+
serverSelectionTimeoutMS: 2000,
|
|
717
|
+
imagePlugin: {
|
|
718
|
+
awsBucket: 'fake',
|
|
719
|
+
awsAccessKeyId: 'fake',
|
|
720
|
+
awsSecretAccessKey: 'fake',
|
|
721
|
+
formats: ['jpg', 'jpeg', 'png', 'ico'],
|
|
722
|
+
filesize: 1000 * 270,
|
|
723
|
+
}
|
|
724
|
+
})).db
|
|
725
|
+
|
|
726
|
+
let user = db.model('user', { fields: {
|
|
727
|
+
imageIco: { type: 'image' },
|
|
728
|
+
imageWebp: { type: 'image', formats: ['webp'] },
|
|
729
|
+
imageSvgBad: { type: 'image' },
|
|
730
|
+
imageSvgGood: { type: 'image', formats: ['svg'] },
|
|
731
|
+
imageSvgAny: { type: 'image', formats: ['any'] },
|
|
732
|
+
imageSize1: { type: 'image', filesize: 1000 * 100 },
|
|
733
|
+
imageSize2: { type: 'image' },
|
|
734
|
+
imageSize3: { type: 'image' },
|
|
735
|
+
}})
|
|
736
|
+
|
|
737
|
+
let plugin = db.imagePluginFile
|
|
738
|
+
let supertest = require('supertest')
|
|
739
|
+
let express = require('express')
|
|
740
|
+
let upload = require('express-fileupload')
|
|
741
|
+
let app = express()
|
|
742
|
+
app.use(upload({ limits: { fileSize: 1000 * 480, files: 10 }}))
|
|
743
|
+
|
|
744
|
+
app.post('/', async (req, res) => {
|
|
745
|
+
try {
|
|
746
|
+
let imageSvgBad = { imageSvgBad: req.files.imageSvgBad }
|
|
747
|
+
let imageSize1 = { imageSize1: req.files.imageSize1 }
|
|
748
|
+
let imageSize2 = { imageSize2: req.files.imageSize2 }
|
|
749
|
+
let imageSize3 = { imageSize3: req.files.imageSize3 }
|
|
750
|
+
delete req.files.imageSvgBad
|
|
751
|
+
delete req.files.imageSize1
|
|
752
|
+
delete req.files.imageSize2
|
|
753
|
+
delete req.files.imageSize3
|
|
754
|
+
// Ico, Webp, and imageSvgGood will throw an error first if it's not a valid type
|
|
755
|
+
await expect(plugin._findValidImages(req.files, user)).resolves.toEqual(expect.any(Array))
|
|
756
|
+
await expect(plugin._findValidImages(imageSvgBad, user)).rejects.toEqual({
|
|
757
|
+
title: 'imageSvgBad',
|
|
758
|
+
detail: 'The file format \'svg\' for \'bad.svg\' is not supported'
|
|
759
|
+
})
|
|
760
|
+
await expect(plugin._findValidImages(imageSize1, user)).rejects.toEqual({
|
|
761
|
+
title: 'imageSize1',
|
|
762
|
+
detail: 'The file size for \'lion1.png\' is bigger than 0.1MB.'
|
|
763
|
+
})
|
|
764
|
+
await expect(plugin._findValidImages(imageSize2, user)).rejects.toEqual({
|
|
765
|
+
title: 'imageSize2',
|
|
766
|
+
detail: 'The file size for \'lion2.jpg\' is bigger than 0.3MB.'
|
|
767
|
+
})
|
|
768
|
+
await expect(plugin._findValidImages(imageSize3, user)).rejects.toEqual({
|
|
769
|
+
title: 'imageSize3',
|
|
770
|
+
detail: 'The file size for \'house.jpg\' is too big.'
|
|
771
|
+
})
|
|
772
|
+
res.send()
|
|
773
|
+
} catch (e) {
|
|
774
|
+
console.log(e.message || e)
|
|
775
|
+
res.status(500).send()
|
|
776
|
+
}
|
|
777
|
+
})
|
|
778
|
+
|
|
779
|
+
// Start tests
|
|
780
|
+
await supertest(app)
|
|
781
|
+
.post('/')
|
|
782
|
+
.attach('imageIco', `${__dirname}/assets/image.ico`)
|
|
783
|
+
.attach('imageWebp', `${__dirname}/assets/image.webp`)
|
|
784
|
+
.attach('imageSvgBad', `${__dirname}/assets/bad.svg`)
|
|
785
|
+
.attach('imageSvgGood', `${__dirname}/assets/bad.svg`)
|
|
786
|
+
.attach('imageSvgAny', `${__dirname}/assets/bad.svg`)
|
|
787
|
+
.attach('imageSize1', `${__dirname}/assets/lion1.png`)
|
|
788
|
+
.attach('imageSize2', `${__dirname}/assets/lion2.jpg`)
|
|
789
|
+
.attach('imageSize3', `${__dirname}/assets/house.jpg`)
|
|
790
|
+
.expect(200)
|
|
791
|
+
|
|
792
|
+
db.close()
|
|
793
|
+
})
|
|
794
|
+
|
|
795
|
+
test('images option getSignedUrls', async () => {
|
|
711
796
|
// latest (2022.02)
|
|
712
797
|
let db = (await opendb(null, {
|
|
713
798
|
timestamps: false,
|
|
714
799
|
serverSelectionTimeoutMS: 2000,
|
|
715
|
-
imagePlugin: {
|
|
800
|
+
imagePlugin: {
|
|
801
|
+
awsBucket: 'fake',
|
|
802
|
+
awsAccessKeyId: 'fake',
|
|
803
|
+
awsSecretAccessKey: 'fake',
|
|
804
|
+
getSignedUrl: true,
|
|
805
|
+
},
|
|
716
806
|
})).db
|
|
717
807
|
|
|
718
808
|
db.model('user', { fields: {
|
|
719
|
-
photos: [{ type: 'image' }],
|
|
720
|
-
photos2: [{ type: 'image'
|
|
809
|
+
photos: [{ type: 'image', getSignedUrl: false }],
|
|
810
|
+
photos2: [{ type: 'image' }],
|
|
721
811
|
}})
|
|
722
812
|
|
|
723
813
|
let image = {
|
|
@@ -728,6 +818,10 @@ module.exports = function(monastery, opendb) {
|
|
|
728
818
|
path: 'test/lion1.png',
|
|
729
819
|
uid: 'lion1'
|
|
730
820
|
}
|
|
821
|
+
let imageWithSignedUrl = {
|
|
822
|
+
...image,
|
|
823
|
+
signedUrl: expect.stringMatching(/^https/)
|
|
824
|
+
}
|
|
731
825
|
|
|
732
826
|
let userInserted = await db.user._insert({
|
|
733
827
|
photos: [image, image],
|
|
@@ -735,7 +829,6 @@ module.exports = function(monastery, opendb) {
|
|
|
735
829
|
})
|
|
736
830
|
|
|
737
831
|
// Find signed URL via query option
|
|
738
|
-
let imageWithSignedUrl = { ...image, signedUrl: expect.stringMatching(/^https/) }
|
|
739
832
|
await expect(db.user.findOne({ query: userInserted._id, getSignedUrls: true })).resolves.toEqual({
|
|
740
833
|
_id: expect.any(Object),
|
|
741
834
|
photos: [imageWithSignedUrl, imageWithSignedUrl],
|
|
@@ -760,4 +853,164 @@ module.exports = function(monastery, opendb) {
|
|
|
760
853
|
db.close()
|
|
761
854
|
})
|
|
762
855
|
|
|
856
|
+
test('images options awsAcl, awsBucket, params, path', async () => {
|
|
857
|
+
let db = (await opendb(null, {
|
|
858
|
+
timestamps: false,
|
|
859
|
+
serverSelectionTimeoutMS: 2000,
|
|
860
|
+
imagePlugin: {
|
|
861
|
+
awsAcl: 'private',
|
|
862
|
+
awsBucket: 'fake',
|
|
863
|
+
awsAccessKeyId: 'fake',
|
|
864
|
+
awsSecretAccessKey: 'fake',
|
|
865
|
+
params: { ContentLanguage: 'DE'},
|
|
866
|
+
path: (uid, basename, ext, file) => `images/${basename}`,
|
|
867
|
+
}
|
|
868
|
+
})).db
|
|
869
|
+
|
|
870
|
+
let user = db.model('user', {
|
|
871
|
+
fields: {
|
|
872
|
+
optionDefaults: { type: 'image' },
|
|
873
|
+
optionOverrides: {
|
|
874
|
+
type: 'image',
|
|
875
|
+
awsAcl: 'public-read-write',
|
|
876
|
+
awsBucket: 'fake2',
|
|
877
|
+
params: { ContentLanguage: 'NZ'},
|
|
878
|
+
path: (uid, basename, ext, file) => `images2/${basename}`,
|
|
879
|
+
},
|
|
880
|
+
}
|
|
881
|
+
})
|
|
882
|
+
|
|
883
|
+
let plugin = db.imagePluginFile
|
|
884
|
+
let supertest = require('supertest')
|
|
885
|
+
let express = require('express')
|
|
886
|
+
let upload = require('express-fileupload')
|
|
887
|
+
let app = express()
|
|
888
|
+
app.use(upload())
|
|
889
|
+
|
|
890
|
+
app.post('/', async function(req, res) {
|
|
891
|
+
try {
|
|
892
|
+
// Files exist
|
|
893
|
+
expect(req.files.optionDefaults).toEqual(expect.any(Object))
|
|
894
|
+
expect(req.files.optionOverrides).toEqual(expect.any(Object))
|
|
895
|
+
let response = await plugin.addImages(
|
|
896
|
+
{ model: user, files: req.files, query: { _id: 1234 }},
|
|
897
|
+
req.body || {},
|
|
898
|
+
true,
|
|
899
|
+
)
|
|
900
|
+
// Updated data object
|
|
901
|
+
expect(response[0]).toEqual({
|
|
902
|
+
optionDefaults: {
|
|
903
|
+
bucket: 'fake',
|
|
904
|
+
date: expect.any(Number),
|
|
905
|
+
filename: 'logo.png',
|
|
906
|
+
filesize: expect.any(Number),
|
|
907
|
+
path: 'images/logo.png',
|
|
908
|
+
uid: expect.any(String),
|
|
909
|
+
},
|
|
910
|
+
optionOverrides: {
|
|
911
|
+
bucket: 'fake2',
|
|
912
|
+
date: expect.any(Number),
|
|
913
|
+
filename: 'logo2.png',
|
|
914
|
+
filesize: expect.any(Number),
|
|
915
|
+
path: 'images2/logo2.png',
|
|
916
|
+
uid: expect.any(String),
|
|
917
|
+
},
|
|
918
|
+
})
|
|
919
|
+
// S3 options
|
|
920
|
+
expect(response[1]).toEqual([
|
|
921
|
+
[{
|
|
922
|
+
ACL: 'private',
|
|
923
|
+
Body: expect.any(Object),
|
|
924
|
+
Bucket: 'fake',
|
|
925
|
+
ContentLanguage: 'DE',
|
|
926
|
+
Key: 'images/logo.png',
|
|
927
|
+
}],
|
|
928
|
+
[{
|
|
929
|
+
ACL: 'public-read-write',
|
|
930
|
+
Body: expect.any(Object),
|
|
931
|
+
Bucket: 'fake2',
|
|
932
|
+
ContentLanguage: 'NZ',
|
|
933
|
+
Key: 'images2/logo2.png',
|
|
934
|
+
}],
|
|
935
|
+
])
|
|
936
|
+
res.send()
|
|
937
|
+
} catch (e) {
|
|
938
|
+
console.log(e.message||e)
|
|
939
|
+
res.status(500).send()
|
|
940
|
+
}
|
|
941
|
+
})
|
|
942
|
+
|
|
943
|
+
// Start tests
|
|
944
|
+
await supertest(app)
|
|
945
|
+
.post('/')
|
|
946
|
+
.attach('optionDefaults', `${__dirname}/assets/logo.png`)
|
|
947
|
+
.attach('optionOverrides', `${__dirname}/assets/logo2.png`)
|
|
948
|
+
.expect(200)
|
|
949
|
+
|
|
950
|
+
db.close()
|
|
951
|
+
})
|
|
952
|
+
|
|
953
|
+
test('images option depreciations', async () => {
|
|
954
|
+
// testing (filename bucketDir)
|
|
955
|
+
let db = (await opendb(null, {
|
|
956
|
+
hideWarnings: true,
|
|
957
|
+
timestamps: false,
|
|
958
|
+
serverSelectionTimeoutMS: 2000,
|
|
959
|
+
imagePlugin: {
|
|
960
|
+
awsBucket: 'fake',
|
|
961
|
+
awsAccessKeyId: 'fake',
|
|
962
|
+
awsSecretAccessKey: 'fake',
|
|
963
|
+
bucketDir: 'old',
|
|
964
|
+
}
|
|
965
|
+
})).db
|
|
966
|
+
|
|
967
|
+
let user = db.model('user', {
|
|
968
|
+
fields: {
|
|
969
|
+
logo: { type: 'image', filename: 'oldLogo' },
|
|
970
|
+
}
|
|
971
|
+
})
|
|
972
|
+
|
|
973
|
+
let plugin = db.imagePluginFile
|
|
974
|
+
let supertest = require('supertest')
|
|
975
|
+
let express = require('express')
|
|
976
|
+
let upload = require('express-fileupload')
|
|
977
|
+
let app = express()
|
|
978
|
+
app.use(upload({ limits: { fileSize: 1000 * 480, files: 10 }}))
|
|
979
|
+
|
|
980
|
+
app.post('/', async (req, res) => {
|
|
981
|
+
try {
|
|
982
|
+
// Files exist
|
|
983
|
+
expect(req.files.logo).toEqual(expect.any(Object))
|
|
984
|
+
let response = await plugin.addImages(
|
|
985
|
+
{ model: user, files: req.files, query: { _id: 1234 }},
|
|
986
|
+
req.body || {},
|
|
987
|
+
true,
|
|
988
|
+
)
|
|
989
|
+
// Updated data object
|
|
990
|
+
expect(response[0]).toEqual({
|
|
991
|
+
logo: {
|
|
992
|
+
bucket: 'fake',
|
|
993
|
+
date: expect.any(Number),
|
|
994
|
+
filename: 'logo.png',
|
|
995
|
+
filesize: expect.any(Number),
|
|
996
|
+
path: expect.stringMatching(/^old\/.*\/oldLogo\.png$/),
|
|
997
|
+
uid: expect.any(String),
|
|
998
|
+
},
|
|
999
|
+
})
|
|
1000
|
+
res.send()
|
|
1001
|
+
} catch (e) {
|
|
1002
|
+
console.log(e.message || e)
|
|
1003
|
+
res.status(500).send()
|
|
1004
|
+
}
|
|
1005
|
+
})
|
|
1006
|
+
|
|
1007
|
+
// Start tests
|
|
1008
|
+
await supertest(app)
|
|
1009
|
+
.post('/')
|
|
1010
|
+
.attach('logo', `${__dirname}/assets/logo.png`)
|
|
1011
|
+
.expect(200)
|
|
1012
|
+
|
|
1013
|
+
db.close()
|
|
1014
|
+
})
|
|
1015
|
+
|
|
763
1016
|
}
|