monastery 2.2.3 → 3.0.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.
@@ -1,1025 +1,940 @@
1
- let util = require('../lib/util.js')
2
-
3
- module.exports = function(monastery, opendb) {
4
-
5
- // Data no images doesn't throw error
1
+ // Data no images doesn't throw error
2
+ const util = require('../lib/util.js')
3
+ const imagePluginFile = require('../plugins/images/index.js')
4
+ const monastery = require('../lib/index.js')
5
+
6
+ let db
7
+ afterAll(async () => { db.close() })
8
+ beforeAll(async () => {
9
+ db = monastery('127.0.0.1/monastery', {
10
+ timestamps: false,
11
+ imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' },
12
+ })
13
+ })
14
+
15
+ test('images no initialisation', async () => {
16
+ const db2 = monastery('127.0.0.1/monastery', { timestamps: false })
17
+ db2.model('company', {
18
+ fields: {
19
+ logo: { type: 'image' },
20
+ },
21
+ })
22
+ db2.model('user', {
23
+ fields: {
24
+ company: { model: 'company' },
25
+ },
26
+ })
27
+ let company = await db2.company.insert({ data: {
28
+ logo: { bucket: 'corex-dev', date: 1598481616 },
29
+ }})
30
+ let user = await db2.user.insert({ data: {
31
+ company: company._id,
32
+ }})
33
+ let foundCompany = await db2.company.find({
34
+ query: company._id,
35
+ })
36
+ let foundUser = await db2.user.find({
37
+ query: user._id, populate: ['company'],
38
+ })
6
39
 
7
- test('images no initialisation', async () => {
8
- let db = (await opendb(null)).db
9
- db.model('company', {
10
- fields: {
11
- logo: { type: 'image' }
12
- }
13
- })
14
- db.model('user', {
15
- fields: {
16
- company: { model: 'company' }
17
- }
18
- })
19
- let company = await db.company.insert({ data: {
20
- logo: { bucket: 'corex-dev', date: 1598481616 }
21
- }})
22
- let user = await db.user.insert({ data: {
23
- company: company._id
24
- }})
25
- let foundCompany = await db.company.find({
26
- query: company._id
27
- })
28
- let foundUser = await db.user.find({
29
- query: user._id, populate: ['company']
30
- })
40
+ // schema
41
+ expect(db2.company.fields.logo).toEqual({
42
+ image: true,
43
+ isAny: true,
44
+ type: 'any',
45
+ })
31
46
 
32
- // schema
33
- expect(db.company.fields.logo).toEqual({
34
- image: true,
35
- isAny: true,
36
- type: 'any'
37
- })
47
+ // found company
48
+ expect(foundCompany).toEqual({
49
+ _id: company._id,
50
+ logo: { bucket: 'corex-dev', date: 1598481616 },
51
+ })
38
52
 
39
- // found company
40
- expect(foundCompany).toEqual({
53
+ // found user
54
+ expect(foundUser).toEqual({
55
+ _id: user._id,
56
+ company: {
41
57
  _id: company._id,
42
58
  logo: { bucket: 'corex-dev', date: 1598481616 },
43
- })
44
-
45
- // found user
46
- expect(foundUser).toEqual({
47
- _id: user._id,
48
- company: {
49
- _id: company._id,
50
- logo: { bucket: 'corex-dev', date: 1598481616 }
51
- }
52
- })
53
-
54
- db.close()
59
+ },
55
60
  })
56
-
57
- test('images initialisation', async () => {
58
- let db = (await opendb(null, {
59
- timestamps: false,
60
- serverSelectionTimeoutMS: 2000,
61
- imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
62
- })).db
63
-
64
- let user = db.model('user', { fields: {
65
- logo: { type: 'image' },
66
- logos: [{ type: 'image' }],
67
- users: [{ logo: { type: 'image' } }]
68
- }})
69
-
70
- // Initialisation success
71
- expect(db.imagePlugin).toEqual({ awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' })
72
-
73
- let expected = {
74
- bucket: { type: 'string', isString: true },
75
- date: { type: 'number', isNumber: true },
76
- filename: { type: 'string', isString: true },
77
- filesize: { type: 'number', isNumber: true },
78
- metadata: { type: 'any', isAny: true },
79
- path: { type: 'string', isString: true },
80
- schema: {
81
- type: 'object', isObject: true, image: true, nullObject: true, default: undefined,
82
- isImageObject: true
83
- },
84
- uid: { type: 'string', isString: true }
61
+ db2.close()
62
+ })
63
+
64
+ test('images initialisation', async () => {
65
+ let user = db.model('user', { fields: {
66
+ logo: { type: 'image' },
67
+ logos: [{ type: 'image' }],
68
+ users: [{ logo: { type: 'image' } }],
69
+ }})
70
+
71
+ // Initialisation success
72
+ expect(db.opts.imagePlugin).toEqual({ awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' })
73
+
74
+ let expected = {
75
+ bucket: { type: 'string', isString: true },
76
+ date: { type: 'number', isNumber: true },
77
+ filename: { type: 'string', isString: true },
78
+ filesize: { type: 'number', isNumber: true },
79
+ metadata: { type: 'any', isAny: true },
80
+ path: { type: 'string', isString: true },
81
+ schema: {
82
+ type: 'object', isObject: true, image: true, nullObject: true, default: undefined,
83
+ isImageObject: true,
84
+ },
85
+ uid: { type: 'string', isString: true },
86
+ }
87
+
88
+ // Logo schema
89
+ expect(user.fields.logo).toEqual(expected)
90
+ expect(user.fields.logos[0]).toEqual(expected)
91
+ expect(user.fields.users[0].logo).toEqual(expected)
92
+ })
93
+
94
+ test('images addImages helper functions', async () => {
95
+ db.model('user', { fields: {
96
+ logo: { type: 'image' },
97
+ logos: [{ type: 'image' }],
98
+ users: [{ logo: { type: 'image' } }],
99
+ }})
100
+
101
+ // Adding
102
+ let image = { file: 'test' }
103
+
104
+ // lvl 1 property
105
+ expect(imagePluginFile._addImageObjectsToData('logo', {}, image))
106
+ .toEqual({ logo: image })
107
+
108
+ // lvl 1 existing property
109
+ expect(imagePluginFile._addImageObjectsToData('logo', { logo: null }, image))
110
+ .toEqual({ logo: image })
111
+
112
+ // lvl 1 array property
113
+ expect(imagePluginFile._addImageObjectsToData('logo.0', {}, image))
114
+ .toEqual({ logo: [image] })
115
+
116
+ // lvl 1 array existing property
117
+ expect(imagePluginFile._addImageObjectsToData('logo.1', { logo: [image] }, image))
118
+ .toEqual({ logo: [image, image] })
119
+
120
+ // lvl 2 property
121
+ expect(imagePluginFile._addImageObjectsToData('user.logo', {}, image))
122
+ .toEqual({ user: { logo: image }})
123
+
124
+ // lvl 2 existing property
125
+ expect(imagePluginFile._addImageObjectsToData('user.logo', { user: { logo: null }}, image))
126
+ .toEqual({ user: { logo: image }})
127
+
128
+ // lvl 2 array property
129
+ expect(imagePluginFile._addImageObjectsToData('user.1.logo', {}, image))
130
+ .toEqual({ user: [undefined, { logo: image }]})
131
+
132
+ // lvl 5 property
133
+ expect(imagePluginFile._addImageObjectsToData('user.a.b.c.logo', {}, image))
134
+ .toEqual({ user: { a: { b: { c: { logo: image }}}}})
135
+ })
136
+
137
+ test('images addImages', async () => {
138
+ let user = db.model('user', { fields: {
139
+ logo: { type: 'image' },
140
+ logos: [{ type: 'image' }],
141
+ users: [{ logo: { type: 'image' } }],
142
+ }})
143
+
144
+ let supertest = require('supertest')
145
+ let express = require('express')
146
+ let upload = require('express-fileupload')
147
+ let app = express()
148
+ app.use(upload({ limits: { fileSize: 1 * 1000 * 1000, files: 10 }}))
149
+
150
+ app.post('/', async function(req, res) {
151
+ try {
152
+ // Files exist
153
+ expect(req.files.logo).toEqual(expect.any(Object))
154
+ let validFiles = await imagePluginFile._findValidImages(req.files, user)
155
+ // Valid file count
156
+ expect(validFiles).toEqual([
157
+ expect.any(Array),
158
+ expect.any(Array),
159
+ expect.any(Array),
160
+ expect.any(Array),
161
+ ])
162
+ // Valid imageField
163
+ expect(validFiles[0].imageField).toEqual(expect.any(Object))
164
+ expect(validFiles[1].imageField).toEqual(expect.any(Object))
165
+ expect(validFiles[2].imageField).toEqual(expect.any(Object))
166
+ expect(validFiles[3].imageField).toEqual(expect.any(Object))
167
+ // Valid inputPath
168
+ expect(validFiles[0].inputPath).toEqual('logo')
169
+ expect(validFiles[1].inputPath).toEqual('logos.0')
170
+ expect(validFiles[2].inputPath).toEqual('users.0.logo')
171
+ expect(validFiles[3].inputPath).toEqual('users.2.logo')
172
+ // Valid type
173
+ expect(validFiles[0][0].format).toEqual('png')
174
+ expect(validFiles[1][0].format).toEqual('png')
175
+ expect(validFiles[2][0].format).toEqual('png')
176
+ expect(validFiles[3][0].format).toEqual('png')
177
+
178
+ let response = await imagePluginFile.addImages(
179
+ { model: user, files: req.files, query: { _id: 1234 }},
180
+ req.body,
181
+ true
182
+ )
183
+ expect(response[0]).toEqual({
184
+ name: 'my awesome avatar',
185
+ logo: {
186
+ bucket: 'fake',
187
+ date: expect.any(Number),
188
+ filename: 'logo.png',
189
+ filesize: expect.any(Number),
190
+ path: expect.any(String),
191
+ uid: expect.any(String),
192
+ },
193
+ logos: [ expect.any(Object) ],
194
+ users: [
195
+ {
196
+ logo: {
197
+ bucket: 'fake',
198
+ date: expect.any(Number),
199
+ filename: 'logo2.png',
200
+ filesize: expect.any(Number),
201
+ path: expect.any(String),
202
+ uid: expect.any(String),
203
+ },
204
+ },
205
+ undefined, // !this will be converted to null on insert/update
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
+ },
215
+ },
216
+ ],
217
+ })
218
+ res.send()
219
+ } catch (e) {
220
+ console.log(e.message || e)
221
+ res.status(500).send()
85
222
  }
86
-
87
- // Logo schema
88
- expect(user.fields.logo).toEqual(expected)
89
- expect(user.fields.logos[0]).toEqual(expected)
90
- expect(user.fields.users[0].logo).toEqual(expected)
91
-
92
- db.close()
93
223
  })
94
224
 
95
- test('images addImages helper functions', async () => {
96
- let db = (await opendb(null, {
97
- timestamps: false,
98
- serverSelectionTimeoutMS: 2000,
99
- imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
100
- })).db
101
- let plugin = db.imagePluginFile
102
- db.model('user', { fields: {
103
- logo: { type: 'image' },
104
- logos: [{ type: 'image' }],
105
- users: [{ logo: { type: 'image' } }]
106
- }})
107
-
108
- // Adding
109
- let image = { file: 'test' }
110
-
111
- // lvl 1 property
112
- expect(plugin._addImageObjectsToData('logo', {}, image))
113
- .toEqual({ logo: image })
114
-
115
- // lvl 1 existing property
116
- expect(plugin._addImageObjectsToData('logo', { logo: null }, image))
117
- .toEqual({ logo: image })
118
-
119
- // lvl 1 array property
120
- expect(plugin._addImageObjectsToData('logo.0', {}, image))
121
- .toEqual({ logo: [image] })
122
-
123
- // lvl 1 array existing property
124
- expect(plugin._addImageObjectsToData('logo.1', { logo: [image] }, image))
125
- .toEqual({ logo: [image, image] })
126
-
127
- // lvl 2 property
128
- expect(plugin._addImageObjectsToData('user.logo', {}, image))
129
- .toEqual({ user: { logo: image }})
130
-
131
- // lvl 2 existing property
132
- expect(plugin._addImageObjectsToData('user.logo', { user: { logo: null }}, image))
133
- .toEqual({ user: { logo: image }})
134
-
135
- // lvl 2 array property
136
- expect(plugin._addImageObjectsToData('user.1.logo', {}, image))
137
- .toEqual({ user: [undefined, { logo: image }]})
138
-
139
- // lvl 5 property
140
- expect(plugin._addImageObjectsToData('user.a.b.c.logo', {}, image))
141
- .toEqual({ user: { a: { b: { c: { logo: image }}}}})
142
-
143
- db.close()
225
+ // Start tests
226
+ await supertest(app)
227
+ .post('/')
228
+ .field('name', 'my awesome avatar')
229
+ .attach('logo', `${__dirname}/assets/logo.png`)
230
+ .attach('logos.0', `${__dirname}/assets/logo2.png`)
231
+ .attach('users.0.logo', `${__dirname}/assets/logo2.png`)
232
+ .attach('users.2.logo', `${__dirname}/assets/logo2.png`)
233
+ .expect(200)
234
+ })
235
+
236
+ test('images removeImages', async () => {
237
+ let user = db.model('user', { fields: {
238
+ logo: { type: 'image' },
239
+ logos: [{ type: 'image' }],
240
+ users: [{ userlogo: { type: 'image' } }],
241
+ deep: { logo: { type: 'image' }},
242
+ }})
243
+
244
+ let image = {
245
+ bucket: 'test',
246
+ date: 1234,
247
+ filename: 'test.png',
248
+ filesize: 1234,
249
+ path: 'test/test123',
250
+ }
251
+ let user1 = await db.user._insert({
252
+ logo: { ...image, uid: 'test1', path: 'dir/test1.png' },
253
+ logos: [
254
+ { ...image, uid: 'test2', path: 'dir/test2.png' },
255
+ { ...image, uid: 'test3', path: 'dir/test3.png' },
256
+ ],
257
+ users: [
258
+ { userlogo: { ...image, uid: 'test4', path: 'dir/test4.png' }},
259
+ null,
260
+ { userlogo: { ...image, uid: 'test4', path: 'dir/test4.png' }},
261
+ { userlogo: { ...image, uid: 'test4', path: 'dir/test4.png' }},
262
+ ],
263
+ deep: {},
144
264
  })
145
265
 
146
- test('images addImages', async () => {
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('/', async function(req, res) {
167
- try {
168
- // Files exist
169
- expect(req.files.logo).toEqual(expect.any(Object))
170
- let validFiles = await plugin._findValidImages(req.files, user)
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
- let response = await plugin.addImages(
195
- { model: user, files: req.files, query: { _id: 1234 }},
196
- req.body,
197
- true
198
- )
199
- expect(response[0]).toEqual({
200
- name: 'my awesome avatar',
201
- logo: {
202
- bucket: 'fake',
203
- date: expect.any(Number),
204
- filename: 'logo.png',
205
- filesize: expect.any(Number),
206
- path: expect.any(String),
207
- uid: expect.any(String)
208
- },
209
- logos: [ expect.any(Object) ],
210
- users: [
211
- {
212
- logo: {
213
- bucket: 'fake',
214
- date: expect.any(Number),
215
- filename: 'logo2.png',
216
- filesize: expect.any(Number),
217
- path: expect.any(String),
218
- uid: expect.any(String)
219
- }
220
- },
221
- undefined, // !this will be converted to null on insert/update
222
- {
223
- logo: {
224
- bucket: 'fake',
225
- date: expect.any(Number),
226
- filename: 'logo2.png',
227
- filesize: expect.any(Number),
228
- path: expect.any(String),
229
- uid: expect.any(String)
230
- }
231
- }
232
- ]
233
- })
234
- res.send()
235
- } catch (e) {
236
- console.log(e.message || e)
237
- res.status(500).send()
238
- }
239
- })
240
-
241
- // Start tests
242
- await supertest(app)
243
- .post('/')
244
- .field('name', 'my awesome avatar')
245
- .attach('logo', `${__dirname}/assets/logo.png`)
246
- .attach('logos.0', `${__dirname}/assets/logo2.png`)
247
- .attach('users.0.logo', `${__dirname}/assets/logo2.png`)
248
- .attach('users.2.logo', `${__dirname}/assets/logo2.png`)
249
- .expect(200)
250
-
251
- db.close()
266
+ let supertest = require('supertest')
267
+ let express = require('express')
268
+ let upload = require('express-fileupload')
269
+ let app = express()
270
+ app.use(upload({ limits: { fileSize: 1 * 1000 * 1000, files: 10 }}))
271
+
272
+ app.post('/', async function(req, res) {
273
+ try {
274
+ req.body.logos = JSON.parse(req.body.logos)
275
+ req.body.users = JSON.parse(req.body.users)
276
+ let options = { files: req.files, model: user, query: { _id: user1._id }}
277
+ let response = await imagePluginFile.removeImages(options, req.body, true)
278
+ expect(response[0]).toEqual({ test1: 1, test2: 0, test3: 1, test4: 0 })
279
+ expect(response[1]).toEqual([
280
+ { Key: 'dir/test2.png' },
281
+ { Key: 'small/test2.jpg' },
282
+ { Key: 'medium/test2.jpg' },
283
+ { Key: 'large/test2.jpg' },
284
+
285
+ { Key: 'dir/test4.png' },
286
+ { Key: 'small/test4.jpg' },
287
+ { Key: 'medium/test4.jpg' },
288
+ { Key: 'large/test4.jpg' },
289
+ ])
290
+ res.send()
291
+ } catch (e) {
292
+ console.log(e.message || e)
293
+ res.status(500).send()
294
+ }
252
295
  })
253
296
 
254
- test('images removeImages', async () => {
255
- let db = (await opendb(null, {
256
- timestamps: false,
257
- serverSelectionTimeoutMS: 2000,
258
- imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
259
- })).db
260
-
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
- }})
297
+ await supertest(app)
298
+ .post('/')
299
+ .field('name', 'my awesome avatar')
300
+ .field('logos', JSON.stringify([ null, { ...image, uid: 'test3', path: 'dir/test3.png' } ]))
301
+ .field('users', JSON.stringify([
302
+ { userlogo: { ...image, uid: 'test1', path: 'dir/test1.png' }},
303
+ null,
304
+ null,
305
+ //null // undefined
306
+ ]))
307
+ .attach('logo', `${__dirname}/assets/logo.png`)
308
+ .attach('logos.0', `${__dirname}/assets/logo2.png`)
309
+ .expect(200)
310
+ })
311
+
312
+ test('images removeImages with no data', async () => {
313
+ // NOTE: Redundent, leaving for now (test was needed to fix a project issue)
314
+ let user = db.model('user', { fields: {
315
+ logo: { type: 'image' },
316
+ }})
317
+
318
+ // let image = {
319
+ // bucket: 'test',
320
+ // date: 1234,
321
+ // filename: 'test.png',
322
+ // filesize: 1234,
323
+ // path: 'test/test123'
324
+ // }
325
+ let user1 = await db.user._insert({
326
+ logo: null,
327
+ })
267
328
 
268
- let image = {
269
- bucket: 'test',
270
- date: 1234,
271
- filename: 'test.png',
272
- filesize: 1234,
273
- path: 'test/test123'
329
+ let supertest = require('supertest')
330
+ let express = require('express')
331
+ let upload = require('express-fileupload')
332
+ let app = express()
333
+ app.use(upload({ limits: { fileSize: 1 * 1000 * 1000, files: 10 }}))
334
+
335
+ app.post('/', async function(req, res) {
336
+ try {
337
+ let options = { files: req.files, model: user, query: { _id: user1._id }}
338
+ let response = await imagePluginFile.removeImages(options, req.body, true)
339
+ expect(response[0]).toEqual({})
340
+ expect(response[1]).toEqual([])
341
+ res.send()
342
+ } catch(e) {
343
+ console.log(e.message || e)
344
+ res.status(500).send()
274
345
  }
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
- })
289
-
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 }}))
296
-
297
- app.post('/', async function(req, res) {
298
- try {
299
- req.body.logos = JSON.parse(req.body.logos)
300
- req.body.users = JSON.parse(req.body.users)
301
- let options = { files: req.files, model: user, query: { _id: user1._id }}
302
- let response = await plugin.removeImages(options, req.body, true)
303
- expect(response[0]).toEqual({ test1: 1, test2: 0, test3: 1, test4: 0 })
304
- expect(response[1]).toEqual([
305
- { Key: 'dir/test2.png' },
306
- { Key: 'small/test2.jpg' },
307
- { Key: 'medium/test2.jpg' },
308
- { Key: 'large/test2.jpg' },
309
-
310
- { Key: 'dir/test4.png' },
311
- { Key: 'small/test4.jpg' },
312
- { Key: 'medium/test4.jpg' },
313
- { Key: 'large/test4.jpg' }
314
- ])
315
- res.send()
316
- } catch (e) {
317
- console.log(e.message || e)
318
- res.status(500).send()
319
- }
320
- })
346
+ })
321
347
 
322
- await supertest(app)
323
- .post('/')
324
- .field('name', 'my awesome avatar')
325
- .field('logos', JSON.stringify([ null, { ...image, uid: 'test3', path: 'dir/test3.png' } ]))
326
- .field('users', JSON.stringify([
327
- { userlogo: { ...image, uid: 'test1', path: 'dir/test1.png' }},
328
- null,
329
- null,
330
- //null // undefined
331
- ]))
332
- .attach('logo', `${__dirname}/assets/logo.png`)
333
- .attach('logos.0', `${__dirname}/assets/logo2.png`)
334
- .expect(200)
335
-
336
- db.close()
348
+ // Start tests
349
+ await supertest(app)
350
+ .post('/')
351
+ .attach('logo', `${__dirname}/assets/logo.png`)
352
+ .expect(200)
353
+ })
354
+
355
+ test('images addImages bad file objects', async () => {
356
+ db.model('user', { fields: {
357
+ logo: { type: 'image' },
358
+ logos: [{ type: 'image' }],
359
+ users: [{ logo: { type: 'image' } }],
360
+ }})
361
+
362
+ let imageObjectMock = {
363
+ bucket: 'temp',
364
+ date: 1234,
365
+ filename: 'temp.png',
366
+ filesize: 1234,
367
+ path: 'temp',
368
+ uid: 'temp',
369
+ }
370
+ let detailLongMock = 'Image fields need to either be null, undefined, file, or an object containing the '
371
+ + 'following fields \'{ bucket, date, filename, filesize, path, uid }\''
372
+
373
+ let supertest = require('supertest')
374
+ let express = require('express')
375
+ let bodyParser = require('body-parser')
376
+ let app = express()
377
+ app.use(bodyParser.json())
378
+ app.use(bodyParser.urlencoded({ extended: true }))
379
+
380
+ app.post('/', async function(req, res) {
381
+ try {
382
+ await db.user.validate(req.body)
383
+ res.send()
384
+ } catch (e) {
385
+ res.status(500).send(e)
386
+ }
337
387
  })
338
388
 
339
- test('images removeImages with no data', async () => {
340
- // NOTE: Redundent, leaving for now (test was needed to fix a project issue)
341
- let db = (await opendb(null, {
342
- timestamps: false,
343
- serverSelectionTimeoutMS: 2000,
344
- imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
345
- })).db
346
-
347
- let user = db.model('user', { fields: {
348
- logo: { type: 'image' }
349
- }})
350
-
351
- // let image = {
352
- // bucket: 'test',
353
- // date: 1234,
354
- // filename: 'test.png',
355
- // filesize: 1234,
356
- // path: 'test/test123'
357
- // }
358
- let user1 = await db.user._insert({
359
- logo: null
389
+ await supertest(app)
390
+ .post('/')
391
+ .send({
392
+ logo: null,//ok
393
+ logos: [undefined, imageObjectMock, {}],//0,1=ok,3=bad
394
+ users: [
395
+ { logo: {} },//bad
396
+ { logo: { bucket: '' }},//bad
397
+ { logo: imageObjectMock },//ok
398
+ { logo: null },//ok
399
+ ],
360
400
  })
361
-
362
- let plugin = db.imagePluginFile
363
- let supertest = require('supertest')
364
- let express = require('express')
365
- let upload = require('express-fileupload')
366
- let app = express()
367
- app.use(upload({ limits: { fileSize: 1 * 1000 * 1000, files: 10 }}))
368
-
369
- app.post('/', async function(req, res) {
370
- try {
371
- let options = { files: req.files, model: user, query: { _id: user1._id }}
372
- let response = await plugin.removeImages(options, req.body, true)
373
- expect(response[0]).toEqual({})
374
- expect(response[1]).toEqual([])
375
- res.send()
376
- } catch(e) {
377
- console.log(e.message || e)
378
- res.status(500).send()
379
- }
401
+ .expect(500)
402
+ .then(res => {
403
+ // console.log(res.body)
404
+ expect(res.body).toEqual([
405
+ {
406
+ detail: 'Invalid image value',
407
+ meta: { rule: 'isImageObject', model: 'user', field: '2', detailLong: detailLongMock },
408
+ status: '400',
409
+ title: 'logos.2',
410
+ }, {
411
+ detail: 'Invalid image value',
412
+ meta: { rule: 'isImageObject', model: 'user', field: 'logo', detailLong: detailLongMock },
413
+ status: '400',
414
+ title: 'users.0.logo',
415
+ }, {
416
+ detail: 'Invalid image value',
417
+ meta: { rule: 'isImageObject', model: 'user', field: 'logo', detailLong: detailLongMock },
418
+ status: '400',
419
+ title: 'users.1.logo',
420
+ },
421
+ ])
380
422
  })
381
-
382
- // Start tests
383
- await supertest(app)
384
- .post('/')
385
- .attach('logo', `${__dirname}/assets/logo.png`)
386
- .expect(200)
387
-
388
- db.close()
423
+ })
424
+
425
+ test('images reorder', async () => {
426
+ let user = db.model('user', { fields: {
427
+ logos: [{ type: 'image' }],
428
+ }})
429
+
430
+ let image = {
431
+ bucket: 'test',
432
+ date: 1234,
433
+ filename: 'lion1.png',
434
+ filesize: 1234,
435
+ path: 'test/lion1.png',
436
+ uid: 'lion1',
437
+ }
438
+
439
+ let user1 = await db.user._insert({
440
+ logos: [image],
389
441
  })
390
442
 
391
- test('images addImages bad file objects', async () => {
392
- let db = (await opendb(null, {
393
- timestamps: false,
394
- serverSelectionTimeoutMS: 2000,
395
- imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
396
- })).db
397
-
398
- db.model('user', { fields: {
399
- logo: { type: 'image' },
400
- logos: [{ type: 'image' }],
401
- users: [{ logo: { type: 'image' } }]
402
- }})
403
-
404
- let imageObjectMock = {
405
- bucket: 'temp',
406
- date: 1234,
407
- filename: 'temp.png',
408
- filesize: 1234,
409
- path: 'temp',
410
- uid: 'temp'
443
+ let supertest = require('supertest')
444
+ let express = require('express')
445
+ let upload = require('express-fileupload')
446
+ let app = express()
447
+ app.use(upload())
448
+
449
+ // Reorder
450
+ app.post('/', async function(req, res) {
451
+ try {
452
+ req.body.logos = JSON.parse(req.body.logos)
453
+ let options = { files: req.files, model: user, query: { _id: user1._id } }
454
+ let response = await imagePluginFile.removeImages(options, req.body, true)
455
+ expect(response[0]).toEqual({ lion1: 1 })
456
+ expect(response[1]).toEqual([])
457
+ res.send()
458
+ } catch (e) {
459
+ console.log(e.message || e)
460
+ res.status(500).send()
411
461
  }
412
- let detailLongMock = 'Image fields need to either be null, undefined, file, or an object containing the '
413
- + 'following fields \'{ bucket, date, filename, filesize, path, uid }\''
414
-
415
- let supertest = require('supertest')
416
- let express = require('express')
417
- let bodyParser = require('body-parser')
418
- let app = express()
419
- app.use(bodyParser.json())
420
- app.use(bodyParser.urlencoded({ extended: true }))
421
-
422
- app.post('/', async function(req, res) {
423
- try {
424
- await db.user.validate(req.body)
425
- res.send()
426
- } catch (e) {
427
- res.status(500).send(e)
428
- }
429
- })
462
+ })
463
+ await supertest(app)
464
+ .post('/')
465
+ .field('logos', JSON.stringify([ null, image ]))
466
+ .expect(200)
467
+ })
468
+
469
+ test('images reorder and added image', async () => {
470
+ // latest (2022.02)
471
+ let user = db.model('user', { fields: {
472
+ photos: [{ type: 'image' }],
473
+ }})
474
+
475
+ let image = {
476
+ bucket: 'test',
477
+ date: 1234,
478
+ filename: 'lion1.png',
479
+ filesize: 1234,
480
+ path: 'test/lion1.png',
481
+ uid: 'lion1',
482
+ }
483
+
484
+ let user1 = await db.user._insert({
485
+ photos: [image],
486
+ })
430
487
 
431
- await supertest(app)
432
- .post('/')
433
- .send({
434
- logo: null,//ok
435
- logos: [undefined, imageObjectMock, {}],//0,1=ok,3=bad
436
- users: [
437
- { logo: {} },//bad
438
- { logo: { bucket: '' }},//bad
439
- { logo: imageObjectMock },//ok
440
- { logo: null },//ok
441
- ]
442
- })
443
- .expect(500)
444
- .then(res => {
445
- // console.log(res.body)
446
- expect(res.body).toEqual([
447
- {
448
- detail: 'Invalid image value',
449
- meta: { rule: 'isImageObject', model: 'user', field: '2', detailLong: detailLongMock },
450
- status: '400',
451
- title: 'logos.2'
452
- }, {
453
- detail: 'Invalid image value',
454
- meta: { rule: 'isImageObject', model: 'user', field: 'logo', detailLong: detailLongMock },
455
- status: '400',
456
- title: 'users.0.logo'
457
- }, {
458
- detail: 'Invalid image value',
459
- meta: { rule: 'isImageObject', model: 'user', field: 'logo', detailLong: detailLongMock },
460
- status: '400',
461
- title: 'users.1.logo'
462
- }
463
- ])
488
+ let supertest = require('supertest')
489
+ let express = require('express')
490
+ let upload = require('express-fileupload')
491
+ let app = express()
492
+ app.use(upload())
493
+
494
+ app.post('/', async function(req, res) {
495
+ try {
496
+ // Parse and validate data which is used before in update/insert
497
+ let options = { files: req.files, model: user, query: { _id: user1._id } }
498
+ let data = await util.parseData(req.body)
499
+ data = await user.validate(data, { ...options, update: true })
500
+
501
+ // Empty photo placeholder not removed in validate?
502
+ expect(data.photos[0]).toEqual(null)
503
+ expect(data.photos[1]).toEqual(image)
504
+
505
+ // Remove images
506
+ let response = await imagePluginFile.removeImages(options, data, true)
507
+ expect(response[0]).toEqual({ lion1: 1 }) // useCount
508
+ expect(response[1]).toEqual([]) // unused
509
+
510
+ // New file exists
511
+ let validFiles = await imagePluginFile._findValidImages(req.files, user)
512
+ expect(((validFiles||[])[0]||{}).inputPath).toEqual('photos.0') // Valid inputPath
513
+
514
+ // Add images
515
+ response = await imagePluginFile.addImages(options, data, true)
516
+ expect(response[0]).toEqual({
517
+ photos: [{
518
+ bucket: 'fake',
519
+ date: expect.any(Number),
520
+ filename: 'lion2.jpg',
521
+ filesize: expect.any(Number),
522
+ path: expect.any(String),
523
+ uid: expect.any(String),
524
+ }, {
525
+ bucket: 'test', // still the same image-object reference (nothing new)
526
+ date: expect.any(Number),
527
+ filename: 'lion1.png',
528
+ filesize: expect.any(Number),
529
+ path: expect.any(String),
530
+ uid: expect.any(String),
531
+ }],
464
532
  })
465
533
 
466
- db.close()
534
+ res.send()
535
+ } catch (e) {
536
+ console.log(e.message || e)
537
+ res.status(500).send()
538
+ }
467
539
  })
468
540
 
469
- test('images reorder', async () => {
470
- let db = (await opendb(null, {
471
- timestamps: false,
472
- serverSelectionTimeoutMS: 2000,
473
- imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
474
- })).db
475
-
476
- let user = db.model('user', { fields: {
477
- logos: [{ type: 'image' }],
478
- }})
541
+ await supertest(app)
542
+ .post('/')
543
+ // Mock multipart/form-data syntax which is not supported by supertest (formdata sent with axios)
544
+ // E.g.
545
+ // req.body = 'photos[1][bucket]' : '...'
546
+ // req.files = 'photos[0]' : { ...binary }
547
+ .field('photos[1][bucket]', image.bucket)
548
+ .field('photos[1][date]', image.date)
549
+ .field('photos[1][filename]', image.filename)
550
+ .field('photos[1][filesize]', image.filesize)
551
+ .field('photos[1][path]', image.path)
552
+ .field('photos[1][uid]', image.uid)
553
+ .attach('photos[0]', `${__dirname}/assets/lion2.jpg`)
554
+ .expect(200)
555
+ })
556
+
557
+ test('images option defaults', async () => {
558
+ // testing (awsAcl filesize formats getSignedUrl path params)
559
+ let user = db.model('user', {
560
+ fields: {
561
+ logo: { type: 'image' },
562
+ },
563
+ })
479
564
 
480
- let image = {
481
- bucket: 'test',
565
+ let supertest = require('supertest')
566
+ let express = require('express')
567
+ let upload = require('express-fileupload')
568
+ let app = express()
569
+ app.use(upload({ limits: { fileSize: 1000 * 480, files: 10 }}))
570
+
571
+ // Basic tests
572
+ expect(imagePluginFile.awsAcl).toEqual('public-read')
573
+ expect(imagePluginFile.filesize).toEqual(undefined)
574
+ expect(imagePluginFile.formats).toEqual(['bmp', 'gif', 'jpg', 'jpeg', 'png', 'tiff'])
575
+ expect(imagePluginFile.getSignedUrl).toEqual(undefined)
576
+ expect(imagePluginFile.metadata).toEqual(undefined)
577
+ expect(imagePluginFile.path).toEqual(expect.any(Function))
578
+ expect(imagePluginFile.params).toEqual({})
579
+
580
+ // Images not signed
581
+ let image
582
+ let userInserted = await db.user._insert({
583
+ logo: (image = {
584
+ bucket: 'fake',
482
585
  date: 1234,
483
586
  filename: 'lion1.png',
484
587
  filesize: 1234,
485
588
  path: 'test/lion1.png',
486
- uid: 'lion1'
487
- }
488
-
489
- let user1 = await db.user._insert({
490
- logos: [image],
491
- })
492
-
493
- let plugin = db.imagePluginFile
494
- let supertest = require('supertest')
495
- let express = require('express')
496
- let upload = require('express-fileupload')
497
- let app = express()
498
- app.use(upload())
499
-
500
- // Reorder
501
- app.post('/', async function(req, res) {
502
- try {
503
- req.body.logos = JSON.parse(req.body.logos)
504
- let options = { files: req.files, model: user, query: { _id: user1._id } }
505
- let response = await plugin.removeImages(options, req.body, true)
506
- expect(response[0]).toEqual({ lion1: 1 })
507
- expect(response[1]).toEqual([])
508
- res.send()
509
- } catch (e) {
510
- console.log(e.message || e)
511
- res.status(500).send()
512
- }
513
- })
514
- await supertest(app)
515
- .post('/')
516
- .field('logos', JSON.stringify([ null, image ]))
517
- .expect(200)
518
-
519
- db.close()
589
+ uid: '1234',
590
+ }),
591
+ })
592
+ await expect(db.user.findOne({ query: userInserted._id })).resolves.toEqual({
593
+ _id: expect.any(Object),
594
+ logo: image,
520
595
  })
521
596
 
522
- test('images reorder and added image', async () => {
523
- // latest (2022.02)
524
- let db = (await opendb(null, {
525
- timestamps: false,
526
- serverSelectionTimeoutMS: 2000,
527
- imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
528
- })).db
529
-
530
- let user = db.model('user', { fields: {
531
- photos: [{ type: 'image' }],
532
- }})
533
-
534
- let image = {
535
- bucket: 'test',
536
- date: 1234,
537
- filename: 'lion1.png',
538
- filesize: 1234,
539
- path: 'test/lion1.png',
540
- uid: 'lion1'
597
+ app.post('/', async (req, res) => {
598
+ try {
599
+ // Files exist
600
+ expect(req.files.logo).toEqual(expect.any(Object))
601
+ let response = await imagePluginFile.addImages(
602
+ { model: user, files: req.files, query: { _id: 1234 }},
603
+ req.body || {},
604
+ true
605
+ )
606
+ // Updated data object
607
+ expect(response[0]).toEqual({
608
+ logo: {
609
+ bucket: 'fake',
610
+ date: expect.any(Number),
611
+ filename: 'logo.png',
612
+ filesize: expect.any(Number),
613
+ path: expect.stringMatching(/^full\/.*png$/),
614
+ uid: expect.any(String),
615
+ },
616
+ })
617
+ // S3 options
618
+ expect(response[1]).toEqual([
619
+ [{
620
+ ACL: 'public-read',
621
+ Body: expect.any(Object),
622
+ Bucket: 'fake',
623
+ Key: expect.stringMatching(/^full\/.*png$/),
624
+ }],
625
+ ])
626
+ res.send()
627
+ } catch (e) {
628
+ console.log(e.message || e)
629
+ res.status(500).send()
541
630
  }
542
-
543
- let user1 = await db.user._insert({
544
- photos: [image],
545
- })
546
-
547
- let plugin = db.imagePluginFile
548
- let supertest = require('supertest')
549
- let express = require('express')
550
- let upload = require('express-fileupload')
551
- let app = express()
552
- app.use(upload())
553
-
554
- app.post('/', async function(req, res) {
555
- try {
556
- // Parse and validate data which is used before in update/insert
557
- let options = { files: req.files, model: user, query: { _id: user1._id } }
558
- let data = await util.parseData(req.body)
559
- data = await user.validate(data, { ...options, update: true })
560
-
561
- // Empty photo placeholder not removed in validate?
562
- expect(data.photos[0]).toEqual(null)
563
- expect(data.photos[1]).toEqual(image)
564
-
565
- // Remove images
566
- let response = await plugin.removeImages(options, data, true)
567
- expect(response[0]).toEqual({ lion1: 1 }) // useCount
568
- expect(response[1]).toEqual([]) // unused
569
-
570
- // New file exists
571
- let validFiles = await plugin._findValidImages(req.files, user)
572
- expect(((validFiles||[])[0]||{}).inputPath).toEqual('photos.0') // Valid inputPath
573
-
574
- // Add images
575
- response = await plugin.addImages(options, data, true)
576
- expect(response[0]).toEqual({
577
- photos: [{
578
- bucket: 'fake',
579
- date: expect.any(Number),
580
- filename: 'lion2.jpg',
581
- filesize: expect.any(Number),
582
- path: expect.any(String),
583
- uid: expect.any(String)
584
- }, {
585
- bucket: 'test', // still the same image-object reference (nothing new)
586
- date: expect.any(Number),
587
- filename: 'lion1.png',
588
- filesize: expect.any(Number),
589
- path: expect.any(String),
590
- uid: expect.any(String)
591
- },],
592
- })
593
-
594
- res.send()
595
- } catch (e) {
596
- console.log(e.message || e)
597
- res.status(500).send()
598
- }
599
- })
600
-
601
- await supertest(app)
602
- .post('/')
603
- // Mock multipart/form-data syntax which is not supported by supertest (formdata sent with axios)
604
- // E.g.
605
- // req.body = 'photos[1][bucket]' : '...'
606
- // req.files = 'photos[0]' : { ...binary }
607
- .field('photos[1][bucket]', image.bucket)
608
- .field('photos[1][date]', image.date)
609
- .field('photos[1][filename]', image.filename)
610
- .field('photos[1][filesize]', image.filesize)
611
- .field('photos[1][path]', image.path)
612
- .field('photos[1][uid]', image.uid)
613
- .attach('photos[0]', `${__dirname}/assets/lion2.jpg`)
614
- .expect(200)
615
-
616
- db.close()
617
631
  })
618
632
 
619
- test('images option defaults', async () => {
620
- // testing (awsAcl filesize formats getSignedUrl path params)
621
- let db = (await opendb(null, {
622
- timestamps: false,
623
- serverSelectionTimeoutMS: 2000,
624
- imagePlugin: {
625
- awsBucket: 'fake',
626
- awsAccessKeyId: 'fake',
627
- awsSecretAccessKey: 'fake',
628
- }
629
- })).db
630
-
631
- let user = db.model('user', {
632
- fields: {
633
- logo: { type: 'image' },
634
- }
635
- })
633
+ // Start tests
634
+ await supertest(app)
635
+ .post('/')
636
+ .attach('logo', `${__dirname}/assets/logo.png`)
637
+ .expect(200)
638
+ })
639
+
640
+ test('images options formats & filesizes', async () => {
641
+ const db3 = monastery('127.0.0.1/monastery', {
642
+ timestamps: false,
643
+ imagePlugin: {
644
+ awsBucket: 'fake',
645
+ awsAccessKeyId: 'fake',
646
+ awsSecretAccessKey: 'fake',
647
+ formats: ['jpg', 'jpeg', 'png', 'ico'],
648
+ filesize: 1000 * 270,
649
+ },
650
+ })
636
651
 
637
- let plugin = db.imagePluginFile
638
- let supertest = require('supertest')
639
- let express = require('express')
640
- let upload = require('express-fileupload')
641
- let app = express()
642
- app.use(upload({ limits: { fileSize: 1000 * 480, files: 10 }}))
643
-
644
- // Basic tests
645
- expect(plugin.awsAcl).toEqual('public-read')
646
- expect(plugin.filesize).toEqual(undefined)
647
- expect(plugin.formats).toEqual(['bmp', 'gif', 'jpg', 'jpeg', 'png', 'tiff'])
648
- expect(plugin.getSignedUrl).toEqual(undefined)
649
- expect(plugin.metadata).toEqual(undefined)
650
- expect(plugin.path).toEqual(expect.any(Function))
651
- expect(plugin.params).toEqual({})
652
-
653
- // Images not signed
654
- let image
655
- let userInserted = await db.user._insert({
656
- logo: (image = {
657
- bucket: 'fake',
658
- date: 1234,
659
- filename: 'lion1.png',
660
- filesize: 1234,
661
- path: 'test/lion1.png',
662
- uid: '1234'
652
+ let user = db3.model('user', { fields: {
653
+ imageIco: { type: 'image' },
654
+ imageWebp: { type: 'image', formats: ['webp'] },
655
+ imageSvgBad: { type: 'image' },
656
+ imageSvgGood: { type: 'image', formats: ['svg'] },
657
+ imageSvgAny: { type: 'image', formats: ['any'] },
658
+ imageSize1: { type: 'image', filesize: 1000 * 100 },
659
+ imageSize2: { type: 'image' },
660
+ imageSize3: { type: 'image' },
661
+ }})
662
+
663
+ let supertest = require('supertest')
664
+ let express = require('express')
665
+ let upload = require('express-fileupload')
666
+ let app = express()
667
+ app.use(upload({ limits: { fileSize: 1000 * 480, files: 10 }}))
668
+
669
+ app.post('/', async (req, res) => {
670
+ try {
671
+ let imageSvgBad = { imageSvgBad: req.files.imageSvgBad }
672
+ let imageSize1 = { imageSize1: req.files.imageSize1 }
673
+ let imageSize2 = { imageSize2: req.files.imageSize2 }
674
+ let imageSize3 = { imageSize3: req.files.imageSize3 }
675
+ delete req.files.imageSvgBad
676
+ delete req.files.imageSize1
677
+ delete req.files.imageSize2
678
+ delete req.files.imageSize3
679
+ // Ico, Webp, and imageSvgGood will throw an error first if it's not a valid type
680
+ await expect(imagePluginFile._findValidImages(req.files, user)).resolves.toEqual(expect.any(Array))
681
+ await expect(imagePluginFile._findValidImages(imageSvgBad, user)).rejects.toEqual({
682
+ title: 'imageSvgBad',
683
+ detail: 'The file format \'svg\' for \'bad.svg\' is not supported',
663
684
  })
664
- })
665
- await expect(db.user.findOne({ query: userInserted._id })).resolves.toEqual({
666
- _id: expect.any(Object),
667
- logo: image,
668
- })
685
+ await expect(imagePluginFile._findValidImages(imageSize1, user)).rejects.toEqual({
686
+ title: 'imageSize1',
687
+ detail: 'The file size for \'lion1.png\' is bigger than 0.1MB.',
688
+ })
689
+ await expect(imagePluginFile._findValidImages(imageSize2, user)).rejects.toEqual({
690
+ title: 'imageSize2',
691
+ detail: 'The file size for \'lion2.jpg\' is bigger than 0.3MB.',
692
+ })
693
+ await expect(imagePluginFile._findValidImages(imageSize3, user)).rejects.toEqual({
694
+ title: 'imageSize3',
695
+ detail: 'The file size for \'house.jpg\' is too big.',
696
+ })
697
+ res.send()
698
+ } catch (e) {
699
+ console.log(e.message || e)
700
+ res.status(500).send()
701
+ }
702
+ })
669
703
 
670
- app.post('/', async (req, res) => {
671
- try {
672
- // Files exist
673
- expect(req.files.logo).toEqual(expect.any(Object))
674
- let response = await plugin.addImages(
675
- { model: user, files: req.files, query: { _id: 1234 }},
676
- req.body || {},
677
- true,
678
- )
679
- // Updated data object
680
- expect(response[0]).toEqual({
681
- logo: {
682
- bucket: 'fake',
683
- date: expect.any(Number),
684
- filename: 'logo.png',
685
- filesize: expect.any(Number),
686
- path: expect.stringMatching(/^full\/.*png$/),
687
- uid: expect.any(String),
688
- },
689
- })
690
- // S3 options
691
- expect(response[1]).toEqual([
692
- [{
693
- ACL: 'public-read',
694
- Body: expect.any(Object),
695
- Bucket: 'fake',
696
- Key: expect.stringMatching(/^full\/.*png$/),
697
- }],
698
- ])
699
- res.send()
700
- } catch (e) {
701
- console.log(e.message || e)
702
- res.status(500).send()
703
- }
704
- })
704
+ // Start tests
705
+ await supertest(app)
706
+ .post('/')
707
+ .attach('imageIco', `${__dirname}/assets/image.ico`)
708
+ .attach('imageWebp', `${__dirname}/assets/image.webp`)
709
+ .attach('imageSvgBad', `${__dirname}/assets/bad.svg`)
710
+ .attach('imageSvgGood', `${__dirname}/assets/bad.svg`)
711
+ .attach('imageSvgAny', `${__dirname}/assets/bad.svg`)
712
+ .attach('imageSize1', `${__dirname}/assets/lion1.png`)
713
+ .attach('imageSize2', `${__dirname}/assets/lion2.jpg`)
714
+ .attach('imageSize3', `${__dirname}/assets/house.jpg`)
715
+ .expect(200)
716
+
717
+ db3.close()
718
+ })
719
+
720
+ test('images option getSignedUrls', async () => {
721
+ // latest (2022.02)
722
+ const db3 = monastery('127.0.0.1/monastery', {
723
+ timestamps: false,
724
+ imagePlugin: {
725
+ awsBucket: 'fake',
726
+ awsAccessKeyId: 'fake',
727
+ awsSecretAccessKey: 'fake',
728
+ awsRegion: 's3-ap-southeast-2',
729
+ getSignedUrl: true,
730
+ },
731
+ })
705
732
 
706
- // Start tests
707
- await supertest(app)
708
- .post('/')
709
- .attach('logo', `${__dirname}/assets/logo.png`)
710
- .expect(200)
733
+ db3.model('user', { fields: {
734
+ photos: [{ type: 'image', getSignedUrl: false }],
735
+ photos2: [{ type: 'image' }],
736
+ }})
737
+
738
+ let image = {
739
+ bucket: 'test',
740
+ date: 1234,
741
+ filename: 'lion1.png',
742
+ filesize: 1234,
743
+ path: 'test/lion1.png',
744
+ uid: 'lion1',
745
+ }
746
+ let imageWithSignedUrl = {
747
+ ...image,
748
+ signedUrl: expect.stringMatching(/^https/),
749
+ }
750
+
751
+ let userInserted = await db3.user._insert({
752
+ photos: [image, image],
753
+ photos2: [image, image],
754
+ })
711
755
 
712
- db.close()
756
+ // Find signed URL via query option
757
+ await expect(db3.user.findOne({ query: userInserted._id, getSignedUrls: true })).resolves.toEqual({
758
+ _id: expect.any(Object),
759
+ photos: [imageWithSignedUrl, imageWithSignedUrl],
760
+ photos2: [imageWithSignedUrl, imageWithSignedUrl],
713
761
  })
714
762
 
715
- test('images options formats & filesizes', async () => {
716
- let db = (await opendb(null, {
717
- timestamps: false,
718
- serverSelectionTimeoutMS: 2000,
719
- imagePlugin: {
720
- awsBucket: 'fake',
721
- awsAccessKeyId: 'fake',
722
- awsSecretAccessKey: 'fake',
723
- formats: ['jpg', 'jpeg', 'png', 'ico'],
724
- filesize: 1000 * 270,
725
- }
726
- })).db
727
-
728
- let user = db.model('user', { fields: {
729
- imageIco: { type: 'image' },
730
- imageWebp: { type: 'image', formats: ['webp'] },
731
- imageSvgBad: { type: 'image' },
732
- imageSvgGood: { type: 'image', formats: ['svg'] },
733
- imageSvgAny: { type: 'image', formats: ['any'] },
734
- imageSize1: { type: 'image', filesize: 1000 * 100 },
735
- imageSize2: { type: 'image' },
736
- imageSize3: { type: 'image' },
737
- }})
738
-
739
- let plugin = db.imagePluginFile
740
- let supertest = require('supertest')
741
- let express = require('express')
742
- let upload = require('express-fileupload')
743
- let app = express()
744
- app.use(upload({ limits: { fileSize: 1000 * 480, files: 10 }}))
745
-
746
- app.post('/', async (req, res) => {
747
- try {
748
- let imageSvgBad = { imageSvgBad: req.files.imageSvgBad }
749
- let imageSize1 = { imageSize1: req.files.imageSize1 }
750
- let imageSize2 = { imageSize2: req.files.imageSize2 }
751
- let imageSize3 = { imageSize3: req.files.imageSize3 }
752
- delete req.files.imageSvgBad
753
- delete req.files.imageSize1
754
- delete req.files.imageSize2
755
- delete req.files.imageSize3
756
- // Ico, Webp, and imageSvgGood will throw an error first if it's not a valid type
757
- await expect(plugin._findValidImages(req.files, user)).resolves.toEqual(expect.any(Array))
758
- await expect(plugin._findValidImages(imageSvgBad, user)).rejects.toEqual({
759
- title: 'imageSvgBad',
760
- detail: 'The file format \'svg\' for \'bad.svg\' is not supported'
761
- })
762
- await expect(plugin._findValidImages(imageSize1, user)).rejects.toEqual({
763
- title: 'imageSize1',
764
- detail: 'The file size for \'lion1.png\' is bigger than 0.1MB.'
765
- })
766
- await expect(plugin._findValidImages(imageSize2, user)).rejects.toEqual({
767
- title: 'imageSize2',
768
- detail: 'The file size for \'lion2.jpg\' is bigger than 0.3MB.'
769
- })
770
- await expect(plugin._findValidImages(imageSize3, user)).rejects.toEqual({
771
- title: 'imageSize3',
772
- detail: 'The file size for \'house.jpg\' is too big.'
773
- })
774
- res.send()
775
- } catch (e) {
776
- console.log(e.message || e)
777
- res.status(500).send()
778
- }
779
- })
763
+ // Find signed URL via schema option
764
+ await expect(db3.user.findOne({ query: userInserted._id })).resolves.toEqual({
765
+ _id: expect.any(Object),
766
+ photos: [image, image],
767
+ photos2: [imageWithSignedUrl, imageWithSignedUrl],
768
+ })
780
769
 
781
- // Start tests
782
- await supertest(app)
783
- .post('/')
784
- .attach('imageIco', `${__dirname}/assets/image.ico`)
785
- .attach('imageWebp', `${__dirname}/assets/image.webp`)
786
- .attach('imageSvgBad', `${__dirname}/assets/bad.svg`)
787
- .attach('imageSvgGood', `${__dirname}/assets/bad.svg`)
788
- .attach('imageSvgAny', `${__dirname}/assets/bad.svg`)
789
- .attach('imageSize1', `${__dirname}/assets/lion1.png`)
790
- .attach('imageSize2', `${__dirname}/assets/lion2.jpg`)
791
- .attach('imageSize3', `${__dirname}/assets/house.jpg`)
792
- .expect(200)
793
-
794
- db.close()
770
+ // Works with _processAfterFind
771
+ let rawUser = await db3.user._findOne({ _id: userInserted._id })
772
+ await expect(db3.user._processAfterFind(rawUser)).resolves.toEqual({
773
+ _id: expect.any(Object),
774
+ photos: [image, image],
775
+ photos2: [imageWithSignedUrl, imageWithSignedUrl],
776
+ })
777
+ db3.close()
778
+ })
779
+
780
+ test('images options awsAcl, awsBucket, metadata, params, path', async () => {
781
+ const db3 = monastery('127.0.0.1/monastery', {
782
+ timestamps: false,
783
+ imagePlugin: {
784
+ awsAcl: 'private',
785
+ awsBucket: 'fake',
786
+ awsAccessKeyId: 'fake',
787
+ awsSecretAccessKey: 'fake',
788
+ metadata: { small: '*x300' , medium: '*x800', large: '*x1200' },
789
+ params: { ContentLanguage: 'DE'},
790
+ path: (uid, basename, ext, file) => `images/${basename}`,
791
+ },
795
792
  })
796
793
 
797
- test('images option getSignedUrls', async () => {
798
- // latest (2022.02)
799
- let db = (await opendb(null, {
800
- timestamps: false,
801
- serverSelectionTimeoutMS: 2000,
802
- imagePlugin: {
803
- awsBucket: 'fake',
804
- awsAccessKeyId: 'fake',
805
- awsSecretAccessKey: 'fake',
806
- awsRegion: 's3-ap-southeast-2',
807
- getSignedUrl: true,
794
+ let user = db3.model('user', {
795
+ fields: {
796
+ optionDefaults: { type: 'image' },
797
+ optionOverrides: {
798
+ type: 'image',
799
+ awsAcl: 'public-read-write',
800
+ awsBucket: 'fake2',
801
+ metadata: { small: '*x100' , medium: '*x400', large: '*x800' },
802
+ params: { ContentLanguage: 'NZ'},
803
+ path: (uid, basename, ext, file) => `images2/${basename}`,
808
804
  },
809
- })).db
810
-
811
- db.model('user', { fields: {
812
- photos: [{ type: 'image', getSignedUrl: false }],
813
- photos2: [{ type: 'image' }],
814
- }})
815
-
816
- let image = {
817
- bucket: 'test',
818
- date: 1234,
819
- filename: 'lion1.png',
820
- filesize: 1234,
821
- path: 'test/lion1.png',
822
- uid: 'lion1'
823
- }
824
- let imageWithSignedUrl = {
825
- ...image,
826
- signedUrl: expect.stringMatching(/^https/)
827
- }
828
-
829
- let userInserted = await db.user._insert({
830
- photos: [image, image],
831
- photos2: [image, image],
832
- })
833
-
834
- // Find signed URL via query option
835
- await expect(db.user.findOne({ query: userInserted._id, getSignedUrls: true })).resolves.toEqual({
836
- _id: expect.any(Object),
837
- photos: [imageWithSignedUrl, imageWithSignedUrl],
838
- photos2: [imageWithSignedUrl, imageWithSignedUrl],
839
- })
840
-
841
- // Find signed URL via schema option
842
- await expect(db.user.findOne({ query: userInserted._id })).resolves.toEqual({
843
- _id: expect.any(Object),
844
- photos: [image, image],
845
- photos2: [imageWithSignedUrl, imageWithSignedUrl],
846
- })
847
-
848
- // Works with _processAfterFind
849
- let rawUser = await db.user._findOne({ _id: userInserted._id })
850
- await expect(db.user._processAfterFind(rawUser)).resolves.toEqual({
851
- _id: expect.any(Object),
852
- photos: [image, image],
853
- photos2: [imageWithSignedUrl, imageWithSignedUrl],
854
- })
855
-
856
- db.close()
805
+ },
857
806
  })
858
807
 
859
- test('images options awsAcl, awsBucket, metadata, params, path', async () => {
860
- let db = (await opendb(null, {
861
- timestamps: false,
862
- serverSelectionTimeoutMS: 2000,
863
- imagePlugin: {
864
- awsAcl: 'private',
865
- awsBucket: 'fake',
866
- awsAccessKeyId: 'fake',
867
- awsSecretAccessKey: 'fake',
868
- metadata: { small: '*x300' , medium: '*x800', large: '*x1200' },
869
- params: { ContentLanguage: 'DE'},
870
- path: (uid, basename, ext, file) => `images/${basename}`,
871
- }
872
- })).db
873
-
874
- let user = db.model('user', {
875
- fields: {
876
- optionDefaults: { type: 'image' },
808
+ let supertest = require('supertest')
809
+ let express = require('express')
810
+ let upload = require('express-fileupload')
811
+ let app = express()
812
+ app.use(upload())
813
+
814
+ app.post('/', async function(req, res) {
815
+ try {
816
+ // Files exist
817
+ expect(req.files.optionDefaults).toEqual(expect.any(Object))
818
+ expect(req.files.optionOverrides).toEqual(expect.any(Object))
819
+ let response = await imagePluginFile.addImages(
820
+ { model: user, files: req.files, query: { _id: 1234 }},
821
+ req.body || {},
822
+ true
823
+ )
824
+ // Updated data object
825
+ expect(response[0]).toEqual({
826
+ optionDefaults: {
827
+ bucket: 'fake',
828
+ date: expect.any(Number),
829
+ filename: 'logo.png',
830
+ filesize: expect.any(Number),
831
+ metadata: { small: '*x300' , medium: '*x800', large: '*x1200' },
832
+ path: 'images/logo.png',
833
+ uid: expect.any(String),
834
+ },
877
835
  optionOverrides: {
878
- type: 'image',
879
- awsAcl: 'public-read-write',
880
- awsBucket: 'fake2',
836
+ bucket: 'fake2',
837
+ date: expect.any(Number),
838
+ filename: 'logo2.png',
839
+ filesize: expect.any(Number),
881
840
  metadata: { small: '*x100' , medium: '*x400', large: '*x800' },
882
- params: { ContentLanguage: 'NZ'},
883
- path: (uid, basename, ext, file) => `images2/${basename}`,
841
+ path: 'images2/logo2.png',
842
+ uid: expect.any(String),
884
843
  },
885
- }
886
- })
887
-
888
- let plugin = db.imagePluginFile
889
- let supertest = require('supertest')
890
- let express = require('express')
891
- let upload = require('express-fileupload')
892
- let app = express()
893
- app.use(upload())
894
-
895
- app.post('/', async function(req, res) {
896
- try {
897
- // Files exist
898
- expect(req.files.optionDefaults).toEqual(expect.any(Object))
899
- expect(req.files.optionOverrides).toEqual(expect.any(Object))
900
- let response = await plugin.addImages(
901
- { model: user, files: req.files, query: { _id: 1234 }},
902
- req.body || {},
903
- true,
904
- )
905
- // Updated data object
906
- expect(response[0]).toEqual({
907
- optionDefaults: {
908
- bucket: 'fake',
909
- date: expect.any(Number),
910
- filename: 'logo.png',
911
- filesize: expect.any(Number),
912
- metadata: { small: '*x300' , medium: '*x800', large: '*x1200' },
913
- path: 'images/logo.png',
914
- uid: expect.any(String),
915
- },
916
- optionOverrides: {
917
- bucket: 'fake2',
918
- date: expect.any(Number),
919
- filename: 'logo2.png',
920
- filesize: expect.any(Number),
921
- metadata: { small: '*x100' , medium: '*x400', large: '*x800' },
922
- path: 'images2/logo2.png',
923
- uid: expect.any(String),
924
- },
925
- })
926
- // S3 options
927
- expect(response[1]).toEqual([
928
- [{
929
- ACL: 'private',
930
- Body: expect.any(Object),
931
- Bucket: 'fake',
932
- ContentLanguage: 'DE',
933
- Key: 'images/logo.png',
934
- Metadata: { small: '*x300' , medium: '*x800', large: '*x1200' },
935
- }],
936
- [{
937
- ACL: 'public-read-write',
938
- Body: expect.any(Object),
939
- Bucket: 'fake2',
940
- ContentLanguage: 'NZ',
941
- Key: 'images2/logo2.png',
942
- Metadata: { small: '*x100' , medium: '*x400', large: '*x800' },
943
- }],
944
- ])
945
- res.send()
946
- } catch (e) {
947
- console.log(e.message||e)
948
- res.status(500).send()
949
- }
950
- })
951
-
952
- // Start tests
953
- await supertest(app)
954
- .post('/')
955
- .attach('optionDefaults', `${__dirname}/assets/logo.png`)
956
- .attach('optionOverrides', `${__dirname}/assets/logo2.png`)
957
- .expect(200)
958
-
959
- db.close()
844
+ })
845
+ // S3 options
846
+ expect(response[1]).toEqual([
847
+ [{
848
+ ACL: 'private',
849
+ Body: expect.any(Object),
850
+ Bucket: 'fake',
851
+ ContentLanguage: 'DE',
852
+ Key: 'images/logo.png',
853
+ Metadata: { small: '*x300' , medium: '*x800', large: '*x1200' },
854
+ }],
855
+ [{
856
+ ACL: 'public-read-write',
857
+ Body: expect.any(Object),
858
+ Bucket: 'fake2',
859
+ ContentLanguage: 'NZ',
860
+ Key: 'images2/logo2.png',
861
+ Metadata: { small: '*x100' , medium: '*x400', large: '*x800' },
862
+ }],
863
+ ])
864
+ res.send()
865
+ } catch (e) {
866
+ console.log(e.message||e)
867
+ res.status(500).send()
868
+ }
960
869
  })
961
870
 
962
- test('images option depreciations', async () => {
963
- // testing (filename bucketDir)
964
- let db = (await opendb(null, {
965
- hideWarnings: true,
966
- timestamps: false,
967
- serverSelectionTimeoutMS: 2000,
968
- imagePlugin: {
969
- awsBucket: 'fake',
970
- awsAccessKeyId: 'fake',
971
- awsSecretAccessKey: 'fake',
972
- bucketDir: 'old',
973
- }
974
- })).db
975
-
976
- let user = db.model('user', {
977
- fields: {
978
- logo: { type: 'image', filename: 'oldLogo' },
979
- }
980
- })
981
-
982
- let plugin = db.imagePluginFile
983
- let supertest = require('supertest')
984
- let express = require('express')
985
- let upload = require('express-fileupload')
986
- let app = express()
987
- app.use(upload({ limits: { fileSize: 1000 * 480, files: 10 }}))
988
-
989
- app.post('/', async (req, res) => {
990
- try {
991
- // Files exist
992
- expect(req.files.logo).toEqual(expect.any(Object))
993
- let response = await plugin.addImages(
994
- { model: user, files: req.files, query: { _id: 1234 }},
995
- req.body || {},
996
- true,
997
- )
998
- // Updated data object
999
- expect(response[0]).toEqual({
1000
- logo: {
1001
- bucket: 'fake',
1002
- date: expect.any(Number),
1003
- filename: 'logo.png',
1004
- filesize: expect.any(Number),
1005
- path: expect.stringMatching(/^old\/.*\/oldLogo\.png$/),
1006
- uid: expect.any(String),
1007
- },
1008
- })
1009
- res.send()
1010
- } catch (e) {
1011
- console.log(e.message || e)
1012
- res.status(500).send()
1013
- }
1014
- })
871
+ // Start tests
872
+ await supertest(app)
873
+ .post('/')
874
+ .attach('optionDefaults', `${__dirname}/assets/logo.png`)
875
+ .attach('optionOverrides', `${__dirname}/assets/logo2.png`)
876
+ .expect(200)
877
+
878
+ db3.close()
879
+ })
880
+
881
+ test('images option depreciations', async () => {
882
+ // testing (filename bucketDir)
883
+ const db3 = monastery('127.0.0.1/monastery', {
884
+ hideWarnings: true,
885
+ timestamps: false,
886
+ imagePlugin: {
887
+ awsBucket: 'fake',
888
+ awsAccessKeyId: 'fake',
889
+ awsSecretAccessKey: 'fake',
890
+ bucketDir: 'old',
891
+ },
892
+ })
1015
893
 
1016
- // Start tests
1017
- await supertest(app)
1018
- .post('/')
1019
- .attach('logo', `${__dirname}/assets/logo.png`)
1020
- .expect(200)
894
+ let user = db3.model('user', {
895
+ fields: {
896
+ logo: { type: 'image', filename: 'oldLogo' },
897
+ },
898
+ })
1021
899
 
1022
- db.close()
900
+ let supertest = require('supertest')
901
+ let express = require('express')
902
+ let upload = require('express-fileupload')
903
+ let app = express()
904
+ app.use(upload({ limits: { fileSize: 1000 * 480, files: 10 }}))
905
+
906
+ app.post('/', async (req, res) => {
907
+ try {
908
+ // Files exist
909
+ expect(req.files.logo).toEqual(expect.any(Object))
910
+ let response = await imagePluginFile.addImages(
911
+ { model: user, files: req.files, query: { _id: 1234 }},
912
+ req.body || {},
913
+ true
914
+ )
915
+ // Updated data object
916
+ expect(response[0]).toEqual({
917
+ logo: {
918
+ bucket: 'fake',
919
+ date: expect.any(Number),
920
+ filename: 'logo.png',
921
+ filesize: expect.any(Number),
922
+ path: expect.stringMatching(/^old\/.*\/oldLogo\.png$/),
923
+ uid: expect.any(String),
924
+ },
925
+ })
926
+ res.send()
927
+ } catch (e) {
928
+ console.log(e.message || e)
929
+ res.status(500).send()
930
+ }
1023
931
  })
1024
932
 
1025
- }
933
+ // Start tests
934
+ await supertest(app)
935
+ .post('/')
936
+ .attach('logo', `${__dirname}/assets/logo.png`)
937
+ .expect(200)
938
+
939
+ db3.close()
940
+ })