文件
yjyf/tests/user_identity_api.js
MiAn b71980dd6b feat: 新增账号安全模块及资质校验功能
- 新增账号安全页面及支付密码验证组件
- 新增用户类型选择流程及商家资质管理
- 新增订单资质校验逻辑,师傅需满足订单证书要求
- 新增企业资质认证相关数据库表及接口
- 优化用户列表展示,区分个人与商家用户类型
2026-11-21 15:37:01 +08:00

75 行
4.9 KiB
JavaScript

const assert = require('assert');
const { execFileSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const http = require('http');
const base = process.env.API_BASE || 'http://yjyf:9560/api';
const envText = fs.readFileSync(path.join(__dirname, '../server/.env'), 'utf8');
const db = envText.split('[database]')[1].split(/\n\s*\[/)[0];
const setting = name => (db.match(new RegExp('^' + name + '\\s*=\\s*(.*)$', 'm')) || [])[1]?.trim();
const mysql = process.env.MYSQL_BIN || 'D:\\Softwares\\mysql-8.0.31-winx64\\bin\\mysql.exe';
function sql(query) {
return execFileSync(mysql, ['-h', setting('hostname'), '-P', setting('hostport'), '-u', setting('username'), '-D', setting('database'), '-N', '-e', query], { encoding: 'utf8', env: { ...process.env, MYSQL_PWD: setting('password') } }).trim();
}
async function api(route, data, token) {
return new Promise((resolve, reject) => {
const request = http.request(base + route, { method: data ? 'POST' : 'GET', headers: { ...(token ? { token } : {}), ...(data ? { 'content-type': 'application/x-www-form-urlencoded' } : {}) } }, response => {
let body = '';
response.setEncoding('utf8');
response.on('data', chunk => { body += chunk; });
response.on('end', () => { try { resolve(JSON.parse(body)); } catch (err) { reject(err); } });
});
request.on('error', reject);
request.end(data ? new URLSearchParams(data).toString() : undefined);
});
}
(async () => {
const mobiles = [];
try {
for (const kind of ['personal', 'merchant']) {
let mobile;
do { mobile = '166' + String(Math.floor(Math.random() * 100000000)).padStart(8, '0'); } while (sql(`SELECT COUNT(*) FROM fa_user WHERE mobile='${mobile}'`) !== '0');
mobiles.push(mobile);
const login = await api('/auth/login', { mobile, password: 'TestPassword123!' });
assert.strictEqual(login.code, 1, login.msg);
const token = login.data.token;
assert.strictEqual(login.data.userinfo.identity_selected, false, 'new user must choose identity');
const invalid = await api('/auth/selectIdentity', { user_type: 'master' }, token);
assert.notStrictEqual(invalid.code, 1, 'only personal and merchant choices are accepted');
const beforeChoice = await api('/auth/login', { mobile, password: 'TestPassword123!' });
assert.strictEqual(beforeChoice.data.userinfo.identity_selected, false, 'unfinished selection survives a new login');
const prematureSave = await api('/auth/saveAccount', { nickname: 'Test identity' }, token);
assert.notStrictEqual(prematureSave.code, 1, 'account cannot be saved before selecting identity');
const select = await api('/auth/selectIdentity', { user_type: kind }, token);
assert.strictEqual(select.code, 1, select.msg);
assert.strictEqual(select.data.userinfo.user_type, kind);
assert.strictEqual(select.data.userinfo.identity_selected, true);
const again = await api('/auth/selectIdentity', { user_type: kind === 'personal' ? 'merchant' : 'personal' }, token);
assert.notStrictEqual(again.code, 1, 'identity cannot be changed');
const bypass = await api('/auth/saveAccount', { nickname: 'Test identity', user_type: kind === 'personal' ? 'merchant' : 'personal' }, token);
assert.notStrictEqual(bypass.code, 1, 'account save cannot change identity');
if (kind === 'merchant') {
const bills = await api('/merchant/bills', null, token);
assert.notStrictEqual(bills.code, 1, 'merchant identity does not grant certified merchant access');
assert.strictEqual(sql(`SELECT COUNT(*) FROM fa_merchant WHERE user_id=${Number(login.data.userinfo.id)}`), '0', 'access must not create a certified merchant');
const save = await api('/auth/saveAccount', { nickname: 'Test identity', company_name: 'Example Company', legal_person: 'Test', legal_mobile: '13812345678', tech_mobile: '13912345678' }, token);
assert.strictEqual(save.code, 1, save.msg);
const profile = await api('/auth/profile', null, token);
assert.strictEqual(profile.data.company_name, 'Example Company');
assert.strictEqual(profile.data.legal_mobile, '13812345678');
assert.strictEqual(profile.data.is_merchant, false, 'identity does not create certified merchant');
}
const relogin = await api('/auth/login', { mobile, password: 'TestPassword123!' });
assert.strictEqual(relogin.data.userinfo.user_type, kind);
assert.strictEqual(relogin.data.userinfo.identity_selected, true);
}
console.log('identity API tests passed');
} finally {
for (const mobile of mobiles) {
sql(`DELETE s FROM fa_user_security s INNER JOIN fa_user u ON u.id=s.user_id WHERE u.mobile='${mobile}' AND u.username='${mobile}'`);
sql(`DELETE FROM fa_user WHERE mobile='${mobile}' AND username='${mobile}'`);
}
}
})().catch(err => { console.error(err); process.exitCode = 1; });