diff options
| author | Nanashia <nanashia.128@gmail.com> | 2023-03-18 09:01:10 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-18 09:01:10 +0900 |
| commit | 8571c692bad95fe940234cecf35a50df1672cf26 (patch) | |
| tree | 49cab53cfc45b29bbcd9999b7df475c0b56c9d2a /packages/backend/test/utils.ts | |
| parent | update pnpm (diff) | |
| download | misskey-8571c692bad95fe940234cecf35a50df1672cf26.tar.gz misskey-8571c692bad95fe940234cecf35a50df1672cf26.tar.bz2 misskey-8571c692bad95fe940234cecf35a50df1672cf26.zip | |
test(backend): Add tests for web resources (#10341)
Diffstat (limited to 'packages/backend/test/utils.ts')
| -rw-r--r-- | packages/backend/test/utils.ts | 89 |
1 files changed, 85 insertions, 4 deletions
diff --git a/packages/backend/test/utils.ts b/packages/backend/test/utils.ts index d1a5d6d949..4d52c2f062 100644 --- a/packages/backend/test/utils.ts +++ b/packages/backend/test/utils.ts @@ -3,6 +3,7 @@ import { isAbsolute, basename } from 'node:path'; import WebSocket from 'ws'; import fetch, { Blob, File, RequestInit } from 'node-fetch'; import { DataSource } from 'typeorm'; +import { JSDOM } from 'jsdom'; import { entities } from '../src/postgres.js'; import { loadConfig } from '../src/config.js'; import type * as misskey from 'misskey-js'; @@ -12,6 +13,10 @@ export { server as startServer } from '@/boot/common.js'; const config = loadConfig(); export const port = config.port; +export const cookie = (me: any): string => { + return `token=${me.token};`; +}; + export const api = async (endpoint: string, params: any, me?: any) => { const normalized = endpoint.replace(/^\//, ''); return await request(`api/${normalized}`, params, me); @@ -71,6 +76,71 @@ export const react = async (user: any, note: any, reaction: string): Promise<any }, user); }; +export const page = async (user: any, page: any = {}): Promise<any> => { + const res = await api('pages/create', { + alignCenter: false, + content: [ + { + id: '2be9a64b-5ada-43a3-85f3-ec3429551ded', + text: 'Hello World!', + type: 'text', + }, + ], + eyeCatchingImageId: null, + font: 'sans-serif', + hideTitleWhenPinned: false, + name: '1678594845072', + script: '', + summary: null, + title: '', + variables: [], + ...page, + }, user); + return res.body; +}; + +export const play = async (user: any, play: any = {}): Promise<any> => { + const res = await api('flash/create', { + permissions: [], + script: 'test', + summary: '', + title: 'test', + ...play, + }, user); + return res.body; +}; + +export const clip = async (user: any, clip: any = {}): Promise<any> => { + const res = await api('clips/create', { + description: null, + isPublic: true, + name: 'test', + ...clip, + }, user); + return res.body; +}; + +export const galleryPost = async (user: any, channel: any = {}): Promise<any> => { + const res = await api('gallery/posts/create', { + description: null, + fileIds: [], + isSensitive: false, + title: 'test', + ...channel, + }, user); + return res.body; +}; + +export const channel = async (user: any, channel: any = {}): Promise<any> => { + const res = await api('channels/create', { + bannerId: null, + description: null, + name: 'test', + ...channel, + }, user); + return res.body; +}; + interface UploadOptions { /** Optional, absolute path or relative from ./resources/ */ path?: string | URL; @@ -196,10 +266,17 @@ export const waitFire = async (user: any, channel: string, trgr: () => any, cond }); }; -export const simpleGet = async (path: string, accept = '*/*'): Promise<{ status: number, body: any, type: string | null, location: string | null }> => { +export type SimpleGetResponse = { + status: number, + body: any | JSDOM | null, + type: string | null, + location: string | null +}; +export const simpleGet = async (path: string, accept = '*/*', cookie: any = undefined): Promise<SimpleGetResponse> => { const res = await relativeFetch(path, { headers: { Accept: accept, + Cookie: cookie, }, redirect: 'manual', }); @@ -208,10 +285,14 @@ export const simpleGet = async (path: string, accept = '*/*'): Promise<{ status: 'application/json; charset=utf-8', 'application/activity+json; charset=utf-8', ]; + const htmlTypes = [ + 'text/html; charset=utf-8', + ]; - const body = jsonTypes.includes(res.headers.get('content-type') ?? '') - ? await res.json() - : null; + const body = + jsonTypes.includes(res.headers.get('content-type') ?? '') ? await res.json() : + htmlTypes.includes(res.headers.get('content-type') ?? '') ? new JSDOM(await res.text()) : + null; return { status: res.status, |