-
Notifications
You must be signed in to change notification settings - Fork 0
/
find2.js
365 lines (327 loc) · 10.8 KB
/
find2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
85
86
87
88
89
90
91
92
93
94
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
136
137
138
139
140
141
142
143
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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// During the test the env variable is set to test
process.env.NODE_ENV = 'test';
// Require the dev-dependencies
const { assert } = require('chai');
const knex = require('../../../../knex');
const Coffee = require('../../../../models/coffee-example');
// Our parent block
describe('TABLE GATEWAY: FIND', () => { // eslint-disable-line
before(async () => { // eslint-disable-line
await knex.seed.run();
});
it('Empty query', async () => { // eslint-disable-line
const results = await Coffee.find({});
assert.equal(results.length, 4);
});
it('With query', async () => { // eslint-disable-line
const results = await Coffee.find({
price: 100,
});
assert.equal(results.length, 2);
for (let i = 0; i < results.length; i++) {
assert.equal(results[i].price, 100);
}
});
it('With columns', async () => { // eslint-disable-line
const results = await Coffee.find({}, ['id', 'name']);
assert.equal(results.length, 4);
for (let i = 0; i < results.length; i++) {
const keys = Object.keys(results[i]);
assert.equal(keys.length, 2);
assert.isTrue(keys.indexOf('id') > -1);
assert.isTrue(keys.indexOf('name') > -1);
}
});
it('With columns = all', async () => { // eslint-disable-line
const results = await Coffee.find({}, 'all');
assert.equal(results.length, 4);
for (let i = 0; i < results.length; i++) {
const keys = Object.keys(results[i]);
assert.equal(keys.length, 5);
}
});
});
describe('Malicious happy path', () => { // eslint-disable-line
before(async () => { // eslint-disable-line
await knex.seed.run();
});
it('Query is undefined', async () => { // eslint-disable-line
const results = await Coffee.find();
assert.equal(results.length, 4);
});
it('Query is not a valid json', async () => { // eslint-disable-line
const results = await Coffee.find();
assert.equal(results.length, 4);
});
it('Query with invalid attr', (done) => { // eslint-disable-line
Coffee.find({
invalid: 'ues',
}).then(() => {
done('SHOULD NOT GET HERE');
}).catch((err) => {
assert.equal(err.code, 400);
assert.equal(err.fullMessage.routine, 'errorMissingColumn');
done();
});
});
it('Columns with invalid column name', (done) => { // eslint-disable-line
Coffee.find({}, ['all']).then(() => {
done('SHOULD NOT GET HERE');
}).catch((err) => {
assert.equal(err.code, 400);
assert.equal(err.fullMessage.routine, 'errorMissingColumn');
done();
});
});
it('Columns invalid json', async () => { // eslint-disable-line
const results = await Coffee.find({}, {
look: 'to me',
});
assert.equal(results.length, 4);
for (let i = 0; i < results.length; i++) {
const keys = Object.keys(results[i]);
assert.equal(keys.length, 5);
}
});
});
describe('with advance settings: group by', () => { // eslint-disable-line
before(async () => { // eslint-disable-line
await knex.seed.run();
});
// cannot do a group by
it('Give group by', (done) => { // eslint-disable-line
Coffee.find({}, 'all', {
groupBy: 'name',
}).then(() => {
done('SHOULD NOT GET HERE');
}).catch((err) => {
assert.equal(err.code, 400);
assert.equal(err.fullMessage.routine, 'check_ungrouped_columns_walker');
done();
});
});
it('invalid group by', (done) => { // eslint-disable-line
Coffee.find({}, 'all', {
groupBy: 'unexistant',
}).then(() => {
done('SHOULD NOT GET HERE');
}).catch((err) => {
assert.equal(err.code, 400);
assert.equal(err.fullMessage.routine, 'errorMissingColumn');
done();
});
});
});
describe('with advance settings: order by', () => { // eslint-disable-line
before(async () => { // eslint-disable-line
await knex.seed.run();
});
it('With order by', async () => { // eslint-disable-line
const results = await Coffee.find({}, 'all', {
orderBy: ['created_at', 'desc'],
});
assert.equal(results.length, 4);
for (let i = 1; i < results.length; i++) {
assert.isTrue(results[i - 1].created_at >= results[i].created_at);
}
});
it('With invalid order by', (done) => { // eslint-disable-line
Coffee.find({}, 'all', {
orderBy: ['unexistant', 'asc'],
}).then(() => {
done('SHOULD NOT GET HERE');
}).catch((err) => {
assert.equal(err.code, 400);
assert.equal(err.fullMessage.routine, 'errorMissingColumn');
done();
});
});
// orders as asc was given
it('With invalid order by: no desc or asc given', async () => { // eslint-disable-line
const results = await Coffee.find({}, 'all', {
orderBy: ['created_at'],
});
assert.equal(results.length, 4);
for (let i = 1; i < results.length; i++) {
assert.isTrue(results[i - 1].created_at <= results[i].created_at);
}
});
// orders as asc was given
it('With invalid order by: given as string', async () => { // eslint-disable-line
const results = await Coffee.find({}, 'all', {
orderBy: 'created_at',
});
assert.equal(results.length, 4);
for (let i = 1; i < results.length; i++) {
assert.isTrue(results[i - 1].created_at <= results[i].created_at);
}
});
// orders as asc was given
it('With invalid order by: nor asc nor desc', async () => { // eslint-disable-line
const results = await Coffee.find({}, 'all', {
orderBy: ['created_at', 'not valid'],
});
assert.equal(results.length, 4);
for (let i = 1; i < results.length; i++) {
assert.isTrue(results[i - 1].created_at <= results[i].created_at);
}
});
});
describe('with advance settings: start_date and end_date', () => { // eslint-disable-line
before(async () => { // eslint-disable-line
await knex.seed.run();
});
it('With start date', async () => { // eslint-disable-line
const date = new Date('2018-11-21T12:06:00.000Z');
const results = await Coffee.find({}, 'all', {
startDate: date,
});
assert.equal(results.length, 2);
for (let i = 0; i < results.length; i++) {
assert.isTrue(results[i].created_at > date);
}
});
it('With end date', async () => { // eslint-disable-line
const date = new Date('2018-11-21T12:06:10.000Z');
const results = await Coffee.find({}, 'all', {
endDate: date,
});
assert.equal(results.length, 3);
for (let i = 0; i < results.length; i++) {
assert.isTrue(results[i].created_at < date);
}
});
it('With start day and end date', async () => { // eslint-disable-line
const startDate = new Date('2018-11-21T11:55:55.000Z');
const endDate = new Date('2018-11-21T12:06:10.000Z');
const results = await Coffee.find({}, 'all', {
endDate,
startDate,
});
assert.equal(results.length, 2);
for (let i = 0; i < results.length; i++) {
assert.isTrue(results[i].created_at < endDate);
assert.isTrue(results[i].created_at > startDate);
}
});
it('invalid start date', (done) => { // eslint-disable-line
const date = 'this is not a date';
Coffee.find({}, 'all', {
startDate: date,
}).then(() => {
done('SHOULD NOT GET HERE');
}).catch((err) => {
assert.equal(err.code, 400);
assert.equal(err.fullMessage.routine, 'DateTimeParseError');
done();
});
});
});
describe('with advance settings: offset', () => { // eslint-disable-line
before(async () => { // eslint-disable-line
await knex.seed.run();
});
it('with limit', async () => { // eslint-disable-line
const results = await Coffee.find({}, 'all', {
limit: 2,
});
assert.equal(results.length, 2);
});
// as if no limit was given
it('with invalid limit', async () => { // eslint-disable-line
const results = await Coffee.find({}, 'all', {
limit: 'yes',
});
assert.equal(results.length, 4);
});
// as if no limit was given
it('with invalid limit: negative value', (done) => { // eslint-disable-line
Coffee.find({}, 'all', {
limit: -1,
}).then(() => {
done('SHOULD NOT GET HERE');
}).catch((err) => {
assert.equal(err.code, 400);
assert.equal(err.fullMessage.routine, 'recompute_limits');
done();
});
});
it('with offset', async () => { // eslint-disable-line
const results = await Coffee.find({}, 'all', {
offset: 1,
});
assert.equal(results.length, 3);
});
it('with invalid offset', (done) => { // eslint-disable-line
Coffee.find({}, 'all', {
offset: -1,
}).then(() => {
done('SHOULD NOT GET HERE');
}).catch((err) => {
assert.equal(err.code, 400);
assert.equal(err.fullMessage.routine, 'recompute_limits');
done();
});
});
});
describe('with advance settings: raw select', () => { // eslint-disable-line
before(async () => { // eslint-disable-line
await knex.seed.run();
});
it('with rawSelect', async () => { // eslint-disable-line
const results = await Coffee.find({}, 'all', {
rawSelect: 'EXTRACT(doy from created_at) as date',
});
assert.equal(results.length, 4);
for (let i = 0; i < results.length; i++) {
const keys = Object.keys(results[i]);
assert.isTrue(keys.length > 1);
assert.isTrue(keys.indexOf('date') > -1);
}
});
it('with rawSelect && columns == undefined', async () => { // eslint-disable-line
const results = await Coffee.find({}, undefined, {
rawSelect: 'EXTRACT(doy from created_at) as date',
});
assert.equal(results.length, 4);
for (let i = 0; i < results.length; i++) {
const keys = Object.keys(results[i]);
assert.isTrue(keys.length === 1);
assert.isTrue(keys.indexOf('date') > -1);
}
});
it('with rawSelect && columns == ["id", "name"]', async () => { // eslint-disable-line
const results = await Coffee.find({}, ['id', 'name'], {
rawSelect: 'EXTRACT(doy from created_at) as date',
});
assert.equal(results.length, 4);
for (let i = 0; i < results.length; i++) {
const keys = Object.keys(results[i]);
assert.isTrue(keys.length === 3);
assert.isTrue(keys.indexOf('date') > -1);
}
});
it('with rawSelect && columns == ["id", "name"] && clearSelect', async () => { // eslint-disable-line
const results = await Coffee.find({}, ['id', 'name'], {
clearSelect: true,
rawSelect: 'EXTRACT(doy from created_at) as date',
});
assert.equal(results.length, 4);
for (let i = 0; i < results.length; i++) {
const keys = Object.keys(results[i]);
assert.isTrue(keys.length === 1);
assert.isTrue(keys.indexOf('date') > -1);
}
});
it('with invalid rawSelect', (done) => { // eslint-disable-line
Coffee.find({}, 'all', {
rawSelect: 'this should fail',
}).then(() => {
done('SHOULD NOT GET HERE');
}).catch((err) => {
assert.equal(err.code, 400);
assert.equal(err.fullMessage.routine, 'scanner_yyerror');
done();
});
});
});