271 lines
7.3 KiB
JavaScript
271 lines
7.3 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const ExpressInterfaceMethodService = require('../../../../../app/service/systemMethods/setting/expressInterface');
|
|
|
|
function createContext() {
|
|
const calls = {
|
|
list: [],
|
|
listByWarehousePlatformCode: [],
|
|
create: [],
|
|
update: [],
|
|
delete: [],
|
|
prepareWhereContext: 0,
|
|
};
|
|
|
|
const ctx = {
|
|
app: {
|
|
config: {
|
|
jwt: {},
|
|
},
|
|
Sequelize: {},
|
|
},
|
|
service: {
|
|
systems: {
|
|
setting: {
|
|
expressInterface: {
|
|
async list(payload) {
|
|
calls.list.push(payload);
|
|
payload.touched = true;
|
|
return {
|
|
code: 200,
|
|
data: {
|
|
list: [
|
|
{
|
|
id: 1,
|
|
name: '顺丰接口',
|
|
express: {
|
|
code: 'SF',
|
|
name: '顺丰',
|
|
},
|
|
express_platform: {
|
|
id: 11,
|
|
interface_url: 'https://api.example.com/express/sf',
|
|
platform_code: 'TB',
|
|
},
|
|
},
|
|
],
|
|
},
|
|
};
|
|
},
|
|
async listByWarehousePlatformCode(payload) {
|
|
calls.listByWarehousePlatformCode.push(payload);
|
|
payload.touched = true;
|
|
return {
|
|
code: 200,
|
|
data: {
|
|
id: 11,
|
|
warehouse_id: 100000,
|
|
platform_code: 'TB',
|
|
express_code: 'SF',
|
|
express: {
|
|
code: 'SF',
|
|
name: '顺丰',
|
|
},
|
|
},
|
|
};
|
|
},
|
|
async create(payload) {
|
|
calls.create.push(payload);
|
|
payload.touched = true;
|
|
return {
|
|
code: 200,
|
|
data: {
|
|
id: 99,
|
|
},
|
|
};
|
|
},
|
|
async update(payload) {
|
|
calls.update.push(payload);
|
|
payload.touched = true;
|
|
return {
|
|
code: 200,
|
|
data: {
|
|
id: 11,
|
|
},
|
|
};
|
|
},
|
|
async delete(payload) {
|
|
calls.delete.push(payload);
|
|
payload.touched = true;
|
|
return {
|
|
code: 200,
|
|
data: {
|
|
id: 11,
|
|
},
|
|
};
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
return {
|
|
ctx,
|
|
calls,
|
|
};
|
|
}
|
|
|
|
describe('test/app/service/systemMethods/setting/expressInterface.test.js', () => {
|
|
it('should copy payload before calling list', async () => {
|
|
const { ctx, calls } = createContext();
|
|
const service = new ExpressInterfaceMethodService(ctx);
|
|
service.prepareWhereContext = function prepareWhereContext(data) {
|
|
calls.prepareWhereContext += 1;
|
|
data.where = {
|
|
quickWhere: '接口',
|
|
};
|
|
return data;
|
|
};
|
|
|
|
const payload = {
|
|
page: 1,
|
|
page_size: 20,
|
|
where: {
|
|
quickWhere: '接口',
|
|
},
|
|
extra: {
|
|
keep: true,
|
|
},
|
|
};
|
|
|
|
const result = await service.list(payload);
|
|
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(result.message, 'ok');
|
|
assert.equal(calls.prepareWhereContext, 1);
|
|
assert.equal(calls.list.length, 1);
|
|
assert.notStrictEqual(calls.list[0], payload);
|
|
assert.equal(payload.touched, undefined);
|
|
assert.deepEqual(calls.list[0].where, { quickWhere: '接口' });
|
|
assert.deepEqual(calls.list[0].pagination, {
|
|
page: 1,
|
|
pageSize: 20,
|
|
});
|
|
assert.equal(result.data.list[0].express.name, '顺丰');
|
|
assert.equal(result.data.list[0].express_platform.interface_url, 'https://api.example.com/express/sf');
|
|
});
|
|
|
|
it('should pass payload through to listByWarehousePlatformCode', async () => {
|
|
const { ctx, calls } = createContext();
|
|
const service = new ExpressInterfaceMethodService(ctx);
|
|
|
|
const payload = {
|
|
warehouse_id: 100000,
|
|
platform_code: 'tb',
|
|
};
|
|
|
|
const result = await service.listByWarehousePlatformCode(payload);
|
|
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(result.message, 'ok');
|
|
assert.equal(calls.listByWarehousePlatformCode.length, 1);
|
|
assert.strictEqual(calls.listByWarehousePlatformCode[0], payload);
|
|
assert.equal(payload.touched, true);
|
|
assert.equal(result.data.express.name, '顺丰');
|
|
});
|
|
|
|
it('should forward normalized create update and delete payloads', async () => {
|
|
const { ctx, calls } = createContext();
|
|
const service = new ExpressInterfaceMethodService(ctx);
|
|
|
|
const createResult = await service.create({
|
|
name: '顺丰接口',
|
|
express_code: 'SF',
|
|
platform_code: 'TB',
|
|
templateId: 5566,
|
|
brandNo: 'BRAND-CREATE',
|
|
providerType: 'KDNIAO',
|
|
sysExpressCode: 'SF',
|
|
weightType: 2,
|
|
address: '上海市浦东新区',
|
|
townName: '张江镇',
|
|
isPickup: 1,
|
|
shipperNo: 'SHIP-001',
|
|
templateUrl: 'https://example.com/template/create',
|
|
extData: {
|
|
mode: 'A4',
|
|
},
|
|
extra: {
|
|
keep: true,
|
|
},
|
|
});
|
|
const updateResult = await service.update({
|
|
id: 11,
|
|
name: '顺丰接口-更新',
|
|
token: 'token-2',
|
|
templateId: '7788',
|
|
platformShopId: '8899',
|
|
brandno: 'BRAND-UPDATE',
|
|
providerType: 'CAINIAO',
|
|
sys_express_code: 'SF',
|
|
weight_type: 3,
|
|
address: '深圳市南山区',
|
|
town_name: '粤海街道',
|
|
ispickup: 0,
|
|
shipperno: 'SHIP-002',
|
|
template_url: 'https://example.com/template/update',
|
|
ext_data: {
|
|
mode: 'thermal',
|
|
},
|
|
});
|
|
const deleteResult = await service.delete({
|
|
id: 11,
|
|
});
|
|
|
|
assert.equal(createResult.flag, 'success');
|
|
assert.equal(updateResult.flag, 'success');
|
|
assert.equal(deleteResult.flag, 'success');
|
|
assert.equal(calls.create.length, 1);
|
|
assert.equal(calls.update.length, 1);
|
|
assert.equal(calls.delete.length, 1);
|
|
assert.deepEqual(calls.create[0], {
|
|
name: '顺丰接口',
|
|
express_code: 'SF',
|
|
platform_code: 'TB',
|
|
template_id: 5566,
|
|
brandno: 'BRAND-CREATE',
|
|
providertype: 'KDNIAO',
|
|
sys_express_code: 'SF',
|
|
weight_type: 2,
|
|
address: '上海市浦东新区',
|
|
town_name: '张江镇',
|
|
ispickup: 1,
|
|
shipperno: 'SHIP-001',
|
|
template_url: 'https://example.com/template/create',
|
|
ext_data: {
|
|
mode: 'A4',
|
|
},
|
|
extra: {
|
|
keep: true,
|
|
},
|
|
touched: true,
|
|
});
|
|
assert.deepEqual(calls.update[0], {
|
|
id: 11,
|
|
name: '顺丰接口-更新',
|
|
token: 'token-2',
|
|
template_id: '7788',
|
|
platform_shop_id: '8899',
|
|
brandno: 'BRAND-UPDATE',
|
|
providertype: 'CAINIAO',
|
|
sys_express_code: 'SF',
|
|
weight_type: 3,
|
|
address: '深圳市南山区',
|
|
town_name: '粤海街道',
|
|
ispickup: 0,
|
|
shipperno: 'SHIP-002',
|
|
template_url: 'https://example.com/template/update',
|
|
ext_data: {
|
|
mode: 'thermal',
|
|
},
|
|
touched: true,
|
|
});
|
|
assert.deepEqual(calls.delete[0], {
|
|
id: 11,
|
|
touched: true,
|
|
});
|
|
});
|
|
});
|