forked from wangziqi/gongxue-base
test: extend remote SMS smoke to phone binding
This commit is contained in:
@@ -31,9 +31,11 @@ function buildConfig(env = process.env) {
|
||||
const apiBaseUrl = envString(env, 'SMS_SMOKE_API_BASE_URL', envString(env, 'API_BASE', ''));
|
||||
const tenantId = envString(env, 'SMS_SMOKE_TENANT_ID', envString(env, 'TENANT_ID', ''));
|
||||
const phone = envString(env, 'SMS_SMOKE_PHONE');
|
||||
const bindPhone = envString(env, 'SMS_SMOKE_BIND_PHONE');
|
||||
const origin = envString(env, 'SMS_SMOKE_ORIGIN', 'https://admin.tjszsb.com');
|
||||
const purpose = envString(env, 'SMS_SMOKE_PURPOSE', 'login');
|
||||
const code = envString(env, 'SMS_SMOKE_CODE');
|
||||
const bindCode = envString(env, 'SMS_SMOKE_BIND_CODE');
|
||||
|
||||
const missing = [];
|
||||
if (!apiBaseUrl) missing.push('SMS_SMOKE_API_BASE_URL');
|
||||
@@ -47,9 +49,11 @@ function buildConfig(env = process.env) {
|
||||
apiBaseUrl: normalizeBaseUrl(apiBaseUrl),
|
||||
tenantId,
|
||||
phone,
|
||||
bindPhone,
|
||||
origin,
|
||||
purpose,
|
||||
code,
|
||||
bindCode,
|
||||
timeoutMs: envNumber(env, 'SMS_SMOKE_TIMEOUT_MS', DEFAULT_TIMEOUT_MS),
|
||||
skipSend: ['1', 'true', 'yes', 'on'].includes(envString(env, 'SMS_SMOKE_SKIP_SEND').toLowerCase()),
|
||||
skipMe: ['1', 'true', 'yes', 'on'].includes(envString(env, 'SMS_SMOKE_SKIP_ME').toLowerCase()),
|
||||
@@ -105,6 +109,16 @@ async function promptCode(config) {
|
||||
}
|
||||
}
|
||||
|
||||
async function promptBindCode(config) {
|
||||
if (config.bindCode) return config.bindCode;
|
||||
const readline = createInterface({ input, output });
|
||||
try {
|
||||
return (await readline.question('Enter received bind_phone SMS verification code: ')).trim();
|
||||
} finally {
|
||||
readline.close();
|
||||
}
|
||||
}
|
||||
|
||||
async function runRemoteSmsLoginSmoke(inputConfig, options = {}) {
|
||||
const config = inputConfig?.apiBaseUrl ? inputConfig : buildConfig(options.env || process.env);
|
||||
if (!options.quiet) {
|
||||
@@ -145,9 +159,37 @@ async function runRemoteSmsLoginSmoke(inputConfig, options = {}) {
|
||||
if (!options.quiet) console.log(`PASS auth.me user=${me.payload?.user?.id || me.payload?.item?.userId || 'unknown'}`);
|
||||
}
|
||||
|
||||
let boundPhone = null;
|
||||
if (config.bindPhone) {
|
||||
const sendBind = await requestJson(config, '/api/auth/sms/send', {
|
||||
method: 'POST',
|
||||
token,
|
||||
body: { phone: config.bindPhone, purpose: 'bind_phone' },
|
||||
});
|
||||
assertOk('sms.bind.send', sendBind);
|
||||
if (!options.quiet) {
|
||||
console.log(`PASS sms.bind.send provider=${sendBind.payload?.item?.provider || 'unknown'} phone=${maskPhone(config.bindPhone)}`);
|
||||
}
|
||||
|
||||
const bindCode = await promptBindCode(config);
|
||||
if (!bindCode) throw new Error('Missing bind_phone SMS verification code');
|
||||
|
||||
const bind = await requestJson(config, '/api/auth/phone/bind', {
|
||||
method: 'POST',
|
||||
token,
|
||||
body: { phone: config.bindPhone, code: bindCode, purpose: 'bind_phone' },
|
||||
});
|
||||
assertOk('auth.phone.bind', bind);
|
||||
boundPhone = bind.payload?.user?.phone || config.bindPhone;
|
||||
if (!options.quiet) {
|
||||
console.log(`PASS auth.phone.bind phone=${maskPhone(boundPhone)} phoneChanged=${bind.payload?.phoneChanged ?? '-'}`);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
sessionToken: token,
|
||||
user: verify.payload?.user || verify.payload?.item || null,
|
||||
boundPhone,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -167,6 +209,8 @@ Required example:
|
||||
Optional:
|
||||
SMS_SMOKE_ORIGIN=https://admin.tjszsb.com
|
||||
SMS_SMOKE_CODE=<code-you-received>
|
||||
SMS_SMOKE_BIND_PHONE=<another-phone-to-bind>
|
||||
SMS_SMOKE_BIND_CODE=<bind-code-you-received>
|
||||
SMS_SMOKE_SKIP_SEND=true
|
||||
`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user