84 lines
3.0 KiB
JavaScript
84 lines
3.0 KiB
JavaScript
/**
|
|
* 合伙人登录 / 手机号校验专项冒烟(需 API 已启动)
|
|
* 用法: node scripts/test-partner-auth.mjs
|
|
*/
|
|
import { loginWithSms } from './sms-test-helper.mjs';
|
|
|
|
const API = process.env.SMOKE_API ?? 'http://localhost:3000/api/v1';
|
|
|
|
async function req(clientApp, path, options = {}) {
|
|
const headers = {
|
|
'Content-Type': 'application/json',
|
|
'X-Client-App': clientApp,
|
|
...(options.token ? { Authorization: `Bearer ${options.token}` } : {}),
|
|
};
|
|
const res = await fetch(`${API}${path}`, { ...options, headers, body: options.body });
|
|
const json = await res.json();
|
|
if (json.code !== 0) throw new Error(`${path}: ${json.message}`);
|
|
return json.data;
|
|
}
|
|
|
|
async function expectFail(clientApp, path, options = {}) {
|
|
const headers = {
|
|
'Content-Type': 'application/json',
|
|
'X-Client-App': clientApp,
|
|
...(options.token ? { Authorization: `Bearer ${options.token}` } : {}),
|
|
};
|
|
const res = await fetch(`${API}${path}`, { ...options, headers, body: options.body });
|
|
const json = await res.json();
|
|
if (json.code === 0) throw new Error(`${path}: expected failure`);
|
|
return json.message;
|
|
}
|
|
|
|
async function main() {
|
|
console.log('1. PARTNER phone/check unknown');
|
|
const unknownCheck = await expectFail('PARTNER_H5', '/partner/auth/phone/check', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ phone: '13899999999' }),
|
|
});
|
|
if (!unknownCheck.includes('未找到合伙人账号')) throw new Error(`unexpected: ${unknownCheck}`);
|
|
|
|
console.log('2. PARTNER sms/send unknown');
|
|
const unknownSms = await expectFail('PARTNER_H5', '/partner/auth/sms/send', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ phone: '13899999999', scene: 'PARTNER_LOGIN' }),
|
|
});
|
|
if (!unknownSms.includes('未找到合伙人账号')) throw new Error(`unexpected: ${unknownSms}`);
|
|
|
|
console.log('3. PARTNER sms/login bound phone');
|
|
const login = await loginWithSms(
|
|
'PARTNER_H5',
|
|
'13700000001',
|
|
'PARTNER_LOGIN',
|
|
'/partner/auth/login/sms',
|
|
'/partner/auth/sms/send',
|
|
);
|
|
if (!login.accessToken) throw new Error('login missing token');
|
|
|
|
console.log('4. PARTNER /partner/me after login');
|
|
const me = await req('PARTNER_H5', '/partner/me', { token: login.accessToken });
|
|
if (!me?.id || !me?.phone) throw new Error('partner/me missing profile');
|
|
|
|
console.log('5. Admin partner logs list');
|
|
const admin = await req('HQ_WEB', '/admin/auth/login/password', {
|
|
method: 'POST',
|
|
body: JSON.stringify({
|
|
loginName: process.env.SUPER_ADMIN_LOGIN ?? 'admin',
|
|
password: process.env.SUPER_ADMIN_PASSWORD ?? 'dukang@123!',
|
|
}),
|
|
});
|
|
const logs = await req('HQ_WEB', '/admin/logs/partners?page=1&pageSize=10&category=login', {
|
|
token: admin.accessToken,
|
|
});
|
|
if (!Array.isArray(logs.items)) throw new Error('partner logs missing items');
|
|
const hasLogin = logs.items.some((r) => r.eventName === 'partner_sms_login' || r.eventName === 'partner_login_success');
|
|
if (!hasLogin) throw new Error('partner login log not found');
|
|
|
|
console.log('\n✅ partner-auth tests passed');
|
|
}
|
|
|
|
main().catch((e) => {
|
|
console.error(e);
|
|
process.exit(1);
|
|
});
|