You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.5 KiB
65 lines
1.5 KiB
import type { APIRequestContext } from "@playwright/test";
|
|
|
|
const FIXED_CAPTCHA = process.env.E2E_FIXED_CAPTCHA || "test1234";
|
|
|
|
export interface CaptchaChallenge {
|
|
captchaId: string;
|
|
imageSvg: string;
|
|
}
|
|
|
|
/**
|
|
* 获取验证码 challenge — 调用公开 API
|
|
*/
|
|
export async function fetchCaptcha(
|
|
request: APIRequestContext,
|
|
baseURL: string,
|
|
): Promise<CaptchaChallenge> {
|
|
const res = await request.get(`${baseURL}/api/auth/captcha`);
|
|
const json = await res.json();
|
|
return {
|
|
captchaId: json.data.captchaId,
|
|
imageSvg: json.data.imageSvg,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 通过 API 登录,返回登录后的 request context(带 session cookie)
|
|
*/
|
|
export async function loginViaAPI(
|
|
request: APIRequestContext,
|
|
baseURL: string,
|
|
username: string,
|
|
password: string,
|
|
): Promise<void> {
|
|
const captcha = await fetchCaptcha(request, baseURL);
|
|
await request.post(`${baseURL}/api/auth/login`, {
|
|
data: {
|
|
username,
|
|
password,
|
|
captchaId: captcha.captchaId,
|
|
captchaAnswer: FIXED_CAPTCHA,
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 通过 API 注册
|
|
*/
|
|
export async function registerViaAPI(
|
|
request: APIRequestContext,
|
|
baseURL: string,
|
|
username: string,
|
|
password: string,
|
|
): Promise<void> {
|
|
const captcha = await fetchCaptcha(request, baseURL);
|
|
await request.post(`${baseURL}/api/auth/register`, {
|
|
data: {
|
|
username,
|
|
password,
|
|
captchaId: captcha.captchaId,
|
|
captchaAnswer: FIXED_CAPTCHA,
|
|
},
|
|
});
|
|
}
|
|
|
|
export { FIXED_CAPTCHA };
|
|
|