diff options
| author | おさむのひと <46447427+samunohito@users.noreply.github.com> | 2024-07-29 21:31:32 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-29 21:31:32 +0900 |
| commit | 72bc78974657b22ab6b1f5a36f6144c294e36de3 (patch) | |
| tree | b2d25d52e254f91e1b718123c25e73afea2039bb /packages/backend/test/utils.ts | |
| parent | New Crowdin updates (#13916) (diff) | |
| download | misskey-72bc78974657b22ab6b1f5a36f6144c294e36de3.tar.gz misskey-72bc78974657b22ab6b1f5a36f6144c294e36de3.tar.bz2 misskey-72bc78974657b22ab6b1f5a36f6144c294e36de3.zip | |
feature: ユーザ作成時にSystemWebhookを発信できるようにする (#14321)
* feature: ユーザ作成時にSystemWebhookを発信できるようにする
* fix CHANGELOG.md
Diffstat (limited to 'packages/backend/test/utils.ts')
| -rw-r--r-- | packages/backend/test/utils.ts | 55 |
1 files changed, 51 insertions, 4 deletions
diff --git a/packages/backend/test/utils.ts b/packages/backend/test/utils.ts index e70befeebe..26de19eaf1 100644 --- a/packages/backend/test/utils.ts +++ b/packages/backend/test/utils.ts @@ -12,13 +12,14 @@ import WebSocket, { ClientOptions } from 'ws'; import fetch, { File, RequestInit, type Headers } from 'node-fetch'; import { DataSource } from 'typeorm'; import { JSDOM } from 'jsdom'; -import { DEFAULT_POLICIES } from '@/core/RoleService.js'; -import { validateContentTypeSetAsActivityPub } from '@/core/activitypub/misc/validator.js'; +import { type Response } from 'node-fetch'; +import Fastify from 'fastify'; import { entities } from '../src/postgres.js'; import { loadConfig } from '../src/config.js'; import type * as misskey from 'misskey-js'; -import { type Response } from 'node-fetch'; -import { ApiError } from "@/server/api/error.js"; +import { DEFAULT_POLICIES } from '@/core/RoleService.js'; +import { validateContentTypeSetAsActivityPub } from '@/core/activitypub/misc/validator.js'; +import { ApiError } from '@/server/api/error.js'; export { server as startServer, jobQueue as startJobQueue } from '@/boot/common.js'; @@ -27,11 +28,23 @@ export interface UserToken { bearer?: boolean; } +export type SystemWebhookPayload = { + server: string; + hookId: string; + eventId: string; + createdAt: string; + type: string; + body: any; +} + const config = loadConfig(); export const port = config.port; export const origin = config.url; export const host = new URL(config.url).host; +export const WEBHOOK_HOST = 'http://localhost:15080'; +export const WEBHOOK_PORT = 15080; + export const cookie = (me: UserToken): string => { return `token=${me.token};`; }; @@ -645,3 +658,37 @@ export async function sendEnvResetRequest() { export function castAsError(obj: Record<string, unknown>): { error: ApiError } { return obj as { error: ApiError }; } + +export async function captureWebhook<T = SystemWebhookPayload>(postAction: () => Promise<void>, port = WEBHOOK_PORT): Promise<T> { + const fastify = Fastify(); + + let timeoutHandle: NodeJS.Timeout | null = null; + const result = await new Promise<string>(async (resolve, reject) => { + fastify.all('/', async (req, res) => { + timeoutHandle && clearTimeout(timeoutHandle); + + const body = JSON.stringify(req.body); + res.status(200).send('ok'); + await fastify.close(); + resolve(body); + }); + + await fastify.listen({ port }); + + timeoutHandle = setTimeout(async () => { + await fastify.close(); + reject(new Error('timeout')); + }, 3000); + + try { + await postAction(); + } catch (e) { + await fastify.close(); + reject(e); + } + }); + + await fastify.close(); + + return JSON.parse(result) as T; +} |