-
Notifications
You must be signed in to change notification settings - Fork 0
/
move.ts
475 lines (406 loc) · 15.3 KB
/
move.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { INestApplicationContext } from '@nestjs/common';
process.env.NODE_ENV = 'test';
import { setTimeout } from 'node:timers/promises';
import * as assert from 'assert';
import { loadConfig } from '@/config.js';
import { MiRepository, MiUser, UsersRepository, miRepository } from '@/models/_.js';
import { secureRndstr } from '@/misc/secure-rndstr.js';
import { jobQueue } from '@/boot/common.js';
import { api, castAsError, initTestDb, signup, successfulApiCall, uploadFile } from '../utils.js';
import type * as misskey from 'misskey-js';
describe('Account Move', () => {
let jq: INestApplicationContext;
let url: URL;
let root: misskey.entities.SignupResponse;
let alice: misskey.entities.SignupResponse;
let bob: misskey.entities.SignupResponse;
let carol: misskey.entities.SignupResponse;
let dave: misskey.entities.SignupResponse;
let eve: misskey.entities.SignupResponse;
let frank: misskey.entities.SignupResponse;
let Users: UsersRepository;
beforeAll(async () => {
jq = await jobQueue();
const config = loadConfig();
url = new URL(config.url);
const connection = await initTestDb(false);
root = await signup({ username: 'root' });
alice = await signup({ username: 'alice' });
bob = await signup({ username: 'bob' });
carol = await signup({ username: 'carol' });
dave = await signup({ username: 'dave' });
eve = await signup({ username: 'eve' });
frank = await signup({ username: 'frank' });
Users = connection.getRepository(MiUser).extend(miRepository as MiRepository<MiUser>);
}, 1000 * 60 * 2);
afterAll(async () => {
await jq.close();
});
describe('Create Alias', () => {
afterEach(async () => {
await Users.update(bob.id, { alsoKnownAs: null });
}, 1000 * 10);
test('Able to create an alias', async () => {
const res = await api('i/update', {
alsoKnownAs: [`@alice@${url.hostname}`],
}, bob);
const newBob = await Users.findOneByOrFail({ id: bob.id });
assert.strictEqual(newBob.alsoKnownAs?.length, 1);
assert.strictEqual(newBob.alsoKnownAs[0], `${url.origin}/users/${alice.id}`);
assert.strictEqual(res.body.alsoKnownAs?.length, 1);
assert.strictEqual(res.body.alsoKnownAs[0], alice.id);
});
test('Able to create a local alias without hostname', async () => {
await api('i/update', {
alsoKnownAs: ['@alice'],
}, bob);
const newBob = await Users.findOneByOrFail({ id: bob.id });
assert.strictEqual(newBob.alsoKnownAs?.length, 1);
assert.strictEqual(newBob.alsoKnownAs[0], `${url.origin}/users/${alice.id}`);
});
test('Able to create a local alias without @', async () => {
await api('i/update', {
alsoKnownAs: ['alice'],
}, bob);
const newBob = await Users.findOneByOrFail({ id: bob.id });
assert.strictEqual(newBob.alsoKnownAs?.length, 1);
assert.strictEqual(newBob.alsoKnownAs[0], `${url.origin}/users/${alice.id}`);
});
test('Able to set remote user (but may fail)', async () => {
const res = await api('i/update', {
alsoKnownAs: ['@[email protected]'],
}, bob);
assert.strictEqual(res.status, 400);
assert.strictEqual(castAsError(res.body).error.code, 'NO_SUCH_USER');
assert.strictEqual(castAsError(res.body).error.id, 'fcd2eef9-a9b2-4c4f-8624-038099e90aa5');
});
test('Unable to add duplicated aliases to alsoKnownAs', async () => {
const res = await api('i/update', {
alsoKnownAs: [`@alice@${url.hostname}`, `@alice@${url.hostname}`],
}, bob);
assert.strictEqual(res.status, 400);
assert.strictEqual(castAsError(res.body).error.code, 'INVALID_PARAM');
assert.strictEqual(castAsError(res.body).error.id, '3d81ceae-475f-4600-b2a8-2bc116157532');
});
test('Unable to add itself', async () => {
const res = await api('i/update', {
alsoKnownAs: [`@bob@${url.hostname}`],
}, bob);
assert.strictEqual(res.status, 400);
assert.strictEqual(castAsError(res.body).error.code, 'FORBIDDEN_TO_SET_YOURSELF');
assert.strictEqual(castAsError(res.body).error.id, '25c90186-4ab0-49c8-9bba-a1fa6c202ba4');
});
test('Unable to add a nonexisting local account to alsoKnownAs', async () => {
const res1 = await api('i/update', {
alsoKnownAs: [`@nonexist@${url.hostname}`],
}, bob);
assert.strictEqual(res1.status, 400);
assert.strictEqual(castAsError(res1.body).error.code, 'NO_SUCH_USER');
assert.strictEqual(castAsError(res1.body).error.id, 'fcd2eef9-a9b2-4c4f-8624-038099e90aa5');
const res2 = await api('i/update', {
alsoKnownAs: ['@alice', 'nonexist'],
}, bob);
assert.strictEqual(res2.status, 400);
assert.strictEqual(castAsError(res2.body).error.code, 'NO_SUCH_USER');
assert.strictEqual(castAsError(res2.body).error.id, 'fcd2eef9-a9b2-4c4f-8624-038099e90aa5');
});
test('Able to add two existing local account to alsoKnownAs', async () => {
await api('i/update', {
alsoKnownAs: [`@alice@${url.hostname}`, `@carol@${url.hostname}`],
}, bob);
const newBob = await Users.findOneByOrFail({ id: bob.id });
assert.strictEqual(newBob.alsoKnownAs?.length, 2);
assert.strictEqual(newBob.alsoKnownAs[0], `${url.origin}/users/${alice.id}`);
assert.strictEqual(newBob.alsoKnownAs[1], `${url.origin}/users/${carol.id}`);
});
test('Able to properly overwrite alsoKnownAs', async () => {
await api('i/update', {
alsoKnownAs: [`@alice@${url.hostname}`],
}, bob);
await api('i/update', {
alsoKnownAs: [`@carol@${url.hostname}`, `@dave@${url.hostname}`],
}, bob);
const newBob = await Users.findOneByOrFail({ id: bob.id });
assert.strictEqual(newBob.alsoKnownAs?.length, 2);
assert.strictEqual(newBob.alsoKnownAs[0], `${url.origin}/users/${carol.id}`);
assert.strictEqual(newBob.alsoKnownAs[1], `${url.origin}/users/${dave.id}`);
});
});
describe('Local to Local', () => {
let antennaId = '';
beforeAll(async () => {
await api('i/update', {
alsoKnownAs: [`@alice@${url.hostname}`],
}, root);
const listRoot = await api('users/lists/create', {
name: secureRndstr(8),
}, root);
await api('users/lists/push', {
listId: listRoot.body.id,
userId: alice.id,
}, root);
await api('following/create', {
userId: root.id,
}, alice);
await api('following/create', {
userId: eve.id,
}, alice);
const antenna = await api('antennas/create', {
name: secureRndstr(8),
src: 'home',
keywords: [[secureRndstr(8)]],
excludeKeywords: [],
users: [],
caseSensitive: false,
localOnly: false,
withReplies: false,
withFile: false,
}, alice);
antennaId = antenna.body.id;
await api('i/update', {
alsoKnownAs: [`@alice@${url.hostname}`],
}, bob);
await api('following/create', {
userId: alice.id,
}, carol);
await api('mute/create', {
userId: alice.id,
}, dave);
await api('blocking/create', {
userId: alice.id,
}, dave);
await api('following/create', {
userId: eve.id,
}, dave);
await api('following/create', {
userId: dave.id,
}, eve);
const listEve = await api('users/lists/create', {
name: secureRndstr(8),
}, eve);
await api('users/lists/push', {
listId: listEve.body.id,
userId: bob.id,
}, eve);
await api('i/update', {
isLocked: true,
}, frank);
await api('following/create', {
userId: frank.id,
}, alice);
await api('following/requests/accept', {
userId: alice.id,
}, frank);
}, 1000 * 10);
test('Prohibit the root account from moving', async () => {
const res = await api('i/move', {
moveToAccount: `@bob@${url.hostname}`,
}, root);
assert.strictEqual(res.status, 400);
assert.strictEqual(castAsError(res.body).error.code, 'NOT_ROOT_FORBIDDEN');
assert.strictEqual(castAsError(res.body).error.id, '4362e8dc-731f-4ad8-a694-be2a88922a24');
});
test('Unable to move to a nonexisting local account', async () => {
const res = await api('i/move', {
moveToAccount: `@nonexist@${url.hostname}`,
}, alice);
assert.strictEqual(res.status, 400);
assert.strictEqual(castAsError(res.body).error.code, 'NO_SUCH_USER');
assert.strictEqual(castAsError(res.body).error.id, 'fcd2eef9-a9b2-4c4f-8624-038099e90aa5');
});
test('Unable to move if alsoKnownAs is invalid', async () => {
const res = await api('i/move', {
moveToAccount: `@carol@${url.hostname}`,
}, alice);
assert.strictEqual(res.status, 400);
assert.strictEqual(castAsError(res.body).error.code, 'DESTINATION_ACCOUNT_FORBIDS');
assert.strictEqual(castAsError(res.body).error.id, 'b5c90186-4ab0-49c8-9bba-a1f766282ba4');
});
test('Relationships have been properly migrated', async () => {
const move = await api('i/move', {
moveToAccount: `@bob@${url.hostname}`,
}, alice);
assert.strictEqual(move.status, 200);
await setTimeout(1000 * 3); // wait for jobs to finish
// Unfollow delayed?
const aliceFollowings = await api('users/following', {
userId: alice.id,
}, alice);
assert.strictEqual(aliceFollowings.status, 200);
assert.ok(aliceFollowings);
assert.strictEqual(aliceFollowings.body.length, 3);
const carolFollowings = await api('users/following', {
userId: carol.id,
}, carol);
assert.strictEqual(carolFollowings.status, 200);
assert.ok(carolFollowings);
assert.strictEqual(carolFollowings.body.length, 2);
assert.strictEqual(carolFollowings.body[0].followeeId, bob.id);
assert.strictEqual(carolFollowings.body[1].followeeId, alice.id);
const blockings = await api('blocking/list', {}, dave);
assert.strictEqual(blockings.status, 200);
assert.ok(blockings);
assert.strictEqual(blockings.body.length, 2);
assert.strictEqual(blockings.body[0].blockeeId, bob.id);
assert.strictEqual(blockings.body[1].blockeeId, alice.id);
const mutings = await api('mute/list', {}, dave);
assert.strictEqual(mutings.status, 200);
assert.ok(mutings);
assert.strictEqual(mutings.body.length, 2);
assert.strictEqual(mutings.body[0].muteeId, bob.id);
assert.strictEqual(mutings.body[1].muteeId, alice.id);
const rootLists = await api('users/lists/list', {}, root);
assert.strictEqual(rootLists.status, 200);
assert.ok(rootLists);
assert.ok(rootLists.body[0].userIds);
assert.strictEqual(rootLists.body[0].userIds.length, 2);
assert.ok(rootLists.body[0].userIds.find((id: string) => id === bob.id));
assert.ok(rootLists.body[0].userIds.find((id: string) => id === alice.id));
const eveLists = await api('users/lists/list', {}, eve);
assert.strictEqual(eveLists.status, 200);
assert.ok(eveLists);
assert.ok(eveLists.body[0].userIds);
assert.strictEqual(eveLists.body[0].userIds.length, 1);
assert.ok(eveLists.body[0].userIds.find((id: string) => id === bob.id));
});
test('A locked account automatically accept the follow request if it had already accepted the old account.', async () => {
await successfulApiCall({
endpoint: 'following/create',
parameters: {
userId: frank.id,
},
user: bob,
});
const followers = await api('users/followers', {
userId: frank.id,
}, frank);
assert.strictEqual(followers.status, 200);
assert.strictEqual(followers.body.length, 2);
assert.strictEqual(followers.body[0].followerId, bob.id);
});
test('Unfollowed after 10 sec (24 hours in production).', async () => {
await setTimeout(1000 * 8);
const following = await api('users/following', {
userId: alice.id,
}, alice);
assert.strictEqual(following.status, 200);
assert.strictEqual(following.body.length, 0);
});
test('Unable to move if the destination account has already moved.', async () => {
const res = await api('i/move', {
moveToAccount: `@alice@${url.hostname}`,
}, bob);
assert.strictEqual(res.status, 400);
assert.strictEqual(castAsError(res.body).error.code, 'DESTINATION_ACCOUNT_FORBIDS');
assert.strictEqual(castAsError(res.body).error.id, 'b5c90186-4ab0-49c8-9bba-a1f766282ba4');
});
test('Follow and follower counts are properly adjusted', async () => {
await api('following/create', {
userId: alice.id,
}, eve);
const newAlice = await Users.findOneByOrFail({ id: alice.id });
const newCarol = await Users.findOneByOrFail({ id: carol.id });
let newEve = await Users.findOneByOrFail({ id: eve.id });
assert.strictEqual(newAlice.movedToUri, `${url.origin}/users/${bob.id}`);
assert.strictEqual(newAlice.followingCount, 0);
assert.strictEqual(newAlice.followersCount, 0);
assert.strictEqual(newCarol.followingCount, 1);
assert.strictEqual(newEve.followingCount, 1);
assert.strictEqual(newEve.followersCount, 1);
await api('following/delete', {
userId: alice.id,
}, eve);
newEve = await Users.findOneByOrFail({ id: eve.id });
assert.strictEqual(newEve.followingCount, 1);
assert.strictEqual(newEve.followersCount, 1);
});
test.each([
'antennas/create',
'channels/create',
'channels/favorite',
'channels/follow',
'channels/unfavorite',
'channels/unfollow',
'clips/add-note',
'clips/create',
'clips/favorite',
'clips/remove-note',
'clips/unfavorite',
'clips/update',
'drive/files/upload-from-url',
'flash/create',
'flash/like',
'flash/unlike',
'flash/update',
'following/create',
'gallery/posts/create',
'gallery/posts/like',
'gallery/posts/unlike',
'gallery/posts/update',
'i/claim-achievement',
'i/move',
'i/import-blocking',
'i/import-following',
'i/import-muting',
'i/import-user-lists',
'i/pin',
'mute/create',
'notes/create',
'notes/favorites/create',
'notes/polls/vote',
'notes/reactions/create',
'pages/create',
'pages/like',
'pages/unlike',
'pages/update',
'renote-mute/create',
'users/lists/create',
'users/lists/pull',
'users/lists/push',
] as const)('Prohibit access after moving: %s', async (endpoint) => {
const res = await api(endpoint, {}, alice);
assert.strictEqual(res.status, 403);
assert.ok(res.body);
assert.strictEqual(castAsError(res.body).error.code, 'YOUR_ACCOUNT_MOVED');
assert.strictEqual(castAsError(res.body).error.id, '56f20ec9-fd06-4fa5-841b-edd6d7d4fa31');
});
test('Prohibit access after moving: /antennas/update', async () => {
const res = await api('antennas/update', {
antennaId,
name: secureRndstr(8),
src: 'users',
keywords: [[secureRndstr(8)]],
excludeKeywords: [],
users: [eve.id],
caseSensitive: false,
localOnly: false,
withReplies: false,
withFile: false,
}, alice);
assert.strictEqual(res.status, 403);
assert.ok(res.body);
assert.strictEqual(castAsError(res.body).error.code, 'YOUR_ACCOUNT_MOVED');
assert.strictEqual(castAsError(res.body).error.id, '56f20ec9-fd06-4fa5-841b-edd6d7d4fa31');
});
test('Prohibit access after moving: /drive/files/create', async () => {
// FIXME: 一旦逃げておく
const res = await uploadFile(alice);
assert.strictEqual(res.status, 403);
assert.ok(res.body);
assert.strictEqual(castAsError(res.body).error.code, 'YOUR_ACCOUNT_MOVED');
assert.strictEqual(castAsError(res.body).error.id, '56f20ec9-fd06-4fa5-841b-edd6d7d4fa31');
});
test('Prohibit updating alsoKnownAs after moving', async () => {
const res = await api('i/update', {
alsoKnownAs: [`@eve@${url.hostname}`],
}, alice);
assert.strictEqual(res.status, 403);
assert.strictEqual(castAsError(res.body).error.code, 'YOUR_ACCOUNT_MOVED');
assert.strictEqual(castAsError(res.body).error.id, '56f20ec9-fd06-4fa5-841b-edd6d7d4fa31');
});
});
});