monastery 1.28.4 → 1.30.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.
@@ -2,7 +2,7 @@ module.exports = function(monastery, opendb) {
2
2
 
3
3
  // Data no images doesn't throw error
4
4
 
5
- test('images no initialisation', async (done) => {
5
+ test('images no initialisation', async () => {
6
6
  let db = (await opendb(null)).db
7
7
  db.model('company', {
8
8
  fields: {
@@ -15,7 +15,7 @@ module.exports = function(monastery, opendb) {
15
15
  }
16
16
  })
17
17
  let company = await db.company.insert({ data: {
18
- logo: { bucket: "corex-dev", date: 1598481616 }
18
+ logo: { bucket: 'corex-dev', date: 1598481616 }
19
19
  }})
20
20
  let user = await db.user.insert({ data: {
21
21
  company: company._id
@@ -31,13 +31,13 @@ module.exports = function(monastery, opendb) {
31
31
  expect(db.company.fields.logo).toEqual({
32
32
  image: true,
33
33
  isAny: true,
34
- type: "any"
34
+ type: 'any'
35
35
  })
36
36
 
37
37
  // found company
38
38
  expect(foundCompany).toEqual({
39
39
  _id: company._id,
40
- logo: { bucket: "corex-dev", date: 1598481616 },
40
+ logo: { bucket: 'corex-dev', date: 1598481616 },
41
41
  })
42
42
 
43
43
  // found user
@@ -45,15 +45,14 @@ module.exports = function(monastery, opendb) {
45
45
  _id: user._id,
46
46
  company: {
47
47
  _id: company._id,
48
- logo: { bucket: "corex-dev", date: 1598481616 }
48
+ logo: { bucket: 'corex-dev', date: 1598481616 }
49
49
  }
50
50
  })
51
51
 
52
52
  db.close()
53
- done()
54
53
  })
55
54
 
56
- test('images initialisation', async (done) => {
55
+ test('images initialisation', async () => {
57
56
  let db = (await opendb(null, {
58
57
  timestamps: false,
59
58
  serverSelectionTimeoutMS: 2000,
@@ -75,7 +74,10 @@ module.exports = function(monastery, opendb) {
75
74
  filename: { type: 'string', isString: true },
76
75
  filesize: { type: 'number', isNumber: true },
77
76
  path: { type: 'string', isString: true },
78
- schema: { type: 'object', isObject: true, image: true, nullObject: true, default: undefined, isImageObject: true },
77
+ schema: {
78
+ type: 'object', isObject: true, image: true, nullObject: true, default: undefined,
79
+ isImageObject: true
80
+ },
79
81
  uid: { type: 'string', isString: true }
80
82
  }
81
83
 
@@ -85,17 +87,16 @@ module.exports = function(monastery, opendb) {
85
87
  expect(user.fields.users[0].logo).toEqual(expected)
86
88
 
87
89
  db.close()
88
- done()
89
90
  })
90
91
 
91
- test('images addImages helper functions', async (done) => {
92
+ test('images addImages helper functions', async () => {
92
93
  let db = (await opendb(null, {
93
94
  timestamps: false,
94
95
  serverSelectionTimeoutMS: 2000,
95
96
  imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
96
97
  })).db
97
98
  let plugin = db.imagePluginFile
98
- let user = db.model('user', { fields: {
99
+ db.model('user', { fields: {
99
100
  logo: { type: 'image' },
100
101
  logos: [{ type: 'image' }],
101
102
  users: [{ logo: { type: 'image' } }]
@@ -137,209 +138,424 @@ module.exports = function(monastery, opendb) {
137
138
  .toEqual({ user: { a: { b: { c: { logo: image }}}}})
138
139
 
139
140
  db.close()
140
- done()
141
141
  })
142
142
 
143
- test('images addImages', async (done) => {
144
- let db = (await opendb(null, {
145
- timestamps: false,
146
- serverSelectionTimeoutMS: 2000,
147
- imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
148
- })).db
149
-
150
- let user = db.model('user', { fields: {
151
- logo: { type: 'image' },
152
- logos: [{ type: 'image' }],
153
- users: [{ logo: { type: 'image' } }]
154
- }})
155
-
156
- let plugin = db.imagePluginFile
157
- let supertest = require('supertest')
158
- let express = require('express')
159
- let upload = require('express-fileupload')
160
- let app = express()
161
- app.use(upload({ limits: { fileSize: 1 * 1000 * 1000, files: 10 }}))
162
-
163
- app.post('/', function(req, res) {
164
- // Files exist
165
- expect(req.files.logo).toEqual(expect.any(Object))
166
- plugin._findValidImages(req.files, user)
167
- .then(validFiles => {
168
- // Valid file count
169
- expect(validFiles).toEqual([
170
- expect.any(Array),
171
- expect.any(Array),
172
- expect.any(Array),
173
- expect.any(Array)
174
- ])
175
- // Valid imageField
176
- expect(validFiles[0].imageField).toEqual(expect.any(Object))
177
- expect(validFiles[1].imageField).toEqual(expect.any(Object))
178
- expect(validFiles[2].imageField).toEqual(expect.any(Object))
179
- expect(validFiles[3].imageField).toEqual(expect.any(Object))
180
- // Valid inputPath
181
- expect(validFiles[0].inputPath).toEqual('logo')
182
- expect(validFiles[1].inputPath).toEqual('logos.0')
183
- expect(validFiles[2].inputPath).toEqual('users.0.logo')
184
- expect(validFiles[3].inputPath).toEqual('users.2.logo')
185
- // Valid type
186
- expect(validFiles[0][0].format).toEqual('png')
187
- expect(validFiles[1][0].format).toEqual('png')
188
- expect(validFiles[2][0].format).toEqual('png')
189
- expect(validFiles[3][0].format).toEqual('png')
190
-
191
- return plugin.addImages({ model: user, files: req.files, query: { _id: 1234 }}, req.body, true)
192
- })
193
- .then(res => {
194
- expect(res[0]).toEqual({
195
- name: 'my awesome avatar',
196
- logo: {
197
- bucket: 'fake',
198
- date: expect.any(Number),
199
- filename: 'logo.png',
200
- filesize: expect.any(Number),
201
- path: expect.any(String),
202
- uid: expect.any(String)
203
- },
204
- logos: [ expect.any(Object) ],
205
- users: [
206
- {
207
- logo: {
208
- bucket: 'fake',
209
- date: expect.any(Number),
210
- filename: 'logo2.png',
211
- filesize: expect.any(Number),
212
- path: expect.any(String),
213
- uid: expect.any(String)
214
- }
143
+ test('images addImages', (done) => {
144
+ (async function run () {
145
+ let db = (await opendb(null, {
146
+ timestamps: false,
147
+ serverSelectionTimeoutMS: 2000,
148
+ imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
149
+ })).db
150
+
151
+ let user = db.model('user', { fields: {
152
+ logo: { type: 'image' },
153
+ logos: [{ type: 'image' }],
154
+ users: [{ logo: { type: 'image' } }]
155
+ }})
156
+
157
+ let plugin = db.imagePluginFile
158
+ let supertest = require('supertest')
159
+ let express = require('express')
160
+ let upload = require('express-fileupload')
161
+ let app = express()
162
+ app.use(upload({ limits: { fileSize: 1 * 1000 * 1000, files: 10 }}))
163
+
164
+ app.post('/', function(req, res) {
165
+ // Files exist
166
+ expect(req.files.logo).toEqual(expect.any(Object))
167
+ plugin._findValidImages(req.files, user)
168
+ .then(validFiles => {
169
+ // Valid file count
170
+ expect(validFiles).toEqual([
171
+ expect.any(Array),
172
+ expect.any(Array),
173
+ expect.any(Array),
174
+ expect.any(Array)
175
+ ])
176
+ // Valid imageField
177
+ expect(validFiles[0].imageField).toEqual(expect.any(Object))
178
+ expect(validFiles[1].imageField).toEqual(expect.any(Object))
179
+ expect(validFiles[2].imageField).toEqual(expect.any(Object))
180
+ expect(validFiles[3].imageField).toEqual(expect.any(Object))
181
+ // Valid inputPath
182
+ expect(validFiles[0].inputPath).toEqual('logo')
183
+ expect(validFiles[1].inputPath).toEqual('logos.0')
184
+ expect(validFiles[2].inputPath).toEqual('users.0.logo')
185
+ expect(validFiles[3].inputPath).toEqual('users.2.logo')
186
+ // Valid type
187
+ expect(validFiles[0][0].format).toEqual('png')
188
+ expect(validFiles[1][0].format).toEqual('png')
189
+ expect(validFiles[2][0].format).toEqual('png')
190
+ expect(validFiles[3][0].format).toEqual('png')
191
+
192
+ return plugin.addImages({ model: user, files: req.files, query: { _id: 1234 }}, req.body, true)
193
+ })
194
+ .then(res => {
195
+ expect(res[0]).toEqual({
196
+ name: 'my awesome avatar',
197
+ logo: {
198
+ bucket: 'fake',
199
+ date: expect.any(Number),
200
+ filename: 'logo.png',
201
+ filesize: expect.any(Number),
202
+ path: expect.any(String),
203
+ uid: expect.any(String)
215
204
  },
216
- undefined, // !this will be converted to null on insert/update
217
- {
218
- logo: {
219
- bucket: 'fake',
220
- date: expect.any(Number),
221
- filename: 'logo2.png',
222
- filesize: expect.any(Number),
223
- path: expect.any(String),
224
- uid: expect.any(String)
205
+ logos: [ expect.any(Object) ],
206
+ users: [
207
+ {
208
+ logo: {
209
+ bucket: 'fake',
210
+ date: expect.any(Number),
211
+ filename: 'logo2.png',
212
+ filesize: expect.any(Number),
213
+ path: expect.any(String),
214
+ uid: expect.any(String)
215
+ }
216
+ },
217
+ undefined, // !this will be converted to null on insert/update
218
+ {
219
+ logo: {
220
+ bucket: 'fake',
221
+ date: expect.any(Number),
222
+ filename: 'logo2.png',
223
+ filesize: expect.any(Number),
224
+ path: expect.any(String),
225
+ uid: expect.any(String)
226
+ }
225
227
  }
226
- }
227
- ]
228
+ ]
229
+ })
228
230
  })
229
- })
230
- .finally(() => {
231
- res.json()
232
- db.close()
233
- done()
234
- })
235
- })
231
+ .finally(() => {
232
+ res.json()
233
+ db.close()
234
+ done()
235
+ })
236
+ })
236
237
 
237
- // Start tests
238
- supertest(app)
239
- .post('/')
240
- .field('name', 'my awesome avatar')
241
- .attach('logo', `${__dirname}/assets/logo.png`)
242
- .attach('logos.0', `${__dirname}/assets/logo2.png`)
243
- .attach('users.0.logo', `${__dirname}/assets/logo2.png`)
244
- .attach('users.2.logo', `${__dirname}/assets/logo2.png`)
245
- .expect(200)
246
- .end((err, res) => { if (err) console.log(err) })
238
+ // Start tests
239
+ supertest(app)
240
+ .post('/')
241
+ .field('name', 'my awesome avatar')
242
+ .attach('logo', `${__dirname}/assets/logo.png`)
243
+ .attach('logos.0', `${__dirname}/assets/logo2.png`)
244
+ .attach('users.0.logo', `${__dirname}/assets/logo2.png`)
245
+ .attach('users.2.logo', `${__dirname}/assets/logo2.png`)
246
+ .expect(200)
247
+ .end((err, res) => { if (err) console.log(err) })
248
+ })()
247
249
  })
248
250
 
249
- test('images removeImages', async (done) => {
250
- let db = (await opendb(null, {
251
- timestamps: false,
252
- serverSelectionTimeoutMS: 2000,
253
- imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
254
- })).db
251
+ test('images removeImages', (done) => {
252
+ (async function run () {
253
+ let db = (await opendb(null, {
254
+ timestamps: false,
255
+ serverSelectionTimeoutMS: 2000,
256
+ imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
257
+ })).db
258
+
259
+ let user = db.model('user', { fields: {
260
+ logo: { type: 'image' },
261
+ logos: [{ type: 'image' }],
262
+ users: [{ userlogo: { type: 'image' } }],
263
+ deep: { logo: { type: 'image' }}
264
+ }})
265
+
266
+ let image = {
267
+ bucket: 'test',
268
+ date: 1234,
269
+ filename: 'test.png',
270
+ filesize: 1234,
271
+ path: 'test/test123'
272
+ }
273
+ let user1 = await db.user._insert({
274
+ logo: { ...image, uid: 'test1', path: 'dir/test1.png' },
275
+ logos: [
276
+ { ...image, uid: 'test2', path: 'dir/test2.png' },
277
+ { ...image, uid: 'test3', path: 'dir/test3.png' }
278
+ ],
279
+ users: [
280
+ { userlogo: { ...image, uid: 'test4', path: 'dir/test4.png' }},
281
+ null,
282
+ { userlogo: { ...image, uid: 'test4', path: 'dir/test4.png' }},
283
+ { userlogo: { ...image, uid: 'test4', path: 'dir/test4.png' }}
284
+ ],
285
+ deep: {}
286
+ })
255
287
 
256
- let user = db.model('user', { fields: {
257
- logo: { type: 'image' },
258
- logos: [{ type: 'image' }],
259
- users: [{ userlogo: { type: 'image' } }],
260
- deep: { logo: { type: 'image' }}
261
- }})
288
+ let plugin = db.imagePluginFile
289
+ let supertest = require('supertest')
290
+ let express = require('express')
291
+ let upload = require('express-fileupload')
292
+ let app = express()
293
+ app.use(upload({ limits: { fileSize: 1 * 1000 * 1000, files: 10 }}))
262
294
 
263
- let image = {
264
- bucket: 'test',
265
- date: 1234,
266
- filename: 'test.png',
267
- filesize: 1234,
268
- path: 'test/test123'
269
- }
270
- let user1 = await db.user._insert({
271
- logo: { ...image, uid: 'test1', path: 'dir/test1.png' },
272
- logos: [
273
- { ...image, uid: 'test2', path: 'dir/test2.png' },
274
- { ...image, uid: 'test3', path: 'dir/test3.png' }
275
- ],
276
- users: [
277
- { userlogo: { ...image, uid: 'test4', path: 'dir/test4.png' }},
278
- null,
279
- { userlogo: { ...image, uid: 'test4', path: 'dir/test4.png' }},
280
- { userlogo: { ...image, uid: 'test4', path: 'dir/test4.png' }}
281
- ],
282
- deep: {}
283
- })
295
+ app.post('/', function(req, res) {
296
+ req.body.logos = JSON.parse(req.body.logos)
297
+ req.body.users = JSON.parse(req.body.users)
298
+ let options = { files: req.files, model: user, query: { _id: user1._id }}
284
299
 
285
- let plugin = db.imagePluginFile
286
- let supertest = require('supertest')
287
- let express = require('express')
288
- let upload = require('express-fileupload')
289
- let app = express()
290
- app.use(upload({ limits: { fileSize: 1 * 1000 * 1000, files: 10 }}))
300
+ plugin.removeImages(options, req.body, true)
301
+ .then(res => {
302
+ expect(res[0]).toEqual({ test1: 1, test2: 0, test3: 1, test4: 0 })
303
+ expect(res[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
+ })
315
+ .finally(() => {
316
+ res.json()
317
+ db.close()
318
+ done()
319
+ })
320
+ })
291
321
 
292
- app.post('/', function(req, res) {
293
- req.body.logos = JSON.parse(req.body.logos)
294
- req.body.users = JSON.parse(req.body.users)
295
- let options = { files: req.files, model: user, query: { _id: user1._id }}
322
+ // Start tests
323
+ supertest(app)
324
+ .post('/')
325
+ .field('name', 'my awesome avatar')
326
+ .field('logos', JSON.stringify([ null, { ...image, uid: 'test3', path: 'dir/test3.png' } ]))
327
+ .field('users', JSON.stringify([
328
+ { userlogo: { ...image, uid: 'test1', path: 'dir/test1.png' }},
329
+ null,
330
+ null,
331
+ //null // undefined
332
+ ]))
333
+ .attach('logo', `${__dirname}/assets/logo.png`)
334
+ .attach('logos.0', `${__dirname}/assets/logo2.png`)
335
+ .expect(200)
336
+ .end((err, res) => { if (err) console.log(err) })
337
+ })()
338
+ })
296
339
 
297
- plugin.removeImages(options, req.body, true)
298
- .then(res => {
299
- expect(res[0]).toEqual({ test1: 0, test2: -1, test3: 1, test4: 0 })
300
- expect(res[1]).toEqual([
301
- { Key: 'dir/test1.png' },
302
- { Key: 'small/test1.jpg' },
303
- { Key: 'medium/test1.jpg' },
304
- { Key: 'large/test1.jpg' },
305
-
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
- ])
340
+ test('images removeImages with no data', (done) => {
341
+ (async function run () {
342
+ // NOTE: Redundent, leaving for now (test was needed to fix a project issue)
343
+ let db = (await opendb(null, {
344
+ timestamps: false,
345
+ serverSelectionTimeoutMS: 2000,
346
+ imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
347
+ })).db
348
+
349
+ let user = db.model('user', { fields: {
350
+ logo: { type: 'image' }
351
+ }})
352
+
353
+ // let image = {
354
+ // bucket: 'test',
355
+ // date: 1234,
356
+ // filename: 'test.png',
357
+ // filesize: 1234,
358
+ // path: 'test/test123'
359
+ // }
360
+ let user1 = await db.user._insert({
361
+ logo: null
362
+ })
363
+
364
+ let plugin = db.imagePluginFile
365
+ let supertest = require('supertest')
366
+ let express = require('express')
367
+ let upload = require('express-fileupload')
368
+ let app = express()
369
+ app.use(upload({ limits: { fileSize: 1 * 1000 * 1000, files: 10 }}))
370
+
371
+ app.post('/', function(req, res) {
372
+ try {
373
+ let options = { files: req.files, model: user, query: { _id: user1._id }}
374
+ plugin.removeImages(options, req.body, true)
375
+ .then(res => {
376
+ expect(res[0]).toEqual({})
377
+ expect(res[1]).toEqual([])
378
+ })
379
+ .finally(() => {
380
+ res.json()
381
+ db.close()
382
+ done()
383
+ })
384
+ } catch(e) {
385
+ console.log(e)
386
+ res.error(e)
387
+ }
388
+ })
389
+
390
+ // Start tests
391
+ supertest(app)
392
+ .post('/')
393
+ .attach('logo', `${__dirname}/assets/logo.png`)
394
+ .expect(200)
395
+ .end((err, res) => { if (err) console.log(err, res.text) })
396
+ })()
397
+ })
398
+
399
+ test('images addImages formats & filesizes', (done) => {
400
+ (async function run () {
401
+ let db = (await opendb(null, {
402
+ timestamps: false,
403
+ serverSelectionTimeoutMS: 2000,
404
+ imagePlugin: {
405
+ awsBucket: 'fake',
406
+ awsAccessKeyId: 'fake',
407
+ awsSecretAccessKey: 'fake',
408
+ formats: ['jpg', 'jpeg', 'png', 'ico']
409
+ }
410
+ })).db
411
+
412
+ let user = db.model('user', { fields: {
413
+ imageIco: { type: 'image' },
414
+ imageWebp: { type: 'image', formats: ['webp'] },
415
+ imageSvgBad: { type: 'image' },
416
+ imageSvgGood: { type: 'image', formats: ['svg'] },
417
+ imageSvgAny: { type: 'image', formats: ['any'] },
418
+ imageSize1: { type: 'image', fileSize: 1000 * 100 },
419
+ imageSize2: { type: 'image' },
420
+ }})
421
+
422
+ let plugin = db.imagePluginFile
423
+ let supertest = require('supertest')
424
+ let express = require('express')
425
+ let upload = require('express-fileupload')
426
+ let app = express()
427
+ app.use(upload({ limits: { fileSize: 1000 * 200, files: 10 }}))
428
+
429
+ app.post('/', async (req, res) => {
430
+ let imageSvgBad = { imageSvgBad: req.files.imageSvgBad }
431
+ let imageSize1 = { imageSize1: req.files.imageSize1 }
432
+ let imageSize2 = { imageSize2: req.files.imageSize2 }
433
+ delete req.files.imageSvgBad
434
+ delete req.files.imageSize1
435
+ delete req.files.imageSize2
436
+ // Ico, Webp, and imageSvgGood will throw an error first if it's not a valid type
437
+ await expect(plugin._findValidImages(req.files, user)).resolves.toEqual(expect.any(Array))
438
+ await expect(plugin._findValidImages(imageSvgBad, user)).rejects.toEqual({
439
+ title: 'imageSvgBad',
440
+ detail: 'The file format \'svg\' for \'bad.svg\' is not supported'
441
+ })
442
+ await expect(plugin._findValidImages(imageSize1, user)).rejects.toEqual({
443
+ title: 'imageSize1',
444
+ detail: 'The file size for \'lion1.png\' is bigger than 0.1MB.'
316
445
  })
317
- .finally(() => {
318
- res.json()
446
+ await expect(plugin._findValidImages(imageSize2, user)).rejects.toEqual({
447
+ title: 'imageSize2',
448
+ detail: 'The file size for \'lion2.jpg\' is too big.'
449
+ })
450
+ res.json()
451
+ })
452
+
453
+ // Start tests
454
+ supertest(app)
455
+ .post('/')
456
+ .attach('imageIco', `${__dirname}/assets/image.ico`)
457
+ .attach('imageWebp', `${__dirname}/assets/image.webp`)
458
+ .attach('imageSvgBad', `${__dirname}/assets/bad.svg`)
459
+ .attach('imageSvgGood', `${__dirname}/assets/bad.svg`)
460
+ .attach('imageSvgAny', `${__dirname}/assets/bad.svg`)
461
+ .attach('imageSize1', `${__dirname}/assets/lion1.png`)
462
+ .attach('imageSize2', `${__dirname}/assets/lion2.jpg`)
463
+ .expect(200)
464
+ .end((err, res) => {
465
+ if (err) console.log(err)
319
466
  db.close()
320
467
  done()
321
468
  })
322
- })
469
+ })()
470
+ })
323
471
 
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) })
472
+ test('images addImages bad file objects', (done) => {
473
+ (async function run () {
474
+ //// latest supertrace setup
475
+ let db = (await opendb(null, {
476
+ timestamps: false,
477
+ serverSelectionTimeoutMS: 2000,
478
+ imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
479
+ })).db
480
+
481
+ db.model('user', { fields: {
482
+ logo: { type: 'image' },
483
+ logos: [{ type: 'image' }],
484
+ users: [{ logo: { type: 'image' } }]
485
+ }})
486
+
487
+ let imageObjectMock = {
488
+ bucket: 'temp',
489
+ date: 1234,
490
+ filename: 'temp.png',
491
+ filesize: 1234,
492
+ path: 'temp',
493
+ uid: 'temp'
494
+ }
495
+ let detailLongMock = 'Image fields need to either be null, undefined, file, or an object containing the '
496
+ + 'following fields \'{ bucket, date, filename, filesize, path, uid }\''
497
+
498
+ let supertest = require('supertest')
499
+ let express = require('express')
500
+ let app = express()
501
+ let bodyParser = require('body-parser')
502
+
503
+ app.use(bodyParser.json())
504
+ app.use(bodyParser.urlencoded({ extended: true }))
505
+ app.post('/', async function(req, res) {
506
+ try {
507
+ await db.user.validate(req.body)
508
+ res.send()
509
+ } catch (err) {
510
+ res.status(500).send(err)
511
+ }
512
+ })
513
+
514
+ // Start tests
515
+ supertest(app)
516
+ .post('/')
517
+ .send({
518
+ logo: null,//ok
519
+ logos: [undefined, imageObjectMock, {}],//0,1=ok
520
+ users: [
521
+ { logo: {} },
522
+ { logo: { bucket: '' }},
523
+ { logo: imageObjectMock },//ok
524
+ { logo: null },//ok
525
+ ]
526
+ })
527
+ .expect(500)
528
+ .then(res => {
529
+ // console.log(res.body)
530
+ expect(res.body).toEqual([
531
+ {
532
+ detail: 'Invalid image value',
533
+ meta: { rule: 'isImageObject', model: 'user', field: '2', detailLong: detailLongMock },
534
+ status: '400',
535
+ title: 'logos.2'
536
+ }, {
537
+ detail: 'Invalid image value',
538
+ meta: { rule: 'isImageObject', model: 'user', field: 'logo', detailLong: detailLongMock },
539
+ status: '400',
540
+ title: 'users.0.logo'
541
+ }, {
542
+ detail: 'Invalid image value',
543
+ meta: { rule: 'isImageObject', model: 'user', field: 'logo', detailLong: detailLongMock },
544
+ status: '400',
545
+ title: 'users.1.logo'
546
+ }
547
+ ])
548
+ db.close()
549
+ done()
550
+ })
551
+ .catch(err => {
552
+ db.close()
553
+ done(err)
554
+ })
555
+ })()
339
556
  })
340
557
 
341
- test('images removeImages with no data', async (done) => {
342
- // NOTE: Redundent, leaving for now (test was needed to fix a project issue)
558
+ test('images reorder', async () => {
343
559
  let db = (await opendb(null, {
344
560
  timestamps: false,
345
561
  serverSelectionTimeoutMS: 2000,
@@ -347,18 +563,20 @@ module.exports = function(monastery, opendb) {
347
563
  })).db
348
564
 
349
565
  let user = db.model('user', { fields: {
350
- logo: { type: 'image' }
566
+ logos: [{ type: 'image' }],
351
567
  }})
352
568
 
353
569
  let image = {
354
570
  bucket: 'test',
355
571
  date: 1234,
356
- filename: 'test.png',
572
+ filename: 'lion1.png',
357
573
  filesize: 1234,
358
- path: 'test/test123'
574
+ path: 'test/lion1.png',
575
+ uid: 'lion1'
359
576
  }
577
+
360
578
  let user1 = await db.user._insert({
361
- logo: null
579
+ logos: [image],
362
580
  })
363
581
 
364
582
  let plugin = db.imagePluginFile
@@ -366,108 +584,32 @@ module.exports = function(monastery, opendb) {
366
584
  let express = require('express')
367
585
  let upload = require('express-fileupload')
368
586
  let app = express()
369
- app.use(upload({ limits: { fileSize: 1 * 1000 * 1000, files: 10 }}))
587
+ app.use(upload())
370
588
 
371
- app.post('/', function(req, res) {
589
+ // Reorder
590
+ app.post('/', async function(req, res) {
372
591
  try {
373
- let options = { files: req.files, model: user, query: { _id: user1._id }}
374
- plugin.removeImages(options, req.body, true)
375
- .then(res => {
376
- expect(res[0]).toEqual({})
377
- expect(res[1]).toEqual([])
378
- })
379
- .finally(() => {
380
- res.json()
381
- db.close()
382
- done()
383
- })
384
- } catch(e) {
385
- console.log(e)
386
- res.error(e)
592
+ req.body.logos = JSON.parse(req.body.logos)
593
+ let options = { files: req.files, model: user, query: { _id: user1._id } }
594
+ let response = await plugin.removeImages(options, req.body, true)
595
+ expect(response[0]).toEqual({ lion1: 1 })
596
+ expect(response[1]).toEqual([])
597
+ res.send()
598
+ } catch (e) {
599
+ console.log(e.message || e)
600
+ res.status(500).send()
387
601
  }
388
602
  })
389
-
390
- // Start tests
391
- supertest(app)
603
+ await supertest(app)
392
604
  .post('/')
393
- .attach('logo', `${__dirname}/assets/logo.png`)
605
+ .field('logos', JSON.stringify([ null, image ]))
394
606
  .expect(200)
395
- .end((err, res) => { if (err) console.log(err, res.text) })
396
- })
397
-
398
- test('images addImages formats & filesizes', async (done) => {
399
- let db = (await opendb(null, {
400
- timestamps: false,
401
- serverSelectionTimeoutMS: 2000,
402
- imagePlugin: {
403
- awsBucket: 'fake',
404
- awsAccessKeyId: 'fake',
405
- awsSecretAccessKey: 'fake',
406
- formats: ['jpg', 'jpeg', 'png', 'ico']
407
- }
408
- })).db
409
-
410
- let user = db.model('user', { fields: {
411
- imageIco: { type: 'image' },
412
- imageWebp: { type: 'image', formats: ['webp'] },
413
- imageSvgBad: { type: 'image' },
414
- imageSvgGood: { type: 'image', formats: ['svg'] },
415
- imageSvgAny: { type: 'image', formats: ['any'] },
416
- imageSize1: { type: 'image', fileSize: 1000 * 100 },
417
- imageSize2: { type: 'image' },
418
- }})
419
-
420
- let plugin = db.imagePluginFile
421
- let supertest = require('supertest')
422
- let express = require('express')
423
- let upload = require('express-fileupload')
424
- let app = express()
425
- app.use(upload({ limits: { fileSize: 1000 * 200, files: 10 }}))
426
-
427
- app.post('/', async (req, res) => {
428
- let imageSvgBad = { imageSvgBad: req.files.imageSvgBad }
429
- let imageSize1 = { imageSize1: req.files.imageSize1 }
430
- let imageSize2 = { imageSize2: req.files.imageSize2 }
431
- delete req.files.imageSvgBad
432
- delete req.files.imageSize1
433
- delete req.files.imageSize2
434
- // Ico, Webp, and imageSvgGood will throw an error first if it's not a valid type
435
- await expect(plugin._findValidImages(req.files, user)).resolves.toEqual(expect.any(Array))
436
- await expect(plugin._findValidImages(imageSvgBad, user)).rejects.toEqual({
437
- title: 'imageSvgBad',
438
- detail: 'The file format \'svg\' for \'bad.svg\' is not supported'
439
- })
440
- await expect(plugin._findValidImages(imageSize1, user)).rejects.toEqual({
441
- title: 'imageSize1',
442
- detail: 'The file size for \'lion1.png\' is bigger than 0.1MB.'
443
- })
444
- await expect(plugin._findValidImages(imageSize2, user)).rejects.toEqual({
445
- title: 'imageSize2',
446
- detail: 'The file size for \'lion2.jpg\' is too big.'
447
- })
448
- res.json()
449
- })
450
607
 
451
- // Start tests
452
- supertest(app)
453
- .post('/')
454
- .attach('imageIco', `${__dirname}/assets/image.ico`)
455
- .attach('imageWebp', `${__dirname}/assets/image.webp`)
456
- .attach('imageSvgBad', `${__dirname}/assets/bad.svg`)
457
- .attach('imageSvgGood', `${__dirname}/assets/bad.svg`)
458
- .attach('imageSvgAny', `${__dirname}/assets/bad.svg`)
459
- .attach('imageSize1', `${__dirname}/assets/lion1.png`)
460
- .attach('imageSize2', `${__dirname}/assets/lion2.jpg`)
461
- .expect(200)
462
- .end((err, res) => {
463
- if (err) console.log(err)
464
- db.close()
465
- done()
466
- })
608
+ db.close()
467
609
  })
468
610
 
469
- test('images addImages bad file objects', async (done) => {
470
- //// latest supertrace setup
611
+ test('images reorder and added image', async () => {
612
+ // latest (2022.02)
471
613
  let db = (await opendb(null, {
472
614
  timestamps: false,
473
615
  serverSelectionTimeoutMS: 2000,
@@ -475,79 +617,77 @@ module.exports = function(monastery, opendb) {
475
617
  })).db
476
618
 
477
619
  let user = db.model('user', { fields: {
478
- logo: { type: 'image' },
479
620
  logos: [{ type: 'image' }],
480
- users: [{ logo: { type: 'image' } }]
481
621
  }})
482
622
 
483
- let imageObjectMock = {
484
- bucket: 'temp',
623
+ let image = {
624
+ bucket: 'test',
485
625
  date: 1234,
486
- filename: 'temp.png',
626
+ filename: 'lion1.png',
487
627
  filesize: 1234,
488
- path: 'temp',
489
- uid: 'temp'
628
+ path: 'test/lion1.png',
629
+ uid: 'lion1'
490
630
  }
491
- let detailLongMock = 'Image fields need to either be null, undefined, file, or an object containing the '
492
- + 'following fields \'{ bucket, date, filename, filesize, path, uid }\''
493
631
 
632
+ let user1 = await db.user._insert({
633
+ logos: [image],
634
+ })
635
+
636
+ let plugin = db.imagePluginFile
494
637
  let supertest = require('supertest')
495
638
  let express = require('express')
639
+ let upload = require('express-fileupload')
496
640
  let app = express()
497
- let bodyParser = require('body-parser')
641
+ app.use(upload())
498
642
 
499
- app.use(bodyParser.json())
500
- app.use(bodyParser.urlencoded({ extended: true }))
501
643
  app.post('/', async function(req, res) {
502
644
  try {
503
- let result = await db.user.validate(req.body)
645
+ req.body.logos = JSON.parse(req.body.logos)
646
+ let options = { files: req.files, model: user, query: { _id: user1._id } }
647
+
648
+ // Remove images
649
+ let response = await plugin.removeImages(options, req.body, true)
650
+ expect(response[0]).toEqual({ lion1: 1 }) // useCount
651
+ expect(response[1]).toEqual([]) // unused
652
+
653
+ // File exists
654
+ let validFiles = await plugin._findValidImages(req.files, user)
655
+ expect(((validFiles||[])[0]||{}).inputPath).toEqual('logos.0') // Valid inputPath
656
+
657
+ // Add images
658
+ response = await plugin.addImages(options, req.body, true)
659
+ expect(response[0]).toEqual({
660
+ logos: [{
661
+ bucket: 'fake',
662
+ date: expect.any(Number),
663
+ filename: 'lion2.jpg',
664
+ filesize: expect.any(Number),
665
+ path: expect.any(String),
666
+ uid: expect.any(String)
667
+ }, {
668
+ bucket: 'test', // still the same image-object reference (nothing new)
669
+ date: expect.any(Number),
670
+ filename: 'lion1.png',
671
+ filesize: expect.any(Number),
672
+ path: expect.any(String),
673
+ uid: expect.any(String)
674
+ },],
675
+ })
676
+
504
677
  res.send()
505
- } catch (err) {
506
- res.status(500).send(err)
678
+ } catch (e) {
679
+ console.log(e.message || e)
680
+ res.status(500).send()
507
681
  }
508
682
  })
509
683
 
510
- // Start tests
511
- supertest(app)
684
+ await supertest(app)
512
685
  .post('/')
513
- .send({
514
- logo: null,//ok
515
- logos: [undefined, imageObjectMock, {}],//0,1=ok
516
- users: [
517
- { logo: {} },
518
- { logo: { bucket: "" }},
519
- { logo: imageObjectMock },//ok
520
- { logo: null },//ok
521
- ]
522
- })
523
- .expect(500)
524
- .then(res => {
525
- // console.log(res.body)
526
- expect(res.body).toEqual([
527
- {
528
- detail: 'Invalid image value',
529
- meta: { rule: 'isImageObject', model: 'user', field: '2', detailLong: detailLongMock },
530
- status: '400',
531
- title: 'logos.2'
532
- }, {
533
- detail: 'Invalid image value',
534
- meta: { rule: 'isImageObject', model: 'user', field: 'logo', detailLong: detailLongMock },
535
- status: '400',
536
- title: 'users.0.logo'
537
- }, {
538
- detail: 'Invalid image value',
539
- meta: { rule: 'isImageObject', model: 'user', field: 'logo', detailLong: detailLongMock },
540
- status: '400',
541
- title: 'users.1.logo'
542
- }
543
- ])
544
- db.close()
545
- done()
546
- })
547
- .catch(err => {
548
- db.close()
549
- done(err)
550
- })
686
+ .field('logos', JSON.stringify([ null, image ]))
687
+ .attach('logos.0', `${__dirname}/assets/lion2.jpg`)
688
+ .expect(200)
689
+
690
+ db.close()
551
691
  })
552
692
 
553
693
  }