forked from misskey-dev/misskey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbuseReportNotificationService.ts
347 lines (287 loc) · 11 KB
/
AbuseReportNotificationService.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { jest } from '@jest/globals';
import { Test, TestingModule } from '@nestjs/testing';
import { randomString } from '../utils.js';
import { AbuseReportNotificationService } from '@/core/AbuseReportNotificationService.js';
import {
AbuseReportNotificationRecipientRepository,
MiAbuseReportNotificationRecipient,
MiSystemWebhook,
MiUser,
SystemWebhooksRepository,
UserProfilesRepository,
UsersRepository,
} from '@/models/_.js';
import { DI } from '@/di-symbols.js';
import { GlobalModule } from '@/GlobalModule.js';
import { IdService } from '@/core/IdService.js';
import { EmailService } from '@/core/EmailService.js';
import { RoleService } from '@/core/RoleService.js';
import { MetaService } from '@/core/MetaService.js';
import { ModerationLogService } from '@/core/ModerationLogService.js';
import { GlobalEventService } from '@/core/GlobalEventService.js';
import { RecipientMethod } from '@/models/AbuseReportNotificationRecipient.js';
import { SystemWebhookService } from '@/core/SystemWebhookService.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
process.env.NODE_ENV = 'test';
describe('AbuseReportNotificationService', () => {
let app: TestingModule;
let service: AbuseReportNotificationService;
// --------------------------------------------------------------------------------------
let usersRepository: UsersRepository;
let userProfilesRepository: UserProfilesRepository;
let systemWebhooksRepository: SystemWebhooksRepository;
let abuseReportNotificationRecipientRepository: AbuseReportNotificationRecipientRepository;
let idService: IdService;
let roleService: jest.Mocked<RoleService>;
let emailService: jest.Mocked<EmailService>;
let webhookService: jest.Mocked<SystemWebhookService>;
// --------------------------------------------------------------------------------------
let root: MiUser;
let alice: MiUser;
let bob: MiUser;
let systemWebhook1: MiSystemWebhook;
let systemWebhook2: MiSystemWebhook;
// --------------------------------------------------------------------------------------
async function createUser(data: Partial<MiUser> = {}) {
const user = await usersRepository
.insert({
id: idService.gen(),
...data,
})
.then(x => usersRepository.findOneByOrFail(x.identifiers[0]));
await userProfilesRepository.insert({
userId: user.id,
});
return user;
}
async function createWebhook(data: Partial<MiSystemWebhook> = {}) {
return systemWebhooksRepository
.insert({
id: idService.gen(),
name: randomString(),
on: ['abuseReport'],
url: 'https://example.com',
secret: randomString(),
...data,
})
.then(x => systemWebhooksRepository.findOneByOrFail(x.identifiers[0]));
}
async function createRecipient(data: Partial<MiAbuseReportNotificationRecipient> = {}) {
return abuseReportNotificationRecipientRepository
.insert({
id: idService.gen(),
isActive: true,
name: randomString(),
...data,
})
.then(x => abuseReportNotificationRecipientRepository.findOneByOrFail(x.identifiers[0]));
}
// --------------------------------------------------------------------------------------
beforeAll(async () => {
app = await Test
.createTestingModule({
imports: [
GlobalModule,
],
providers: [
AbuseReportNotificationService,
IdService,
{
provide: RoleService, useFactory: () => ({ getModeratorIds: jest.fn() }),
},
{
provide: SystemWebhookService, useFactory: () => ({ enqueueSystemWebhook: jest.fn() }),
},
{
provide: UserEntityService, useFactory: () => ({ pack: (v: any) => v }),
},
{
provide: EmailService, useFactory: () => ({ sendEmail: jest.fn() }),
},
{
provide: MetaService, useFactory: () => ({ fetch: jest.fn() }),
},
{
provide: ModerationLogService, useFactory: () => ({ log: () => Promise.resolve() }),
},
{
provide: GlobalEventService, useFactory: () => ({ publishAdminStream: jest.fn() }),
},
],
})
.compile();
usersRepository = app.get(DI.usersRepository);
userProfilesRepository = app.get(DI.userProfilesRepository);
systemWebhooksRepository = app.get(DI.systemWebhooksRepository);
abuseReportNotificationRecipientRepository = app.get(DI.abuseReportNotificationRecipientRepository);
service = app.get(AbuseReportNotificationService);
idService = app.get(IdService);
roleService = app.get(RoleService) as jest.Mocked<RoleService>;
emailService = app.get<EmailService>(EmailService) as jest.Mocked<EmailService>;
webhookService = app.get<SystemWebhookService>(SystemWebhookService) as jest.Mocked<SystemWebhookService>;
app.enableShutdownHooks();
});
beforeEach(async () => {
root = await createUser({ username: 'root', usernameLower: 'root', isRoot: true });
alice = await createUser({ username: 'alice', usernameLower: 'alice', isRoot: false });
bob = await createUser({ username: 'bob', usernameLower: 'bob', isRoot: false });
systemWebhook1 = await createWebhook();
systemWebhook2 = await createWebhook();
roleService.getModeratorIds.mockResolvedValue([root.id, alice.id, bob.id]);
});
afterEach(async () => {
emailService.sendEmail.mockClear();
webhookService.enqueueSystemWebhook.mockClear();
await usersRepository.delete({});
await userProfilesRepository.delete({});
await systemWebhooksRepository.delete({});
await abuseReportNotificationRecipientRepository.delete({});
});
afterAll(async () => {
await app.close();
});
// --------------------------------------------------------------------------------------
describe('createRecipient', () => {
test('作成成功1', async () => {
const params = {
isActive: true,
name: randomString(),
method: 'email' as RecipientMethod,
userId: alice.id,
systemWebhookId: null,
};
const recipient1 = await service.createRecipient(params, root);
expect(recipient1).toMatchObject(params);
});
test('作成成功2', async () => {
const params = {
isActive: true,
name: randomString(),
method: 'webhook' as RecipientMethod,
userId: null,
systemWebhookId: systemWebhook1.id,
};
const recipient1 = await service.createRecipient(params, root);
expect(recipient1).toMatchObject(params);
});
});
describe('updateRecipient', () => {
test('更新成功1', async () => {
const recipient1 = await createRecipient({
method: 'email',
userId: alice.id,
});
const params = {
id: recipient1.id,
isActive: false,
name: randomString(),
method: 'email' as RecipientMethod,
userId: bob.id,
systemWebhookId: null,
};
const recipient2 = await service.updateRecipient(params, root);
expect(recipient2).toMatchObject(params);
});
test('更新成功2', async () => {
const recipient1 = await createRecipient({
method: 'webhook',
systemWebhookId: systemWebhook1.id,
});
const params = {
id: recipient1.id,
isActive: false,
name: randomString(),
method: 'webhook' as RecipientMethod,
userId: null,
systemWebhookId: systemWebhook2.id,
};
const recipient2 = await service.updateRecipient(params, root);
expect(recipient2).toMatchObject(params);
});
});
describe('deleteRecipient', () => {
test('削除成功1', async () => {
const recipient1 = await createRecipient({
method: 'email',
userId: alice.id,
});
await service.deleteRecipient(recipient1.id, root);
await expect(abuseReportNotificationRecipientRepository.findOneBy({ id: recipient1.id })).resolves.toBeNull();
});
});
describe('fetchRecipients', () => {
async function create() {
const recipient1 = await createRecipient({
method: 'email',
userId: alice.id,
});
const recipient2 = await createRecipient({
method: 'email',
userId: bob.id,
});
const recipient3 = await createRecipient({
method: 'webhook',
systemWebhookId: systemWebhook1.id,
});
const recipient4 = await createRecipient({
method: 'webhook',
systemWebhookId: systemWebhook2.id,
});
return [recipient1, recipient2, recipient3, recipient4];
}
test('フィルタなし', async () => {
const [recipient1, recipient2, recipient3, recipient4] = await create();
const recipients = await service.fetchRecipients({});
expect(recipients).toEqual([recipient1, recipient2, recipient3, recipient4]);
});
test('フィルタなし(非モデレータは除外される)', async () => {
roleService.getModeratorIds.mockClear();
roleService.getModeratorIds.mockResolvedValue([root.id, bob.id]);
const [recipient1, recipient2, recipient3, recipient4] = await create();
const recipients = await service.fetchRecipients({});
// aliceはモデレータではないので除外される
expect(recipients).toEqual([recipient2, recipient3, recipient4]);
});
test('フィルタなし(非モデレータでも除外されないオプション設定)', async () => {
roleService.getModeratorIds.mockClear();
roleService.getModeratorIds.mockResolvedValue([root.id, bob.id]);
const [recipient1, recipient2, recipient3, recipient4] = await create();
const recipients = await service.fetchRecipients({}, { removeUnauthorized: false });
expect(recipients).toEqual([recipient1, recipient2, recipient3, recipient4]);
});
test('emailのみ', async () => {
const [recipient1, recipient2, recipient3, recipient4] = await create();
const recipients = await service.fetchRecipients({ method: ['email'] });
expect(recipients).toEqual([recipient1, recipient2]);
});
test('webhookのみ', async () => {
const [recipient1, recipient2, recipient3, recipient4] = await create();
const recipients = await service.fetchRecipients({ method: ['webhook'] });
expect(recipients).toEqual([recipient3, recipient4]);
});
test('すべて', async () => {
const [recipient1, recipient2, recipient3, recipient4] = await create();
const recipients = await service.fetchRecipients({ method: ['email', 'webhook'] });
expect(recipients).toEqual([recipient1, recipient2, recipient3, recipient4]);
});
test('ID指定', async () => {
const [recipient1, recipient2, recipient3, recipient4] = await create();
const recipients = await service.fetchRecipients({ ids: [recipient1.id, recipient3.id] });
expect(recipients).toEqual([recipient1, recipient3]);
});
test('ID指定(method=emailではないIDが混ざりこまない)', async () => {
const [recipient1, recipient2, recipient3, recipient4] = await create();
const recipients = await service.fetchRecipients({ ids: [recipient1.id, recipient3.id], method: ['email'] });
expect(recipients).toEqual([recipient1]);
});
test('ID指定(method=webhookではないIDが混ざりこまない)', async () => {
const [recipient1, recipient2, recipient3, recipient4] = await create();
const recipients = await service.fetchRecipients({ ids: [recipient1.id, recipient3.id], method: ['webhook'] });
expect(recipients).toEqual([recipient3]);
});
});
});