Files
jiapu/tests/api-client.test.js
T

142 lines
4.1 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: 'https://test-genealogy-api.ddxcjp.cn',
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');
await client.sendSmsCode({
sceneCode: 'WEB_H5_LOGIN',
phone: '13800000000',
validToken: 'captcha-ticket'
});
assert.strictEqual(calls[8].url, '/genealogy/pc/auth/sms/code');
assert.strictEqual(calls[8].headers.Authorization, undefined);
assert.strictEqual(calls[8].data.grantType, 'sms');
assert.strictEqual(calls[8].data.sceneCode, 'WEB_H5_LOGIN');
assert.strictEqual(calls[8].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, 9);
console.log('api-client tests passed');
}
run();