w-serv-orm 1.1.13 → 1.1.14

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.
@@ -105,18 +105,17 @@ describe('R-ID-02 字串型主鍵欄之數字主鍵以字串處置', function()
105
105
  assert.strictEqual(err, 'failed to delete data: id[123]')
106
106
  })
107
107
 
108
- it('N03 查詢條件之比較運算子(含$not與巢狀)轉型, 他欄與其餘運算子不變, 呼叫端條件不變', async function() {
109
- //R-ID-02 規則2(查詢條件)、規則4
108
+ it('N03 查詢條件之白名單內比較運算子(含$and、$or巢狀)轉型, 他欄與其餘運算子不變, 呼叫端條件不變', async function() {
109
+ //R-ID-02 規則2(查詢條件)、規則4; R-WL-01 規則3(轉型範圍只及系列白名單: $eq $ne $gt $gte $lt $lte $in $nin, 文件層$and $or)
110
110
  let so = WServOrm(dsStr, WOrmStub, 'u', 'd')
111
111
  let finds = [
112
112
  { id: 123 },
113
113
  { id: { $eq: 1, $ne: 2, $gt: 3, $gte: 4, $lt: 5, $lte: 6 } },
114
- { id: { $in: [7, 'a'], $nin: [8], $all: [9] } },
115
- { id: { $not: { $eq: 10 } } },
116
- { id: { $not: { $in: [11, 12] } } },
117
- { $or: [{ id: 13 }, { name: 'x' }], $and: [{ id: { $in: [14] } }], $nor: [{ id: 15 }] },
114
+ { id: { $in: [7, 'a'], $nin: [8] } },
115
+ { $or: [{ id: 13 }, { name: 'x' }], $and: [{ id: { $in: [14] } }] },
118
116
  { $and: [{ $or: [{ id: 16 }] }] },
119
117
  { name: 17, id: { $regex: '^a', $exists: true } },
118
+ { id: { $eq: [9], $ne: [10, 'b'], $gt: [11], $gte: [12], $lt: [13], $lte: [14] } }, //比較運算子之值為陣列者逐一轉型(替身ORM; 真後端對 $in、$nin 以外之陣列值本就reject)
120
119
  ]
121
120
  let findsCopy = JSON.parse(JSON.stringify(finds))
122
121
  for (let f of finds) {
@@ -127,12 +126,11 @@ describe('R-ID-02 字串型主鍵欄之數字主鍵以字串處置', function()
127
126
  assert.deepStrictEqual(calls.map((c) => c.data), [
128
127
  { id: '123' },
129
128
  { id: { $eq: '1', $ne: '2', $gt: '3', $gte: '4', $lt: '5', $lte: '6' } },
130
- { id: { $in: ['7', 'a'], $nin: ['8'], $all: ['9'] } },
131
- { id: { $not: { $eq: '10' } } },
132
- { id: { $not: { $in: ['11', '12'] } } },
133
- { $or: [{ id: '13' }, { name: 'x' }], $and: [{ id: { $in: ['14'] } }], $nor: [{ id: '15' }] },
129
+ { id: { $in: ['7', 'a'], $nin: ['8'] } },
130
+ { $or: [{ id: '13' }, { name: 'x' }], $and: [{ id: { $in: ['14'] } }] },
134
131
  { $and: [{ $or: [{ id: '16' }] }] },
135
132
  { name: 17, id: { $regex: '^a', $exists: true } },
133
+ { id: { $eq: ['9'], $ne: ['10', 'b'], $gt: ['11'], $gte: ['12'], $lt: ['13'], $lte: ['14'] } },
136
134
  { id: '18' },
137
135
  { id: { $in: ['19'] } },
138
136
  ])
@@ -218,6 +216,60 @@ describe('R-ID-02 字串型主鍵欄之數字主鍵以字串處置', function()
218
216
  assert.deepStrictEqual(r, [{ id: '1' }, { id: '2' }])
219
217
  })
220
218
 
219
+ it('N10 系列白名單外之$not、$all、$nor及其內之id原樣交ORM而不轉型, 白名單由後端把關', async function() {
220
+ //R-WL-01 規則2、3: 本套件不依運算子名稱放行或攔截, 也不轉型白名單外運算子內之id; 還原$not、$all、$nor之轉型分支(死碼)則本案轉紅
221
+ let so = WServOrm(dsStr, WOrmStub, 'u', 'd')
222
+ let finds = [
223
+ { id: { $not: { $eq: 10 } } },
224
+ { id: { $not: { $in: [11, 12] } } },
225
+ { id: { $in: [7], $all: [9] } },
226
+ { $nor: [{ id: 15 }] },
227
+ { $or: [{ id: 13 }, { $nor: [{ id: 15 }] }], $and: [{ id: { $not: { $eq: 10 } } }] },
228
+ ]
229
+ let findsCopy = JSON.parse(JSON.stringify(finds))
230
+ let expected = [
231
+ { id: { $not: { $eq: 10 } } },
232
+ { id: { $not: { $in: [11, 12] } } },
233
+ { id: { $in: ['7'], $all: [9] } }, //同條件物件內之白名單運算子($in)仍轉型
234
+ { $nor: [{ id: 15 }] },
235
+ { $or: [{ id: '13' }, { $nor: [{ id: 15 }] }], $and: [{ id: { $not: { $eq: 10 } } }] },
236
+ ]
237
+ for (let mode of ['select', 'delAll']) {
238
+ calls = []
239
+ for (let f of finds) {
240
+ await so.procOrm('u1', 'tests', mode, f)
241
+ }
242
+ assert.deepStrictEqual(calls.map((c) => c.fn), finds.map(() => mode))
243
+ assert.deepStrictEqual(calls.map((c) => c.data), expected, mode)
244
+ }
245
+ assert.deepStrictEqual(finds, findsCopy)
246
+ })
247
+
248
+ it('N11 ORM以reject拒絕條件: select以failed to select data、delAll以failed to delete data固定字串回傳, ORM原文只進log', async function() {
249
+ //R-WL-01 規則4; R-ERR-01 規則1: 後端(白名單)拒絕時之錯誤回傳路徑, ORM之reject為Error物件或字串皆然
250
+ let so = WServOrm(dsStr, WOrmStub, 'u', 'd')
251
+ let find = { id: { $not: { $eq: 5 } } }
252
+ for (let orig of [new Error('invalid find: unsupported operator [$not] at [find.id.$not]'), 'invalid find: unsupported operator [$not] at [find.id.$not]']) {
253
+ for (let [mode, msg] of [['select', 'failed to select data'], ['delAll', 'failed to delete data']]) {
254
+ calls = []
255
+ plan = { [mode]: () => Promise.reject(orig) }
256
+ let logs = []
257
+ let logOrig = console.log
258
+ console.log = (...args) => logs.push(args)
259
+ let err
260
+ try {
261
+ err = await getReject(so.procOrm('u1', 'tests', mode, find))
262
+ }
263
+ finally {
264
+ console.log = logOrig
265
+ }
266
+ assert.strictEqual(err, msg, mode)
267
+ assert.strictEqual(calls.length, 1, `${mode} ORM被呼叫一次`)
268
+ assert.ok(logs.some((a) => a.includes(orig)), `${mode} ORM原文進log`)
269
+ }
270
+ }
271
+ })
272
+
221
273
  })
222
274
 
223
275