@flusys/nestjs-shared 6.0.1 → 6.1.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/cjs/classes/api-controller.class.spec.js +684 -0
- package/cjs/classes/api-service.class.spec.js +809 -0
- package/cjs/classes/handle-custome-db.spec.js +82 -0
- package/cjs/classes/hybrid-cache.class.spec.js +168 -0
- package/cjs/classes/winston.logger.class.spec.js +330 -0
- package/cjs/constants/index.js +1 -1
- package/cjs/decorators/api-response.decorator.spec.js +126 -0
- package/cjs/decorators/current-user.decorator.spec.js +74 -0
- package/cjs/decorators/log-action.decorator.spec.js +353 -0
- package/cjs/decorators/public.decorator.spec.js +50 -0
- package/cjs/decorators/require-permission.decorator.spec.js +147 -0
- package/cjs/exceptions/base-app.exception.spec.js +157 -0
- package/cjs/exceptions/permission.exception.spec.js +82 -0
- package/cjs/filters/global-exception.filter.spec.js +440 -0
- package/cjs/guards/jwt-auth.guard.spec.js +75 -0
- package/cjs/guards/optional-jwt-auth.guard.spec.js +55 -0
- package/cjs/guards/permission.guard.js +11 -10
- package/cjs/guards/permission.guard.spec.js +358 -0
- package/cjs/interceptors/delete-empty-id-from-body.interceptor.spec.js +107 -0
- package/cjs/interceptors/idempotency.interceptor.spec.js +116 -0
- package/cjs/interceptors/response-meta.interceptor.spec.js +89 -0
- package/cjs/interceptors/set-user-field-on-body.interceptor.spec.js +128 -0
- package/cjs/interceptors/slug.interceptor.spec.js +110 -0
- package/cjs/interfaces/index.js +1 -0
- package/cjs/interfaces/permission-sync-adapter.interface.js +11 -0
- package/cjs/middlewares/logger.middleware.spec.js +462 -0
- package/cjs/modules/cache/cache.module.spec.js +48 -0
- package/cjs/modules/datasource/multi-tenant-datasource.service.spec.js +381 -0
- package/cjs/modules/utils/utils.service.spec.js +169 -0
- package/cjs/utils/date-time.util.spec.js +84 -0
- package/cjs/utils/html-sanitizer.util.spec.js +76 -0
- package/cjs/utils/index.js +1 -0
- package/cjs/utils/permission-cache-key.util.js +31 -0
- package/cjs/utils/permission-cache-key.util.spec.js +88 -0
- package/cjs/utils/query-helpers.util.spec.js +209 -0
- package/cjs/utils/request.util.spec.js +249 -0
- package/cjs/utils/string.util.spec.js +63 -0
- package/constants/index.d.ts +1 -1
- package/fesm/classes/api-controller.class.spec.js +680 -0
- package/fesm/classes/api-service.class.spec.js +805 -0
- package/fesm/classes/handle-custome-db.spec.js +78 -0
- package/fesm/classes/hybrid-cache.class.spec.js +164 -0
- package/fesm/classes/winston.logger.class.spec.js +327 -0
- package/fesm/constants/index.js +1 -1
- package/fesm/decorators/api-response.decorator.spec.js +122 -0
- package/fesm/decorators/current-user.decorator.spec.js +73 -0
- package/fesm/decorators/log-action.decorator.spec.js +349 -0
- package/fesm/decorators/public.decorator.spec.js +46 -0
- package/fesm/decorators/require-permission.decorator.spec.js +143 -0
- package/fesm/exceptions/base-app.exception.spec.js +153 -0
- package/fesm/exceptions/permission.exception.spec.js +78 -0
- package/fesm/filters/global-exception.filter.spec.js +436 -0
- package/fesm/guards/jwt-auth.guard.spec.js +71 -0
- package/fesm/guards/optional-jwt-auth.guard.spec.js +51 -0
- package/fesm/guards/permission.guard.js +12 -11
- package/fesm/guards/permission.guard.spec.js +354 -0
- package/fesm/interceptors/delete-empty-id-from-body.interceptor.spec.js +103 -0
- package/fesm/interceptors/idempotency.interceptor.spec.js +112 -0
- package/fesm/interceptors/response-meta.interceptor.spec.js +85 -0
- package/fesm/interceptors/set-user-field-on-body.interceptor.spec.js +124 -0
- package/fesm/interceptors/slug.interceptor.spec.js +106 -0
- package/fesm/interfaces/index.js +1 -0
- package/fesm/interfaces/permission-sync-adapter.interface.js +1 -0
- package/fesm/middlewares/logger.middleware.spec.js +459 -0
- package/fesm/modules/cache/cache.module.spec.js +44 -0
- package/fesm/modules/datasource/multi-tenant-datasource.service.spec.js +377 -0
- package/fesm/modules/utils/utils.service.spec.js +165 -0
- package/fesm/utils/date-time.util.spec.js +80 -0
- package/fesm/utils/html-sanitizer.util.spec.js +72 -0
- package/fesm/utils/index.js +1 -0
- package/fesm/utils/permission-cache-key.util.js +10 -0
- package/fesm/utils/permission-cache-key.util.spec.js +84 -0
- package/fesm/utils/query-helpers.util.spec.js +205 -0
- package/fesm/utils/request.util.spec.js +245 -0
- package/fesm/utils/string.util.spec.js +59 -0
- package/guards/permission.guard.d.ts +0 -1
- package/interfaces/index.d.ts +1 -0
- package/interfaces/permission-sync-adapter.interface.d.ts +7 -0
- package/package.json +2 -2
- package/utils/index.d.ts +1 -0
- package/utils/permission-cache-key.util.d.ts +9 -0
|
@@ -0,0 +1,680 @@
|
|
|
1
|
+
function _define_property(obj, key, value) {
|
|
2
|
+
if (key in obj) {
|
|
3
|
+
Object.defineProperty(obj, key, {
|
|
4
|
+
value: value,
|
|
5
|
+
enumerable: true,
|
|
6
|
+
configurable: true,
|
|
7
|
+
writable: true
|
|
8
|
+
});
|
|
9
|
+
} else {
|
|
10
|
+
obj[key] = value;
|
|
11
|
+
}
|
|
12
|
+
return obj;
|
|
13
|
+
}
|
|
14
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
15
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
16
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
17
|
+
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
18
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
19
|
+
}
|
|
20
|
+
function _ts_metadata(k, v) {
|
|
21
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
22
|
+
}
|
|
23
|
+
import 'reflect-metadata';
|
|
24
|
+
import { Expose } from 'class-transformer';
|
|
25
|
+
import { HttpStatus } from '@nestjs/common';
|
|
26
|
+
import { createApiController } from './api-controller.class';
|
|
27
|
+
import { Identity } from '../entities';
|
|
28
|
+
import { BaseAppException, NotFoundException } from '../exceptions';
|
|
29
|
+
import { IS_PUBLIC_KEY, PERMISSIONS_KEY } from '../constants';
|
|
30
|
+
import { buildMockUser } from '@test-utils/mocks/logged-user.mock';
|
|
31
|
+
const GUARDS_METADATA = '__guards__';
|
|
32
|
+
const API_EXCLUDE_ENDPOINT_METADATA = 'swagger/apiExcludeEndpoint';
|
|
33
|
+
function guardNames(target, method) {
|
|
34
|
+
const guards = Reflect.getMetadata(GUARDS_METADATA, target.prototype[method]) ?? [];
|
|
35
|
+
return guards.map((g)=>g.name);
|
|
36
|
+
}
|
|
37
|
+
let ProductEntity = class ProductEntity extends Identity {
|
|
38
|
+
constructor(...args){
|
|
39
|
+
super(...args), _define_property(this, "name", void 0), _define_property(this, "secret", void 0);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
let CreateProductDto = class CreateProductDto {
|
|
43
|
+
constructor(){
|
|
44
|
+
_define_property(this, "name", void 0);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
let UpdateProductDto = class UpdateProductDto {
|
|
48
|
+
constructor(){
|
|
49
|
+
_define_property(this, "id", void 0);
|
|
50
|
+
_define_property(this, "name", void 0);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
let ProductResponseDto = class ProductResponseDto {
|
|
54
|
+
constructor(){
|
|
55
|
+
_define_property(this, "id", void 0);
|
|
56
|
+
_define_property(this, "name", void 0);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
_ts_decorate([
|
|
60
|
+
Expose(),
|
|
61
|
+
_ts_metadata("design:type", String)
|
|
62
|
+
], ProductResponseDto.prototype, "id", void 0);
|
|
63
|
+
_ts_decorate([
|
|
64
|
+
Expose(),
|
|
65
|
+
_ts_metadata("design:type", String)
|
|
66
|
+
], ProductResponseDto.prototype, "name", void 0);
|
|
67
|
+
function makeController(options = {}) {
|
|
68
|
+
const ControllerClass = createApiController(CreateProductDto, UpdateProductDto, ProductResponseDto, options);
|
|
69
|
+
const mockService = {
|
|
70
|
+
insert: jest.fn(),
|
|
71
|
+
insertMany: jest.fn(),
|
|
72
|
+
findById: jest.fn(),
|
|
73
|
+
findByIds: jest.fn(),
|
|
74
|
+
update: jest.fn(),
|
|
75
|
+
updateMany: jest.fn(),
|
|
76
|
+
bulkUpsert: jest.fn(),
|
|
77
|
+
getAll: jest.fn(),
|
|
78
|
+
delete: jest.fn()
|
|
79
|
+
};
|
|
80
|
+
const controller = new ControllerClass(mockService);
|
|
81
|
+
return {
|
|
82
|
+
controller,
|
|
83
|
+
mockService,
|
|
84
|
+
ControllerClass
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
describe('createApiController', ()=>{
|
|
88
|
+
const mockUser = buildMockUser();
|
|
89
|
+
describe('isEnabled', ()=>{
|
|
90
|
+
it('should treat every endpoint as enabled by default', ()=>{
|
|
91
|
+
const { controller } = makeController();
|
|
92
|
+
expect(controller.isEnabled('insert')).toBe(true);
|
|
93
|
+
expect(controller.isEnabled('delete')).toBe(true);
|
|
94
|
+
});
|
|
95
|
+
it('should only enable the endpoints explicitly listed', ()=>{
|
|
96
|
+
const { controller } = makeController({
|
|
97
|
+
enabledEndpoints: [
|
|
98
|
+
'getById'
|
|
99
|
+
]
|
|
100
|
+
});
|
|
101
|
+
expect(controller.isEnabled('getById')).toBe(true);
|
|
102
|
+
expect(controller.isEnabled('insert')).toBe(false);
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
describe('insert', ()=>{
|
|
106
|
+
it('should return a success response with the converted dto', async ()=>{
|
|
107
|
+
const { controller, mockService } = makeController();
|
|
108
|
+
mockService.insert.mockResolvedValue({
|
|
109
|
+
id: '1',
|
|
110
|
+
name: 'Widget',
|
|
111
|
+
secret: 'shh'
|
|
112
|
+
});
|
|
113
|
+
const result = await controller.insert({
|
|
114
|
+
name: 'Widget'
|
|
115
|
+
}, mockUser);
|
|
116
|
+
expect(result.success).toBe(true);
|
|
117
|
+
expect(result.messageKey).toBe('item.create.success');
|
|
118
|
+
expect(result.message).toBe('Item created successfully');
|
|
119
|
+
expect(result.data).toEqual({
|
|
120
|
+
id: '1',
|
|
121
|
+
name: 'Widget'
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
it('should strip fields without @Expose() by default (excludeExtraneousValues)', async ()=>{
|
|
125
|
+
const { controller, mockService } = makeController();
|
|
126
|
+
mockService.insert.mockResolvedValue({
|
|
127
|
+
id: '1',
|
|
128
|
+
name: 'Widget',
|
|
129
|
+
secret: 'shh'
|
|
130
|
+
});
|
|
131
|
+
const result = await controller.insert({
|
|
132
|
+
name: 'Widget'
|
|
133
|
+
}, mockUser);
|
|
134
|
+
expect(result.data.secret).toBeUndefined();
|
|
135
|
+
});
|
|
136
|
+
it('should keep all fields when excludeExtraneousValues is false', async ()=>{
|
|
137
|
+
const { controller, mockService } = makeController({
|
|
138
|
+
excludeExtraneousValues: false
|
|
139
|
+
});
|
|
140
|
+
mockService.insert.mockResolvedValue({
|
|
141
|
+
id: '1',
|
|
142
|
+
name: 'Widget',
|
|
143
|
+
secret: 'shh'
|
|
144
|
+
});
|
|
145
|
+
const result = await controller.insert({
|
|
146
|
+
name: 'Widget'
|
|
147
|
+
}, mockUser);
|
|
148
|
+
expect(result.data.secret).toBe('shh');
|
|
149
|
+
});
|
|
150
|
+
it('should use the configured entityName in message/messageKey', async ()=>{
|
|
151
|
+
const { controller, mockService } = makeController({
|
|
152
|
+
entityName: 'product'
|
|
153
|
+
});
|
|
154
|
+
mockService.insert.mockResolvedValue({
|
|
155
|
+
id: '1',
|
|
156
|
+
name: 'Widget'
|
|
157
|
+
});
|
|
158
|
+
const result = await controller.insert({
|
|
159
|
+
name: 'Widget'
|
|
160
|
+
}, mockUser);
|
|
161
|
+
expect(result.message).toBe('Product created successfully');
|
|
162
|
+
expect(result.messageKey).toBe('product.create.success');
|
|
163
|
+
});
|
|
164
|
+
it('should throw a 403 BaseAppException when insert is disabled', async ()=>{
|
|
165
|
+
const { controller } = makeController({
|
|
166
|
+
enabledEndpoints: [
|
|
167
|
+
'getById'
|
|
168
|
+
]
|
|
169
|
+
});
|
|
170
|
+
await expect(controller.insert({
|
|
171
|
+
name: 'Widget'
|
|
172
|
+
}, mockUser)).rejects.toMatchObject({
|
|
173
|
+
message: expect.stringContaining("'insert' is disabled")
|
|
174
|
+
});
|
|
175
|
+
try {
|
|
176
|
+
await controller.insert({
|
|
177
|
+
name: 'Widget'
|
|
178
|
+
}, mockUser);
|
|
179
|
+
fail('expected throw');
|
|
180
|
+
} catch (e) {
|
|
181
|
+
expect(e).toBeInstanceOf(BaseAppException);
|
|
182
|
+
expect(e.getStatus()).toBe(HttpStatus.FORBIDDEN);
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
describe('insertMany', ()=>{
|
|
187
|
+
it('should return bulk meta counting successes and failures', async ()=>{
|
|
188
|
+
const { controller, mockService } = makeController();
|
|
189
|
+
mockService.insertMany.mockResolvedValue([
|
|
190
|
+
{
|
|
191
|
+
id: '1',
|
|
192
|
+
name: 'A'
|
|
193
|
+
}
|
|
194
|
+
]);
|
|
195
|
+
const result = await controller.insertMany([
|
|
196
|
+
{
|
|
197
|
+
name: 'A'
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
name: 'B'
|
|
201
|
+
}
|
|
202
|
+
], mockUser);
|
|
203
|
+
expect(result.data).toHaveLength(1);
|
|
204
|
+
expect(result.meta).toEqual({
|
|
205
|
+
count: 1,
|
|
206
|
+
total: 2,
|
|
207
|
+
failed: 1
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
describe('getById', ()=>{
|
|
212
|
+
it('should return the converted entity', async ()=>{
|
|
213
|
+
const { controller, mockService } = makeController();
|
|
214
|
+
mockService.findById.mockResolvedValue({
|
|
215
|
+
id: '1',
|
|
216
|
+
name: 'Widget'
|
|
217
|
+
});
|
|
218
|
+
const result = await controller.getById('1', {
|
|
219
|
+
select: [
|
|
220
|
+
'name'
|
|
221
|
+
]
|
|
222
|
+
}, mockUser);
|
|
223
|
+
expect(mockService.findById).toHaveBeenCalledWith('1', mockUser, [
|
|
224
|
+
'name'
|
|
225
|
+
]);
|
|
226
|
+
expect(result.data).toEqual({
|
|
227
|
+
id: '1',
|
|
228
|
+
name: 'Widget'
|
|
229
|
+
});
|
|
230
|
+
});
|
|
231
|
+
it('should throw a 403 when getById is disabled', async ()=>{
|
|
232
|
+
const { controller } = makeController({
|
|
233
|
+
enabledEndpoints: []
|
|
234
|
+
});
|
|
235
|
+
await expect(controller.getById('1', {}, mockUser)).rejects.toBeInstanceOf(BaseAppException);
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
describe('getByIds', ()=>{
|
|
239
|
+
it('should return correct pluralization and meta for multiple items', async ()=>{
|
|
240
|
+
const { controller, mockService } = makeController();
|
|
241
|
+
mockService.findByIds.mockResolvedValue([
|
|
242
|
+
{
|
|
243
|
+
id: '1',
|
|
244
|
+
name: 'A'
|
|
245
|
+
},
|
|
246
|
+
{
|
|
247
|
+
id: '2',
|
|
248
|
+
name: 'B'
|
|
249
|
+
}
|
|
250
|
+
]);
|
|
251
|
+
const result = await controller.getByIds({
|
|
252
|
+
ids: [
|
|
253
|
+
'1',
|
|
254
|
+
'2'
|
|
255
|
+
]
|
|
256
|
+
}, mockUser);
|
|
257
|
+
expect(result.message).toBe('2 items retrieved successfully');
|
|
258
|
+
expect(result.meta).toEqual({
|
|
259
|
+
total: 2,
|
|
260
|
+
page: 0,
|
|
261
|
+
pageSize: 2,
|
|
262
|
+
count: 2,
|
|
263
|
+
totalPages: 1
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
it('should use singular wording for exactly one item', async ()=>{
|
|
267
|
+
const { controller, mockService } = makeController();
|
|
268
|
+
mockService.findByIds.mockResolvedValue([
|
|
269
|
+
{
|
|
270
|
+
id: '1',
|
|
271
|
+
name: 'A'
|
|
272
|
+
}
|
|
273
|
+
]);
|
|
274
|
+
const result = await controller.getByIds({
|
|
275
|
+
ids: [
|
|
276
|
+
'1'
|
|
277
|
+
]
|
|
278
|
+
}, mockUser);
|
|
279
|
+
expect(result.message).toBe('1 item retrieved successfully');
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
describe('update / updateMany', ()=>{
|
|
283
|
+
it('update should return the updated entity', async ()=>{
|
|
284
|
+
const { controller, mockService } = makeController();
|
|
285
|
+
mockService.update.mockResolvedValue({
|
|
286
|
+
id: '1',
|
|
287
|
+
name: 'Updated'
|
|
288
|
+
});
|
|
289
|
+
const result = await controller.update({
|
|
290
|
+
id: '1',
|
|
291
|
+
name: 'Updated'
|
|
292
|
+
}, mockUser);
|
|
293
|
+
expect(result.data).toEqual({
|
|
294
|
+
id: '1',
|
|
295
|
+
name: 'Updated'
|
|
296
|
+
});
|
|
297
|
+
expect(result.messageKey).toBe('item.update.success');
|
|
298
|
+
});
|
|
299
|
+
it('updateMany should report per-item failure counts', async ()=>{
|
|
300
|
+
const { controller, mockService } = makeController();
|
|
301
|
+
mockService.updateMany.mockResolvedValue([
|
|
302
|
+
{
|
|
303
|
+
id: '1',
|
|
304
|
+
name: 'A'
|
|
305
|
+
}
|
|
306
|
+
]);
|
|
307
|
+
const result = await controller.updateMany([
|
|
308
|
+
{
|
|
309
|
+
id: '1',
|
|
310
|
+
name: 'A'
|
|
311
|
+
},
|
|
312
|
+
{
|
|
313
|
+
id: '2',
|
|
314
|
+
name: 'B'
|
|
315
|
+
}
|
|
316
|
+
], mockUser);
|
|
317
|
+
expect(result.meta).toEqual({
|
|
318
|
+
count: 1,
|
|
319
|
+
total: 2,
|
|
320
|
+
failed: 1
|
|
321
|
+
});
|
|
322
|
+
});
|
|
323
|
+
});
|
|
324
|
+
describe('bulkUpsert', ()=>{
|
|
325
|
+
it('should split dtos into insert/update sets based on a truthy id', async ()=>{
|
|
326
|
+
const { controller, mockService } = makeController();
|
|
327
|
+
mockService.bulkUpsert.mockResolvedValue([
|
|
328
|
+
{
|
|
329
|
+
id: '1',
|
|
330
|
+
name: 'A'
|
|
331
|
+
}
|
|
332
|
+
]);
|
|
333
|
+
await controller.bulkUpsert([
|
|
334
|
+
{
|
|
335
|
+
name: 'NoId'
|
|
336
|
+
},
|
|
337
|
+
{
|
|
338
|
+
id: '',
|
|
339
|
+
name: 'EmptyId'
|
|
340
|
+
},
|
|
341
|
+
{
|
|
342
|
+
id: 'x-1',
|
|
343
|
+
name: 'HasId'
|
|
344
|
+
}
|
|
345
|
+
], mockUser);
|
|
346
|
+
expect(mockService.bulkUpsert).toHaveBeenCalledWith([
|
|
347
|
+
{
|
|
348
|
+
name: 'NoId'
|
|
349
|
+
},
|
|
350
|
+
{
|
|
351
|
+
id: '',
|
|
352
|
+
name: 'EmptyId'
|
|
353
|
+
}
|
|
354
|
+
], [
|
|
355
|
+
{
|
|
356
|
+
id: 'x-1',
|
|
357
|
+
name: 'HasId'
|
|
358
|
+
}
|
|
359
|
+
], mockUser);
|
|
360
|
+
});
|
|
361
|
+
});
|
|
362
|
+
describe('getByFilter', ()=>{
|
|
363
|
+
it('should query getAll with a single-item page and return the first match', async ()=>{
|
|
364
|
+
const { controller, mockService } = makeController();
|
|
365
|
+
mockService.getAll.mockResolvedValue({
|
|
366
|
+
data: [
|
|
367
|
+
{
|
|
368
|
+
id: '1',
|
|
369
|
+
name: 'A'
|
|
370
|
+
}
|
|
371
|
+
],
|
|
372
|
+
total: 1
|
|
373
|
+
});
|
|
374
|
+
const result = await controller.getByFilter({
|
|
375
|
+
name: 'A'
|
|
376
|
+
}, mockUser);
|
|
377
|
+
expect(mockService.getAll).toHaveBeenCalledWith('', {
|
|
378
|
+
filter: {
|
|
379
|
+
name: 'A'
|
|
380
|
+
},
|
|
381
|
+
pagination: {
|
|
382
|
+
currentPage: 0,
|
|
383
|
+
pageSize: 1
|
|
384
|
+
}
|
|
385
|
+
}, mockUser);
|
|
386
|
+
expect(result.data).toEqual({
|
|
387
|
+
id: '1',
|
|
388
|
+
name: 'A'
|
|
389
|
+
});
|
|
390
|
+
});
|
|
391
|
+
it('should throw NotFoundException when nothing matches the filter', async ()=>{
|
|
392
|
+
const { controller, mockService } = makeController();
|
|
393
|
+
mockService.getAll.mockResolvedValue({
|
|
394
|
+
data: [],
|
|
395
|
+
total: 0
|
|
396
|
+
});
|
|
397
|
+
await expect(controller.getByFilter({
|
|
398
|
+
name: 'ghost'
|
|
399
|
+
}, mockUser)).rejects.toThrow(NotFoundException);
|
|
400
|
+
});
|
|
401
|
+
});
|
|
402
|
+
describe('getAll', ()=>{
|
|
403
|
+
it('should compute pagination meta with hasMore=true when more pages remain', async ()=>{
|
|
404
|
+
const { controller, mockService } = makeController();
|
|
405
|
+
mockService.getAll.mockResolvedValue({
|
|
406
|
+
data: [
|
|
407
|
+
{
|
|
408
|
+
id: '1',
|
|
409
|
+
name: 'A'
|
|
410
|
+
}
|
|
411
|
+
],
|
|
412
|
+
total: 25
|
|
413
|
+
});
|
|
414
|
+
const result = await controller.getAll({
|
|
415
|
+
pagination: {
|
|
416
|
+
currentPage: 0,
|
|
417
|
+
pageSize: 10
|
|
418
|
+
}
|
|
419
|
+
}, mockUser);
|
|
420
|
+
expect(result.meta).toEqual({
|
|
421
|
+
total: 25,
|
|
422
|
+
page: 0,
|
|
423
|
+
pageSize: 10,
|
|
424
|
+
count: 1,
|
|
425
|
+
hasMore: true,
|
|
426
|
+
totalPages: 3
|
|
427
|
+
});
|
|
428
|
+
});
|
|
429
|
+
it('should compute hasMore=false on the last page', async ()=>{
|
|
430
|
+
const { controller, mockService } = makeController();
|
|
431
|
+
mockService.getAll.mockResolvedValue({
|
|
432
|
+
data: [
|
|
433
|
+
{
|
|
434
|
+
id: '3',
|
|
435
|
+
name: 'C'
|
|
436
|
+
}
|
|
437
|
+
],
|
|
438
|
+
total: 25
|
|
439
|
+
});
|
|
440
|
+
const result = await controller.getAll({
|
|
441
|
+
pagination: {
|
|
442
|
+
currentPage: 2,
|
|
443
|
+
pageSize: 10
|
|
444
|
+
}
|
|
445
|
+
}, mockUser);
|
|
446
|
+
expect(result.meta.hasMore).toBe(false);
|
|
447
|
+
expect(result.meta.totalPages).toBe(3);
|
|
448
|
+
});
|
|
449
|
+
it('should default page/pageSize and force totalPages=1 when no pagination is supplied', async ()=>{
|
|
450
|
+
const { controller, mockService } = makeController();
|
|
451
|
+
mockService.getAll.mockResolvedValue({
|
|
452
|
+
data: [
|
|
453
|
+
{
|
|
454
|
+
id: '1',
|
|
455
|
+
name: 'A'
|
|
456
|
+
},
|
|
457
|
+
{
|
|
458
|
+
id: '2',
|
|
459
|
+
name: 'B'
|
|
460
|
+
}
|
|
461
|
+
],
|
|
462
|
+
total: 2
|
|
463
|
+
});
|
|
464
|
+
const result = await controller.getAll({}, mockUser);
|
|
465
|
+
expect(result.meta.page).toBe(0);
|
|
466
|
+
expect(result.meta.pageSize).toBe(2);
|
|
467
|
+
expect(result.meta.totalPages).toBe(1);
|
|
468
|
+
});
|
|
469
|
+
it('should forward the search query param to the service', async ()=>{
|
|
470
|
+
const { controller, mockService } = makeController();
|
|
471
|
+
mockService.getAll.mockResolvedValue({
|
|
472
|
+
data: [],
|
|
473
|
+
total: 0
|
|
474
|
+
});
|
|
475
|
+
await controller.getAll({}, mockUser, 'widget');
|
|
476
|
+
expect(mockService.getAll).toHaveBeenCalledWith('widget', {}, mockUser);
|
|
477
|
+
});
|
|
478
|
+
it('should default search to an empty string when the q param is absent', async ()=>{
|
|
479
|
+
const { controller, mockService } = makeController();
|
|
480
|
+
mockService.getAll.mockResolvedValue({
|
|
481
|
+
data: [],
|
|
482
|
+
total: 0
|
|
483
|
+
});
|
|
484
|
+
await controller.getAll({}, mockUser);
|
|
485
|
+
expect(mockService.getAll).toHaveBeenCalledWith('', {}, mockUser);
|
|
486
|
+
});
|
|
487
|
+
});
|
|
488
|
+
describe('delete', ()=>{
|
|
489
|
+
it('should report singular deletion for one id', async ()=>{
|
|
490
|
+
const { controller, mockService } = makeController();
|
|
491
|
+
mockService.delete.mockResolvedValue(null);
|
|
492
|
+
const result = await controller.delete({
|
|
493
|
+
id: '1',
|
|
494
|
+
type: 'delete'
|
|
495
|
+
}, mockUser);
|
|
496
|
+
expect(result.message).toBe('1 item deleted successfully');
|
|
497
|
+
expect(result.messageKey).toBe('item.delete.success');
|
|
498
|
+
expect(result.messageVariables).toEqual({
|
|
499
|
+
count: 1
|
|
500
|
+
});
|
|
501
|
+
});
|
|
502
|
+
it('should report plural deletion for multiple ids', async ()=>{
|
|
503
|
+
const { controller, mockService } = makeController();
|
|
504
|
+
mockService.delete.mockResolvedValue(null);
|
|
505
|
+
const result = await controller.delete({
|
|
506
|
+
id: [
|
|
507
|
+
'1',
|
|
508
|
+
'2',
|
|
509
|
+
'3'
|
|
510
|
+
],
|
|
511
|
+
type: 'permanent'
|
|
512
|
+
}, mockUser);
|
|
513
|
+
expect(result.message).toBe('3 items deleted successfully');
|
|
514
|
+
});
|
|
515
|
+
it('should label the action "restore" (not delete) for type=restore', async ()=>{
|
|
516
|
+
const { controller, mockService } = makeController();
|
|
517
|
+
mockService.delete.mockResolvedValue(null);
|
|
518
|
+
const result = await controller.delete({
|
|
519
|
+
id: [
|
|
520
|
+
'1',
|
|
521
|
+
'2'
|
|
522
|
+
],
|
|
523
|
+
type: 'restore'
|
|
524
|
+
}, mockUser);
|
|
525
|
+
expect(result.message).toBe('2 items restored successfully');
|
|
526
|
+
expect(result.messageKey).toBe('item.restore.success');
|
|
527
|
+
});
|
|
528
|
+
it('should throw a 403 when delete is disabled', async ()=>{
|
|
529
|
+
const { controller } = makeController({
|
|
530
|
+
enabledEndpoints: [
|
|
531
|
+
'getById'
|
|
532
|
+
]
|
|
533
|
+
});
|
|
534
|
+
await expect(controller.delete({
|
|
535
|
+
id: '1',
|
|
536
|
+
type: 'delete'
|
|
537
|
+
}, mockUser)).rejects.toBeInstanceOf(BaseAppException);
|
|
538
|
+
});
|
|
539
|
+
});
|
|
540
|
+
describe('security configuration', ()=>{
|
|
541
|
+
it('should default every endpoint to public when no security option is given at all', ()=>{
|
|
542
|
+
const { ControllerClass } = makeController();
|
|
543
|
+
expect(Reflect.getMetadata(IS_PUBLIC_KEY, ControllerClass.prototype.insert)).toBe(true);
|
|
544
|
+
expect(Reflect.getMetadata(IS_PUBLIC_KEY, ControllerClass.prototype.delete)).toBe(true);
|
|
545
|
+
expect(guardNames(ControllerClass, 'insert')).toEqual([]);
|
|
546
|
+
});
|
|
547
|
+
it('should apply JwtAuthGuard + ApiBearerAuth to every endpoint for global "jwt" security', ()=>{
|
|
548
|
+
const { ControllerClass } = makeController({
|
|
549
|
+
security: 'jwt'
|
|
550
|
+
});
|
|
551
|
+
expect(guardNames(ControllerClass, 'insert')).toEqual([
|
|
552
|
+
'JwtAuthGuard'
|
|
553
|
+
]);
|
|
554
|
+
expect(guardNames(ControllerClass, 'delete')).toEqual([
|
|
555
|
+
'JwtAuthGuard'
|
|
556
|
+
]);
|
|
557
|
+
expect(Reflect.getMetadata(IS_PUBLIC_KEY, ControllerClass.prototype.insert)).toBeUndefined();
|
|
558
|
+
});
|
|
559
|
+
it('should apply JwtAuthGuard + PermissionGuard and AND-logic permissions by default', ()=>{
|
|
560
|
+
const { ControllerClass } = makeController({
|
|
561
|
+
security: {
|
|
562
|
+
level: 'permission',
|
|
563
|
+
permissions: [
|
|
564
|
+
'product.create'
|
|
565
|
+
]
|
|
566
|
+
}
|
|
567
|
+
});
|
|
568
|
+
expect(guardNames(ControllerClass, 'insert')).toEqual([
|
|
569
|
+
'JwtAuthGuard',
|
|
570
|
+
'PermissionGuard'
|
|
571
|
+
]);
|
|
572
|
+
expect(Reflect.getMetadata(PERMISSIONS_KEY, ControllerClass.prototype.insert)).toEqual({
|
|
573
|
+
permissions: [
|
|
574
|
+
'product.create'
|
|
575
|
+
],
|
|
576
|
+
operator: 'AND'
|
|
577
|
+
});
|
|
578
|
+
});
|
|
579
|
+
it('should use OR-logic permissions when operator is "OR"', ()=>{
|
|
580
|
+
const { ControllerClass } = makeController({
|
|
581
|
+
security: {
|
|
582
|
+
level: 'permission',
|
|
583
|
+
permissions: [
|
|
584
|
+
'a',
|
|
585
|
+
'b'
|
|
586
|
+
],
|
|
587
|
+
operator: 'OR'
|
|
588
|
+
}
|
|
589
|
+
});
|
|
590
|
+
expect(Reflect.getMetadata(PERMISSIONS_KEY, ControllerClass.prototype.insert)).toEqual({
|
|
591
|
+
permissions: [
|
|
592
|
+
'a',
|
|
593
|
+
'b'
|
|
594
|
+
],
|
|
595
|
+
operator: 'OR'
|
|
596
|
+
});
|
|
597
|
+
});
|
|
598
|
+
it('should prefer complex `logic` over `permissions`/`operator` when both are given', ()=>{
|
|
599
|
+
const logic = {
|
|
600
|
+
type: 'action',
|
|
601
|
+
actionId: 'product.create'
|
|
602
|
+
};
|
|
603
|
+
const { ControllerClass } = makeController({
|
|
604
|
+
security: {
|
|
605
|
+
level: 'permission',
|
|
606
|
+
permissions: [
|
|
607
|
+
'ignored'
|
|
608
|
+
],
|
|
609
|
+
logic: logic
|
|
610
|
+
}
|
|
611
|
+
});
|
|
612
|
+
expect(Reflect.getMetadata(PERMISSIONS_KEY, ControllerClass.prototype.insert)).toEqual(logic);
|
|
613
|
+
});
|
|
614
|
+
it('should apply no permission decorator when permission level has neither logic nor permissions', ()=>{
|
|
615
|
+
const { ControllerClass } = makeController({
|
|
616
|
+
security: {
|
|
617
|
+
level: 'permission'
|
|
618
|
+
}
|
|
619
|
+
});
|
|
620
|
+
expect(guardNames(ControllerClass, 'insert')).toEqual([
|
|
621
|
+
'JwtAuthGuard',
|
|
622
|
+
'PermissionGuard'
|
|
623
|
+
]);
|
|
624
|
+
expect(Reflect.getMetadata(PERMISSIONS_KEY, ControllerClass.prototype.insert)).toBeUndefined();
|
|
625
|
+
});
|
|
626
|
+
it('should apply per-endpoint security for configured endpoints', ()=>{
|
|
627
|
+
const { ControllerClass } = makeController({
|
|
628
|
+
security: {
|
|
629
|
+
insert: 'public',
|
|
630
|
+
delete: {
|
|
631
|
+
level: 'permission',
|
|
632
|
+
permissions: [
|
|
633
|
+
'x.delete'
|
|
634
|
+
]
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
});
|
|
638
|
+
expect(Reflect.getMetadata(IS_PUBLIC_KEY, ControllerClass.prototype.insert)).toBe(true);
|
|
639
|
+
expect(guardNames(ControllerClass, 'delete')).toEqual([
|
|
640
|
+
'JwtAuthGuard',
|
|
641
|
+
'PermissionGuard'
|
|
642
|
+
]);
|
|
643
|
+
});
|
|
644
|
+
// KNOWN GAP: the `defaultSecurity = { level: 'jwt' }` fallback (api-controller.class.ts
|
|
645
|
+
// lines 230-232) is dead code. It is only ever assigned to endpoints when `isGlobalSecurity`
|
|
646
|
+
// is true, in which case every endpoint already shares the same global config anyway. In
|
|
647
|
+
// per-endpoint mode (or when no `security` option is passed at all), each endpoint's security
|
|
648
|
+
// is resolved independently via `normalizeSecurity(securityConfig?.<endpoint>)`, and
|
|
649
|
+
// `normalizeSecurity(undefined)` returns `{ level: 'public' }` -- not the intended 'jwt'
|
|
650
|
+
// fallback described in the source comment. This means any endpoint omitted from a
|
|
651
|
+
// per-endpoint `security` config (or the config itself) is silently PUBLIC. Pin the current
|
|
652
|
+
// (buggy) behavior here so a fix is visible as an intentional test change, not a silent one.
|
|
653
|
+
it('BUG: leaves endpoints unconfigured in per-endpoint mode PUBLIC instead of defaulting to jwt', ()=>{
|
|
654
|
+
const { ControllerClass } = makeController({
|
|
655
|
+
security: {
|
|
656
|
+
insert: 'public',
|
|
657
|
+
delete: {
|
|
658
|
+
level: 'permission',
|
|
659
|
+
permissions: [
|
|
660
|
+
'x.delete'
|
|
661
|
+
]
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
});
|
|
665
|
+
expect(Reflect.getMetadata(IS_PUBLIC_KEY, ControllerClass.prototype.getAll)).toBe(true);
|
|
666
|
+
expect(guardNames(ControllerClass, 'getAll')).toEqual([]);
|
|
667
|
+
});
|
|
668
|
+
it('should mark disabled endpoints with ApiExcludeEndpoint', ()=>{
|
|
669
|
+
const { ControllerClass } = makeController({
|
|
670
|
+
enabledEndpoints: [
|
|
671
|
+
'getById'
|
|
672
|
+
]
|
|
673
|
+
});
|
|
674
|
+
expect(Reflect.getMetadata(API_EXCLUDE_ENDPOINT_METADATA, ControllerClass.prototype.insert)).toEqual({
|
|
675
|
+
disable: true
|
|
676
|
+
});
|
|
677
|
+
expect(Reflect.getMetadata(API_EXCLUDE_ENDPOINT_METADATA, ControllerClass.prototype.getById)).toBeUndefined();
|
|
678
|
+
});
|
|
679
|
+
});
|
|
680
|
+
});
|