@flusys/nestjs-notification 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/adapters/notification.adapter.spec.js +73 -0
- package/cjs/controllers/notification.controller.spec.js +214 -0
- package/cjs/docs/notification-swagger.config.spec.js +68 -0
- package/cjs/entities/index.spec.js +13 -0
- package/cjs/gateways/notification.gateway.spec.js +359 -0
- package/cjs/modules/notification.module.spec.js +152 -0
- package/cjs/services/notification-config.service.spec.js +113 -0
- package/cjs/services/notification-datasource.provider.spec.js +136 -0
- package/cjs/services/notification-helper.service.spec.js +306 -0
- package/cjs/services/notification.service.spec.js +490 -0
- package/fesm/adapters/notification.adapter.spec.js +69 -0
- package/fesm/controllers/notification.controller.spec.js +210 -0
- package/fesm/docs/notification-swagger.config.spec.js +64 -0
- package/fesm/entities/index.spec.js +9 -0
- package/fesm/gateways/notification.gateway.spec.js +355 -0
- package/fesm/modules/notification.module.spec.js +148 -0
- package/fesm/services/notification-config.service.spec.js +109 -0
- package/fesm/services/notification-datasource.provider.spec.js +132 -0
- package/fesm/services/notification-helper.service.spec.js +302 -0
- package/fesm/services/notification.service.spec.js +486 -0
- package/package.json +3 -3
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
const _notificationgateway = require("./notification.gateway");
|
|
6
|
+
function createMockServer() {
|
|
7
|
+
const room = {
|
|
8
|
+
emit: jest.fn()
|
|
9
|
+
};
|
|
10
|
+
return {
|
|
11
|
+
to: jest.fn().mockReturnValue(room),
|
|
12
|
+
emit: jest.fn()
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
function createMockSocket(overrides = {}) {
|
|
16
|
+
return {
|
|
17
|
+
id: 'socket-id-1',
|
|
18
|
+
handshake: {
|
|
19
|
+
auth: {},
|
|
20
|
+
query: {},
|
|
21
|
+
headers: {}
|
|
22
|
+
},
|
|
23
|
+
data: {},
|
|
24
|
+
join: jest.fn(),
|
|
25
|
+
disconnect: jest.fn(),
|
|
26
|
+
...overrides
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
describe('NotificationGateway', ()=>{
|
|
30
|
+
let gateway;
|
|
31
|
+
let mockConfigService;
|
|
32
|
+
let mockJwtService;
|
|
33
|
+
let mockServer;
|
|
34
|
+
beforeEach(()=>{
|
|
35
|
+
mockConfigService = {
|
|
36
|
+
getJwtSecret: jest.fn().mockReturnValue('jwt-secret')
|
|
37
|
+
};
|
|
38
|
+
mockJwtService = {
|
|
39
|
+
verify: jest.fn()
|
|
40
|
+
};
|
|
41
|
+
gateway = new _notificationgateway.NotificationGateway(mockConfigService, mockJwtService);
|
|
42
|
+
mockServer = createMockServer();
|
|
43
|
+
gateway.server = mockServer;
|
|
44
|
+
});
|
|
45
|
+
describe('afterInit', ()=>{
|
|
46
|
+
it('does not throw', ()=>{
|
|
47
|
+
expect(()=>gateway.afterInit()).not.toThrow();
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
describe('handleConnection', ()=>{
|
|
51
|
+
it('disconnects the client when no token is present anywhere', async ()=>{
|
|
52
|
+
const client = createMockSocket();
|
|
53
|
+
await gateway.handleConnection(client);
|
|
54
|
+
expect(client.disconnect).toHaveBeenCalled();
|
|
55
|
+
expect(mockJwtService.verify).not.toHaveBeenCalled();
|
|
56
|
+
});
|
|
57
|
+
it('accepts a token from handshake.auth and joins the user + company rooms', async ()=>{
|
|
58
|
+
const client = createMockSocket({
|
|
59
|
+
handshake: {
|
|
60
|
+
auth: {
|
|
61
|
+
token: 'auth-token'
|
|
62
|
+
},
|
|
63
|
+
query: {},
|
|
64
|
+
headers: {}
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
mockJwtService.verify.mockReturnValue({
|
|
68
|
+
id: 'user-1',
|
|
69
|
+
companyId: 'company-1'
|
|
70
|
+
});
|
|
71
|
+
await gateway.handleConnection(client);
|
|
72
|
+
expect(mockJwtService.verify).toHaveBeenCalledWith('auth-token', {
|
|
73
|
+
secret: 'jwt-secret',
|
|
74
|
+
algorithms: [
|
|
75
|
+
'HS256'
|
|
76
|
+
]
|
|
77
|
+
});
|
|
78
|
+
expect(client.data.userId).toBe('user-1');
|
|
79
|
+
expect(client.data.companyId).toBe('company-1');
|
|
80
|
+
expect(client.join).toHaveBeenCalledWith('user:user-1');
|
|
81
|
+
expect(client.join).toHaveBeenCalledWith('company:company-1');
|
|
82
|
+
expect(client.join).toHaveBeenCalledWith('user:user-1:company:company-1');
|
|
83
|
+
expect(client.disconnect).not.toHaveBeenCalled();
|
|
84
|
+
expect(gateway.isUserOnline('user-1')).toBe(true);
|
|
85
|
+
});
|
|
86
|
+
it('accepts a token from the query string', async ()=>{
|
|
87
|
+
const client = createMockSocket({
|
|
88
|
+
handshake: {
|
|
89
|
+
auth: {},
|
|
90
|
+
query: {
|
|
91
|
+
token: 'query-token'
|
|
92
|
+
},
|
|
93
|
+
headers: {}
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
mockJwtService.verify.mockReturnValue({
|
|
97
|
+
id: 'user-2'
|
|
98
|
+
});
|
|
99
|
+
await gateway.handleConnection(client);
|
|
100
|
+
expect(mockJwtService.verify).toHaveBeenCalledWith('query-token', expect.any(Object));
|
|
101
|
+
expect(client.join).toHaveBeenCalledWith('user:user-2');
|
|
102
|
+
});
|
|
103
|
+
it('accepts a token from the Authorization header', async ()=>{
|
|
104
|
+
const client = createMockSocket({
|
|
105
|
+
handshake: {
|
|
106
|
+
auth: {},
|
|
107
|
+
query: {},
|
|
108
|
+
headers: {
|
|
109
|
+
authorization: 'Bearer header-token'
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
mockJwtService.verify.mockReturnValue({
|
|
114
|
+
id: 'user-3'
|
|
115
|
+
});
|
|
116
|
+
await gateway.handleConnection(client);
|
|
117
|
+
expect(mockJwtService.verify).toHaveBeenCalledWith('header-token', expect.any(Object));
|
|
118
|
+
expect(client.join).toHaveBeenCalledWith('user:user-3');
|
|
119
|
+
});
|
|
120
|
+
it('ignores a malformed Authorization header without the Bearer prefix', async ()=>{
|
|
121
|
+
const client = createMockSocket({
|
|
122
|
+
handshake: {
|
|
123
|
+
auth: {},
|
|
124
|
+
query: {},
|
|
125
|
+
headers: {
|
|
126
|
+
authorization: 'Basic abc123'
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
await gateway.handleConnection(client);
|
|
131
|
+
expect(client.disconnect).toHaveBeenCalled();
|
|
132
|
+
expect(mockJwtService.verify).not.toHaveBeenCalled();
|
|
133
|
+
});
|
|
134
|
+
it('joins only the personal room when the token payload has no companyId', async ()=>{
|
|
135
|
+
const client = createMockSocket({
|
|
136
|
+
handshake: {
|
|
137
|
+
auth: {
|
|
138
|
+
token: 't'
|
|
139
|
+
},
|
|
140
|
+
query: {},
|
|
141
|
+
headers: {}
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
mockJwtService.verify.mockReturnValue({
|
|
145
|
+
id: 'user-4'
|
|
146
|
+
});
|
|
147
|
+
await gateway.handleConnection(client);
|
|
148
|
+
expect(client.join).toHaveBeenCalledWith('user:user-4');
|
|
149
|
+
expect(client.join).toHaveBeenCalledTimes(1);
|
|
150
|
+
});
|
|
151
|
+
it('disconnects the client when the JWT secret is not configured', async ()=>{
|
|
152
|
+
mockConfigService.getJwtSecret.mockReturnValue(undefined);
|
|
153
|
+
const client = createMockSocket({
|
|
154
|
+
handshake: {
|
|
155
|
+
auth: {
|
|
156
|
+
token: 't'
|
|
157
|
+
},
|
|
158
|
+
query: {},
|
|
159
|
+
headers: {}
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
await gateway.handleConnection(client);
|
|
163
|
+
expect(client.disconnect).toHaveBeenCalled();
|
|
164
|
+
expect(mockJwtService.verify).not.toHaveBeenCalled();
|
|
165
|
+
});
|
|
166
|
+
it('disconnects the client when token verification throws (invalid/expired token)', async ()=>{
|
|
167
|
+
const client = createMockSocket({
|
|
168
|
+
handshake: {
|
|
169
|
+
auth: {
|
|
170
|
+
token: 'bad-token'
|
|
171
|
+
},
|
|
172
|
+
query: {},
|
|
173
|
+
headers: {}
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
mockJwtService.verify.mockImplementation(()=>{
|
|
177
|
+
throw new Error('jwt expired');
|
|
178
|
+
});
|
|
179
|
+
await gateway.handleConnection(client);
|
|
180
|
+
expect(client.disconnect).toHaveBeenCalled();
|
|
181
|
+
expect(gateway.isUserOnline('user-1')).toBe(false);
|
|
182
|
+
});
|
|
183
|
+
it('tracks multiple sockets for the same user', async ()=>{
|
|
184
|
+
mockJwtService.verify.mockReturnValue({
|
|
185
|
+
id: 'user-5'
|
|
186
|
+
});
|
|
187
|
+
const clientA = createMockSocket({
|
|
188
|
+
id: 'socket-a',
|
|
189
|
+
handshake: {
|
|
190
|
+
auth: {
|
|
191
|
+
token: 't'
|
|
192
|
+
},
|
|
193
|
+
query: {},
|
|
194
|
+
headers: {}
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
const clientB = createMockSocket({
|
|
198
|
+
id: 'socket-b',
|
|
199
|
+
handshake: {
|
|
200
|
+
auth: {
|
|
201
|
+
token: 't'
|
|
202
|
+
},
|
|
203
|
+
query: {},
|
|
204
|
+
headers: {}
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
await gateway.handleConnection(clientA);
|
|
208
|
+
await gateway.handleConnection(clientB);
|
|
209
|
+
expect(gateway.isUserOnline('user-5')).toBe(true);
|
|
210
|
+
expect(gateway.getOnlineUserCount()).toBe(1);
|
|
211
|
+
});
|
|
212
|
+
it('disconnects the client when an unexpected error occurs during connection handling', async ()=>{
|
|
213
|
+
const client = createMockSocket({
|
|
214
|
+
handshake: {
|
|
215
|
+
auth: {
|
|
216
|
+
token: 't'
|
|
217
|
+
},
|
|
218
|
+
query: {},
|
|
219
|
+
headers: {}
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
mockJwtService.verify.mockReturnValue({
|
|
223
|
+
id: 'user-6'
|
|
224
|
+
});
|
|
225
|
+
client.join = jest.fn().mockImplementation(()=>{
|
|
226
|
+
throw new Error('join failed');
|
|
227
|
+
});
|
|
228
|
+
await gateway.handleConnection(client);
|
|
229
|
+
expect(client.disconnect).toHaveBeenCalled();
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
describe('handleDisconnect', ()=>{
|
|
233
|
+
it('removes the socket and marks the user offline when it was their only connection', async ()=>{
|
|
234
|
+
mockJwtService.verify.mockReturnValue({
|
|
235
|
+
id: 'user-1'
|
|
236
|
+
});
|
|
237
|
+
const client = createMockSocket({
|
|
238
|
+
handshake: {
|
|
239
|
+
auth: {
|
|
240
|
+
token: 't'
|
|
241
|
+
},
|
|
242
|
+
query: {},
|
|
243
|
+
headers: {}
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
await gateway.handleConnection(client);
|
|
247
|
+
expect(gateway.isUserOnline('user-1')).toBe(true);
|
|
248
|
+
gateway.handleDisconnect(client);
|
|
249
|
+
expect(gateway.isUserOnline('user-1')).toBe(false);
|
|
250
|
+
expect(gateway.getOnlineUserCount()).toBe(0);
|
|
251
|
+
});
|
|
252
|
+
it('keeps the user online when they still have another connected socket', async ()=>{
|
|
253
|
+
mockJwtService.verify.mockReturnValue({
|
|
254
|
+
id: 'user-1'
|
|
255
|
+
});
|
|
256
|
+
const clientA = createMockSocket({
|
|
257
|
+
id: 'socket-a',
|
|
258
|
+
handshake: {
|
|
259
|
+
auth: {
|
|
260
|
+
token: 't'
|
|
261
|
+
},
|
|
262
|
+
query: {},
|
|
263
|
+
headers: {}
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
const clientB = createMockSocket({
|
|
267
|
+
id: 'socket-b',
|
|
268
|
+
handshake: {
|
|
269
|
+
auth: {
|
|
270
|
+
token: 't'
|
|
271
|
+
},
|
|
272
|
+
query: {},
|
|
273
|
+
headers: {}
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
await gateway.handleConnection(clientA);
|
|
277
|
+
await gateway.handleConnection(clientB);
|
|
278
|
+
gateway.handleDisconnect(clientA);
|
|
279
|
+
expect(gateway.isUserOnline('user-1')).toBe(true);
|
|
280
|
+
});
|
|
281
|
+
it('does nothing when the client never authenticated (no userId on socket data)', ()=>{
|
|
282
|
+
const client = createMockSocket();
|
|
283
|
+
expect(()=>gateway.handleDisconnect(client)).not.toThrow();
|
|
284
|
+
expect(gateway.getOnlineUserCount()).toBe(0);
|
|
285
|
+
});
|
|
286
|
+
it('is a no-op when disconnecting an unknown user id', ()=>{
|
|
287
|
+
const client = createMockSocket({
|
|
288
|
+
data: {
|
|
289
|
+
userId: 'ghost-user'
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
expect(()=>gateway.handleDisconnect(client)).not.toThrow();
|
|
293
|
+
expect(gateway.isUserOnline('ghost-user')).toBe(false);
|
|
294
|
+
});
|
|
295
|
+
});
|
|
296
|
+
describe('emitToUser', ()=>{
|
|
297
|
+
it('emits the event to the user-scoped room', ()=>{
|
|
298
|
+
gateway.emitToUser('user-1', 'notification:new', {
|
|
299
|
+
title: 'hi'
|
|
300
|
+
});
|
|
301
|
+
expect(mockServer.to).toHaveBeenCalledWith('user:user-1');
|
|
302
|
+
expect(mockServer.to('user:user-1').emit).toHaveBeenCalledWith('notification:new', {
|
|
303
|
+
title: 'hi'
|
|
304
|
+
});
|
|
305
|
+
});
|
|
306
|
+
it('does not throw when the server has not been initialized yet', ()=>{
|
|
307
|
+
const freshGateway = new _notificationgateway.NotificationGateway(mockConfigService, mockJwtService);
|
|
308
|
+
expect(()=>freshGateway.emitToUser('user-1', 'notification:new', {})).not.toThrow();
|
|
309
|
+
});
|
|
310
|
+
});
|
|
311
|
+
describe('emitToUserInCompany', ()=>{
|
|
312
|
+
it('emits to the combined user+company room when companyId is provided', ()=>{
|
|
313
|
+
gateway.emitToUserInCompany('user-1', 'company-1', 'notification:new', {
|
|
314
|
+
a: 1
|
|
315
|
+
});
|
|
316
|
+
expect(mockServer.to).toHaveBeenCalledWith('user:user-1:company:company-1');
|
|
317
|
+
});
|
|
318
|
+
it('falls back to emitToUser when companyId is null', ()=>{
|
|
319
|
+
gateway.emitToUserInCompany('user-1', null, 'notification:new', {
|
|
320
|
+
a: 1
|
|
321
|
+
});
|
|
322
|
+
expect(mockServer.to).toHaveBeenCalledWith('user:user-1');
|
|
323
|
+
});
|
|
324
|
+
it('falls back to emitToUser when companyId is undefined', ()=>{
|
|
325
|
+
gateway.emitToUserInCompany('user-1', undefined, 'notification:new', {
|
|
326
|
+
a: 1
|
|
327
|
+
});
|
|
328
|
+
expect(mockServer.to).toHaveBeenCalledWith('user:user-1');
|
|
329
|
+
});
|
|
330
|
+
it('does not throw when the server has not been initialized yet', ()=>{
|
|
331
|
+
const freshGateway = new _notificationgateway.NotificationGateway(mockConfigService, mockJwtService);
|
|
332
|
+
expect(()=>freshGateway.emitToUserInCompany('user-1', 'company-1', 'notification:new', {})).not.toThrow();
|
|
333
|
+
});
|
|
334
|
+
});
|
|
335
|
+
describe('emitToCompany', ()=>{
|
|
336
|
+
it('emits the event to the company-scoped room', ()=>{
|
|
337
|
+
gateway.emitToCompany('company-1', 'notification:new', {
|
|
338
|
+
b: 2
|
|
339
|
+
});
|
|
340
|
+
expect(mockServer.to).toHaveBeenCalledWith('company:company-1');
|
|
341
|
+
});
|
|
342
|
+
});
|
|
343
|
+
describe('broadcast', ()=>{
|
|
344
|
+
it('emits the event to all connected sockets', ()=>{
|
|
345
|
+
gateway.broadcast('server:announcement', {
|
|
346
|
+
text: 'hi all'
|
|
347
|
+
});
|
|
348
|
+
expect(mockServer.emit).toHaveBeenCalledWith('server:announcement', {
|
|
349
|
+
text: 'hi all'
|
|
350
|
+
});
|
|
351
|
+
});
|
|
352
|
+
});
|
|
353
|
+
describe('isUserOnline / getOnlineUserCount', ()=>{
|
|
354
|
+
it('reports zero online users when nobody has connected', ()=>{
|
|
355
|
+
expect(gateway.isUserOnline('nobody')).toBe(false);
|
|
356
|
+
expect(gateway.getOnlineUserCount()).toBe(0);
|
|
357
|
+
});
|
|
358
|
+
});
|
|
359
|
+
});
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
const _interfaces = require("@flusys/nestjs-shared/interfaces");
|
|
6
|
+
const _notificationmodule = require("./notification.module");
|
|
7
|
+
const _notificationconstants = require("../config/notification.constants");
|
|
8
|
+
const _controllers = require("../controllers");
|
|
9
|
+
const _gateways = require("../gateways");
|
|
10
|
+
const _adapters = require("../adapters");
|
|
11
|
+
function _define_property(obj, key, value) {
|
|
12
|
+
if (key in obj) {
|
|
13
|
+
Object.defineProperty(obj, key, {
|
|
14
|
+
value: value,
|
|
15
|
+
enumerable: true,
|
|
16
|
+
configurable: true,
|
|
17
|
+
writable: true
|
|
18
|
+
});
|
|
19
|
+
} else {
|
|
20
|
+
obj[key] = value;
|
|
21
|
+
}
|
|
22
|
+
return obj;
|
|
23
|
+
}
|
|
24
|
+
function providerTokens(providers) {
|
|
25
|
+
return providers.map((p)=>typeof p === 'function' ? p : p.provide);
|
|
26
|
+
}
|
|
27
|
+
describe('NotificationModule', ()=>{
|
|
28
|
+
describe('forRoot', ()=>{
|
|
29
|
+
it('should default to a non-global module with the notification controller registered', ()=>{
|
|
30
|
+
const dynamicModule = _notificationmodule.NotificationModule.forRoot({});
|
|
31
|
+
expect(dynamicModule.module).toBe(_notificationmodule.NotificationModule);
|
|
32
|
+
expect(dynamicModule.global).toBe(false);
|
|
33
|
+
expect(dynamicModule.controllers).toEqual([
|
|
34
|
+
_controllers.NotificationController
|
|
35
|
+
]);
|
|
36
|
+
});
|
|
37
|
+
it('should respect global: true', ()=>{
|
|
38
|
+
expect(_notificationmodule.NotificationModule.forRoot({
|
|
39
|
+
global: true
|
|
40
|
+
}).global).toBe(true);
|
|
41
|
+
});
|
|
42
|
+
it('should register no controllers when includeController is false', ()=>{
|
|
43
|
+
expect(_notificationmodule.NotificationModule.forRoot({
|
|
44
|
+
includeController: false
|
|
45
|
+
}).controllers).toEqual([]);
|
|
46
|
+
});
|
|
47
|
+
it('should register the NOTIFICATION_ADAPTER provider backed by NotificationAdapter', ()=>{
|
|
48
|
+
const dynamicModule = _notificationmodule.NotificationModule.forRoot({});
|
|
49
|
+
const adapterProvider = dynamicModule.providers.find((p)=>p.provide === _interfaces.NOTIFICATION_ADAPTER);
|
|
50
|
+
expect(adapterProvider.useClass).toBe(_adapters.NotificationAdapter);
|
|
51
|
+
expect(providerTokens(dynamicModule.providers)).toContain(_gateways.NotificationGateway);
|
|
52
|
+
});
|
|
53
|
+
it('should export NotificationGateway and NOTIFICATION_ADAPTER alongside the base services', ()=>{
|
|
54
|
+
const dynamicModule = _notificationmodule.NotificationModule.forRoot({});
|
|
55
|
+
expect(dynamicModule.exports).toEqual(expect.arrayContaining([
|
|
56
|
+
_gateways.NotificationGateway,
|
|
57
|
+
_interfaces.NOTIFICATION_ADAPTER
|
|
58
|
+
]));
|
|
59
|
+
});
|
|
60
|
+
it('should register a NOTIFICATION_MODULE_OPTIONS provider carrying the given options', ()=>{
|
|
61
|
+
const options = {
|
|
62
|
+
bootstrapAppConfig: {
|
|
63
|
+
enableCompanyFeature: true
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
const dynamicModule = _notificationmodule.NotificationModule.forRoot(options);
|
|
67
|
+
const provider = dynamicModule.providers.find((p)=>p.provide === _notificationconstants.NOTIFICATION_MODULE_OPTIONS);
|
|
68
|
+
expect(provider.useValue).toBe(options);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
describe('forRootAsync', ()=>{
|
|
72
|
+
it('should forward external imports alongside CacheModule/UtilsModule', ()=>{
|
|
73
|
+
let FakeImportedModule = class FakeImportedModule {
|
|
74
|
+
};
|
|
75
|
+
const dynamicModule = _notificationmodule.NotificationModule.forRootAsync({
|
|
76
|
+
imports: [
|
|
77
|
+
FakeImportedModule
|
|
78
|
+
]
|
|
79
|
+
});
|
|
80
|
+
expect(dynamicModule.imports).toEqual(expect.arrayContaining([
|
|
81
|
+
FakeImportedModule
|
|
82
|
+
]));
|
|
83
|
+
});
|
|
84
|
+
it('should still wire the adapter/gateway providers when using async registration', ()=>{
|
|
85
|
+
const dynamicModule = _notificationmodule.NotificationModule.forRootAsync({
|
|
86
|
+
useFactory: jest.fn()
|
|
87
|
+
});
|
|
88
|
+
const adapterProvider = dynamicModule.providers.find((p)=>p.provide === _interfaces.NOTIFICATION_ADAPTER);
|
|
89
|
+
expect(adapterProvider.useClass).toBe(_adapters.NotificationAdapter);
|
|
90
|
+
});
|
|
91
|
+
it('useFactory: builds a NOTIFICATION_MODULE_OPTIONS provider merging resolved config', async ()=>{
|
|
92
|
+
const useFactory = jest.fn().mockResolvedValue({
|
|
93
|
+
foo: 'bar'
|
|
94
|
+
});
|
|
95
|
+
const dynamicModule = _notificationmodule.NotificationModule.forRootAsync({
|
|
96
|
+
useFactory,
|
|
97
|
+
inject: [
|
|
98
|
+
'SOME_TOKEN'
|
|
99
|
+
]
|
|
100
|
+
});
|
|
101
|
+
const provider = dynamicModule.providers.find((p)=>p.provide === _notificationconstants.NOTIFICATION_MODULE_OPTIONS);
|
|
102
|
+
expect(provider.inject).toEqual([
|
|
103
|
+
'SOME_TOKEN'
|
|
104
|
+
]);
|
|
105
|
+
const resolved = await provider.useFactory('injected');
|
|
106
|
+
expect(useFactory).toHaveBeenCalledWith('injected');
|
|
107
|
+
expect(resolved.config).toEqual({
|
|
108
|
+
foo: 'bar'
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
it('useClass: registers both the class provider and an options provider calling createNotificationOptions()', async ()=>{
|
|
112
|
+
let MyOptionsFactory = class MyOptionsFactory {
|
|
113
|
+
constructor(){
|
|
114
|
+
_define_property(this, "createNotificationOptions", jest.fn().mockResolvedValue({
|
|
115
|
+
foo: 'from-class'
|
|
116
|
+
}));
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
const dynamicModule = _notificationmodule.NotificationModule.forRootAsync({
|
|
120
|
+
useClass: MyOptionsFactory
|
|
121
|
+
});
|
|
122
|
+
const classProvider = dynamicModule.providers.find((p)=>p.provide === MyOptionsFactory);
|
|
123
|
+
expect(classProvider.useClass).toBe(MyOptionsFactory);
|
|
124
|
+
const optionsProvider = dynamicModule.providers.find((p)=>p.provide === _notificationconstants.NOTIFICATION_MODULE_OPTIONS);
|
|
125
|
+
const instance = new MyOptionsFactory();
|
|
126
|
+
const resolved = await optionsProvider.useFactory(instance);
|
|
127
|
+
expect(instance.createNotificationOptions).toHaveBeenCalled();
|
|
128
|
+
expect(resolved.config).toEqual({
|
|
129
|
+
foo: 'from-class'
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
it('useExisting: registers a single options provider without a duplicate class registration', ()=>{
|
|
133
|
+
let MyOptionsFactory = class MyOptionsFactory {
|
|
134
|
+
constructor(){
|
|
135
|
+
_define_property(this, "createNotificationOptions", jest.fn());
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
const dynamicModule = _notificationmodule.NotificationModule.forRootAsync({
|
|
139
|
+
useExisting: MyOptionsFactory
|
|
140
|
+
});
|
|
141
|
+
expect(dynamicModule.providers.filter((p)=>p.provide === MyOptionsFactory)).toHaveLength(0);
|
|
142
|
+
const optionsProvider = dynamicModule.providers.find((p)=>p.provide === _notificationconstants.NOTIFICATION_MODULE_OPTIONS);
|
|
143
|
+
expect(optionsProvider.inject).toEqual([
|
|
144
|
+
MyOptionsFactory
|
|
145
|
+
]);
|
|
146
|
+
});
|
|
147
|
+
it('should register no NOTIFICATION_MODULE_OPTIONS provider when neither useFactory, useClass, nor useExisting is given', ()=>{
|
|
148
|
+
const dynamicModule = _notificationmodule.NotificationModule.forRootAsync({});
|
|
149
|
+
expect(providerTokens(dynamicModule.providers)).not.toContain(_notificationconstants.NOTIFICATION_MODULE_OPTIONS);
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
});
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
const _notificationconfigservice = require("./notification-config.service");
|
|
6
|
+
function buildOptions(overrides = {}) {
|
|
7
|
+
return {
|
|
8
|
+
bootstrapAppConfig: {
|
|
9
|
+
databaseMode: 'single',
|
|
10
|
+
enableCompanyFeature: false
|
|
11
|
+
},
|
|
12
|
+
config: {},
|
|
13
|
+
...overrides
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
describe('NotificationConfigService', ()=>{
|
|
17
|
+
describe('isCompanyFeatureEnabled', ()=>{
|
|
18
|
+
it('returns true when bootstrapAppConfig enables the company feature', ()=>{
|
|
19
|
+
const service = new _notificationconfigservice.NotificationConfigService(buildOptions({
|
|
20
|
+
bootstrapAppConfig: {
|
|
21
|
+
databaseMode: 'single',
|
|
22
|
+
enableCompanyFeature: true
|
|
23
|
+
}
|
|
24
|
+
}));
|
|
25
|
+
expect(service.isCompanyFeatureEnabled()).toBe(true);
|
|
26
|
+
});
|
|
27
|
+
it('returns false when bootstrapAppConfig disables the company feature', ()=>{
|
|
28
|
+
const service = new _notificationconfigservice.NotificationConfigService(buildOptions({
|
|
29
|
+
bootstrapAppConfig: {
|
|
30
|
+
databaseMode: 'single',
|
|
31
|
+
enableCompanyFeature: false
|
|
32
|
+
}
|
|
33
|
+
}));
|
|
34
|
+
expect(service.isCompanyFeatureEnabled()).toBe(false);
|
|
35
|
+
});
|
|
36
|
+
it('defaults to false when bootstrapAppConfig is missing', ()=>{
|
|
37
|
+
const service = new _notificationconfigservice.NotificationConfigService(buildOptions({
|
|
38
|
+
bootstrapAppConfig: undefined
|
|
39
|
+
}));
|
|
40
|
+
expect(service.isCompanyFeatureEnabled()).toBe(false);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
describe('getDatabaseMode', ()=>{
|
|
44
|
+
it('returns the configured database mode', ()=>{
|
|
45
|
+
const service = new _notificationconfigservice.NotificationConfigService(buildOptions({
|
|
46
|
+
bootstrapAppConfig: {
|
|
47
|
+
databaseMode: 'multi-tenant',
|
|
48
|
+
enableCompanyFeature: true
|
|
49
|
+
}
|
|
50
|
+
}));
|
|
51
|
+
expect(service.getDatabaseMode()).toBe('multi-tenant');
|
|
52
|
+
});
|
|
53
|
+
it('defaults to "single" when bootstrapAppConfig is missing', ()=>{
|
|
54
|
+
const service = new _notificationconfigservice.NotificationConfigService(buildOptions({
|
|
55
|
+
bootstrapAppConfig: undefined
|
|
56
|
+
}));
|
|
57
|
+
expect(service.getDatabaseMode()).toBe('single');
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
describe('isMultiTenant', ()=>{
|
|
61
|
+
it('returns true when database mode is multi-tenant', ()=>{
|
|
62
|
+
const service = new _notificationconfigservice.NotificationConfigService(buildOptions({
|
|
63
|
+
bootstrapAppConfig: {
|
|
64
|
+
databaseMode: 'multi-tenant',
|
|
65
|
+
enableCompanyFeature: true
|
|
66
|
+
}
|
|
67
|
+
}));
|
|
68
|
+
expect(service.isMultiTenant()).toBe(true);
|
|
69
|
+
});
|
|
70
|
+
it('returns false when database mode is single', ()=>{
|
|
71
|
+
const service = new _notificationconfigservice.NotificationConfigService(buildOptions({
|
|
72
|
+
bootstrapAppConfig: {
|
|
73
|
+
databaseMode: 'single',
|
|
74
|
+
enableCompanyFeature: false
|
|
75
|
+
}
|
|
76
|
+
}));
|
|
77
|
+
expect(service.isMultiTenant()).toBe(false);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
describe('getJwtSecret', ()=>{
|
|
81
|
+
it('returns the configured jwt secret', ()=>{
|
|
82
|
+
const service = new _notificationconfigservice.NotificationConfigService(buildOptions({
|
|
83
|
+
config: {
|
|
84
|
+
jwtSecret: 'top-secret'
|
|
85
|
+
}
|
|
86
|
+
}));
|
|
87
|
+
expect(service.getJwtSecret()).toBe('top-secret');
|
|
88
|
+
});
|
|
89
|
+
it('returns undefined when no jwt secret is configured', ()=>{
|
|
90
|
+
const service = new _notificationconfigservice.NotificationConfigService(buildOptions({
|
|
91
|
+
config: {}
|
|
92
|
+
}));
|
|
93
|
+
expect(service.getJwtSecret()).toBeUndefined();
|
|
94
|
+
});
|
|
95
|
+
it('returns undefined when config is missing entirely', ()=>{
|
|
96
|
+
const service = new _notificationconfigservice.NotificationConfigService(buildOptions({
|
|
97
|
+
config: undefined
|
|
98
|
+
}));
|
|
99
|
+
expect(service.getJwtSecret()).toBeUndefined();
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
describe('getOptions', ()=>{
|
|
103
|
+
it('returns the raw options object passed to the constructor', ()=>{
|
|
104
|
+
const options = buildOptions({
|
|
105
|
+
config: {
|
|
106
|
+
jwtSecret: 'abc'
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
const service = new _notificationconfigservice.NotificationConfigService(options);
|
|
110
|
+
expect(service.getOptions()).toBe(options);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
});
|