summaryrefslogtreecommitdiff
path: root/packages/backend/test/e2e/synalio/user-create.ts
blob: cb0f68dfeab7e9626de241d9d9bb1095217f07fa (plain)
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
/*
 * SPDX-FileCopyrightText: syuilo and misskey-project
 * SPDX-License-Identifier: AGPL-3.0-only
 */

import { setTimeout } from 'node:timers/promises';
import { entities } from 'misskey-js';
import { beforeEach, describe, test } from '@jest/globals';
import {
	api,
	captureWebhook,
	randomString,
	role,
	signup,
	startJobQueue,
	UserToken,
	WEBHOOK_HOST,
} from '../../utils.js';
import type { INestApplicationContext } from '@nestjs/common';

describe('[シナリオ] ユーザ作成', () => {
	let queue: INestApplicationContext;
	let admin: entities.SignupResponse;

	async function createSystemWebhook(args?: Partial<entities.AdminSystemWebhookCreateRequest>, credential?: UserToken): Promise<entities.AdminSystemWebhookCreateResponse> {
		const res = await api(
			'admin/system-webhook/create',
			{
				isActive: true,
				name: randomString(),
				on: ['userCreated'],
				url: WEBHOOK_HOST,
				secret: randomString(),
				...args,
			},
			credential ?? admin,
		);
		return res.body;
	}

	// -------------------------------------------------------------------------------------------

	beforeAll(async () => {
		queue = await startJobQueue();
		admin = await signup({ username: 'admin' });

		await role(admin, { isAdministrator: true });
	}, 1000 * 60 * 2);

	afterAll(async () => {
		await queue.close();
	});

	// -------------------------------------------------------------------------------------------

	describe('SystemWebhook', () => {
		beforeEach(async () => {
			const webhooks = await api('admin/system-webhook/list', {}, admin);
			for (const webhook of webhooks.body) {
				await api('admin/system-webhook/delete', { id: webhook.id }, admin);
			}
		});

		test('ユーザが作成された -> userCreatedが送出される', async () => {
			const webhook = await createSystemWebhook({
				on: ['userCreated'],
				isActive: true,
			});

			let alice: any = null;
			const webhookBody = await captureWebhook(async () => {
				alice = await signup({ username: 'alice' });
			});

			// webhookの送出後にいろいろやってるのでちょっと待つ必要がある
			await setTimeout(2000);

			console.log(alice);
			console.log(JSON.stringify(webhookBody, null, 2));

			expect(webhookBody.hookId).toBe(webhook.id);
			expect(webhookBody.type).toBe('userCreated');

			const body = webhookBody.body as entities.UserLite;
			expect(alice.id).toBe(body.id);
			expect(alice.name).toBe(body.name);
			expect(alice.username).toBe(body.username);
			expect(alice.host).toBe(body.host);
			expect(alice.avatarUrl).toBe(body.avatarUrl);
			expect(alice.avatarBlurhash).toBe(body.avatarBlurhash);
			expect(alice.avatarDecorations).toEqual(body.avatarDecorations);
			expect(alice.isBot).toBe(body.isBot);
			expect(alice.isCat).toBe(body.isCat);
			expect(alice.instance).toEqual(body.instance);
			expect(alice.emojis).toEqual(body.emojis);
			expect(alice.onlineStatus).toBe(body.onlineStatus);
			expect(alice.badgeRoles).toEqual(body.badgeRoles);
		});

		test('ユーザ作成 -> userCreatedが未許可の場合は送出されない', async () => {
			await createSystemWebhook({
				on: [],
				isActive: true,
			});

			let alice: any = null;
			const webhookBody = await captureWebhook(async () => {
				alice = await signup({ username: 'alice' });
			}).catch(e => e.message);

			expect(webhookBody).toBe('timeout');
			expect(alice.id).not.toBeNull();
		});

		test('ユーザ作成 -> Webhookが無効の場合は送出されない', async () => {
			await createSystemWebhook({
				on: ['userCreated'],
				isActive: false,
			});

			let alice: any = null;
			const webhookBody = await captureWebhook(async () => {
				alice = await signup({ username: 'alice' });
			}).catch(e => e.message);

			expect(webhookBody).toBe('timeout');
			expect(alice.id).not.toBeNull();
		});
	});
});