Files
jiapu/tests/api-client.test.js
T
2026-07-11 09:41:32 +08:00

246 lines
7.3 KiB
JavaScript

const assert = require('assert');
const GenealogyApi = require('../utils/ApiClient.js');
function createStore(initial) {
const values = Object.assign({}, initial);
return {
getItem(key) {
return Object.prototype.hasOwnProperty.call(values, key) ? values[key] : null;
},
setItem(key, value) {
values[key] = String(value);
},
removeItem(key) {
delete values[key];
},
values
};
}
async function run() {
const calls = [];
const store = createStore({
'token-key': 'abc-token'
});
// 接口基础地址只能由根目录 config.js 负责,公共客户端不能重复声明。
assert.strictEqual(GenealogyApi.DEFAULT_BASE_URL, undefined);
const client = GenealogyApi.createClient({
baseUrl: 'http://182.61.18.23:8080',
clientId: 'client-x',
tenantId: 'tenant-x',
tokenKey: 'token-key',
tokenHeaderName: 'Authorization',
tokenStore: store,
axiosInstance: {
request: async (config) => {
calls.push(config);
return {
status: 200,
data: {
code: 200,
msg: '操作成功',
data: { token: 'server-token', url: config.url }
}
};
}
}
});
await client.login({ phone: '13800000000', password: 'md5' });
assert.strictEqual(calls[0].url, '/genealogy/pc/auth/login');
assert.strictEqual(calls[0].headers.clientid, 'client-x');
assert.strictEqual(calls[0].headers.Authorization, undefined);
assert.strictEqual(calls[0].data.tenantId, 'tenant-x');
assert.strictEqual(store.values['token-key'], 'server-token');
await client.getProfile();
assert.strictEqual(calls[1].url, '/genealogy/pc/auth/profile');
assert.strictEqual(calls[1].headers.Authorization, 'Bearer server-token');
await client.updateProfile({
nickName: '张三',
provinceCode: '510000',
cityCode: '510100',
districtCode: '510104'
});
assert.strictEqual(calls[2].url, '/genealogy/pc/auth/profile');
assert.strictEqual(calls[2].method, 'put');
await client.uploadFile('file-content');
assert.strictEqual(calls[3].url, '/genealogy/pc/files/upload');
assert.strictEqual(calls[3].headers['Content-Type'], undefined);
await client.initResumableUpload({
fileName: 'cover.jpg',
fileSize: 9532,
fileMd5: 'md5',
chunkSize: 4194304,
totalChunks: 1
});
assert.strictEqual(calls[4].url, '/genealogy/pc/files/resumable/init');
await client.getRegionChildren('51');
assert.strictEqual(calls[5].url, '/genealogy/region/children');
assert.deepStrictEqual(calls[5].params, { parentCode: '51' });
await client.captchaRequire({
sceneCode: 'WEB_H5_LOGIN',
subject: '13800000000'
});
assert.strictEqual(
calls[6].url,
'/captcha/require'
);
assert.deepStrictEqual(calls[6].params, {
tenantId: 'tenant-x',
clientId: 'client-x',
sceneCode: 'WEB_H5_LOGIN',
subject: '13800000000'
});
assert.strictEqual(calls[6].headers.Authorization, undefined);
await client.loginBySms({
phone: '13800000000',
smsCode: '6666',
validToken: 'sms-ticket'
});
assert.strictEqual(calls[7].url, '/genealogy/pc/auth/login/sms');
assert.strictEqual(calls[7].headers.Authorization, undefined);
assert.strictEqual(calls[7].data.grantType, 'sms');
assert.strictEqual(calls[7].data.tenantId, 'tenant-x');
assert.strictEqual(calls[7].data.clientId, 'client-x');
assert.strictEqual(store.values['token-key'], 'server-token');
store.setItem('token-key', 'existing-token');
const registerResponse = await client.register({
phone: '13900000000',
password: 'register-md5',
nickName: 'new-user',
validToken: 'register-ticket'
});
assert.deepStrictEqual(registerResponse, {
token: 'server-token',
url: '/genealogy/pc/auth/register'
});
assert.strictEqual(calls[8].url, '/genealogy/pc/auth/register');
assert.strictEqual(calls[8].headers.Authorization, undefined);
assert.strictEqual(calls[8].data.grantType, 'password');
assert.strictEqual(calls[8].data.registerSource, 'PC');
assert.strictEqual(store.getItem('token-key'), null);
const failedRegisterStore = createStore({
'token-key': 'existing-token'
});
const failedRegisterClient = GenealogyApi.createClient({
baseUrl: 'http://182.61.18.23:8080',
clientId: 'client-x',
tenantId: 'tenant-x',
tokenKey: 'token-key',
tokenStore: failedRegisterStore,
axiosInstance: {
request: async () => {
throw new Error('register failed');
}
}
});
await assert.rejects(
failedRegisterClient.register({
phone: '13900000001',
password: 'register-md5',
nickName: 'failed-user',
validToken: 'register-ticket'
}),
/register failed/
);
assert.strictEqual(failedRegisterStore.getItem('token-key'), 'existing-token');
const missingTokenStore = createStore();
const missingTokenClient = GenealogyApi.createClient({
baseUrl: 'http://182.61.18.23:8080',
clientId: 'client-x',
tenantId: 'tenant-x',
tokenKey: 'token-key',
tokenStore: missingTokenStore,
axiosInstance: {
request: async () => ({
status: 200,
data: { code: 200, data: {} }
})
}
});
await assert.rejects(
missingTokenClient.login({ phone: '13800000000', password: 'md5' }),
(error) => error.message === '登录响应缺少 token'
);
assert.strictEqual(missingTokenStore.getItem('token-key'), null);
const snakeTokenStore = createStore();
const snakeTokenClient = GenealogyApi.createClient({
baseUrl: 'http://182.61.18.23:8080',
clientId: 'client-x',
tenantId: 'tenant-x',
tokenKey: 'token-key',
tokenStore: snakeTokenStore,
axiosInstance: {
request: async () => ({
status: 200,
data: { code: 200, data: { access_token: 'snake-token' } }
})
}
});
await snakeTokenClient.login({ phone: '13800000000', password: 'md5' });
assert.strictEqual(snakeTokenStore.getItem('token-key'), 'snake-token');
store.setItem('token-key', 'logout-token');
await client.logout();
assert.strictEqual(calls[9].url, '/genealogy/pc/auth/logout');
assert.strictEqual(store.getItem('token-key'), null);
const failedLogoutStore = createStore({
'token-key': 'logout-token'
});
const failedLogoutClient = GenealogyApi.createClient({
baseUrl: 'http://182.61.18.23:8080',
clientId: 'client-x',
tenantId: 'tenant-x',
tokenKey: 'token-key',
tokenStore: failedLogoutStore,
axiosInstance: {
request: async () => {
throw new Error('logout failed');
}
}
});
await assert.rejects(failedLogoutClient.logout(), /logout failed/);
assert.strictEqual(failedLogoutStore.getItem('token-key'), null);
await client.sendSmsCode({
sceneCode: 'WEB_H5_LOGIN',
phone: '13800000000',
validToken: 'captcha-ticket'
});
assert.strictEqual(calls[10].url, '/genealogy/pc/auth/sms/code');
assert.strictEqual(calls[10].headers.Authorization, undefined);
assert.strictEqual(calls[10].data.grantType, 'sms');
assert.strictEqual(calls[10].data.sceneCode, 'WEB_H5_LOGIN');
assert.strictEqual(calls[10].data.validToken, 'captcha-ticket');
assert.throws(
() => client.listGenealogies(),
(error) => error.code === 'PC_API_NOT_AVAILABLE'
);
assert.throws(
() => client.publicGenealogies(),
(error) => error.code === 'PC_API_NOT_AVAILABLE'
);
assert.strictEqual(calls.length, 11);
console.log('api-client tests passed');
}
run();