monastery 1.28.3 → 1.30.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +30 -4
- package/docs/Gemfile +8 -18
- package/docs/_config.yml +1 -4
- package/docs/errors.md +0 -0
- package/docs/image-plugin.md +0 -0
- package/docs/manager/model.md +0 -0
- package/docs/manager/models.md +0 -0
- package/docs/model/findOne.md +0 -0
- package/docs/model/index.md +0 -0
- package/docs/model/remove.md +0 -0
- package/docs/readme.md +1 -1
- package/docs/rules.md +0 -0
- package/lib/index.js +0 -1
- package/lib/model-crud.js +33 -17
- package/lib/model-validate.js +25 -25
- package/lib/model.js +23 -12
- package/lib/rules.js +26 -31
- package/lib/util.js +13 -7
- package/package.json +17 -16
- package/plugins/images/index.js +4 -4
- package/test/assets/bad.svg +0 -0
- package/test/assets/image.ico +0 -0
- package/test/assets/image.webp +0 -0
- package/test/assets/lion1.png +0 -0
- package/test/assets/lion2.jpg +0 -0
- package/test/assets/logo.png +0 -0
- package/test/assets/logo2.png +0 -0
- package/test/blacklisting.js +4 -8
- package/test/crud.js +28 -31
- package/test/model.js +114 -28
- package/test/monk.js +4 -4
- package/test/plugin-images.js +400 -390
- package/test/populate.js +15 -18
- package/test/util.js +8 -8
- package/test/validate.js +227 -108
- package/test/virtuals.js +2 -4
package/test/plugin-images.js
CHANGED
|
@@ -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 (
|
|
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:
|
|
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:
|
|
34
|
+
type: 'any'
|
|
35
35
|
})
|
|
36
36
|
|
|
37
37
|
// found company
|
|
38
38
|
expect(foundCompany).toEqual({
|
|
39
39
|
_id: company._id,
|
|
40
|
-
logo: { bucket:
|
|
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:
|
|
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 (
|
|
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: {
|
|
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 (
|
|
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
|
-
|
|
99
|
+
db.model('user', { fields: {
|
|
99
100
|
logo: { type: 'image' },
|
|
100
101
|
logos: [{ type: 'image' }],
|
|
101
102
|
users: [{ logo: { type: 'image' } }]
|
|
@@ -137,417 +138,426 @@ 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',
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
.
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
expect.
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
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
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
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
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
})
|
|
236
|
-
|
|
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) })
|
|
247
|
-
})
|
|
248
|
-
|
|
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
|
|
255
|
-
|
|
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
|
-
}})
|
|
262
|
-
|
|
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
|
-
})
|
|
284
|
-
|
|
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 }}))
|
|
291
|
-
|
|
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 }}
|
|
296
|
-
|
|
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
|
-
])
|
|
316
|
-
})
|
|
317
|
-
.finally(() => {
|
|
318
|
-
res.json()
|
|
319
|
-
db.close()
|
|
320
|
-
done()
|
|
321
|
-
})
|
|
322
|
-
})
|
|
231
|
+
.finally(() => {
|
|
232
|
+
res.json()
|
|
233
|
+
db.close()
|
|
234
|
+
done()
|
|
235
|
+
})
|
|
236
|
+
})
|
|
323
237
|
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
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) })
|
|
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
|
+
})()
|
|
339
249
|
})
|
|
340
250
|
|
|
341
|
-
test('images removeImages
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
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
|
+
})
|
|
352
287
|
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
}
|
|
360
|
-
let user1 = await db.user._insert({
|
|
361
|
-
logo: null
|
|
362
|
-
})
|
|
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 }}))
|
|
363
294
|
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
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 {
|
|
295
|
+
app.post('/', function(req, res) {
|
|
296
|
+
req.body.logos = JSON.parse(req.body.logos)
|
|
297
|
+
req.body.users = JSON.parse(req.body.users)
|
|
373
298
|
let options = { files: req.files, model: user, query: { _id: user1._id }}
|
|
299
|
+
|
|
374
300
|
plugin.removeImages(options, req.body, true)
|
|
375
301
|
.then(res => {
|
|
376
|
-
expect(res[0]).toEqual({})
|
|
377
|
-
expect(res[1]).toEqual([
|
|
302
|
+
expect(res[0]).toEqual({ test1: 0, test2: -1, test3: 1, test4: 0 })
|
|
303
|
+
expect(res[1]).toEqual([
|
|
304
|
+
{ Key: 'dir/test1.png' },
|
|
305
|
+
{ Key: 'small/test1.jpg' },
|
|
306
|
+
{ Key: 'medium/test1.jpg' },
|
|
307
|
+
{ Key: 'large/test1.jpg' },
|
|
308
|
+
|
|
309
|
+
{ Key: 'dir/test2.png' },
|
|
310
|
+
{ Key: 'small/test2.jpg' },
|
|
311
|
+
{ Key: 'medium/test2.jpg' },
|
|
312
|
+
{ Key: 'large/test2.jpg' },
|
|
313
|
+
|
|
314
|
+
{ Key: 'dir/test4.png' },
|
|
315
|
+
{ Key: 'small/test4.jpg' },
|
|
316
|
+
{ Key: 'medium/test4.jpg' },
|
|
317
|
+
{ Key: 'large/test4.jpg' }
|
|
318
|
+
])
|
|
378
319
|
})
|
|
379
320
|
.finally(() => {
|
|
380
321
|
res.json()
|
|
381
322
|
db.close()
|
|
382
323
|
done()
|
|
383
324
|
})
|
|
384
|
-
}
|
|
385
|
-
console.log(e)
|
|
386
|
-
res.error(e)
|
|
387
|
-
}
|
|
388
|
-
})
|
|
325
|
+
})
|
|
389
326
|
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
327
|
+
// Start tests
|
|
328
|
+
supertest(app)
|
|
329
|
+
.post('/')
|
|
330
|
+
.field('name', 'my awesome avatar')
|
|
331
|
+
.field('logos', JSON.stringify([ null, { ...image, uid: 'test3', path: 'dir/test3.png' } ]))
|
|
332
|
+
.field('users', JSON.stringify([
|
|
333
|
+
{ userlogo: { ...image, uid: 'test1', path: 'dir/test1.png' }},
|
|
334
|
+
null,
|
|
335
|
+
null,
|
|
336
|
+
//null // undefined
|
|
337
|
+
]))
|
|
338
|
+
.attach('logo', `${__dirname}/assets/logo.png`)
|
|
339
|
+
.attach('logos.0', `${__dirname}/assets/logo2.png`)
|
|
340
|
+
.expect(200)
|
|
341
|
+
.end((err, res) => { if (err) console.log(err) })
|
|
342
|
+
})()
|
|
396
343
|
})
|
|
397
344
|
|
|
398
|
-
test('images
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
awsAccessKeyId: 'fake',
|
|
405
|
-
|
|
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
|
-
}})
|
|
345
|
+
test('images removeImages with no data', (done) => {
|
|
346
|
+
(async function run () {
|
|
347
|
+
// NOTE: Redundent, leaving for now (test was needed to fix a project issue)
|
|
348
|
+
let db = (await opendb(null, {
|
|
349
|
+
timestamps: false,
|
|
350
|
+
serverSelectionTimeoutMS: 2000,
|
|
351
|
+
imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
|
|
352
|
+
})).db
|
|
419
353
|
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
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.'
|
|
354
|
+
let user = db.model('user', { fields: {
|
|
355
|
+
logo: { type: 'image' }
|
|
356
|
+
}})
|
|
357
|
+
|
|
358
|
+
// let image = {
|
|
359
|
+
// bucket: 'test',
|
|
360
|
+
// date: 1234,
|
|
361
|
+
// filename: 'test.png',
|
|
362
|
+
// filesize: 1234,
|
|
363
|
+
// path: 'test/test123'
|
|
364
|
+
// }
|
|
365
|
+
let user1 = await db.user._insert({
|
|
366
|
+
logo: null
|
|
447
367
|
})
|
|
448
|
-
res.json()
|
|
449
|
-
})
|
|
450
368
|
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
.
|
|
457
|
-
|
|
458
|
-
.
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
369
|
+
let plugin = db.imagePluginFile
|
|
370
|
+
let supertest = require('supertest')
|
|
371
|
+
let express = require('express')
|
|
372
|
+
let upload = require('express-fileupload')
|
|
373
|
+
let app = express()
|
|
374
|
+
app.use(upload({ limits: { fileSize: 1 * 1000 * 1000, files: 10 }}))
|
|
375
|
+
|
|
376
|
+
app.post('/', function(req, res) {
|
|
377
|
+
try {
|
|
378
|
+
let options = { files: req.files, model: user, query: { _id: user1._id }}
|
|
379
|
+
plugin.removeImages(options, req.body, true)
|
|
380
|
+
.then(res => {
|
|
381
|
+
expect(res[0]).toEqual({})
|
|
382
|
+
expect(res[1]).toEqual([])
|
|
383
|
+
})
|
|
384
|
+
.finally(() => {
|
|
385
|
+
res.json()
|
|
386
|
+
db.close()
|
|
387
|
+
done()
|
|
388
|
+
})
|
|
389
|
+
} catch(e) {
|
|
390
|
+
console.log(e)
|
|
391
|
+
res.error(e)
|
|
392
|
+
}
|
|
466
393
|
})
|
|
394
|
+
|
|
395
|
+
// Start tests
|
|
396
|
+
supertest(app)
|
|
397
|
+
.post('/')
|
|
398
|
+
.attach('logo', `${__dirname}/assets/logo.png`)
|
|
399
|
+
.expect(200)
|
|
400
|
+
.end((err, res) => { if (err) console.log(err, res.text) })
|
|
401
|
+
})()
|
|
467
402
|
})
|
|
468
403
|
|
|
469
|
-
test('images addImages
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
404
|
+
test('images addImages formats & filesizes', (done) => {
|
|
405
|
+
(async function run () {
|
|
406
|
+
let db = (await opendb(null, {
|
|
407
|
+
timestamps: false,
|
|
408
|
+
serverSelectionTimeoutMS: 2000,
|
|
409
|
+
imagePlugin: {
|
|
410
|
+
awsBucket: 'fake',
|
|
411
|
+
awsAccessKeyId: 'fake',
|
|
412
|
+
awsSecretAccessKey: 'fake',
|
|
413
|
+
formats: ['jpg', 'jpeg', 'png', 'ico']
|
|
414
|
+
}
|
|
415
|
+
})).db
|
|
416
|
+
|
|
417
|
+
let user = db.model('user', { fields: {
|
|
418
|
+
imageIco: { type: 'image' },
|
|
419
|
+
imageWebp: { type: 'image', formats: ['webp'] },
|
|
420
|
+
imageSvgBad: { type: 'image' },
|
|
421
|
+
imageSvgGood: { type: 'image', formats: ['svg'] },
|
|
422
|
+
imageSvgAny: { type: 'image', formats: ['any'] },
|
|
423
|
+
imageSize1: { type: 'image', fileSize: 1000 * 100 },
|
|
424
|
+
imageSize2: { type: 'image' },
|
|
425
|
+
}})
|
|
426
|
+
|
|
427
|
+
let plugin = db.imagePluginFile
|
|
428
|
+
let supertest = require('supertest')
|
|
429
|
+
let express = require('express')
|
|
430
|
+
let upload = require('express-fileupload')
|
|
431
|
+
let app = express()
|
|
432
|
+
app.use(upload({ limits: { fileSize: 1000 * 200, files: 10 }}))
|
|
433
|
+
|
|
434
|
+
app.post('/', async (req, res) => {
|
|
435
|
+
let imageSvgBad = { imageSvgBad: req.files.imageSvgBad }
|
|
436
|
+
let imageSize1 = { imageSize1: req.files.imageSize1 }
|
|
437
|
+
let imageSize2 = { imageSize2: req.files.imageSize2 }
|
|
438
|
+
delete req.files.imageSvgBad
|
|
439
|
+
delete req.files.imageSize1
|
|
440
|
+
delete req.files.imageSize2
|
|
441
|
+
// Ico, Webp, and imageSvgGood will throw an error first if it's not a valid type
|
|
442
|
+
await expect(plugin._findValidImages(req.files, user)).resolves.toEqual(expect.any(Array))
|
|
443
|
+
await expect(plugin._findValidImages(imageSvgBad, user)).rejects.toEqual({
|
|
444
|
+
title: 'imageSvgBad',
|
|
445
|
+
detail: 'The file format \'svg\' for \'bad.svg\' is not supported'
|
|
446
|
+
})
|
|
447
|
+
await expect(plugin._findValidImages(imageSize1, user)).rejects.toEqual({
|
|
448
|
+
title: 'imageSize1',
|
|
449
|
+
detail: 'The file size for \'lion1.png\' is bigger than 0.1MB.'
|
|
450
|
+
})
|
|
451
|
+
await expect(plugin._findValidImages(imageSize2, user)).rejects.toEqual({
|
|
452
|
+
title: 'imageSize2',
|
|
453
|
+
detail: 'The file size for \'lion2.jpg\' is too big.'
|
|
454
|
+
})
|
|
455
|
+
res.json()
|
|
456
|
+
})
|
|
476
457
|
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
458
|
+
// Start tests
|
|
459
|
+
supertest(app)
|
|
460
|
+
.post('/')
|
|
461
|
+
.attach('imageIco', `${__dirname}/assets/image.ico`)
|
|
462
|
+
.attach('imageWebp', `${__dirname}/assets/image.webp`)
|
|
463
|
+
.attach('imageSvgBad', `${__dirname}/assets/bad.svg`)
|
|
464
|
+
.attach('imageSvgGood', `${__dirname}/assets/bad.svg`)
|
|
465
|
+
.attach('imageSvgAny', `${__dirname}/assets/bad.svg`)
|
|
466
|
+
.attach('imageSize1', `${__dirname}/assets/lion1.png`)
|
|
467
|
+
.attach('imageSize2', `${__dirname}/assets/lion2.jpg`)
|
|
468
|
+
.expect(200)
|
|
469
|
+
.end((err, res) => {
|
|
470
|
+
if (err) console.log(err)
|
|
471
|
+
db.close()
|
|
472
|
+
done()
|
|
473
|
+
})
|
|
474
|
+
})()
|
|
475
|
+
})
|
|
482
476
|
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
} catch (err) {
|
|
506
|
-
res.status(500).send(err)
|
|
477
|
+
test('images addImages bad file objects', (done) => {
|
|
478
|
+
(async function run () {
|
|
479
|
+
//// latest supertrace setup
|
|
480
|
+
let db = (await opendb(null, {
|
|
481
|
+
timestamps: false,
|
|
482
|
+
serverSelectionTimeoutMS: 2000,
|
|
483
|
+
imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
|
|
484
|
+
})).db
|
|
485
|
+
|
|
486
|
+
db.model('user', { fields: {
|
|
487
|
+
logo: { type: 'image' },
|
|
488
|
+
logos: [{ type: 'image' }],
|
|
489
|
+
users: [{ logo: { type: 'image' } }]
|
|
490
|
+
}})
|
|
491
|
+
|
|
492
|
+
let imageObjectMock = {
|
|
493
|
+
bucket: 'temp',
|
|
494
|
+
date: 1234,
|
|
495
|
+
filename: 'temp.png',
|
|
496
|
+
filesize: 1234,
|
|
497
|
+
path: 'temp',
|
|
498
|
+
uid: 'temp'
|
|
507
499
|
}
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
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)
|
|
500
|
+
let detailLongMock = 'Image fields need to either be null, undefined, file, or an object containing the '
|
|
501
|
+
+ 'following fields \'{ bucket, date, filename, filesize, path, uid }\''
|
|
502
|
+
|
|
503
|
+
let supertest = require('supertest')
|
|
504
|
+
let express = require('express')
|
|
505
|
+
let app = express()
|
|
506
|
+
let bodyParser = require('body-parser')
|
|
507
|
+
|
|
508
|
+
app.use(bodyParser.json())
|
|
509
|
+
app.use(bodyParser.urlencoded({ extended: true }))
|
|
510
|
+
app.post('/', async function(req, res) {
|
|
511
|
+
try {
|
|
512
|
+
await db.user.validate(req.body)
|
|
513
|
+
res.send()
|
|
514
|
+
} catch (err) {
|
|
515
|
+
res.status(500).send(err)
|
|
516
|
+
}
|
|
550
517
|
})
|
|
518
|
+
|
|
519
|
+
// Start tests
|
|
520
|
+
supertest(app)
|
|
521
|
+
.post('/')
|
|
522
|
+
.send({
|
|
523
|
+
logo: null,//ok
|
|
524
|
+
logos: [undefined, imageObjectMock, {}],//0,1=ok
|
|
525
|
+
users: [
|
|
526
|
+
{ logo: {} },
|
|
527
|
+
{ logo: { bucket: '' }},
|
|
528
|
+
{ logo: imageObjectMock },//ok
|
|
529
|
+
{ logo: null },//ok
|
|
530
|
+
]
|
|
531
|
+
})
|
|
532
|
+
.expect(500)
|
|
533
|
+
.then(res => {
|
|
534
|
+
// console.log(res.body)
|
|
535
|
+
expect(res.body).toEqual([
|
|
536
|
+
{
|
|
537
|
+
detail: 'Invalid image value',
|
|
538
|
+
meta: { rule: 'isImageObject', model: 'user', field: '2', detailLong: detailLongMock },
|
|
539
|
+
status: '400',
|
|
540
|
+
title: 'logos.2'
|
|
541
|
+
}, {
|
|
542
|
+
detail: 'Invalid image value',
|
|
543
|
+
meta: { rule: 'isImageObject', model: 'user', field: 'logo', detailLong: detailLongMock },
|
|
544
|
+
status: '400',
|
|
545
|
+
title: 'users.0.logo'
|
|
546
|
+
}, {
|
|
547
|
+
detail: 'Invalid image value',
|
|
548
|
+
meta: { rule: 'isImageObject', model: 'user', field: 'logo', detailLong: detailLongMock },
|
|
549
|
+
status: '400',
|
|
550
|
+
title: 'users.1.logo'
|
|
551
|
+
}
|
|
552
|
+
])
|
|
553
|
+
db.close()
|
|
554
|
+
done()
|
|
555
|
+
})
|
|
556
|
+
.catch(err => {
|
|
557
|
+
db.close()
|
|
558
|
+
done(err)
|
|
559
|
+
})
|
|
560
|
+
})()
|
|
551
561
|
})
|
|
552
562
|
|
|
553
563
|
}
|