@flusys/nestjs-shared 6.0.2 → 6.1.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.
- 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/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.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/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/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/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/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.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/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/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/package.json +2 -2
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
const _cachemodule = require("./cache.module");
|
|
6
|
+
const _constants = require("../../constants");
|
|
7
|
+
const mockHybridCacheCtor = jest.fn();
|
|
8
|
+
jest.mock('../../classes/hybrid-cache.class', ()=>({
|
|
9
|
+
HybridCache: class {
|
|
10
|
+
constructor(...args){
|
|
11
|
+
mockHybridCacheCtor(...args);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}));
|
|
15
|
+
describe('CacheModule', ()=>{
|
|
16
|
+
beforeEach(()=>{
|
|
17
|
+
mockHybridCacheCtor.mockClear();
|
|
18
|
+
});
|
|
19
|
+
it('should default to a global module when isGlobal is not specified', ()=>{
|
|
20
|
+
const dynamicModule = _cachemodule.CacheModule.forRoot();
|
|
21
|
+
expect(dynamicModule.module).toBe(_cachemodule.CacheModule);
|
|
22
|
+
expect(dynamicModule.global).toBe(true);
|
|
23
|
+
});
|
|
24
|
+
it('should respect isGlobal=false', ()=>{
|
|
25
|
+
const dynamicModule = _cachemodule.CacheModule.forRoot(false);
|
|
26
|
+
expect(dynamicModule.global).toBe(false);
|
|
27
|
+
});
|
|
28
|
+
it('should provide and export CACHE_INSTANCE', ()=>{
|
|
29
|
+
const dynamicModule = _cachemodule.CacheModule.forRoot();
|
|
30
|
+
expect(dynamicModule.exports).toEqual([
|
|
31
|
+
_constants.CACHE_INSTANCE
|
|
32
|
+
]);
|
|
33
|
+
expect(dynamicModule.providers).toHaveLength(1);
|
|
34
|
+
expect(dynamicModule.providers[0].provide).toBe(_constants.CACHE_INSTANCE);
|
|
35
|
+
});
|
|
36
|
+
it('should build the HybridCache with default ttl/size when the factory runs with no args', ()=>{
|
|
37
|
+
const dynamicModule = _cachemodule.CacheModule.forRoot();
|
|
38
|
+
const factory = dynamicModule.providers[0].useFactory;
|
|
39
|
+
factory();
|
|
40
|
+
expect(mockHybridCacheCtor).toHaveBeenCalledWith(60_000, 5000);
|
|
41
|
+
});
|
|
42
|
+
it('should forward custom memoryTtl and memorySize to the HybridCache factory', ()=>{
|
|
43
|
+
const dynamicModule = _cachemodule.CacheModule.forRoot(true, 30_000, 1000);
|
|
44
|
+
const factory = dynamicModule.providers[0].useFactory;
|
|
45
|
+
factory();
|
|
46
|
+
expect(mockHybridCacheCtor).toHaveBeenCalledWith(30_000, 1000);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
const _common = require("@nestjs/common");
|
|
6
|
+
const _multitenantdatasourceservice = require("./multi-tenant-datasource.service");
|
|
7
|
+
const _constants = require("../../constants");
|
|
8
|
+
function _define_property(obj, key, value) {
|
|
9
|
+
if (key in obj) {
|
|
10
|
+
Object.defineProperty(obj, key, {
|
|
11
|
+
value: value,
|
|
12
|
+
enumerable: true,
|
|
13
|
+
configurable: true,
|
|
14
|
+
writable: true
|
|
15
|
+
});
|
|
16
|
+
} else {
|
|
17
|
+
obj[key] = value;
|
|
18
|
+
}
|
|
19
|
+
return obj;
|
|
20
|
+
}
|
|
21
|
+
const mockInitialize = jest.fn();
|
|
22
|
+
const mockDestroy = jest.fn();
|
|
23
|
+
const mockGetRepository = jest.fn();
|
|
24
|
+
let FakeDataSource = class FakeDataSource {
|
|
25
|
+
constructor(options){
|
|
26
|
+
_define_property(this, "isInitialized", false);
|
|
27
|
+
_define_property(this, "options", void 0);
|
|
28
|
+
_define_property(this, "initialize", jest.fn(async ()=>{
|
|
29
|
+
this.isInitialized = true;
|
|
30
|
+
await mockInitialize(this.options);
|
|
31
|
+
return this;
|
|
32
|
+
}));
|
|
33
|
+
_define_property(this, "destroy", jest.fn(async ()=>{
|
|
34
|
+
this.isInitialized = false;
|
|
35
|
+
await mockDestroy();
|
|
36
|
+
}));
|
|
37
|
+
_define_property(this, "getRepository", (entity)=>mockGetRepository(entity));
|
|
38
|
+
this.options = options;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
jest.mock('typeorm', ()=>({
|
|
42
|
+
...jest.requireActual('typeorm'),
|
|
43
|
+
DataSource: FakeDataSource
|
|
44
|
+
}));
|
|
45
|
+
function resetStatics() {
|
|
46
|
+
const cls = _multitenantdatasourceservice.MultiTenantDataSourceService;
|
|
47
|
+
cls.tenantConnections = new Map();
|
|
48
|
+
cls.singleDataSource = null;
|
|
49
|
+
cls.tenantsRegistry = new Map();
|
|
50
|
+
cls.initialized = false;
|
|
51
|
+
cls.connectionLocks = new Map();
|
|
52
|
+
cls.singleConnectionLock = null;
|
|
53
|
+
}
|
|
54
|
+
describe('MultiTenantDataSourceService', ()=>{
|
|
55
|
+
const defaultDatabaseConfig = {
|
|
56
|
+
type: 'postgres',
|
|
57
|
+
host: 'localhost',
|
|
58
|
+
port: 5432,
|
|
59
|
+
username: 'root',
|
|
60
|
+
password: 'pw',
|
|
61
|
+
database: 'flusys_default'
|
|
62
|
+
};
|
|
63
|
+
beforeEach(()=>{
|
|
64
|
+
jest.clearAllMocks();
|
|
65
|
+
resetStatics();
|
|
66
|
+
});
|
|
67
|
+
function makeOptions(overrides = {}) {
|
|
68
|
+
return {
|
|
69
|
+
defaultDatabaseConfig,
|
|
70
|
+
bootstrapAppConfig: {
|
|
71
|
+
databaseMode: 'single'
|
|
72
|
+
},
|
|
73
|
+
...overrides
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
function makeRequest(headers = {}) {
|
|
77
|
+
return {
|
|
78
|
+
headers
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
describe('database mode', ()=>{
|
|
82
|
+
it('should default to single mode when no options are provided', ()=>{
|
|
83
|
+
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService();
|
|
84
|
+
expect(service.getDatabaseMode()).toBe('single');
|
|
85
|
+
expect(service.isMultiTenant()).toBe(false);
|
|
86
|
+
});
|
|
87
|
+
it('should report multi-tenant mode from bootstrap config', ()=>{
|
|
88
|
+
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions({
|
|
89
|
+
bootstrapAppConfig: {
|
|
90
|
+
databaseMode: 'multi-tenant'
|
|
91
|
+
}
|
|
92
|
+
}));
|
|
93
|
+
expect(service.isMultiTenant()).toBe(true);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
describe('tenant registration', ()=>{
|
|
97
|
+
it('should register tenants from options exactly once across instances', ()=>{
|
|
98
|
+
const tenants = [
|
|
99
|
+
{
|
|
100
|
+
id: 'tenant-a',
|
|
101
|
+
database: 'db_a'
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
id: 'tenant-b',
|
|
105
|
+
database: 'db_b'
|
|
106
|
+
}
|
|
107
|
+
];
|
|
108
|
+
const options = makeOptions({
|
|
109
|
+
tenants
|
|
110
|
+
});
|
|
111
|
+
const service1 = new _multitenantdatasourceservice.MultiTenantDataSourceService(options);
|
|
112
|
+
const service2 = new _multitenantdatasourceservice.MultiTenantDataSourceService(options);
|
|
113
|
+
expect(service1.getTenant('tenant-a')?.database).toBe('db_a');
|
|
114
|
+
expect(service2.getTenant('tenant-b')?.database).toBe('db_b');
|
|
115
|
+
});
|
|
116
|
+
it('should return null for an unregistered tenant', ()=>{
|
|
117
|
+
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions());
|
|
118
|
+
expect(service.getTenant('unknown')).toBeNull();
|
|
119
|
+
});
|
|
120
|
+
it('should be a no-op when constructed without options', ()=>{
|
|
121
|
+
expect(()=>new _multitenantdatasourceservice.MultiTenantDataSourceService()).not.toThrow();
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
describe('getCurrentTenantId', ()=>{
|
|
125
|
+
it('should return null when there is no request (e.g. background job)', ()=>{
|
|
126
|
+
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions());
|
|
127
|
+
expect(service.getCurrentTenantId()).toBeNull();
|
|
128
|
+
});
|
|
129
|
+
it('should return null when the tenant header is missing', ()=>{
|
|
130
|
+
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions(), makeRequest());
|
|
131
|
+
expect(service.getCurrentTenantId()).toBeNull();
|
|
132
|
+
});
|
|
133
|
+
it('should return the tenant id from the header when valid', ()=>{
|
|
134
|
+
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions(), makeRequest({
|
|
135
|
+
'x-tenant-id': 'tenant-a'
|
|
136
|
+
}));
|
|
137
|
+
expect(service.getCurrentTenantId()).toBe('tenant-a');
|
|
138
|
+
});
|
|
139
|
+
it('should reject tenant ids with invalid characters', ()=>{
|
|
140
|
+
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions(), makeRequest({
|
|
141
|
+
'x-tenant-id': 'tenant a; DROP TABLE users;'
|
|
142
|
+
}));
|
|
143
|
+
expect(()=>service.getCurrentTenantId()).toThrow(_common.BadRequestException);
|
|
144
|
+
try {
|
|
145
|
+
service.getCurrentTenantId();
|
|
146
|
+
} catch (e) {
|
|
147
|
+
expect(e.getResponse().messageKey).toBe(_constants.SYSTEM_MESSAGES.INVALID_TENANT_ID);
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
it('should accept tenant ids with letters, numbers, underscores and dashes', ()=>{
|
|
151
|
+
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions(), makeRequest({
|
|
152
|
+
'x-tenant-id': 'Tenant_123-ABC'
|
|
153
|
+
}));
|
|
154
|
+
expect(service.getCurrentTenantId()).toBe('Tenant_123-ABC');
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
describe('getCurrentTenant', ()=>{
|
|
158
|
+
it('should resolve the registered tenant config for the current request', ()=>{
|
|
159
|
+
const tenants = [
|
|
160
|
+
{
|
|
161
|
+
id: 'tenant-a',
|
|
162
|
+
database: 'db_a'
|
|
163
|
+
}
|
|
164
|
+
];
|
|
165
|
+
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions({
|
|
166
|
+
tenants
|
|
167
|
+
}), makeRequest({
|
|
168
|
+
'x-tenant-id': 'tenant-a'
|
|
169
|
+
}));
|
|
170
|
+
expect(service.getCurrentTenant()?.database).toBe('db_a');
|
|
171
|
+
});
|
|
172
|
+
it('should return null when there is no current tenant id', ()=>{
|
|
173
|
+
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions(), makeRequest());
|
|
174
|
+
expect(service.getCurrentTenant()).toBeNull();
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
describe('getSingleDataSource / getDataSource (single mode)', ()=>{
|
|
178
|
+
it('should create and initialize a datasource from the default config', async ()=>{
|
|
179
|
+
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions());
|
|
180
|
+
const ds = await service.getDataSource();
|
|
181
|
+
expect(ds.isInitialized).toBe(true);
|
|
182
|
+
expect(mockInitialize).toHaveBeenCalledWith(expect.objectContaining({
|
|
183
|
+
database: 'flusys_default'
|
|
184
|
+
}));
|
|
185
|
+
});
|
|
186
|
+
it('should reuse the cached datasource on subsequent calls', async ()=>{
|
|
187
|
+
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions());
|
|
188
|
+
const ds1 = await service.getDataSource();
|
|
189
|
+
const ds2 = await service.getDataSource();
|
|
190
|
+
expect(ds1).toBe(ds2);
|
|
191
|
+
expect(mockInitialize).toHaveBeenCalledTimes(1);
|
|
192
|
+
});
|
|
193
|
+
it('should dedupe concurrent creation attempts into a single connection', async ()=>{
|
|
194
|
+
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions());
|
|
195
|
+
const [ds1, ds2] = await Promise.all([
|
|
196
|
+
service.getDataSource(),
|
|
197
|
+
service.getDataSource()
|
|
198
|
+
]);
|
|
199
|
+
expect(ds1).toBe(ds2);
|
|
200
|
+
expect(mockInitialize).toHaveBeenCalledTimes(1);
|
|
201
|
+
});
|
|
202
|
+
it('should throw InternalServerErrorException when no database config is available', async ()=>{
|
|
203
|
+
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions({
|
|
204
|
+
defaultDatabaseConfig: undefined
|
|
205
|
+
}));
|
|
206
|
+
await expect(service.getDataSource()).rejects.toThrow(_common.InternalServerErrorException);
|
|
207
|
+
});
|
|
208
|
+
it('should fall back to tenantDefaultDatabaseConfig when defaultDatabaseConfig is absent', async ()=>{
|
|
209
|
+
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions({
|
|
210
|
+
defaultDatabaseConfig: undefined,
|
|
211
|
+
tenantDefaultDatabaseConfig: defaultDatabaseConfig
|
|
212
|
+
}));
|
|
213
|
+
const ds = await service.getDataSource();
|
|
214
|
+
expect(ds.isInitialized).toBe(true);
|
|
215
|
+
});
|
|
216
|
+
it('getRepository should resolve the datasource then delegate to it', async ()=>{
|
|
217
|
+
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions());
|
|
218
|
+
let Product = class Product {
|
|
219
|
+
};
|
|
220
|
+
mockGetRepository.mockReturnValue('the-repo');
|
|
221
|
+
const repo = await service.getRepository(Product);
|
|
222
|
+
expect(mockGetRepository).toHaveBeenCalledWith(Product);
|
|
223
|
+
expect(repo).toBe('the-repo');
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
describe('getTenantDataSource / getDataSource (multi-tenant mode)', ()=>{
|
|
227
|
+
function multiTenantOptions(tenants) {
|
|
228
|
+
return makeOptions({
|
|
229
|
+
bootstrapAppConfig: {
|
|
230
|
+
databaseMode: 'multi-tenant'
|
|
231
|
+
},
|
|
232
|
+
tenants
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
it('should throw BadRequestException when no tenant header is present', async ()=>{
|
|
236
|
+
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(multiTenantOptions([
|
|
237
|
+
{
|
|
238
|
+
id: 'tenant-a',
|
|
239
|
+
database: 'db_a'
|
|
240
|
+
}
|
|
241
|
+
]), makeRequest());
|
|
242
|
+
await expect(service.getDataSource()).rejects.toThrow(_common.BadRequestException);
|
|
243
|
+
});
|
|
244
|
+
it('should throw BadRequestException when the tenant is not registered', async ()=>{
|
|
245
|
+
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(multiTenantOptions([]), makeRequest({
|
|
246
|
+
'x-tenant-id': 'ghost-tenant'
|
|
247
|
+
}));
|
|
248
|
+
await expect(service.getDataSource()).rejects.toThrow(_common.BadRequestException);
|
|
249
|
+
});
|
|
250
|
+
it('should create a tenant-specific connection merging tenant overrides onto the default config', async ()=>{
|
|
251
|
+
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(multiTenantOptions([
|
|
252
|
+
{
|
|
253
|
+
id: 'tenant-a',
|
|
254
|
+
database: 'db_a',
|
|
255
|
+
host: 'tenant-a-host'
|
|
256
|
+
}
|
|
257
|
+
]), makeRequest({
|
|
258
|
+
'x-tenant-id': 'tenant-a'
|
|
259
|
+
}));
|
|
260
|
+
const ds = await service.getDataSource();
|
|
261
|
+
expect(ds.isInitialized).toBe(true);
|
|
262
|
+
expect(mockInitialize).toHaveBeenCalledWith(expect.objectContaining({
|
|
263
|
+
database: 'db_a',
|
|
264
|
+
host: 'tenant-a-host',
|
|
265
|
+
username: defaultDatabaseConfig.username
|
|
266
|
+
}));
|
|
267
|
+
});
|
|
268
|
+
it('should cache the tenant connection and reuse it for subsequent requests to the same tenant', async ()=>{
|
|
269
|
+
const tenants = [
|
|
270
|
+
{
|
|
271
|
+
id: 'tenant-a',
|
|
272
|
+
database: 'db_a'
|
|
273
|
+
}
|
|
274
|
+
];
|
|
275
|
+
const service1 = new _multitenantdatasourceservice.MultiTenantDataSourceService(multiTenantOptions(tenants), makeRequest({
|
|
276
|
+
'x-tenant-id': 'tenant-a'
|
|
277
|
+
}));
|
|
278
|
+
const service2 = new _multitenantdatasourceservice.MultiTenantDataSourceService(multiTenantOptions(tenants), makeRequest({
|
|
279
|
+
'x-tenant-id': 'tenant-a'
|
|
280
|
+
}));
|
|
281
|
+
const ds1 = await service1.getDataSource();
|
|
282
|
+
const ds2 = await service2.getDataSource();
|
|
283
|
+
expect(ds1).toBe(ds2);
|
|
284
|
+
expect(mockInitialize).toHaveBeenCalledTimes(1);
|
|
285
|
+
});
|
|
286
|
+
it('should maintain separate connections per tenant', async ()=>{
|
|
287
|
+
const tenants = [
|
|
288
|
+
{
|
|
289
|
+
id: 'tenant-a',
|
|
290
|
+
database: 'db_a'
|
|
291
|
+
},
|
|
292
|
+
{
|
|
293
|
+
id: 'tenant-b',
|
|
294
|
+
database: 'db_b'
|
|
295
|
+
}
|
|
296
|
+
];
|
|
297
|
+
const serviceA = new _multitenantdatasourceservice.MultiTenantDataSourceService(multiTenantOptions(tenants), makeRequest({
|
|
298
|
+
'x-tenant-id': 'tenant-a'
|
|
299
|
+
}));
|
|
300
|
+
const serviceB = new _multitenantdatasourceservice.MultiTenantDataSourceService(multiTenantOptions(tenants), makeRequest({
|
|
301
|
+
'x-tenant-id': 'tenant-b'
|
|
302
|
+
}));
|
|
303
|
+
const dsA = await serviceA.getDataSource();
|
|
304
|
+
const dsB = await serviceB.getDataSource();
|
|
305
|
+
expect(dsA).not.toBe(dsB);
|
|
306
|
+
expect(mockInitialize).toHaveBeenCalledTimes(2);
|
|
307
|
+
});
|
|
308
|
+
it('should throw InternalServerErrorException when no default config exists to base tenant configs on', async ()=>{
|
|
309
|
+
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService({
|
|
310
|
+
...multiTenantOptions([
|
|
311
|
+
{
|
|
312
|
+
id: 'tenant-a',
|
|
313
|
+
database: 'db_a'
|
|
314
|
+
}
|
|
315
|
+
]),
|
|
316
|
+
defaultDatabaseConfig: undefined,
|
|
317
|
+
tenantDefaultDatabaseConfig: undefined
|
|
318
|
+
}, makeRequest({
|
|
319
|
+
'x-tenant-id': 'tenant-a'
|
|
320
|
+
}));
|
|
321
|
+
await expect(service.getDataSource()).rejects.toThrow(_common.InternalServerErrorException);
|
|
322
|
+
});
|
|
323
|
+
});
|
|
324
|
+
describe('closeTenantConnection', ()=>{
|
|
325
|
+
it('should destroy and remove an initialized tenant connection', async ()=>{
|
|
326
|
+
const tenants = [
|
|
327
|
+
{
|
|
328
|
+
id: 'tenant-a',
|
|
329
|
+
database: 'db_a'
|
|
330
|
+
}
|
|
331
|
+
];
|
|
332
|
+
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions({
|
|
333
|
+
bootstrapAppConfig: {
|
|
334
|
+
databaseMode: 'multi-tenant'
|
|
335
|
+
},
|
|
336
|
+
tenants
|
|
337
|
+
}), makeRequest({
|
|
338
|
+
'x-tenant-id': 'tenant-a'
|
|
339
|
+
}));
|
|
340
|
+
await service.getDataSource();
|
|
341
|
+
await service.closeTenantConnection('tenant-a');
|
|
342
|
+
expect(mockDestroy).toHaveBeenCalledTimes(1);
|
|
343
|
+
expect(_multitenantdatasourceservice.MultiTenantDataSourceService.tenantConnections.has('tenant-a')).toBe(false);
|
|
344
|
+
});
|
|
345
|
+
it('should be a no-op for a tenant with no active connection', async ()=>{
|
|
346
|
+
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions());
|
|
347
|
+
await expect(service.closeTenantConnection('never-connected')).resolves.toBeUndefined();
|
|
348
|
+
expect(mockDestroy).not.toHaveBeenCalled();
|
|
349
|
+
});
|
|
350
|
+
});
|
|
351
|
+
describe('onModuleDestroy', ()=>{
|
|
352
|
+
it('should close every tracked tenant connection', async ()=>{
|
|
353
|
+
const tenants = [
|
|
354
|
+
{
|
|
355
|
+
id: 'tenant-a',
|
|
356
|
+
database: 'db_a'
|
|
357
|
+
},
|
|
358
|
+
{
|
|
359
|
+
id: 'tenant-b',
|
|
360
|
+
database: 'db_b'
|
|
361
|
+
}
|
|
362
|
+
];
|
|
363
|
+
const options = makeOptions({
|
|
364
|
+
bootstrapAppConfig: {
|
|
365
|
+
databaseMode: 'multi-tenant'
|
|
366
|
+
},
|
|
367
|
+
tenants
|
|
368
|
+
});
|
|
369
|
+
const serviceA = new _multitenantdatasourceservice.MultiTenantDataSourceService(options, makeRequest({
|
|
370
|
+
'x-tenant-id': 'tenant-a'
|
|
371
|
+
}));
|
|
372
|
+
const serviceB = new _multitenantdatasourceservice.MultiTenantDataSourceService(options, makeRequest({
|
|
373
|
+
'x-tenant-id': 'tenant-b'
|
|
374
|
+
}));
|
|
375
|
+
await serviceA.getDataSource();
|
|
376
|
+
await serviceB.getDataSource();
|
|
377
|
+
await serviceA.onModuleDestroy();
|
|
378
|
+
expect(mockDestroy).toHaveBeenCalledTimes(2);
|
|
379
|
+
});
|
|
380
|
+
});
|
|
381
|
+
});
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
const _utilsservice = require("./utils.service");
|
|
6
|
+
describe('UtilsService', ()=>{
|
|
7
|
+
let service;
|
|
8
|
+
let mockCache;
|
|
9
|
+
beforeEach(()=>{
|
|
10
|
+
service = new _utilsservice.UtilsService();
|
|
11
|
+
mockCache = {
|
|
12
|
+
get: jest.fn(),
|
|
13
|
+
set: jest.fn(),
|
|
14
|
+
del: jest.fn()
|
|
15
|
+
};
|
|
16
|
+
});
|
|
17
|
+
describe('getCacheKey', ()=>{
|
|
18
|
+
it('should build an "all" key from the entity name and params when no id is given', ()=>{
|
|
19
|
+
const key = service.getCacheKey('product', {
|
|
20
|
+
page: 1
|
|
21
|
+
});
|
|
22
|
+
expect(key).toBe('entity_product_all_{"page":1}');
|
|
23
|
+
});
|
|
24
|
+
it('should build a per-id key with a select suffix when params are given', ()=>{
|
|
25
|
+
const key = service.getCacheKey('product', {
|
|
26
|
+
fields: [
|
|
27
|
+
'name'
|
|
28
|
+
]
|
|
29
|
+
}, 'prod-1');
|
|
30
|
+
expect(key).toBe('entity_product_id_prod-1_select_{"fields":["name"]}');
|
|
31
|
+
});
|
|
32
|
+
it('should build a per-id key without a select suffix when params are falsy', ()=>{
|
|
33
|
+
const key = service.getCacheKey('product', null, 'prod-1');
|
|
34
|
+
expect(key).toBe('entity_product_id_prod-1');
|
|
35
|
+
});
|
|
36
|
+
it('should prefix the key with the tenant id when provided', ()=>{
|
|
37
|
+
const key = service.getCacheKey('product', {
|
|
38
|
+
page: 1
|
|
39
|
+
}, undefined, 'tenant-a');
|
|
40
|
+
expect(key).toBe('tenant_tenant-a_entity_product_all_{"page":1}');
|
|
41
|
+
});
|
|
42
|
+
it('should prefix a per-id key with the tenant id too', ()=>{
|
|
43
|
+
const key = service.getCacheKey('product', undefined, 'prod-1', 'tenant-a');
|
|
44
|
+
expect(key).toBe('tenant_tenant-a_entity_product_id_prod-1');
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
describe('trackCacheKey', ()=>{
|
|
48
|
+
it('should create a new tracking list when none exists yet', async ()=>{
|
|
49
|
+
mockCache.get.mockResolvedValue(undefined);
|
|
50
|
+
await service.trackCacheKey('cache-key-1', 'product', mockCache);
|
|
51
|
+
expect(mockCache.set).toHaveBeenCalledWith('entity_product_keys', [
|
|
52
|
+
'cache-key-1'
|
|
53
|
+
]);
|
|
54
|
+
});
|
|
55
|
+
it('should append to an existing tracking list without duplicating entries', async ()=>{
|
|
56
|
+
mockCache.get.mockResolvedValue([
|
|
57
|
+
'cache-key-1'
|
|
58
|
+
]);
|
|
59
|
+
await service.trackCacheKey('cache-key-1', 'product', mockCache);
|
|
60
|
+
expect(mockCache.set).toHaveBeenCalledWith('entity_product_keys', [
|
|
61
|
+
'cache-key-1'
|
|
62
|
+
]);
|
|
63
|
+
});
|
|
64
|
+
it('should append a new key alongside existing ones', async ()=>{
|
|
65
|
+
mockCache.get.mockResolvedValue([
|
|
66
|
+
'cache-key-1'
|
|
67
|
+
]);
|
|
68
|
+
await service.trackCacheKey('cache-key-2', 'product', mockCache);
|
|
69
|
+
expect(mockCache.set).toHaveBeenCalledWith('entity_product_keys', [
|
|
70
|
+
'cache-key-1',
|
|
71
|
+
'cache-key-2'
|
|
72
|
+
]);
|
|
73
|
+
});
|
|
74
|
+
it('should scope the tracking key by entity id and tenant', async ()=>{
|
|
75
|
+
mockCache.get.mockResolvedValue(undefined);
|
|
76
|
+
await service.trackCacheKey('cache-key-1', 'product', mockCache, 'prod-1', 'tenant-a');
|
|
77
|
+
expect(mockCache.get).toHaveBeenCalledWith('tenant_tenant-a_entity_product_id_prod-1_keys');
|
|
78
|
+
expect(mockCache.set).toHaveBeenCalledWith('tenant_tenant-a_entity_product_id_prod-1_keys', [
|
|
79
|
+
'cache-key-1'
|
|
80
|
+
]);
|
|
81
|
+
});
|
|
82
|
+
it('should swallow cache read errors without throwing', async ()=>{
|
|
83
|
+
mockCache.get.mockRejectedValue(new Error('cache down'));
|
|
84
|
+
await expect(service.trackCacheKey('k', 'product', mockCache)).resolves.toBeUndefined();
|
|
85
|
+
expect(mockCache.set).not.toHaveBeenCalled();
|
|
86
|
+
});
|
|
87
|
+
it('should swallow cache write errors without throwing', async ()=>{
|
|
88
|
+
mockCache.get.mockResolvedValue([]);
|
|
89
|
+
mockCache.set.mockRejectedValue(new Error('cache down'));
|
|
90
|
+
await expect(service.trackCacheKey('k', 'product', mockCache)).resolves.toBeUndefined();
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
describe('clearCache', ()=>{
|
|
94
|
+
it('should delete every tracked key plus the tracking key itself', async ()=>{
|
|
95
|
+
mockCache.get.mockResolvedValue([
|
|
96
|
+
'k1',
|
|
97
|
+
'k2'
|
|
98
|
+
]);
|
|
99
|
+
await service.clearCache('product', mockCache);
|
|
100
|
+
expect(mockCache.del).toHaveBeenCalledWith('k1');
|
|
101
|
+
expect(mockCache.del).toHaveBeenCalledWith('k2');
|
|
102
|
+
expect(mockCache.del).toHaveBeenCalledWith('entity_product_keys');
|
|
103
|
+
expect(mockCache.del).toHaveBeenCalledTimes(3);
|
|
104
|
+
});
|
|
105
|
+
it('should only delete the tracking key when no keys were tracked', async ()=>{
|
|
106
|
+
mockCache.get.mockResolvedValue(undefined);
|
|
107
|
+
await service.clearCache('product', mockCache);
|
|
108
|
+
expect(mockCache.del).toHaveBeenCalledTimes(1);
|
|
109
|
+
expect(mockCache.del).toHaveBeenCalledWith('entity_product_keys');
|
|
110
|
+
});
|
|
111
|
+
it('should continue deleting remaining keys even if one deletion rejects', async ()=>{
|
|
112
|
+
mockCache.get.mockResolvedValue([
|
|
113
|
+
'k1',
|
|
114
|
+
'k2'
|
|
115
|
+
]);
|
|
116
|
+
mockCache.del.mockImplementation((key)=>key === 'k1' ? Promise.reject(new Error('fail')) : Promise.resolve());
|
|
117
|
+
await service.clearCache('product', mockCache);
|
|
118
|
+
expect(mockCache.del).toHaveBeenCalledWith('k2');
|
|
119
|
+
expect(mockCache.del).toHaveBeenCalledWith('entity_product_keys');
|
|
120
|
+
});
|
|
121
|
+
it('should scope deletion by entity id and tenant', async ()=>{
|
|
122
|
+
mockCache.get.mockResolvedValue([
|
|
123
|
+
'k1'
|
|
124
|
+
]);
|
|
125
|
+
await service.clearCache('product', mockCache, 'prod-1', 'tenant-a');
|
|
126
|
+
expect(mockCache.get).toHaveBeenCalledWith('tenant_tenant-a_entity_product_id_prod-1_keys');
|
|
127
|
+
});
|
|
128
|
+
it('should swallow errors from the initial cache read', async ()=>{
|
|
129
|
+
mockCache.get.mockRejectedValue(new Error('cache down'));
|
|
130
|
+
await expect(service.clearCache('product', mockCache)).resolves.toBeUndefined();
|
|
131
|
+
expect(mockCache.del).not.toHaveBeenCalled();
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
describe('transformToSlug', ()=>{
|
|
135
|
+
it('should lowercase and dash-separate a normal string', ()=>{
|
|
136
|
+
expect(service.transformToSlug('Hello World')).toBe('hello-world');
|
|
137
|
+
});
|
|
138
|
+
it('should collapse runs of non-alphanumeric characters into a single dash', ()=>{
|
|
139
|
+
expect(service.transformToSlug('Hello World!!!')).toBe('hello-world-');
|
|
140
|
+
});
|
|
141
|
+
it('should trim leading/trailing whitespace before slugging', ()=>{
|
|
142
|
+
expect(service.transformToSlug(' Hello World ')).toBe('hello-world');
|
|
143
|
+
});
|
|
144
|
+
it('should append a random numeric salt when requested', ()=>{
|
|
145
|
+
const randomSpy = jest.spyOn(service, 'getRandomInt').mockReturnValue(42);
|
|
146
|
+
expect(service.transformToSlug('Hello World', true)).toBe('hello-world-42');
|
|
147
|
+
randomSpy.mockRestore();
|
|
148
|
+
});
|
|
149
|
+
it('should handle an empty string', ()=>{
|
|
150
|
+
expect(service.transformToSlug('')).toBe('');
|
|
151
|
+
});
|
|
152
|
+
it('should handle strings that are entirely non-alphanumeric', ()=>{
|
|
153
|
+
expect(service.transformToSlug('!!!')).toBe('-');
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
describe('getRandomInt', ()=>{
|
|
157
|
+
it('should return the exact bound when min equals max', ()=>{
|
|
158
|
+
expect(service.getRandomInt(5, 5)).toBe(5);
|
|
159
|
+
});
|
|
160
|
+
it('should stay within an inclusive [min, max] range across many samples', ()=>{
|
|
161
|
+
for(let i = 0; i < 200; i++){
|
|
162
|
+
const value = service.getRandomInt(1, 10);
|
|
163
|
+
expect(value).toBeGreaterThanOrEqual(1);
|
|
164
|
+
expect(value).toBeLessThanOrEqual(10);
|
|
165
|
+
expect(Number.isInteger(value)).toBe(true);
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
});
|