monastery 1.36.2 → 1.37.1

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.
@@ -142,419 +142,327 @@ module.exports = function(monastery, opendb) {
142
142
  db.close()
143
143
  })
144
144
 
145
- test('images addImages', (done) => {
146
- (async function run () {
147
- let db = (await opendb(null, {
148
- timestamps: false,
149
- serverSelectionTimeoutMS: 2000,
150
- imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
151
- })).db
152
-
153
- let user = db.model('user', { fields: {
154
- logo: { type: 'image' },
155
- logos: [{ type: 'image' }],
156
- users: [{ logo: { type: 'image' } }]
157
- }})
158
-
159
- let plugin = db.imagePluginFile
160
- let supertest = require('supertest')
161
- let express = require('express')
162
- let upload = require('express-fileupload')
163
- let app = express()
164
- app.use(upload({ limits: { fileSize: 1 * 1000 * 1000, files: 10 }}))
165
-
166
- app.post('/', function(req, res) {
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
- .then(validFiles => {
171
- // Valid file count
172
- expect(validFiles).toEqual([
173
- expect.any(Array),
174
- expect.any(Array),
175
- expect.any(Array),
176
- expect.any(Array)
177
- ])
178
- // Valid imageField
179
- expect(validFiles[0].imageField).toEqual(expect.any(Object))
180
- expect(validFiles[1].imageField).toEqual(expect.any(Object))
181
- expect(validFiles[2].imageField).toEqual(expect.any(Object))
182
- expect(validFiles[3].imageField).toEqual(expect.any(Object))
183
- // Valid inputPath
184
- expect(validFiles[0].inputPath).toEqual('logo')
185
- expect(validFiles[1].inputPath).toEqual('logos.0')
186
- expect(validFiles[2].inputPath).toEqual('users.0.logo')
187
- expect(validFiles[3].inputPath).toEqual('users.2.logo')
188
- // Valid type
189
- expect(validFiles[0][0].format).toEqual('png')
190
- expect(validFiles[1][0].format).toEqual('png')
191
- expect(validFiles[2][0].format).toEqual('png')
192
- expect(validFiles[3][0].format).toEqual('png')
193
-
194
- return plugin.addImages({ model: user, files: req.files, query: { _id: 1234 }}, req.body, true)
195
- })
196
- .then(res => {
197
- expect(res[0]).toEqual({
198
- name: 'my awesome avatar',
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: 'logo.png',
214
+ filename: 'logo2.png',
203
215
  filesize: expect.any(Number),
204
216
  path: expect.any(String),
205
217
  uid: expect.any(String)
206
- },
207
- logos: [ expect.any(Object) ],
208
- users: [
209
- {
210
- logo: {
211
- bucket: 'fake',
212
- date: expect.any(Number),
213
- filename: 'logo2.png',
214
- filesize: expect.any(Number),
215
- path: expect.any(String),
216
- uid: expect.any(String)
217
- }
218
- },
219
- undefined, // !this will be converted to null on insert/update
220
- {
221
- logo: {
222
- bucket: 'fake',
223
- date: expect.any(Number),
224
- filename: 'logo2.png',
225
- filesize: expect.any(Number),
226
- path: expect.any(String),
227
- uid: expect.any(String)
228
- }
229
- }
230
- ]
231
- })
232
- })
233
- .finally(() => {
234
- res.json()
235
- db.close()
236
- done()
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
- // Start tests
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', (done) => {
254
- (async function run () {
255
- let db = (await opendb(null, {
256
- timestamps: false,
257
- serverSelectionTimeoutMS: 2000,
258
- imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
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
- let user = db.model('user', { fields: {
262
- logo: { type: 'image' },
263
- logos: [{ type: 'image' }],
264
- users: [{ userlogo: { type: 'image' } }],
265
- deep: { logo: { type: 'image' }}
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
- let image = {
269
- bucket: 'test',
270
- date: 1234,
271
- filename: 'test.png',
272
- filesize: 1234,
273
- path: 'test/test123'
274
- }
275
- let user1 = await db.user._insert({
276
- logo: { ...image, uid: 'test1', path: 'dir/test1.png' },
277
- logos: [
278
- { ...image, uid: 'test2', path: 'dir/test2.png' },
279
- { ...image, uid: 'test3', path: 'dir/test3.png' }
280
- ],
281
- users: [
282
- { userlogo: { ...image, uid: 'test4', path: 'dir/test4.png' }},
283
- null,
284
- { userlogo: { ...image, uid: 'test4', path: 'dir/test4.png' }},
285
- { userlogo: { ...image, uid: 'test4', path: 'dir/test4.png' }}
286
- ],
287
- deep: {}
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
- let plugin = db.imagePluginFile
291
- let supertest = require('supertest')
292
- let express = require('express')
293
- let upload = require('express-fileupload')
294
- let app = express()
295
- app.use(upload({ limits: { fileSize: 1 * 1000 * 1000, files: 10 }}))
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
- app.post('/', function(req, res) {
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
- plugin.removeImages(options, req.body, true)
303
- .then(res => {
304
- expect(res[0]).toEqual({ test1: 1, test2: 0, test3: 1, test4: 0 })
305
- expect(res[1]).toEqual([
306
- { Key: 'dir/test2.png' },
307
- { Key: 'small/test2.jpg' },
308
- { Key: 'medium/test2.jpg' },
309
- { Key: 'large/test2.jpg' },
310
-
311
- { Key: 'dir/test4.png' },
312
- { Key: 'small/test4.jpg' },
313
- { Key: 'medium/test4.jpg' },
314
- { Key: 'large/test4.jpg' }
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
- // Start tests
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', (done) => {
343
- (async function run () {
344
- // NOTE: Redundent, leaving for now (test was needed to fix a project issue)
345
- let db = (await opendb(null, {
346
- timestamps: false,
347
- serverSelectionTimeoutMS: 2000,
348
- imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
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
- let user = db.model('user', { fields: {
352
- logo: { type: 'image' }
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
- let plugin = db.imagePluginFile
367
- let supertest = require('supertest')
368
- let express = require('express')
369
- let upload = require('express-fileupload')
370
- let app = express()
371
- app.use(upload({ limits: { fileSize: 1 * 1000 * 1000, files: 10 }}))
372
-
373
- app.post('/', function(req, res) {
374
- try {
375
- let options = { files: req.files, model: user, query: { _id: user1._id }}
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
- // Start tests
393
- supertest(app)
394
- .post('/')
395
- .attach('logo', `${__dirname}/assets/logo.png`)
396
- .expect(200)
397
- .end((err, res) => { if (err) console.log(err, res.text) })
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
- test('images addImages formats & filesizes', (done) => {
402
- (async function run () {
403
- let db = (await opendb(null, {
404
- timestamps: false,
405
- serverSelectionTimeoutMS: 2000,
406
- imagePlugin: {
407
- awsBucket: 'fake',
408
- awsAccessKeyId: 'fake',
409
- awsSecretAccessKey: 'fake',
410
- formats: ['jpg', 'jpeg', 'png', 'ico']
411
- }
412
- })).db
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
- // Start tests
456
- supertest(app)
457
- .post('/')
458
- .attach('imageIco', `${__dirname}/assets/image.ico`)
459
- .attach('imageWebp', `${__dirname}/assets/image.webp`)
460
- .attach('imageSvgBad', `${__dirname}/assets/bad.svg`)
461
- .attach('imageSvgGood', `${__dirname}/assets/bad.svg`)
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', (done) => {
475
- (async function run () {
476
- //// latest supertrace setup
477
- let db = (await opendb(null, {
478
- timestamps: false,
479
- serverSelectionTimeoutMS: 2000,
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
- db.model('user', { fields: {
484
- logo: { type: 'image' },
485
- logos: [{ type: 'image' }],
486
- users: [{ logo: { type: 'image' } }]
487
- }})
397
+ db.model('user', { fields: {
398
+ logo: { type: 'image' },
399
+ logos: [{ type: 'image' }],
400
+ users: [{ logo: { type: 'image' } }]
401
+ }})
488
402
 
489
- let imageObjectMock = {
490
- bucket: 'temp',
491
- date: 1234,
492
- filename: 'temp.png',
493
- filesize: 1234,
494
- path: 'temp',
495
- uid: 'temp'
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
- let detailLongMock = 'Image fields need to either be null, undefined, file, or an object containing the '
498
- + 'following fields \'{ bucket, date, filename, filesize, path, uid }\''
499
-
500
- let supertest = require('supertest')
501
- let express = require('express')
502
- let app = express()
503
- let bodyParser = require('body-parser')
504
-
505
- app.use(bodyParser.json())
506
- app.use(bodyParser.urlencoded({ extended: true }))
507
- app.post('/', async function(req, res) {
508
- try {
509
- await db.user.validate(req.body)
510
- res.send()
511
- } catch (err) {
512
- res.status(500).send(err)
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
- // Start tests
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 getSignedUrls', async () => {
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: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
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', getSignedUrl: true }],
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
  }