414 lines
17 KiB
JavaScript
414 lines
17 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',
|
|
};
|
|
|
|
describe('test/app/controller/system.setting.storeWarehouse.test.js', () => {
|
|
const METHOD_PREFIX = 'storeWarehouse.store';
|
|
let token = '';
|
|
let sampleRelation = null;
|
|
|
|
before(async () => {
|
|
const auth = await getAuthToken({ forceLogin: true });
|
|
token = auth.token;
|
|
});
|
|
|
|
it('should list store-warehouse contracts in current subject scope', async () => {
|
|
const res = await invokeSystemMethod(`${METHOD_PREFIX}.list`, {
|
|
page: 1,
|
|
page_size: 10,
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(res.status, 200);
|
|
assert.equal(res.body.flag, 'success');
|
|
assert.equal(res.body.code, 200);
|
|
assert.ok(Array.isArray(res.body?.data?.list), 'list 应返回数组');
|
|
assert.ok(res.body?.data?.current_subject, '应返回当前主体信息');
|
|
sampleRelation = Array.isArray(res.body?.data?.list) && res.body.data.list.length > 0
|
|
? res.body.data.list[0]
|
|
: null;
|
|
});
|
|
|
|
it('should allow renaming current-side remask and reflect it in list display', async () => {
|
|
assert.ok(sampleRelation, '需要至少一条现有签约关系用于改名回归');
|
|
|
|
const listRes = await invokeSystemMethod(`${METHOD_PREFIX}.list`, {
|
|
page: 1,
|
|
page_size: 30,
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(listRes.status, 200);
|
|
assert.equal(listRes.body.flag, 'success');
|
|
assert.equal(listRes.body.code, 200);
|
|
|
|
const currentRole = String(listRes.body?.data?.current_subject?.role || '');
|
|
const candidates = Array.isArray(listRes.body?.data?.list) ? listRes.body.data.list : [];
|
|
const candidate = candidates.find(item => Number(item.id) > 0);
|
|
assert.ok(candidate, '需要至少一条签约关系用于改名回归');
|
|
|
|
const isWarehouse = currentRole === 'warehouse';
|
|
const originRemask = String(candidate.remask || '');
|
|
const nextRemask = `autotest_alias_${Date.now()}`;
|
|
|
|
try {
|
|
const renameRes = await invokeSystemMethod(`${METHOD_PREFIX}.rename`, {
|
|
id: Number(candidate.id),
|
|
remask: nextRemask,
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(renameRes.status, 200);
|
|
assert.equal(renameRes.body.flag, 'success');
|
|
assert.equal(renameRes.body.code, 200);
|
|
assert.equal(String(renameRes.body?.data?.contract?.remask), nextRemask);
|
|
assert.equal(
|
|
String(isWarehouse ? renameRes.body?.data?.contract?.store_name : renameRes.body?.data?.contract?.warehouse_name),
|
|
nextRemask
|
|
);
|
|
|
|
const verifyRes = await invokeSystemMethod(`${METHOD_PREFIX}.list`, {
|
|
id: Number(candidate.id),
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(verifyRes.status, 200);
|
|
assert.equal(verifyRes.body.flag, 'success');
|
|
assert.equal(verifyRes.body.code, 200);
|
|
|
|
const verified = Array.isArray(verifyRes.body?.data?.list) ? verifyRes.body.data.list[0] : null;
|
|
assert.ok(verified, '改名后应能查到对应签约关系');
|
|
assert.equal(
|
|
String(isWarehouse ? verified.store_name : verified.warehouse_name),
|
|
nextRemask
|
|
);
|
|
} finally {
|
|
await invokeSystemMethod(`${METHOD_PREFIX}.rename`, {
|
|
id: Number(candidate.id),
|
|
remask: originRemask,
|
|
}, token, EXTRA_HEADERS);
|
|
}
|
|
});
|
|
|
|
it('should generate a temp code and support preview before confirmation', async () => {
|
|
assert.ok(sampleRelation, '需要至少一条现有签约关系用于临时码回归');
|
|
|
|
const candidateRes = await invokeSystemMethod(`${METHOD_PREFIX}.list`, {
|
|
page: 1,
|
|
page_size: 30,
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(candidateRes.status, 200);
|
|
assert.equal(candidateRes.body.flag, 'success');
|
|
assert.equal(candidateRes.body.code, 200);
|
|
|
|
const candidates = Array.isArray(candidateRes.body?.data?.list) ? candidateRes.body.data.list : [];
|
|
assert.ok(candidates.length > 0, '需要至少一条签约关系用于临时码回归');
|
|
|
|
let initRes = null;
|
|
for (const item of candidates) {
|
|
const attempt = await invokeSystemMethod(`${METHOD_PREFIX}.sign`, {
|
|
counterparty_id: Number(item.counterparty_id),
|
|
apply_reason: 'autotest temp sign flow',
|
|
remask: `autotest_remask_${Date.now()}`,
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
if (attempt.status === 200 && attempt.body && attempt.body.flag === 'success') {
|
|
initRes = attempt;
|
|
break;
|
|
}
|
|
}
|
|
|
|
assert.ok(initRes, '未找到可发起临时码签约的对方主体,请确认测试账号签约数据');
|
|
|
|
assert.equal(initRes.status, 200);
|
|
assert.equal(initRes.body.flag, 'success');
|
|
assert.equal(initRes.body.code, 200);
|
|
assert.equal(initRes.body?.data?.mode, 'initiate');
|
|
assert.ok(String(initRes.body?.data?.temp_code || '').length >= 8, '发起签约后应返回临时码');
|
|
|
|
const previewRes = await invokeSystemMethod(`${METHOD_PREFIX}.sign`, {
|
|
temp_code: initRes.body.data.temp_code,
|
|
action: 'preview',
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(previewRes.status, 200);
|
|
assert.equal(previewRes.body.flag, 'success');
|
|
assert.equal(previewRes.body.code, 200);
|
|
assert.equal(previewRes.body?.data?.mode, 'preview');
|
|
assert.equal(String(previewRes.body?.data?.temp_code || ''), String(initRes.body?.data?.temp_code || ''));
|
|
assert.equal(Boolean(previewRes.body?.data?.can_confirm), false, '同一发起方不应直接确认自己的临时码');
|
|
});
|
|
|
|
it('should only change use_status when unbinding', async () => {
|
|
assert.ok(sampleRelation, '需要至少一条现有签约关系用于解除回归');
|
|
|
|
const originalAuthStatus = Number(sampleRelation.auth_status || 0);
|
|
const originalUseStatus = Number(sampleRelation.use_status || 0);
|
|
const targetUseStatus = originalUseStatus === 1 ? 0 : 1;
|
|
|
|
try {
|
|
const unbindRes = await invokeSystemMethod(`${METHOD_PREFIX}.unbind`, {
|
|
id: Number(sampleRelation.id),
|
|
use_status: targetUseStatus,
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(unbindRes.status, 200);
|
|
assert.equal(unbindRes.body.flag, 'success');
|
|
assert.equal(unbindRes.body.code, 200);
|
|
assert.equal(Number(unbindRes.body?.data?.contract?.use_status), targetUseStatus);
|
|
assert.equal(Number(unbindRes.body?.data?.contract?.auth_status), originalAuthStatus, '解除接口不应改动 auth_status');
|
|
} finally {
|
|
await invokeSystemMethod(`${METHOD_PREFIX}.unbind`, {
|
|
id: Number(sampleRelation.id),
|
|
use_status: originalUseStatus,
|
|
}, token, EXTRA_HEADERS);
|
|
}
|
|
});
|
|
|
|
it('should allow warehouse side to save and get policy config on a contract', async function() {
|
|
const listRes = await invokeSystemMethod(`${METHOD_PREFIX}.list`, {
|
|
page: 1,
|
|
page_size: 30,
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(listRes.status, 200);
|
|
assert.equal(listRes.body.flag, 'success');
|
|
assert.equal(listRes.body.code, 200);
|
|
|
|
const currentRole = String(listRes.body?.data?.current_subject?.role || '');
|
|
if (currentRole !== 'warehouse') {
|
|
this.skip();
|
|
}
|
|
|
|
const candidates = Array.isArray(listRes.body?.data?.list) ? listRes.body.data.list : [];
|
|
const candidate = candidates.find(item => Number(item.id) > 0);
|
|
assert.ok(candidate, '需要至少一条签约关系用于策略配置回归');
|
|
|
|
const originalPolicyConfig = candidate.policy_config && typeof candidate.policy_config === 'object'
|
|
? { ...candidate.policy_config }
|
|
: null;
|
|
const nextPolicyConfig = {
|
|
receive_mode: 'strict',
|
|
sku_visibility: 'whitelist',
|
|
auto_allocate_bin: 1,
|
|
allow_mix_batch: 0,
|
|
max_daily_inbound: 5000,
|
|
remark: `autotest_${Date.now()}`,
|
|
};
|
|
|
|
try {
|
|
const saveRes = await invokeSystemMethod(`${METHOD_PREFIX}.savePolicyConfig`, {
|
|
id: Number(candidate.id),
|
|
policy_config: nextPolicyConfig,
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(saveRes.status, 200);
|
|
assert.equal(saveRes.body.flag, 'success');
|
|
assert.equal(saveRes.body.code, 200);
|
|
assert.equal(Number(saveRes.body?.data?.contract_id || 0), Number(candidate.id));
|
|
assert.equal(String(saveRes.body?.data?.policy_config?.receive_mode || ''), 'strict');
|
|
assert.equal(Number(saveRes.body?.data?.policy_config?.auto_allocate_bin || 0), 1);
|
|
|
|
const getRes = await invokeSystemMethod(`${METHOD_PREFIX}.getPolicyConfig`, {
|
|
id: Number(candidate.id),
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(getRes.status, 200);
|
|
assert.equal(getRes.body.flag, 'success');
|
|
assert.equal(getRes.body.code, 200);
|
|
assert.equal(Number(getRes.body?.data?.contract_id || 0), Number(candidate.id));
|
|
assert.equal(String(getRes.body?.data?.policy_config?.sku_visibility || ''), 'whitelist');
|
|
assert.equal(Number(getRes.body?.data?.policy_config?.max_daily_inbound || 0), 5000);
|
|
} finally {
|
|
if (originalPolicyConfig) {
|
|
await invokeSystemMethod(`${METHOD_PREFIX}.savePolicyConfig`, {
|
|
id: Number(candidate.id),
|
|
policy_config: originalPolicyConfig,
|
|
}, token, EXTRA_HEADERS);
|
|
}
|
|
}
|
|
});
|
|
|
|
it('should reject unsupported policy config fields on a contract', async function() {
|
|
const listRes = await invokeSystemMethod(`${METHOD_PREFIX}.list`, {
|
|
page: 1,
|
|
page_size: 30,
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(listRes.status, 200);
|
|
assert.equal(listRes.body.flag, 'success');
|
|
assert.equal(listRes.body.code, 200);
|
|
|
|
const currentRole = String(listRes.body?.data?.current_subject?.role || '');
|
|
if (currentRole !== 'warehouse') {
|
|
this.skip();
|
|
}
|
|
|
|
const candidates = Array.isArray(listRes.body?.data?.list) ? listRes.body.data.list : [];
|
|
const candidate = candidates.find(item => Number(item.id) > 0);
|
|
assert.ok(candidate, '需要至少一条签约关系用于策略字段限制回归');
|
|
|
|
const res = await invokeSystemMethod(`${METHOD_PREFIX}.savePolicyConfig`, {
|
|
id: Number(candidate.id),
|
|
policy_config: {
|
|
receive_mode: 'strict',
|
|
unexpected_field: 'blocked',
|
|
},
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(res.status, 200);
|
|
assert.equal(res.body.flag, 'failure');
|
|
assert.equal(Number(res.body.code), 400);
|
|
assert.match(String(res.body.message || ''), /unexpected_field|不支持字段/);
|
|
});
|
|
|
|
it('should allow warehouse side to save, update and delete qimen config on a contract', async function() {
|
|
const listRes = await invokeSystemMethod(`${METHOD_PREFIX}.list`, {
|
|
page: 1,
|
|
page_size: 30,
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(listRes.status, 200);
|
|
assert.equal(listRes.body.flag, 'success');
|
|
assert.equal(listRes.body.code, 200);
|
|
|
|
const currentRole = String(listRes.body?.data?.current_subject?.role || '');
|
|
if (currentRole !== 'warehouse') {
|
|
this.skip();
|
|
}
|
|
|
|
const candidates = Array.isArray(listRes.body?.data?.list) ? listRes.body.data.list : [];
|
|
const candidate = candidates.find(item => Number(item.id) > 0);
|
|
assert.ok(candidate, '需要至少一条签约关系用于奇门配置回归');
|
|
|
|
const originalQimenConfig = candidate.qimen_config ? { ...candidate.qimen_config } : null;
|
|
const seed = Date.now();
|
|
const createPayload = {
|
|
id: Number(candidate.id),
|
|
app_name: `autotest_qimen_${seed}`,
|
|
qimen_app_key: `autotest_app_${seed}`,
|
|
qimen_customer_id: `autotest_customer_${seed}`,
|
|
qimen_warehouse_code: `autotest_wh_${seed}`,
|
|
contact_person: 'autotest',
|
|
contact_phone: '13800138000',
|
|
email: `autotest_${seed}@example.com`,
|
|
is_enabled: 1,
|
|
};
|
|
|
|
try {
|
|
const saveRes = await invokeSystemMethod(`${METHOD_PREFIX}.saveQimenConfig`, createPayload, token, EXTRA_HEADERS);
|
|
assert.equal(saveRes.status, 200);
|
|
assert.equal(saveRes.body.flag, 'success');
|
|
assert.equal(saveRes.body.code, 200);
|
|
assert.match(String(saveRes.body?.data?.action || ''), /create|update/);
|
|
assert.equal(String(saveRes.body?.data?.qimen_config?.qimen_app_key || ''), createPayload.qimen_app_key);
|
|
assert.equal(String(saveRes.body?.data?.qimen_config?.qimen_customer_id || ''), createPayload.qimen_customer_id);
|
|
assert.equal(String(saveRes.body?.data?.qimen_config?.qimen_warehouse_code || ''), createPayload.qimen_warehouse_code);
|
|
|
|
const updatePayload = {
|
|
...createPayload,
|
|
app_name: `${createPayload.app_name}_updated`,
|
|
qimen_customer_id: `${createPayload.qimen_customer_id}_updated`,
|
|
qimen_warehouse_code: `${createPayload.qimen_warehouse_code}_updated`,
|
|
is_enabled: 0,
|
|
};
|
|
const updateRes = await invokeSystemMethod(`${METHOD_PREFIX}.saveQimenConfig`, updatePayload, token, EXTRA_HEADERS);
|
|
assert.equal(updateRes.status, 200);
|
|
assert.equal(updateRes.body.flag, 'success');
|
|
assert.equal(updateRes.body.code, 200);
|
|
assert.equal(String(updateRes.body?.data?.action || ''), 'update');
|
|
assert.equal(String(updateRes.body?.data?.qimen_config?.qimen_customer_id || ''), updatePayload.qimen_customer_id);
|
|
assert.equal(String(updateRes.body?.data?.qimen_config?.qimen_warehouse_code || ''), updatePayload.qimen_warehouse_code);
|
|
assert.equal(Number(updateRes.body?.data?.qimen_config?.is_enabled), 0);
|
|
|
|
const verifyRes = await invokeSystemMethod(`${METHOD_PREFIX}.list`, {
|
|
id: Number(candidate.id),
|
|
}, token, EXTRA_HEADERS);
|
|
assert.equal(verifyRes.status, 200);
|
|
assert.equal(verifyRes.body.flag, 'success');
|
|
assert.equal(verifyRes.body.code, 200);
|
|
const verified = Array.isArray(verifyRes.body?.data?.list) ? verifyRes.body.data.list[0] : null;
|
|
assert.ok(verified, '保存奇门配置后应能查询到签约关系');
|
|
assert.equal(String(verified?.qimen_config?.qimen_customer_id || ''), updatePayload.qimen_customer_id);
|
|
|
|
const configListRes = await invokeSystemMethod(`${METHOD_PREFIX}.listQimenConfig`, {
|
|
id: Number(candidate.id),
|
|
}, token, EXTRA_HEADERS);
|
|
assert.equal(configListRes.status, 200);
|
|
assert.equal(configListRes.body.flag, 'success');
|
|
assert.equal(configListRes.body.code, 200);
|
|
assert.equal(Number(configListRes.body?.data?.contract_id || 0), Number(candidate.id));
|
|
assert.ok(Array.isArray(configListRes.body?.data?.list), '应返回奇门配置列表');
|
|
assert.ok((configListRes.body?.data?.list || []).length >= 1, '保存后应至少返回一条奇门配置');
|
|
assert.equal(String(configListRes.body?.data?.list?.[0]?.qimen_customer_id || ''), updatePayload.qimen_customer_id);
|
|
|
|
const deleteRes = await invokeSystemMethod(`${METHOD_PREFIX}.deleteQimenConfig`, {
|
|
id: Number(candidate.id),
|
|
}, token, EXTRA_HEADERS);
|
|
assert.equal(deleteRes.status, 200);
|
|
assert.equal(deleteRes.body.flag, 'success');
|
|
assert.equal(deleteRes.body.code, 200);
|
|
assert.equal(Boolean(deleteRes.body?.data?.deleted), true);
|
|
|
|
const verifyDeleteRes = await invokeSystemMethod(`${METHOD_PREFIX}.list`, {
|
|
id: Number(candidate.id),
|
|
}, token, EXTRA_HEADERS);
|
|
assert.equal(verifyDeleteRes.status, 200);
|
|
assert.equal(verifyDeleteRes.body.flag, 'success');
|
|
assert.equal(verifyDeleteRes.body.code, 200);
|
|
const deletedView = Array.isArray(verifyDeleteRes.body?.data?.list) ? verifyDeleteRes.body.data.list[0] : null;
|
|
assert.ok(deletedView, '删除奇门配置后仍应能查询到签约关系');
|
|
assert.equal(deletedView?.qimen_config || null, null);
|
|
|
|
const emptyListRes = await invokeSystemMethod(`${METHOD_PREFIX}.listQimenConfig`, {
|
|
id: Number(candidate.id),
|
|
}, token, EXTRA_HEADERS);
|
|
assert.equal(emptyListRes.status, 200);
|
|
assert.equal(emptyListRes.body.flag, 'success');
|
|
assert.equal(emptyListRes.body.code, 200);
|
|
assert.ok(Array.isArray(emptyListRes.body?.data?.list), '删除后仍应返回列表结构');
|
|
assert.equal(Number(emptyListRes.body?.data?.total || 0), 0);
|
|
} finally {
|
|
if (originalQimenConfig) {
|
|
await invokeSystemMethod(`${METHOD_PREFIX}.saveQimenConfig`, {
|
|
id: Number(candidate.id),
|
|
app_name: originalQimenConfig.app_name,
|
|
qimen_app_key: originalQimenConfig.qimen_app_key,
|
|
qimen_customer_id: originalQimenConfig.qimen_customer_id,
|
|
qimen_warehouse_code: originalQimenConfig.qimen_warehouse_code,
|
|
contact_person: originalQimenConfig.contact_person,
|
|
contact_phone: originalQimenConfig.contact_phone,
|
|
email: originalQimenConfig.email,
|
|
is_enabled: Number(originalQimenConfig.is_enabled || 0),
|
|
}, token, EXTRA_HEADERS);
|
|
}
|
|
}
|
|
});
|
|
|
|
it('should reject sign when counterparty is missing', async () => {
|
|
const res = await invokeSystemMethod(`${METHOD_PREFIX}.sign`, {
|
|
print_quantity: 0,
|
|
}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(res.status, 200);
|
|
assert.equal(res.body.flag, 'failure');
|
|
assert.equal(Number(res.body.code), 400);
|
|
assert.match(String(res.body.message || ''), /counterparty_id|store_id|warehouse_id/);
|
|
});
|
|
|
|
it('should reject unbind when no target relation is provided', async () => {
|
|
const res = await invokeSystemMethod(`${METHOD_PREFIX}.unbind`, {}, token, EXTRA_HEADERS);
|
|
|
|
assert.equal(res.status, 200);
|
|
assert.equal(res.body.flag, 'failure');
|
|
assert.equal(Number(res.body.code), 400);
|
|
assert.match(String(res.body.message || ''), /id|counterparty_id|store_id|warehouse_id/);
|
|
});
|
|
});
|