85 lines
2.6 KiB
JavaScript
85 lines
2.6 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const {
|
|
getAuthToken,
|
|
invokeSystemMethod,
|
|
} = require('../../support/system_method_client');
|
|
|
|
const EXTRA_HEADERS = {
|
|
platform: 'web',
|
|
project: 'all',
|
|
};
|
|
|
|
const FALLBACK_ROLE_IDS = [ 4, 6, 30 ];
|
|
|
|
async function switchToWarehouseRole(token) {
|
|
let currentToken = token;
|
|
const infoRes = await invokeSystemMethod('user.info', {}, currentToken, EXTRA_HEADERS);
|
|
|
|
assert.equal(infoRes.status, 200);
|
|
assert.equal(infoRes.body.flag, 'success');
|
|
assert.equal(infoRes.body.code, 200);
|
|
|
|
const employees = Array.isArray(infoRes.body?.data?.employees) ? infoRes.body.data.employees : [];
|
|
const warehouseRoles = employees.filter(item => item && item.role_type === 'warehouse' && Number(item.id));
|
|
const candidateRoleIds = [ ...new Set([
|
|
...warehouseRoles.map(item => Number(item.id)),
|
|
...FALLBACK_ROLE_IDS,
|
|
].filter(Boolean)) ];
|
|
|
|
assert.ok(candidateRoleIds.length > 0, '当前账号没有可尝试的仓库角色');
|
|
|
|
for (const roleId of candidateRoleIds) {
|
|
const changeRes = await invokeSystemMethod('user.changeRole', {
|
|
roleId,
|
|
}, currentToken, EXTRA_HEADERS);
|
|
|
|
if (changeRes.status !== 200 || changeRes.body?.flag !== 'success' || changeRes.body?.code !== 200) {
|
|
continue;
|
|
}
|
|
|
|
currentToken = changeRes.body?.data?.token || currentToken;
|
|
|
|
const listRes = await invokeSystemMethod('stock.warehouse.list', {
|
|
pagination: { page: 1, pageSize: 1 },
|
|
where: {},
|
|
}, currentToken, EXTRA_HEADERS);
|
|
|
|
if (listRes.status === 200 && listRes.body?.flag === 'success' && listRes.body?.code === 200) {
|
|
return {
|
|
token: currentToken,
|
|
roleId,
|
|
};
|
|
}
|
|
}
|
|
|
|
throw new Error('未找到可访问仓库商品列表的仓库角色');
|
|
}
|
|
|
|
describe('test/app/controller/system.stock.warehouse.namespaced.test.js', () => {
|
|
it('should query stock warehouse list via namespaced method', async () => {
|
|
const { token } = await getAuthToken();
|
|
const switched = await switchToWarehouseRole(token);
|
|
const payload = {
|
|
pagination: {
|
|
page: 1,
|
|
pageSize: 5,
|
|
},
|
|
where: {},
|
|
};
|
|
|
|
const namespacedRes = await invokeSystemMethod('stock.warehouse.list', payload, switched.token, EXTRA_HEADERS);
|
|
|
|
assert.equal(namespacedRes.status, 200);
|
|
assert.equal(namespacedRes.body.flag, 'success');
|
|
assert.equal(namespacedRes.body.code, 200);
|
|
|
|
const namespacedData = namespacedRes.body.data;
|
|
|
|
assert.ok(namespacedData, '新入口缺少 data');
|
|
assert.ok(Array.isArray(namespacedData.list), '新入口 list 不是数组');
|
|
assert.ok(namespacedData.pagination, '新入口缺少 pagination');
|
|
});
|
|
});
|