piper-utils 1.0.4 → 1.0.5-7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,216 +1,368 @@
1
- import { accessRightsUtils, checkModule, checkWriteAccess, getBusinessesInfo, userDefaultBid } from '../../../index.js';
2
- import { getModuleInfo } from './accessRightsUtils.js';
3
-
4
- export const mock = {
5
- pathParameters: {
6
- id: 1
7
- },
8
- queryStringParameters: {
9
- businessIds: 'ABC,1'
10
- },
11
- requestContext: {
12
- authorizer: {
13
- claims: {
14
- 'custom:AR': JSON.stringify({ businessIds: { 'ABC': 'A', 'XYZ': 'A' } }),
15
- 'custom:DBI': JSON.stringify({ defaultBid: 'spam' }),
16
- 'custom:MOD': JSON.stringify({ module: { part: true } })
17
- }
18
- }
19
- },
20
- body: { number: 'B201', businessId: 'ABC', taxRate: { rate: .325, name: 'name', agency: 'agency', ref: '' }, defaultLocationId: 1, uoms: [{ name: 'EA', conversion: 1 }] }
21
- };
22
-
23
- export const mockNOAccess = {
24
- pathParameters: {
25
- id: 1
26
- },
27
- queryStringParameters: {
28
- businessIds: 'XYZ'
29
- },
30
- requestContext: {
31
- authorizer: {
32
- claims: {
33
- 'custom:AR': JSON.stringify({ businessIds: { 'ABC': 'A' } }),
34
- 'custom:DBI': JSON.stringify({ defaultBid: 'spam' }),
35
- 'custom:MOD': JSON.stringify({ module: { part: true } })
36
- }
37
- }
38
- },
39
- body: { number: 'B201', taxRate: { rate: .325, name: 'name', agency: 'agency', ref: '' }, defaultLocationId: 1, uoms: [{ name: 'EA', conversion: 1 }] }
40
- };
41
- export const mockNoneRequested = {
42
- pathParameters: {
43
- id: 1
44
- },
45
- queryStringParameters: {
46
- businessIds: ''
47
- },
48
- requestContext: {
49
- authorizer: {
50
- claims: {
51
- 'custom:AR': JSON.stringify({ businessIds: { 'ABC': 'A' } }),
52
- 'custom:DBI': JSON.stringify({ defaultBid: 'spam' }),
53
- 'custom:MOD': JSON.stringify({ module: { part: true } })
54
- }
55
- }
56
- },
57
- body: { number: 'B201', taxRate: { rate: .325, name: 'name', agency: 'agency', ref: '' }, defaultLocationId: 1, uoms: [{ name: 'EA', conversion: 1 }] }
58
- };
59
-
60
- export const mock2 = {
61
- pathParameters: {
62
- id: 1
63
- },
64
- queryStringParameters: {
65
- businessIds: '2'
66
- },
67
- body: { number: 'B201', taxRate: { rate: .325, name: 'name', agency: 'agency', ref: '' }, defaultLocationId: 1, uoms: [{ name: 'EA', conversion: 1 }] }
68
- };
69
- export const mock3 = {
70
- pathParameters: {
71
- id: 1
72
- },
73
- queryStringParameters: {
74
- businessIds: 'ABC'
75
- },
76
- requestContext: {
77
- authorizer: {
78
- claims: {
79
- 'custom:AR': JSON.stringify({ businessIds: { 'ABC': 'R', 'XYZ': 'R' } }),
80
- 'custom:DBI': JSON.stringify({ defaultBid: 'spam' }),
81
- 'custom:MOD': JSON.stringify({ module: { part: true } })
82
- }
83
- }
84
- },
85
- body: { businessId: 'ABC' }
86
- };
87
-
88
- describe('accessRightsUtils', () => {
89
- describe('accessRightsUtils', () => {
90
- it('should return localBidList no options', () => {
91
- const event = {
92
- requestContext: {
93
- authorizer: {
94
- claims: null,
95
- custom: {
96
- AR: null
97
- }
98
- }
99
- }
100
- };
101
- const result = accessRightsUtils(event);
102
- expect(result).toEqual(['1']);
103
- });
104
- it('should return access of request query stings if they are granted in JWT', () => {
105
- const result = accessRightsUtils(mock);
106
- expect(result).toEqual(['ABC']);
107
- });
108
- it('should return access 2', () => {
109
- const result = accessRightsUtils(mock2);
110
- expect(result).toEqual([]);
111
- });
112
- it('should not return access', () => {
113
- const result = accessRightsUtils(mockNOAccess);
114
- expect(result).toEqual([]);
115
- });
116
- it('should return access for all if not requested in list', () => {
117
- const result = accessRightsUtils(mockNoneRequested);
118
- expect(result).toEqual(['ABC']);
119
- });
120
- it('not prod or stg', () => {
121
- const result = accessRightsUtils({});
122
- expect(result).toEqual(['1']);
123
- });
124
- });
125
- describe('userDefaultBid', () => {
126
- it('should return defaultBid', () => {
127
- const result = userDefaultBid(mock);
128
- expect(result).toEqual('spam');
129
- });
130
- it('should return defaultBid', () => {
131
- const result = userDefaultBid(mock2);
132
- expect(result).toEqual('1');
133
- });
134
- });
135
- describe('getModuleInfo', () => {
136
- it('should return default module list', () => {
137
- const result = getModuleInfo(mock);
138
- expect(result).toEqual({ part: true });
139
- });
140
- it('should return empty module', () => {
141
- const result = getModuleInfo(mock2);
142
- expect(result).toEqual({});
143
- });
144
- });
145
- describe('checkModule', () => {
146
- it('check for module access', () => {
147
- checkModule('part', mock);
148
- });
149
- it('should throw', () => {
150
- try {
151
- checkModule('span', mock);
152
- } catch (e) {
153
- expect(e).toEqual({ message: 'UNAUTHORIZED', errorCode: '4111', statusCode: 401 });
154
- }
155
- });
156
- });
157
- describe('checkWriteAccess', () => {
158
- it('check for write access', () => {
159
- const result = checkWriteAccess(mock);
160
- expect(result).toEqual('ABC');
161
- });
162
- it('should throw if no access', () => {
163
- try {
164
- checkWriteAccess({});
165
- } catch (e) {
166
- expect(e).toEqual({ message: 'UNAUTHORIZED', errorCode: '4111', statusCode: 401 });
167
- }
168
- });
169
- it('should throw if read only', () => {
170
- try {
171
- checkWriteAccess(mock3);
172
- } catch (e) {
173
- expect(e).toEqual({ message: 'UNAUTHORIZED', errorCode: '4111', statusCode: 401 });
174
- }
175
- });
176
- });
177
- });
178
-
179
- describe('special env cases', () => {
180
- beforeEach(() => {
181
- process.env.BUILD_ENV = 'local';
182
- });
183
- afterEach(() => {
184
- process.env.BUILD_ENV = 'test';
185
- })
186
- it('should return localBidList and support use cognito bid option', () => {
187
- const event = {
188
- requestContext: {
189
- authorizer: {
190
- claims: {
191
- 'custom:AR': JSON.stringify({ businessIds: { 'ABC': 'A', 'XYZ': 'A' } }),
192
- },
193
- }
194
- }
195
- };
196
- const result = accessRightsUtils(event, { useCognitoBid: true });
197
- expect(result).toEqual([ 'ABC', 'XYZ' ]);
198
- });
199
- it('should return localBidList if no claims and support use cognito bid option', () => {
200
- const event = {
201
- requestContext: {
202
- authorizer: {
203
- claims: {},
204
- }
205
- }
206
- };
207
- const result = accessRightsUtils(event, { useCognitoBid: true });
208
- expect(result).toEqual([ '1']);
209
- });
210
- describe('getBusinessesInfo', () => {
211
- it('should handle local', () => {
212
- const result = getBusinessesInfo(mock);
213
- expect(result).toEqual({ '1': 'A', ABC: 'A', XYZ: 'A' });
214
- });
215
- });
216
- })
1
+ import { accessRightsUtils, checkModule, checkWriteAccess, getBusinessesInfo, userDefaultBid } from '../../../index.js';
2
+ import { getModuleInfo, getRequestedBusinessIds } from './accessRightsUtils.js';
3
+
4
+ export const mock = {
5
+ pathParameters: {
6
+ id: 1
7
+ },
8
+ queryStringParameters: {
9
+ businessIds: 'ABC,1'
10
+ },
11
+ requestContext: {
12
+ authorizer: {
13
+ claims: {
14
+ 'custom:AR': JSON.stringify({ businessIds: { 'ABC': 'A', 'XYZ': 'A' } }),
15
+ 'custom:DBI': JSON.stringify({ defaultBid: 'spam' }),
16
+ 'custom:MOD': JSON.stringify({ module: { part: true } })
17
+ }
18
+ }
19
+ },
20
+ body: { number: 'B201', businessId: 'ABC', taxRate: { rate: .325, name: 'name', agency: 'agency', ref: '' }, defaultLocationId: 1, uoms: [{ name: 'EA', conversion: 1 }] }
21
+ };
22
+ export const mock2NoAuth = {
23
+ pathParameters: {
24
+ id: 1
25
+ },
26
+ queryStringParameters: {
27
+ businessIds: 'ABC,1'
28
+ },
29
+ body: JSON.stringify({ number: 'B201', businessId: 'ABC', taxRate: { rate: .325, name: 'name', agency: 'agency', ref: '' }, defaultLocationId: 1, uoms: [{ name: 'EA', conversion: 1 }] })
30
+ };
31
+ export const mockSystem = {
32
+ pathParameters: {
33
+ id: 1
34
+ },
35
+ queryStringParameters: {
36
+ businessIds: 'ABC,1'
37
+ },
38
+ requestContext: {
39
+ authorizer: {
40
+ claims: {
41
+ 'custom:AR': JSON.stringify({ businessIds: { 'ABC': 'A', 'XYZ': 'A' } }),
42
+ 'custom:DBI': JSON.stringify({ defaultBid: 'spam' }),
43
+ 'custom:MOD': JSON.stringify({ module: { part: false } }),
44
+ 'custom:SYSTEM': JSON.stringify('true')
45
+ }
46
+ }
47
+ },
48
+ body: { number: 'B201', businessId: 'ABC', taxRate: { rate: .325, name: 'name', agency: 'agency', ref: '' }, defaultLocationId: 1, uoms: [{ name: 'EA', conversion: 1 }] }
49
+ };
50
+
51
+ export const mockNOAccess = {
52
+ pathParameters: {
53
+ id: 1
54
+ },
55
+ queryStringParameters: {
56
+ businessIds: 'XYZ'
57
+ },
58
+ requestContext: {
59
+ authorizer: {
60
+ claims: {
61
+ 'custom:AR': JSON.stringify({ businessIds: { 'ABC': 'A' } }),
62
+ 'custom:DBI': JSON.stringify({ defaultBid: 'spam' }),
63
+ 'custom:MOD': JSON.stringify({ module: { part: true } })
64
+ }
65
+ }
66
+ },
67
+ body: { number: 'B201', taxRate: { rate: .325, name: 'name', agency: 'agency', ref: '' }, defaultLocationId: 1, uoms: [{ name: 'EA', conversion: 1 }] }
68
+ };
69
+ export const mockNoneRequested = {
70
+ pathParameters: {
71
+ id: 1
72
+ },
73
+ queryStringParameters: {
74
+ businessIds: ''
75
+ },
76
+ requestContext: {
77
+ authorizer: {
78
+ claims: {
79
+ 'custom:AR': JSON.stringify({ businessIds: { 'ABC': 'A' } }),
80
+ 'custom:DBI': JSON.stringify({ defaultBid: 'spam' }),
81
+ 'custom:MOD': JSON.stringify({ module: { part: true } })
82
+ }
83
+ }
84
+ },
85
+ body: { number: 'B201', taxRate: { rate: .325, name: 'name', agency: 'agency', ref: '' }, defaultLocationId: 1, uoms: [{ name: 'EA', conversion: 1 }] }
86
+ };
87
+ export const mockNoneRequestedSystem = {
88
+ pathParameters: {
89
+ id: 1
90
+ },
91
+ queryStringParameters: {
92
+ businessIds: 'DUCK'
93
+ },
94
+ requestContext: {
95
+ authorizer: {
96
+ claims: {
97
+ 'custom:AR': JSON.stringify({ businessIds: { 'ABC': 'A' } }),
98
+ 'custom:DBI': JSON.stringify({ defaultBid: 'spam' }),
99
+ 'custom:MOD': JSON.stringify({ module: { part: true } }),
100
+ 'custom:SYSTEM': JSON.stringify('true')
101
+ }
102
+ }
103
+ },
104
+ body: { number: 'B201', taxRate: { rate: .325, name: 'name', agency: 'agency', ref: '' }, defaultLocationId: 1, uoms: [{ name: 'EA', conversion: 1 }] }
105
+ };
106
+
107
+ export const mock2 = {
108
+ pathParameters: {
109
+ id: 1
110
+ },
111
+ queryStringParameters: {
112
+ businessIds: '2'
113
+ },
114
+ body: { number: 'B201', taxRate: { rate: .325, name: 'name', agency: 'agency', ref: '' }, defaultLocationId: 1, uoms: [{ name: 'EA', conversion: 1 }] }
115
+ };
116
+ export const mock3 = {
117
+ pathParameters: {
118
+ id: 1
119
+ },
120
+ queryStringParameters: {
121
+ businessIds: 'ABC'
122
+ },
123
+ requestContext: {
124
+ authorizer: {
125
+ claims: {
126
+ 'custom:AR': JSON.stringify({ businessIds: { 'ABC': 'R', 'XYZ': 'R' } }),
127
+ 'custom:DBI': JSON.stringify({ defaultBid: 'spam' }),
128
+ 'custom:MOD': JSON.stringify({ module: { part: true } })
129
+ }
130
+ }
131
+ },
132
+ body: { businessId: 'ABC' }
133
+ };
134
+
135
+ export const mock4 = {
136
+ pathParameters: {
137
+ id: 1
138
+ },
139
+ queryStringParameters: {
140
+ businessIds: 'ALPHA'
141
+ },
142
+ requestContext: {
143
+ authorizer: {
144
+ claims: {
145
+ 'custom:AR': JSON.stringify({ businessIds: { 'ABC': 'R', 'XYZ': 'R' } }),
146
+ 'custom:DBI': JSON.stringify({ defaultBid: 'spam' }),
147
+ 'custom:MOD': JSON.stringify({ module: { part: true } }),
148
+ 'custom:SYSTEM': JSON.stringify('true')
149
+ }
150
+ }
151
+ },
152
+ body: { businessId: 'ALPHA' }
153
+ };
154
+ export const mock5 = {
155
+ pathParameters: {
156
+ id: 1
157
+ },
158
+ queryStringParameters: {
159
+ businessIds: 'ALPHA'
160
+ },
161
+ requestContext: {
162
+ authorizer: {
163
+ claims: {
164
+ 'custom:AR': JSON.stringify({ businessIds: { 'ABC': 'R', 'XYZ': 'R' } }),
165
+ 'custom:DBI': JSON.stringify({ defaultBid: 'spam' }),
166
+ 'custom:MOD': JSON.stringify({ module: { part: true } }),
167
+ 'custom:SYSTEM': JSON.stringify('true')
168
+ }
169
+ }
170
+ },
171
+ body: { businessId: 'DOG' }
172
+ };
173
+ export const mock6 = {
174
+ pathParameters: {
175
+ id: 1
176
+ },
177
+ requestContext: {
178
+ authorizer: {
179
+ claims: {
180
+ 'custom:AR': JSON.stringify({ businessIds: { 'ABC': 'R', 'XYZ': 'R' } }),
181
+ 'custom:DBI': JSON.stringify({ defaultBid: 'spam' }),
182
+ 'custom:MOD': JSON.stringify({ module: { part: true } }),
183
+ 'custom:SYSTEM': JSON.stringify('true')
184
+ }
185
+ }
186
+ },
187
+ body: { businessId: 'DOG' }
188
+ };
189
+ export const mock7 = {
190
+ pathParameters: {
191
+ id: 1
192
+ },
193
+ requestContext: {
194
+ authorizer: {
195
+ claims: {
196
+ 'custom:AR': JSON.stringify({ businessIds: { 'ABC': 'R', 'XYZ': 'R' } }),
197
+ 'custom:DBI': JSON.stringify({ defaultBid: 'spam' }),
198
+ 'custom:MOD': JSON.stringify({ module: { part: true } }),
199
+ 'custom:SYSTEM': JSON.stringify('true')
200
+ }
201
+ }
202
+ },
203
+ body: null
204
+ };
205
+
206
+ describe('accessRightsUtils', () => {
207
+ describe('getRequestedBusinessIds', () => {
208
+ it('should get bid from query', () => {
209
+ const r = getRequestedBusinessIds(mock5);
210
+
211
+ expect(r).toEqual(['ALPHA']);
212
+ });
213
+ it('should get bid from body', () => {
214
+ const r = getRequestedBusinessIds(mock6);
215
+
216
+ expect(r).toEqual(['DOG']);
217
+ });
218
+ it('returns [] if no bids found', () => {
219
+ const r = getRequestedBusinessIds(mock7);
220
+
221
+ expect(r).toEqual([]);
222
+ });
223
+ });
224
+
225
+ describe('accessRightsUtils', () => {
226
+ it('should return access of request query stings if they are granted in JWT', () => {
227
+ const result = accessRightsUtils(mock);
228
+ expect(result).toEqual(['ABC']);
229
+ });
230
+ it('should return access 2', () => {
231
+ const result = accessRightsUtils(mock2);
232
+ expect(result).toEqual([]);
233
+ });
234
+ it('should not return access', () => {
235
+ const result = accessRightsUtils(mockNOAccess);
236
+ expect(result).toEqual([]);
237
+ });
238
+ it('should return access for all if not requested in list', () => {
239
+ const result = accessRightsUtils(mockNoneRequested);
240
+ expect(result).toEqual(['ABC']);
241
+ });
242
+ it('should return access for all if not requested in list', () => {
243
+ const result = accessRightsUtils(mockNoneRequestedSystem);
244
+ expect(result).toEqual(['DUCK']);
245
+ });
246
+ });
247
+ describe('userDefaultBid', () => {
248
+ it('should return defaultBid', () => {
249
+ const result = userDefaultBid(mock);
250
+ expect(result).toEqual('spam');
251
+ });
252
+ it('should return defaultBid', () => {
253
+ const result = userDefaultBid(mock2);
254
+ expect(result).toEqual('1');
255
+ });
256
+ });
257
+ describe('getModuleInfo', () => {
258
+ it('should return default module list', () => {
259
+ const result = getModuleInfo(mock);
260
+ expect(result).toEqual({ part: true });
261
+ });
262
+ it('should return empty module', () => {
263
+ const result = getModuleInfo(mock2);
264
+ expect(result).toEqual({});
265
+ });
266
+ });
267
+ describe('checkModule', () => {
268
+ it('all system users all access', () => {
269
+ checkModule('part', mockSystem);
270
+ });
271
+ it('check for module access', () => {
272
+ checkModule('part', mock);
273
+ });
274
+ it('should throw', () => {
275
+ try {
276
+ checkModule('span', mock);
277
+ } catch (e) {
278
+ expect(e).toEqual({ message: 'UNAUTHORIZED', errorCode: '4111', statusCode: 401 });
279
+ }
280
+ });
281
+ });
282
+ describe('checkWriteAccess', () => {
283
+ it('check for write access', () => {
284
+ const result = checkWriteAccess(mock);
285
+ expect(result).toEqual('ABC');
286
+ });
287
+ it('should throw if no access', () => {
288
+ try {
289
+ checkWriteAccess({});
290
+ } catch (e) {
291
+ expect(e).toEqual({ message: 'UNAUTHORIZED', errorCode: '4111', statusCode: 401 });
292
+ }
293
+ });
294
+ it('should throw if read only', () => {
295
+ try {
296
+ checkWriteAccess(mock3);
297
+ } catch (e) {
298
+ expect(e).toEqual({ message: 'UNAUTHORIZED', errorCode: '4111', statusCode: 401 });
299
+ }
300
+ });
301
+ it('return businessId in body if is a system uer', () => {
302
+ const v = checkWriteAccess(mock4);
303
+ expect(v).toEqual('ALPHA');
304
+
305
+ });
306
+ });
307
+ });
308
+
309
+ describe('special env cases', () => {
310
+ beforeEach(() => {
311
+ process.env.BUILD_ENV = 'local';
312
+ });
313
+ afterEach(() => {
314
+ process.env.BUILD_ENV = 'test';
315
+ });
316
+ it('should return localBidList and support use cognito bid option', () => {
317
+ const event = {
318
+ requestContext: {
319
+ authorizer: {
320
+ claims: {
321
+ 'custom:AR': JSON.stringify({ businessIds: { 'ABC': 'A', 'XYZ': 'A' } })
322
+ }
323
+ }
324
+ }
325
+ };
326
+ const result = accessRightsUtils(event, { useCognitoBid: true });
327
+ expect(result).toEqual(['ABC', 'XYZ']);
328
+ });
329
+ it('should return localBidList if no claims and support use cognito bid option', () => {
330
+ const event = {
331
+ requestContext: {
332
+ authorizer: {
333
+ claims: {}
334
+ }
335
+ }
336
+ };
337
+ const result = accessRightsUtils(event, { useCognitoBid: true });
338
+ expect(result).toEqual(['1']);
339
+ });
340
+ describe('getBusinessesInfo', () => {
341
+ it('should handle local', () => {
342
+ const result = getBusinessesInfo(mock);
343
+ expect(result).toEqual({ '1': 'A', ABC: 'A', XYZ: 'A' });
344
+ });
345
+ it('should handle local v2', () => {
346
+ const result = getBusinessesInfo(mock2NoAuth);
347
+ expect(result).toEqual({ '1': 'A', ABC: 'A' });
348
+ });
349
+ });
350
+ it('should return localBidList no options', () => {
351
+ const event = {
352
+ requestContext: {
353
+ authorizer: {
354
+ claims: null,
355
+ custom: {
356
+ AR: null
357
+ }
358
+ }
359
+ }
360
+ };
361
+ const result = accessRightsUtils(event);
362
+ expect(result).toEqual(['1']);
363
+ });
364
+ it('not prod or stg', () => {
365
+ const result = accessRightsUtils({});
366
+ expect(result).toEqual(['1']);
367
+ });
368
+ });
@@ -11,8 +11,9 @@ import { accessRightsUtils } from './accessRightsUtils.js';
11
11
  * @param {object} objectFilters - the default filters object created by createDefaultFilters
12
12
  * @returns {object} An sequelize where object
13
13
  */
14
- export const createFilters = function (event, objectFilters){
14
+ export const createFilters = function (event, objectFilters) {
15
15
  let query = _.get(event, 'queryStringParameters', {}) || {};
16
+ const searchString = _.get(query, 'searchString');
16
17
  const startDate = _.get(query, 'startDate');
17
18
  const endDate = _.get(query, 'endDate');
18
19
  const where = Object.keys(query).reduce((acc, item) => {
@@ -86,6 +87,40 @@ export const createFilters = function (event, objectFilters){
86
87
  filterAdds.createdAt = dates;
87
88
  }
88
89
 
90
+ if (searchString) {
91
+ const adds = Object.keys(objectFilters).reduce((acc, item) => {
92
+ const type = objectFilters[item].filterType;
93
+ const dataType = objectFilters[item].type;
94
+
95
+ if (type === DB.Op.iLike) {
96
+ const newOr = {};
97
+ const itemName = item.includes('.')
98
+ ? `$${item}$`
99
+ : item;
100
+ newOr[itemName] = {
101
+ [DB.Op.iLike]: `%${query['searchString']}%`
102
+ };
103
+ acc.push(newOr);
104
+ }
105
+
106
+ if (!_.isNaN(Number(query['searchString'])) && !(dataType instanceof DB.BOOLEAN)) {
107
+
108
+ if (type === DB.Op.eq) {
109
+ const newOr = {};
110
+ const itemName = item.includes('.') ? `$${item}$` : item;
111
+ newOr[itemName] = {
112
+ [DB.Op.eq]: `${query['searchString']}`
113
+ };
114
+ acc.push(newOr);
115
+ }
116
+ }
117
+
118
+ return acc;
119
+ }, []);
120
+
121
+ filterAdds[DB.Op.or] = adds;
122
+ }
123
+
89
124
  const withDate = Object.assign(where, filterAdds);
90
125
 
91
126
  return withDate;