262 lines
7.6 KiB
JavaScript
262 lines
7.6 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const Sequelize = require('sequelize');
|
|
const StoreWarehouseService = require('../../../../app/service/systems/store_warehouse');
|
|
|
|
/**
|
|
* 构造 saveApiConfig 测试上下文。
|
|
*
|
|
* @param {Object} [options={}] 测试选项。
|
|
* @return {{service: StoreWarehouseService, capture: object, transaction: object}} 服务实例与埋点数据。
|
|
*/
|
|
function createSaveApiConfigHarness(options = {}) {
|
|
const interfaceType = {
|
|
id: Number(options.interfaceTypeId || 7),
|
|
interface_category: options.interfaceCategory || 'QM_WMS',
|
|
};
|
|
const relation = {
|
|
id: Number(options.relationId || 88),
|
|
warehouse_id: Number(options.warehouseId || 100000),
|
|
};
|
|
const capture = {
|
|
qimenAppFindOneArgs: [],
|
|
apiAppFindOneArgs: [],
|
|
apiAppCreatePayload: null,
|
|
apiAppUpdatePayload: null,
|
|
operationLogPayload: null,
|
|
clearedConfigs: null,
|
|
};
|
|
const transaction = {
|
|
committed: false,
|
|
rolledBack: false,
|
|
async commit() {
|
|
this.committed = true;
|
|
},
|
|
async rollback() {
|
|
this.rolledBack = true;
|
|
},
|
|
};
|
|
|
|
let persistedConfig = options.persistedConfig || null;
|
|
let existingConfig = null;
|
|
if (options.existingConfig) {
|
|
persistedConfig = {
|
|
...options.existingConfig,
|
|
interface_type: {
|
|
id: interfaceType.id,
|
|
interface_category: interfaceType.interface_category,
|
|
},
|
|
};
|
|
existingConfig = {
|
|
...options.existingConfig,
|
|
async update(payload) {
|
|
capture.apiAppUpdatePayload = payload;
|
|
persistedConfig = {
|
|
...persistedConfig,
|
|
...payload,
|
|
interface_type: {
|
|
id: interfaceType.id,
|
|
interface_category: interfaceType.interface_category,
|
|
},
|
|
};
|
|
return this;
|
|
},
|
|
};
|
|
}
|
|
|
|
const ctx = {
|
|
app: {
|
|
config: {},
|
|
Sequelize,
|
|
},
|
|
user: {
|
|
id: 10001,
|
|
employees_id: 10001,
|
|
employees_name: '测试用户',
|
|
},
|
|
model: {
|
|
transaction: async () => transaction,
|
|
QimenApp: {
|
|
async findOne(args = {}) {
|
|
capture.qimenAppFindOneArgs.push(args);
|
|
return options.qimenAppRecord || null;
|
|
},
|
|
},
|
|
ApiApp: {
|
|
async findOne(args = {}) {
|
|
capture.apiAppFindOneArgs.push(args);
|
|
if (Object.prototype.hasOwnProperty.call(args.where || {}, 'contract_id')) {
|
|
return existingConfig;
|
|
}
|
|
return options.duplicateConfig || null;
|
|
},
|
|
async create(payload) {
|
|
capture.apiAppCreatePayload = payload;
|
|
persistedConfig = {
|
|
id: Number(options.createdConfigId || 321),
|
|
...payload,
|
|
interface_type: {
|
|
id: interfaceType.id,
|
|
interface_category: interfaceType.interface_category,
|
|
},
|
|
};
|
|
return {
|
|
id: persistedConfig.id,
|
|
};
|
|
},
|
|
async findByPk(id) {
|
|
return {
|
|
id,
|
|
...persistedConfig,
|
|
};
|
|
},
|
|
},
|
|
InterfaceType: {},
|
|
StoreWarehouse: {
|
|
async findByPk(id) {
|
|
return {
|
|
id,
|
|
warehouse_id: relation.warehouse_id,
|
|
};
|
|
},
|
|
},
|
|
},
|
|
service: {
|
|
systems: {
|
|
operationLog: {
|
|
async createOperationLog(payload) {
|
|
capture.operationLogPayload = payload;
|
|
},
|
|
},
|
|
},
|
|
},
|
|
logger: {
|
|
info() {},
|
|
warn() {},
|
|
error() {},
|
|
},
|
|
};
|
|
|
|
ctx.app.model = ctx.model;
|
|
|
|
const service = new StoreWarehouseService(ctx);
|
|
service.getCurrentSubject = async () => ({ role: 'warehouse' });
|
|
service.findScopedRelation = async () => relation;
|
|
service.ensureWarehouseSubject = () => {};
|
|
service.getApiInterfaceType = async () => interfaceType;
|
|
service.getRelationIncludes = () => [];
|
|
service.serializeRelation = result => ({
|
|
id: Number(result.id || relation.id),
|
|
warehouse_id: Number(result.warehouse_id || relation.warehouse_id),
|
|
});
|
|
service.clearQimenContractCache = async configs => {
|
|
capture.clearedConfigs = configs;
|
|
};
|
|
|
|
return {
|
|
service,
|
|
capture,
|
|
transaction,
|
|
};
|
|
}
|
|
|
|
describe('test/app/service/systems/store_warehouse.saveApiConfig.test.js', () => {
|
|
it('should derive qimen_customer_id from QimenApp when request omits qimen_customer_id', async () => {
|
|
const { service, capture, transaction } = createSaveApiConfigHarness({
|
|
qimenAppRecord: {
|
|
id: 11,
|
|
app_key: 'qm-app-key-create',
|
|
customer_id: 'db_customer_create',
|
|
},
|
|
});
|
|
|
|
const result = await service.saveApiConfig({
|
|
id: 88,
|
|
interface_type_id: 7,
|
|
app_name: '奇门配置创建',
|
|
qimen_app_key: 'qm-app-key-create',
|
|
qimen_warehouse_code: 'QM_WH_CREATE',
|
|
contact_person: '测试联系人',
|
|
contact_phone: '13800138000',
|
|
email: 'create@example.com',
|
|
is_enabled: 1,
|
|
});
|
|
|
|
assert.equal(capture.qimenAppFindOneArgs.length, 1);
|
|
assert.deepEqual(capture.qimenAppFindOneArgs[0].where, {
|
|
app_key: 'qm-app-key-create',
|
|
});
|
|
assert.equal(String(capture.apiAppCreatePayload.qimen_customer_id || ''), 'db_customer_create');
|
|
assert.equal(String(result.data.api_config.qimen_customer_id || ''), 'db_customer_create');
|
|
assert.equal(transaction.committed, true);
|
|
assert.equal(transaction.rolledBack, false);
|
|
});
|
|
|
|
it('should use database customer_id instead of external qimen_customer_id on update', async () => {
|
|
const { service, capture, transaction } = createSaveApiConfigHarness({
|
|
qimenAppRecord: {
|
|
id: 12,
|
|
app_key: 'qm-app-key-update',
|
|
customer_id: 'db_customer_update',
|
|
},
|
|
existingConfig: {
|
|
id: 322,
|
|
app_name: '旧配置',
|
|
interface_type_id: 7,
|
|
contract_id: 88,
|
|
warehouse_id: 100000,
|
|
qimen_app_key: 'qm-app-key-update',
|
|
qimen_customer_id: 'legacy_customer',
|
|
qimen_warehouse_code: 'QM_WH_OLD',
|
|
api_version: '1.0',
|
|
sign_method: 'md5',
|
|
rate_limit: 1000,
|
|
timeout_seconds: 30,
|
|
is_enabled: 1,
|
|
},
|
|
});
|
|
|
|
const result = await service.saveApiConfig({
|
|
id: 88,
|
|
interface_type_id: 7,
|
|
app_name: '奇门配置更新',
|
|
qimen_app_key: 'qm-app-key-update',
|
|
qimen_customer_id: 'external_customer_should_be_ignored',
|
|
qimen_warehouse_code: 'QM_WH_NEW',
|
|
is_enabled: 0,
|
|
});
|
|
|
|
assert.equal(String(capture.apiAppUpdatePayload.qimen_customer_id || ''), 'db_customer_update');
|
|
assert.notEqual(String(capture.apiAppUpdatePayload.qimen_customer_id || ''), 'external_customer_should_be_ignored');
|
|
assert.equal(String(result.data.api_config.qimen_customer_id || ''), 'db_customer_update');
|
|
assert.equal(transaction.committed, true);
|
|
assert.equal(transaction.rolledBack, false);
|
|
});
|
|
|
|
it('should fail when qimen_app_key has no matching qimen app record', async () => {
|
|
const { service, capture, transaction } = createSaveApiConfigHarness({
|
|
qimenAppRecord: null,
|
|
});
|
|
|
|
await assert.rejects(async () => {
|
|
await service.saveApiConfig({
|
|
id: 88,
|
|
interface_type_id: 7,
|
|
app_name: '奇门配置失败',
|
|
qimen_app_key: 'qm-app-key-missing',
|
|
qimen_warehouse_code: 'QM_WH_MISSING',
|
|
is_enabled: 1,
|
|
});
|
|
}, error => {
|
|
assert.equal(Number(error.code), 400);
|
|
assert.match(String(error.message || ''), /qimen_app_key|奇门应用配置/);
|
|
return true;
|
|
});
|
|
|
|
assert.equal(capture.apiAppCreatePayload, null);
|
|
assert.equal(transaction.committed, false);
|
|
assert.equal(transaction.rolledBack, true);
|
|
});
|
|
});
|