WMS_MAIN/test/app/service/openapi.test.js
2026-04-27 16:09:26 +08:00

301 lines
7.4 KiB
JavaScript

'use strict';
const assert = require('node:assert/strict');
const OpenapiService = require('../../../app/service/openapi');
function createService({
query = {},
rawBody,
body,
apiSignService,
openapiMethods,
serviceOverrides,
} = {}) {
const defaultOpenapiMethods = {
index: {
async ping(payload = {}) {
return {
pong: true,
method: payload.query && payload.query.method || '',
};
},
},
ping: {
async echo(payload = {}) {
return {
pong: true,
method: payload.query && payload.query.method || '',
body: payload.body || {},
};
},
},
};
const baseService = {
systems: {
apiSign: apiSignService || {
async getApiAppSecretByAppKey(appKey) {
return {
id: 1,
appKey,
selfAppSecret: 'secret-1',
};
},
async verifyRequestSignByAppKey() {
return {
ok: true,
message: '验签通过',
};
},
},
},
openapiMethods: openapiMethods || defaultOpenapiMethods,
};
const ctx = {
query,
request: {
rawBody,
body,
},
req: {
on() {},
},
service: serviceOverrides || baseService,
};
const service = Object.create(OpenapiService.prototype);
service.ctx = ctx;
service.app = {};
return service;
}
describe('test/app/service/openapi.test.js', () => {
it('缺少 method/v/timestamp 时应返回参数错误', async () => {
const service = createService({
query: {
method: 'order.create',
v: '1.0',
},
rawBody: JSON.stringify({ orderNo: 'X-1' }),
});
const result = await service.processRequest();
assert.equal(result.ok, false);
assert.equal(result.httpStatus, 400);
assert.equal(result.code, 400);
assert.match(result.message, /method\/v\/timestamp/);
});
it('缺少 app_key 时应返回参数错误', async () => {
const service = createService({
query: {
method: 'order.create',
v: '1.0',
timestamp: '1711111111',
},
rawBody: JSON.stringify({ orderNo: 'X-2' }),
});
const result = await service.processRequest();
assert.equal(result.ok, false);
assert.equal(result.httpStatus, 400);
assert.match(result.message, /app_key/);
});
it('签名错误时应返回 401', async () => {
const service = createService({
query: {
method: 'order.create',
v: '1.0',
timestamp: '1711111111',
app_key: 'app-key-1',
sign: 'bad-sign',
},
rawBody: JSON.stringify({ orderNo: 'X-3' }),
apiSignService: {
async getApiAppSecretByAppKey(appKey) {
return {
id: 1,
appKey,
selfAppSecret: 'secret-1',
};
},
async verifyRequestSignByAppKey() {
return {
ok: false,
message: '签名不一致',
};
},
},
});
const result = await service.processRequest();
assert.equal(result.ok, false);
assert.equal(result.httpStatus, 401);
assert.equal(result.code, 401);
assert.match(result.message, /签名校验失败/);
});
it('签名正确时应返回成功结构且不依赖 platform 请求头', async () => {
const service = createService({
query: {
method: 'ping.echo',
v: '1.0',
timestamp: '1711111111',
app_key: 'app-key-1',
sign: 'ok-sign',
},
rawBody: JSON.stringify({ orderNo: 'X-4' }),
});
const result = await service.processRequest();
assert.equal(result.ok, true);
assert.equal(result.httpStatus, 200);
assert.equal(result.code, 1);
assert.equal(result.data.pong, true);
assert.equal(result.data.method, 'ping.echo');
assert.deepEqual(result.data.body, { orderNo: 'X-4' });
});
it('openapi method 命中新目录方法时应返回方法结果', async () => {
let called = 0;
const service = createService({
query: {
method: 'ping.echo',
v: '1.0',
timestamp: '1711111111',
app_key: 'app-key-1',
sign: 'ok-sign',
},
rawBody: JSON.stringify({ text: 'hello' }),
openapiMethods: {
ping: {
async echo(payload = {}) {
called += 1;
return {
ok: true,
payload,
};
},
},
},
});
const result = await service.processRequest();
assert.equal(called, 1);
assert.equal(result.ok, true);
assert.equal(result.httpStatus, 200);
assert.equal(result.code, 1);
assert.equal(result.data.ok, true);
assert.equal(result.data.payload.query.method, 'ping.echo');
assert.deepEqual(result.data.payload.body, { text: 'hello' });
});
it('openapi method= index.ping 时应命中 openapiMethods.index.ping', async () => {
const service = createService({
query: {
method: 'index.ping',
v: '1.0',
timestamp: '1711111111',
app_key: 'app-key-1',
sign: 'ok-sign',
},
rawBody: JSON.stringify({ any: 'value' }),
});
const result = await service.processRequest();
assert.equal(result.ok, true);
assert.equal(result.httpStatus, 200);
assert.equal(result.code, 1);
assert.equal(result.message, 'ok');
assert.equal(result.data.pong, true);
assert.equal(result.data.method, 'index.ping');
});
it('openapi method 不存在时应返回可读错误且非 system 风格', async () => {
const service = createService({
query: {
method: 'ping.notFound',
v: '1.0',
timestamp: '1711111111',
app_key: 'app-key-1',
sign: 'ok-sign',
},
rawBody: JSON.stringify({ text: 'hello' }),
});
const result = await service.processRequest();
assert.equal(result.ok, false);
assert.equal(result.httpStatus, 400);
assert.equal(result.code, 400);
assert.match(result.message, /openapi method 不存在/);
assert.doesNotMatch(result.message, /服务 .* 不存在|方法 .* 不存在/);
});
it('openapi 分发不应访问 systemMethods', async () => {
let accessCount = 0;
const serviceProxy = {
systems: {
apiSign: {
async getApiAppSecretByAppKey(appKey) {
return {
id: 1,
appKey,
selfAppSecret: 'secret-1',
};
},
async verifyRequestSignByAppKey() {
return {
ok: true,
message: '验签通过',
};
},
},
},
openapiMethods: {
ping: {
async echo() {
return {
ok: true,
};
},
},
},
};
Object.defineProperty(serviceProxy, 'systemMethods', {
configurable: true,
enumerable: true,
get() {
accessCount += 1;
throw new Error('不应访问 systemMethods');
},
});
const service = createService({
query: {
method: 'ping.echo',
v: '1.0',
timestamp: '1711111111',
app_key: 'app-key-1',
sign: 'ok-sign',
},
rawBody: JSON.stringify({ text: 'hello' }),
serviceOverrides: serviceProxy,
});
const result = await service.processRequest();
assert.equal(result.ok, true);
assert.equal(accessCount, 0);
});
});