monastery 2.2.3 → 3.0.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 +10 -1
- package/changelog.md +1 -1
- package/docs/_config.yml +2 -2
- package/docs/assets/imgs/monastery.jpg +0 -0
- package/docs/definition/index.md +1 -2
- package/docs/manager/index.md +19 -11
- package/docs/manager/model.md +1 -1
- package/docs/manager/models.md +2 -3
- package/docs/model/...rawMethods.md +289 -0
- package/docs/model/count.md +25 -0
- package/docs/model/find.md +5 -9
- package/docs/model/findOne.md +1 -1
- package/docs/model/findOneAndUpdate.md +1 -1
- package/docs/model/index.md +5 -30
- package/docs/model/insert.md +4 -6
- package/docs/model/remove.md +4 -6
- package/docs/model/update.md +4 -6
- package/docs/readme.md +80 -47
- package/lib/collection.js +324 -0
- package/lib/index.js +207 -67
- package/lib/model-crud.js +605 -619
- package/lib/model-validate.js +227 -245
- package/lib/model.js +70 -91
- package/lib/rules.js +36 -35
- package/lib/util.js +69 -15
- package/package.json +12 -12
- package/plugins/images/index.js +15 -26
- package/test/blacklisting.js +506 -537
- package/test/collection.js +445 -0
- package/test/crud.js +810 -730
- package/test/index.test.js +26 -0
- package/test/manager.js +77 -0
- package/test/mock/blacklisting.js +23 -23
- package/test/model.js +611 -572
- package/test/plugin-images.js +880 -965
- package/test/populate.js +249 -262
- package/test/util.js +126 -45
- package/test/validate.js +1074 -1121
- package/test/virtuals.js +222 -227
- package/lib/monk-monkey-patches.js +0 -90
- package/test/monk.js +0 -53
- package/test/test.js +0 -38
package/test/plugin-images.js
CHANGED
|
@@ -1,1025 +1,940 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
})
|
|
47
|
+
// found company
|
|
48
|
+
expect(foundCompany).toEqual({
|
|
49
|
+
_id: company._id,
|
|
50
|
+
logo: { bucket: 'corex-dev', date: 1598481616 },
|
|
51
|
+
})
|
|
38
52
|
|
|
39
|
-
|
|
40
|
-
|
|
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
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
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
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
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
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
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
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
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
|
-
|
|
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
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
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
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
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
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
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
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
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
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
logos
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
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
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
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
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
})
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
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
|
-
|
|
534
|
+
res.send()
|
|
535
|
+
} catch (e) {
|
|
536
|
+
console.log(e.message || e)
|
|
537
|
+
res.status(500).send()
|
|
538
|
+
}
|
|
467
539
|
})
|
|
468
540
|
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
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
|
-
|
|
481
|
-
|
|
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: '
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
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
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
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
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
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
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
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
|
-
|
|
666
|
-
|
|
667
|
-
|
|
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
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
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
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
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
|
-
|
|
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
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
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
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
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
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
awsBucket: '
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
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
|
-
}
|
|
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
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
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
|
-
|
|
879
|
-
|
|
880
|
-
|
|
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
|
-
|
|
883
|
-
|
|
841
|
+
path: 'images2/logo2.png',
|
|
842
|
+
uid: expect.any(String),
|
|
884
843
|
},
|
|
885
|
-
}
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
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
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
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
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
894
|
+
let user = db3.model('user', {
|
|
895
|
+
fields: {
|
|
896
|
+
logo: { type: 'image', filename: 'oldLogo' },
|
|
897
|
+
},
|
|
898
|
+
})
|
|
1021
899
|
|
|
1022
|
-
|
|
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
|
+
})
|