Files
dukang/scripts/test-shop-auth.mjs
T
2026-07-07 00:04:07 +08:00

83 lines
3.0 KiB
JavaScript

/**
* 门店登录 / 建店短信校验专项冒烟(需 API 已启动且 MOCK_SMS 开启)
* 用法: node scripts/test-shop-auth.mjs
*/
const API = process.env.SMOKE_API ?? 'http://localhost:3000/api/v1';
const MOCK_SMS_CODE = process.env.MOCK_SMS_CODE ?? '123456';
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. STORE_LOGIN unbound phone');
const unbound = await expectFail('SHOP_H5', '/shop/auth/sms/send', {
method: 'POST',
body: JSON.stringify({ phone: '13899999999', scene: 'STORE_LOGIN' }),
});
if (!unbound.includes('未绑定门店')) throw new Error(`unexpected: ${unbound}`);
console.log('2. STORE_LOGIN bound phone');
await req('SHOP_H5', '/shop/auth/sms/send', {
method: 'POST',
body: JSON.stringify({ phone: '13910000001', scene: 'STORE_LOGIN' }),
});
const login = await req('SHOP_H5', '/shop/auth/login/sms', {
method: 'POST',
body: JSON.stringify({ phone: '13910000001', code: MOCK_SMS_CODE }),
});
if (!login.accessToken || !login.refreshToken) throw new Error('login missing tokens');
console.log('3. Shop session me + refresh');
const me = await req('SHOP_H5', '/shop/auth/me', { token: login.accessToken });
if (me.phone !== '13910000001') throw new Error('me phone mismatch');
const refreshed = await req('SHOP_H5', '/shop/auth/token/refresh', {
method: 'POST',
body: JSON.stringify({ refreshToken: login.refreshToken }),
});
if (!refreshed.accessToken) throw new Error('refresh failed');
console.log('4. STORE_ACCOUNT_OPEN occupied phone');
await req('HQ_WEB', '/admin/auth/sms/send', {
method: 'POST',
body: JSON.stringify({ phone: '13600000001', scene: 'HQ_LOGIN' }),
});
const admin = await req('HQ_WEB', '/admin/auth/login/sms', {
method: 'POST',
body: JSON.stringify({ phone: '13600000001', code: MOCK_SMS_CODE }),
});
const occupied = await expectFail('HQ_WEB', '/admin/stores/phone/sms/send', {
method: 'POST',
token: admin.accessToken,
body: JSON.stringify({ phone: '13910000001' }),
});
if (!occupied.includes('已绑定')) throw new Error(`unexpected: ${occupied}`);
console.log('\n✅ shop-auth tests passed');
}
main().catch((e) => {
console.error(e);
process.exit(1);
});