341 lines
9.8 KiB
JavaScript
341 lines
9.8 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const { Op } = require('sequelize');
|
|
const SettingExpressInterfaceService = require('../../../../../app/service/systems/setting/expressInterface');
|
|
|
|
function buildRecord(row, calls) {
|
|
return {
|
|
...row,
|
|
async update(updates) {
|
|
calls.updatePayload = {
|
|
id: Number(this.id),
|
|
updates: {
|
|
...updates,
|
|
},
|
|
};
|
|
Object.assign(this, updates);
|
|
return this;
|
|
},
|
|
async destroy() {
|
|
calls.destroyPayload = {
|
|
id: Number(this.id),
|
|
};
|
|
return 1;
|
|
},
|
|
get() {
|
|
return { ...this };
|
|
},
|
|
};
|
|
}
|
|
|
|
function createHarness(options = {}) {
|
|
const calls = {
|
|
findAndCountAllCalls: 0,
|
|
findOneCalls: [],
|
|
createPayload: null,
|
|
updatePayload: null,
|
|
destroyPayload: null,
|
|
};
|
|
|
|
const defaultRows = options.rows || [
|
|
{
|
|
id: 11,
|
|
warehouse_id: 100000,
|
|
name: '顺丰接口',
|
|
express_code: 'SF',
|
|
platform_code: 'TB',
|
|
express_platform_id: 1,
|
|
sender_name: '张三',
|
|
sender_tel: '13800000000',
|
|
weight: 10,
|
|
token: 'token-1',
|
|
viewer: '验视员A',
|
|
client_id: 'client-1',
|
|
is_share: 1,
|
|
share_id: 9001,
|
|
can_use_count: 3,
|
|
province_name: '广东省',
|
|
city_name: '深圳市',
|
|
district_name: '南山区',
|
|
site_code: 'SITE-01',
|
|
product_type: 'STD',
|
|
shop_name: '旗舰店',
|
|
},
|
|
{
|
|
id: 12,
|
|
warehouse_id: 100000,
|
|
name: '圆通接口',
|
|
express_code: 'YTO',
|
|
platform_code: 'JD',
|
|
express_platform_id: 2,
|
|
},
|
|
{
|
|
id: 21,
|
|
warehouse_id: 200000,
|
|
name: '其他仓库接口',
|
|
express_code: 'SF',
|
|
platform_code: 'TB',
|
|
express_platform_id: 1,
|
|
},
|
|
];
|
|
|
|
const ctx = {
|
|
user: {
|
|
store_id: options.storeId || 100000,
|
|
},
|
|
where: options.where || {},
|
|
whereParsed: options.whereParsed || null,
|
|
app: {
|
|
Sequelize: {
|
|
Op,
|
|
},
|
|
},
|
|
model: {
|
|
ExpressInterface: {
|
|
async findAndCountAll(query) {
|
|
calls.findAndCountAllCalls += 1;
|
|
calls.findAndCountAllQuery = query;
|
|
return {
|
|
rows: options.listRows || defaultRows,
|
|
count: options.count !== undefined ? options.count : (options.listRows || defaultRows).length,
|
|
};
|
|
},
|
|
async findOne(query) {
|
|
calls.findOneCalls.push(query);
|
|
const where = query.where || {};
|
|
const excludedId = where.id && where.id[Op.ne] ? Number(where.id[Op.ne]) : null;
|
|
const found = defaultRows.find(row => {
|
|
const sameWarehouse = Number(row.warehouse_id) === Number(where.warehouse_id);
|
|
const isExcluded = excludedId && Number(row.id) === excludedId;
|
|
|
|
if (Object.prototype.hasOwnProperty.call(where, 'id') && !Object.prototype.hasOwnProperty.call(where, 'express_code') && !Object.prototype.hasOwnProperty.call(where, 'platform_code')) {
|
|
return sameWarehouse && Number(row.id) === Number(where.id) && !isExcluded;
|
|
}
|
|
|
|
if (Object.prototype.hasOwnProperty.call(where, 'express_code')) {
|
|
const sameCode = String(row.express_code || '') === String(where.express_code || '');
|
|
if (!sameCode) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (Object.prototype.hasOwnProperty.call(where, 'platform_code')) {
|
|
const samePlatform = String(row.platform_code || '') === String(where.platform_code || '');
|
|
if (!samePlatform) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (Object.prototype.hasOwnProperty.call(where, 'express_platform_id')) {
|
|
if (Number(row.express_platform_id) !== Number(where.express_platform_id)) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return sameWarehouse && !isExcluded;
|
|
});
|
|
|
|
return found ? buildRecord({ ...found }, calls) : null;
|
|
},
|
|
async create(payload) {
|
|
calls.createPayload = {
|
|
...payload,
|
|
};
|
|
return buildRecord({
|
|
id: options.createdId || 99,
|
|
...payload,
|
|
}, calls);
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
return {
|
|
ctx,
|
|
calls,
|
|
};
|
|
}
|
|
|
|
describe('test/app/service/systems/setting/expressInterface.test.js', () => {
|
|
it('should list express interfaces with pagination and quick where', async () => {
|
|
const { ctx, calls } = createHarness({
|
|
where: {
|
|
quickWhere: '接口',
|
|
},
|
|
whereParsed: {
|
|
dynamicConditions: [],
|
|
rawConditionsByField: {
|
|
quickWhere: {
|
|
value: '接口',
|
|
},
|
|
},
|
|
},
|
|
listRows: [
|
|
{
|
|
id: 11,
|
|
warehouse_id: 100000,
|
|
name: '顺丰接口',
|
|
express_code: 'SF',
|
|
platform_code: 'TB',
|
|
express_platform_id: 1,
|
|
sender_name: '张三',
|
|
sender_tel: '13800000000',
|
|
weight: 10,
|
|
token: 'token-1',
|
|
viewer: '验视员A',
|
|
client_id: 'client-1',
|
|
is_share: 1,
|
|
share_id: 9001,
|
|
can_use_count: 3,
|
|
province_name: '广东省',
|
|
city_name: '深圳市',
|
|
district_name: '南山区',
|
|
site_code: 'SITE-01',
|
|
product_type: 'STD',
|
|
shop_name: '旗舰店',
|
|
},
|
|
],
|
|
count: 1,
|
|
});
|
|
const service = new SettingExpressInterfaceService(ctx);
|
|
|
|
const result = await service.list({
|
|
page: 2,
|
|
page_size: 1,
|
|
where: {
|
|
quickWhere: '接口',
|
|
},
|
|
});
|
|
|
|
assert.equal(calls.findAndCountAllCalls, 1);
|
|
assert.equal(calls.findAndCountAllQuery.offset, 1);
|
|
assert.equal(calls.findAndCountAllQuery.limit, 1);
|
|
assert.equal(calls.findAndCountAllQuery.where[Op.and][0].warehouse_id, 100000);
|
|
assert.equal(result.code, 200);
|
|
assert.equal(result.data.total, 1);
|
|
assert.equal(result.data.page, 2);
|
|
assert.equal(result.data.page_size, 1);
|
|
assert.equal(result.data.list[0].name, '顺丰接口');
|
|
assert.equal(result.data.list[0].platform_code, 'TB');
|
|
assert.equal(result.data.list[0].express_platform_id, 1);
|
|
});
|
|
|
|
it('should create a new express interface and normalize codes', async () => {
|
|
const { ctx, calls } = createHarness();
|
|
const service = new SettingExpressInterfaceService(ctx);
|
|
|
|
const result = await service.create({
|
|
name: '京东接口',
|
|
express_code: 'jd',
|
|
platform_code: 'tb',
|
|
express_platform_id: 3,
|
|
sender_name: '李四',
|
|
sender_tel: '13900000000',
|
|
token: ' token-2 ',
|
|
is_share: 1,
|
|
can_use_count: 5,
|
|
});
|
|
|
|
assert.equal(calls.createPayload.warehouse_id, 100000);
|
|
assert.equal(calls.createPayload.express_code, 'JD');
|
|
assert.equal(calls.createPayload.platform_code, 'TB');
|
|
assert.equal(calls.createPayload.express_platform_id, 3);
|
|
assert.equal(result.data.express_code, 'JD');
|
|
assert.equal(result.data.platform_code, 'TB');
|
|
assert.equal(result.data.sender_name, '李四');
|
|
assert.equal(result.data.token, 'token-2');
|
|
});
|
|
|
|
it('should reject create when express code conflicts inside the current warehouse', async () => {
|
|
const { ctx } = createHarness();
|
|
const service = new SettingExpressInterfaceService(ctx);
|
|
|
|
await assert.rejects(async () => {
|
|
await service.create({
|
|
name: '重复编码接口',
|
|
express_code: 'SF',
|
|
platform_code: 'WX',
|
|
express_platform_id: 4,
|
|
});
|
|
}, /快递编码已被当前仓库使用/);
|
|
});
|
|
|
|
it('should reject create when platform and interface combination conflicts', async () => {
|
|
const { ctx } = createHarness();
|
|
const service = new SettingExpressInterfaceService(ctx);
|
|
|
|
await assert.rejects(async () => {
|
|
await service.create({
|
|
name: '重复接口',
|
|
express_code: 'ZTO',
|
|
platform_code: 'TB',
|
|
express_platform_id: 1,
|
|
});
|
|
}, /当前平台和快递接口组合已被使用/);
|
|
});
|
|
|
|
it('should update a record with boundary checks and partial fields', async () => {
|
|
const { ctx, calls } = createHarness();
|
|
const service = new SettingExpressInterfaceService(ctx);
|
|
|
|
const result = await service.update({
|
|
id: 11,
|
|
name: '顺丰接口-更新',
|
|
token: ' token-updated ',
|
|
can_use_count: 0,
|
|
});
|
|
|
|
assert.equal(calls.findOneCalls.length >= 1, true);
|
|
assert.equal(calls.updatePayload.id, 11);
|
|
assert.equal(calls.updatePayload.updates.name, '顺丰接口-更新');
|
|
assert.equal(calls.updatePayload.updates.token, 'token-updated');
|
|
assert.equal(calls.updatePayload.updates.can_use_count, 0);
|
|
assert.equal(result.data.name, '顺丰接口-更新');
|
|
});
|
|
|
|
it('should delete a record only inside the current warehouse', async () => {
|
|
const { ctx, calls } = createHarness();
|
|
const service = new SettingExpressInterfaceService(ctx);
|
|
|
|
const result = await service.delete({
|
|
id: 11,
|
|
});
|
|
|
|
assert.equal(calls.destroyPayload.id, 11);
|
|
assert.equal(result.code, 200);
|
|
assert.equal(result.data.deleted, true);
|
|
assert.equal(result.data.warehouse_id, 100000);
|
|
});
|
|
|
|
it('should reject cross-warehouse updates and deletes', async () => {
|
|
const { ctx } = createHarness({
|
|
storeId: 100000,
|
|
rows: [
|
|
{
|
|
id: 21,
|
|
warehouse_id: 200000,
|
|
name: '其他仓库接口',
|
|
express_code: 'SF',
|
|
platform_code: 'TB',
|
|
express_platform_id: 1,
|
|
},
|
|
],
|
|
});
|
|
const service = new SettingExpressInterfaceService(ctx);
|
|
|
|
await assert.rejects(async () => {
|
|
await service.update({
|
|
id: 21,
|
|
name: '越权更新',
|
|
});
|
|
}, /快递接口不存在或无权限操作/);
|
|
|
|
await assert.rejects(async () => {
|
|
await service.delete({
|
|
id: 21,
|
|
});
|
|
}, /快递接口不存在或无权限操作/);
|
|
});
|
|
});
|