refactor(backend/test): add `interface UserToken` (#11050)

This commit is contained in:
Kagami Sascha Rosylight 2023-06-27 01:07:20 +02:00 committed by GitHub
parent 58a898dfe0
commit d48172e9d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 17 deletions

View File

@ -13,14 +13,16 @@ import type * as misskey from 'misskey-js';
export { server as startServer } from '@/boot/common.js'; export { server as startServer } from '@/boot/common.js';
interface UserToken { token: string }
const config = loadConfig(); const config = loadConfig();
export const port = config.port; export const port = config.port;
export const cookie = (me: any): string => { export const cookie = (me: UserToken): string => {
return `token=${me.token};`; return `token=${me.token};`;
}; };
export const api = async (endpoint: string, params: any, me?: any) => { export const api = async (endpoint: string, params: any, me?: UserToken) => {
const normalized = endpoint.replace(/^\//, ''); const normalized = endpoint.replace(/^\//, '');
return await request(`api/${normalized}`, params, me); return await request(`api/${normalized}`, params, me);
}; };
@ -28,7 +30,7 @@ export const api = async (endpoint: string, params: any, me?: any) => {
export type ApiRequest = { export type ApiRequest = {
endpoint: string, endpoint: string,
parameters: object, parameters: object,
user: object | undefined, user: UserToken | undefined,
}; };
export const successfulApiCall = async <T, >(request: ApiRequest, assertion: { export const successfulApiCall = async <T, >(request: ApiRequest, assertion: {
@ -55,7 +57,7 @@ export const failedApiCall = async <T, >(request: ApiRequest, assertion: {
return res.body; return res.body;
}; };
const request = async (path: string, params: any, me?: any): Promise<{ body: any, status: number }> => { const request = async (path: string, params: any, me?: UserToken): Promise<{ body: any, status: number }> => {
const auth = me ? { const auth = me ? {
i: me.token, i: me.token,
} : {}; } : {};
@ -94,7 +96,7 @@ export const signup = async (params?: Partial<misskey.Endpoints['signup']['req']
return res.body; return res.body;
}; };
export const post = async (user: any, params?: misskey.Endpoints['notes/create']['req']): Promise<misskey.entities.Note> => { export const post = async (user: UserToken, params?: misskey.Endpoints['notes/create']['req']): Promise<misskey.entities.Note> => {
const q = params; const q = params;
const res = await api('notes/create', q, user); const res = await api('notes/create', q, user);
@ -117,21 +119,21 @@ export const hiddenNote = (note: any): any => {
return temp; return temp;
}; };
export const react = async (user: any, note: any, reaction: string): Promise<any> => { export const react = async (user: UserToken, note: any, reaction: string): Promise<any> => {
await api('notes/reactions/create', { await api('notes/reactions/create', {
noteId: note.id, noteId: note.id,
reaction: reaction, reaction: reaction,
}, user); }, user);
}; };
export const userList = async (user: any, userList: any = {}): Promise<any> => { export const userList = async (user: UserToken, userList: any = {}): Promise<any> => {
const res = await api('users/lists/create', { const res = await api('users/lists/create', {
name: 'test', name: 'test',
}, user); }, user);
return res.body; return res.body;
}; };
export const page = async (user: any, page: any = {}): Promise<any> => { export const page = async (user: UserToken, page: any = {}): Promise<any> => {
const res = await api('pages/create', { const res = await api('pages/create', {
alignCenter: false, alignCenter: false,
content: [ content: [
@ -154,7 +156,7 @@ export const page = async (user: any, page: any = {}): Promise<any> => {
return res.body; return res.body;
}; };
export const play = async (user: any, play: any = {}): Promise<any> => { export const play = async (user: UserToken, play: any = {}): Promise<any> => {
const res = await api('flash/create', { const res = await api('flash/create', {
permissions: [], permissions: [],
script: 'test', script: 'test',
@ -165,7 +167,7 @@ export const play = async (user: any, play: any = {}): Promise<any> => {
return res.body; return res.body;
}; };
export const clip = async (user: any, clip: any = {}): Promise<any> => { export const clip = async (user: UserToken, clip: any = {}): Promise<any> => {
const res = await api('clips/create', { const res = await api('clips/create', {
description: null, description: null,
isPublic: true, isPublic: true,
@ -175,7 +177,7 @@ export const clip = async (user: any, clip: any = {}): Promise<any> => {
return res.body; return res.body;
}; };
export const galleryPost = async (user: any, channel: any = {}): Promise<any> => { export const galleryPost = async (user: UserToken, channel: any = {}): Promise<any> => {
const res = await api('gallery/posts/create', { const res = await api('gallery/posts/create', {
description: null, description: null,
fileIds: [], fileIds: [],
@ -186,7 +188,7 @@ export const galleryPost = async (user: any, channel: any = {}): Promise<any> =>
return res.body; return res.body;
}; };
export const channel = async (user: any, channel: any = {}): Promise<any> => { export const channel = async (user: UserToken, channel: any = {}): Promise<any> => {
const res = await api('channels/create', { const res = await api('channels/create', {
bannerId: null, bannerId: null,
description: null, description: null,
@ -196,7 +198,7 @@ export const channel = async (user: any, channel: any = {}): Promise<any> => {
return res.body; return res.body;
}; };
export const role = async (user: any, role: any = {}, policies: any = {}): Promise<any> => { export const role = async (user: UserToken, role: any = {}, policies: any = {}): Promise<any> => {
const res = await api('admin/roles/create', { const res = await api('admin/roles/create', {
asBadge: false, asBadge: false,
canEditMembersByModerator: false, canEditMembersByModerator: false,
@ -239,7 +241,7 @@ interface UploadOptions {
* Upload file * Upload file
* @param user User * @param user User
*/ */
export const uploadFile = async (user: any, { path, name, blob }: UploadOptions = {}): Promise<any> => { export const uploadFile = async (user: UserToken, { path, name, blob }: UploadOptions = {}): Promise<any> => {
const absPath = path == null const absPath = path == null
? new URL('resources/Lenna.jpg', import.meta.url) ? new URL('resources/Lenna.jpg', import.meta.url)
: isAbsolute(path.toString()) : isAbsolute(path.toString())
@ -268,7 +270,7 @@ export const uploadFile = async (user: any, { path, name, blob }: UploadOptions
}; };
}; };
export const uploadUrl = async (user: any, url: string) => { export const uploadUrl = async (user: UserToken, url: string) => {
let file: any; let file: any;
const marker = Math.random().toString(); const marker = Math.random().toString();
@ -290,7 +292,7 @@ export const uploadUrl = async (user: any, url: string) => {
return file; return file;
}; };
export function connectStream(user: any, channel: string, listener: (message: Record<string, any>) => any, params?: any): Promise<WebSocket> { export function connectStream(user: UserToken, channel: string, listener: (message: Record<string, any>) => any, params?: any): Promise<WebSocket> {
return new Promise((res, rej) => { return new Promise((res, rej) => {
const ws = new WebSocket(`ws://127.0.0.1:${port}/streaming?i=${user.token}`); const ws = new WebSocket(`ws://127.0.0.1:${port}/streaming?i=${user.token}`);
@ -317,7 +319,7 @@ export function connectStream(user: any, channel: string, listener: (message: Re
}); });
} }
export const waitFire = async (user: any, channel: string, trgr: () => any, cond: (msg: Record<string, any>) => boolean, params?: any) => { export const waitFire = async (user: UserToken, channel: string, trgr: () => any, cond: (msg: Record<string, any>) => boolean, params?: any) => {
return new Promise<boolean>(async (res, rej) => { return new Promise<boolean>(async (res, rej) => {
let timer: NodeJS.Timeout | null = null; let timer: NodeJS.Timeout | null = null;