-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathmessage.controller.spec.ts
238 lines (220 loc) · 8.43 KB
/
message.controller.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* Copyright © 2024 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { MongooseModule } from '@nestjs/mongoose';
import { Test } from '@nestjs/testing';
import { AttachmentRepository } from '@/attachment/repositories/attachment.repository';
import { AttachmentModel } from '@/attachment/schemas/attachment.schema';
import { AttachmentService } from '@/attachment/services/attachment.service';
import { ChannelService } from '@/channel/channel.service';
import { MenuRepository } from '@/cms/repositories/menu.repository';
import { MenuModel } from '@/cms/schemas/menu.schema';
import { MenuService } from '@/cms/services/menu.service';
import { I18nService } from '@/i18n/services/i18n.service';
import { LoggerService } from '@/logger/logger.service';
import { NlpService } from '@/nlp/services/nlp.service';
import { SettingService } from '@/setting/services/setting.service';
import { RoleRepository } from '@/user/repositories/role.repository';
import { UserRepository } from '@/user/repositories/user.repository';
import { PermissionModel } from '@/user/schemas/permission.schema';
import { RoleModel } from '@/user/schemas/role.schema';
import { User, UserModel } from '@/user/schemas/user.schema';
import { RoleService } from '@/user/services/role.service';
import { UserService } from '@/user/services/user.service';
import {
installMessageFixtures,
messageFixtures,
} from '@/utils/test/fixtures/message';
import { getPageQuery } from '@/utils/test/pagination';
import {
closeInMongodConnection,
rootMongooseTestModule,
} from '@/utils/test/test';
import { MessageRepository } from '../repositories/message.repository';
import { SubscriberRepository } from '../repositories/subscriber.repository';
import { Message, MessageModel } from '../schemas/message.schema';
import { Subscriber, SubscriberModel } from '../schemas/subscriber.schema';
import { MessageService } from '../services/message.service';
import { SubscriberService } from '../services/subscriber.service';
import { MessageController } from './message.controller';
describe('MessageController', () => {
let messageController: MessageController;
let messageService: MessageService;
let subscriberService: SubscriberService;
let userService: UserService;
let sender: Subscriber;
let recipient: Subscriber;
let user: User;
let message: Message;
let allMessages: Message[];
let allUsers: User[];
let allSubscribers: Subscriber[];
beforeAll(async () => {
const module = await Test.createTestingModule({
controllers: [MessageController],
imports: [
rootMongooseTestModule(installMessageFixtures),
MongooseModule.forFeature([
SubscriberModel,
MessageModel,
UserModel,
RoleModel,
PermissionModel,
AttachmentModel,
MenuModel,
]),
],
providers: [
MessageController,
MessageRepository,
MessageService,
SubscriberService,
UserService,
UserRepository,
RoleService,
RoleRepository,
SubscriberRepository,
ChannelService,
AttachmentService,
AttachmentRepository,
MenuService,
MenuRepository,
{
provide: I18nService,
useValue: {
t: jest.fn().mockImplementation((t) => t),
},
},
{
provide: NlpService,
useValue: {
getNLP: jest.fn(() => undefined),
},
},
{
provide: SettingService,
useValue: {
getConfig: jest.fn(() => ({
chatbot: { lang: { default: 'fr' } },
})),
getSettings: jest.fn(() => ({})),
},
},
EventEmitter2,
{
provide: CACHE_MANAGER,
useValue: {
del: jest.fn(),
get: jest.fn(),
set: jest.fn(),
},
},
LoggerService,
],
}).compile();
messageService = module.get<MessageService>(MessageService);
userService = module.get<UserService>(UserService);
subscriberService = module.get<SubscriberService>(SubscriberService);
messageController = module.get<MessageController>(MessageController);
message = (await messageService.findOne({ mid: 'mid-1' }))!;
sender = (await subscriberService.findOne(message.sender!))!;
recipient = (await subscriberService.findOne(message.recipient!))!;
user = (await userService.findOne(message.sentBy!))!;
allSubscribers = await subscriberService.findAll();
allUsers = await userService.findAll();
allMessages = await messageService.findAll();
});
afterEach(jest.clearAllMocks);
afterAll(closeInMongodConnection);
function toArray(value?: string | string[]): string[] {
return value ? (Array.isArray(value) ? value : [value]) : [];
}
describe('count', () => {
it('should count messages', async () => {
jest.spyOn(messageService, 'count');
const result = await messageController.filterCount();
expect(messageService.count).toHaveBeenCalled();
expect(result).toEqual({ count: messageFixtures.length });
});
});
describe('findOne', () => {
it('should find message by id, and populate its corresponding sender and recipient', async () => {
jest.spyOn(messageService, 'findOneAndPopulate');
const result = await messageController.findOne(message.id, [
'sender',
'recipient',
]);
expect(messageService.findOneAndPopulate).toHaveBeenCalledWith(
message.id,
);
const expectedFixture = messageFixtures.find(
({ mid }) =>
JSON.stringify(toArray(mid)) === JSON.stringify(message.mid),
);
expect(result).toEqualPayload({
...expectedFixture,
mid: toArray(expectedFixture?.mid),
sender,
recipient,
sentBy: user.id,
});
});
it('should find message by id', async () => {
jest.spyOn(messageService, 'findOne');
const result = await messageController.findOne(message.id, []);
expect(messageService.findOne).toHaveBeenCalledWith(message.id);
const expectedFixture = messageFixtures.find(
({ mid }) =>
JSON.stringify(toArray(mid)) === JSON.stringify(message.mid),
);
expect(result).toEqualPayload({
...expectedFixture,
mid: toArray(expectedFixture?.mid),
sender: sender.id,
recipient: recipient.id,
sentBy: user.id,
});
});
});
describe('findPage', () => {
const pageQuery = getPageQuery<Message>();
it('should find messages', async () => {
jest.spyOn(messageService, 'find');
const result = await messageController.findPage(pageQuery, [], {});
const messagesWithSenderAndRecipient = allMessages.map((message) => ({
...message,
sender: allSubscribers.find(({ id }) => id === message.sender)?.id,
recipient: allSubscribers.find(({ id }) => id === message.recipient)
?.id,
sentBy: allUsers.find(({ id }) => id === message.sentBy)?.id,
}));
expect(messageService.find).toHaveBeenCalledWith({}, pageQuery);
expect(result).toEqualPayload(messagesWithSenderAndRecipient);
});
it('should find messages, and foreach message populate the corresponding sender and recipient', async () => {
jest.spyOn(messageService, 'findAndPopulate');
const result = await messageController.findPage(
pageQuery,
['sender', 'recipient'],
{},
);
const messages = allMessages.map((message) => ({
...message,
sender: allSubscribers.find(({ id }) => id === message.sender),
recipient: allSubscribers.find(({ id }) => id === message.recipient),
sentBy: allUsers.find(({ id }) => id === message.sentBy)?.id,
}));
expect(messageService.findAndPopulate).toHaveBeenCalledWith(
{},
pageQuery,
);
expect(result).toEqualPayload(messages);
});
});
});