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

327 lines
9.8 KiB
JavaScript

'use strict';
const { strict: assert } = require('node:assert');
const { app, mm } = require('egg-mock/bootstrap');
const ApiSignService = require('../../../app/service/systems/api_sign');
const HomeController = require('../../../app/controller/home');
const apiSign = require('../../../app/lib/api_sign');
describe('test/app/controller/openapi.test.js', () => {
it('openapi 正文非 JSON 时应返回统一失败结构', async () => {
const res = await app.httpRequest()
.post('/openapi?method=order.create&v=1.0&timestamp=1711111111&app_key=app-key-1&sign=abc')
.send('plain-text-body')
.expect('Content-Type', /json/)
.expect(400);
assert.equal(res.body.flag, 'failure');
assert.equal(res.body.code, 400);
assert.match(String(res.body.message || ''), /json数据解析错误/);
assert.equal(typeof res.body.timestamp, 'number');
});
it('openapi 请求缺少 appkey 时应返回失败', async () => {
const res = await app.httpRequest()
.post('/openapi?method=order.create&v=1.0&timestamp=1711111111&sign=abc')
.send({ orderNo: 'X-1' })
.expect('Content-Type', /json/)
.expect(400);
assert.equal(res.body.flag, 'failure');
assert.equal(res.body.code, 400);
assert.match(String(res.body.message || ''), /app_key/);
});
it('appkey 不存在或无效时应返回失败', async () => {
try {
mm(ApiSignService.prototype, 'getApiAppSecretByAppKey', async () => {
throw new Error('未找到对应的 API 应用配置');
});
const res = await app.httpRequest()
.post('/openapi?method=order.create&v=1.0&timestamp=1711111111&app_key=missing-key&sign=abc')
.send({ orderNo: 'X-1' })
.expect('Content-Type', /json/)
.expect(401);
assert.equal(res.body.flag, 'failure');
assert.equal(res.body.code, 401);
assert.match(String(res.body.message || ''), /app_key 校验失败/);
} finally {
mm.restore();
}
});
it('签名错误时应返回失败', async () => {
try {
mm(ApiSignService.prototype, 'getApiAppSecretByAppKey', async appKey => {
return {
id: 10,
appKey,
interface_type_id: 2,
selfAppSecret: 'secret-1',
};
});
const res = await app.httpRequest()
.post('/openapi?method=order.create&v=1.0&timestamp=1711111111&app_key=app-key-1&sign=wrong-sign')
.send({ orderNo: 'X-1' })
.expect('Content-Type', /json/)
.expect(401);
assert.equal(res.body.flag, 'failure');
assert.equal(res.body.code, 401);
assert.match(String(res.body.message || ''), /签名校验失败/);
} finally {
mm.restore();
}
});
it('openapi 缺少 platform 请求头时仍应返回签名校验失败而非 platform 错误', async () => {
try {
mm(ApiSignService.prototype, 'getApiAppSecretByAppKey', async appKey => {
return {
id: 13,
appKey,
interface_type_id: 2,
selfAppSecret: 'secret-1',
};
});
const res = await app.httpRequest()
.post('/openapi?method=order.create&v=1.0&timestamp=1711111111&app_key=app-key-1&sign=wrong-sign')
.send({ orderNo: 'X-1' })
.expect('Content-Type', /json/)
.expect(401);
assert.equal(res.body.flag, 'failure');
assert.equal(res.body.code, 401);
assert.match(String(res.body.message || ''), /签名校验失败/);
assert.doesNotMatch(String(res.body.message || ''), /缺少请求头 platform/);
} finally {
mm.restore();
}
});
it('签名正确时应返回成功', async () => {
const query = {
method: 'ping.echo',
v: '1.0',
timestamp: '1711111111',
app_key: 'app-key-1',
};
const body = {
orderNo: 'X-1',
};
const signed = apiSign.signRequest({
query,
body,
selfAppSecret: 'secret-1',
});
try {
mm(ApiSignService.prototype, 'getApiAppSecretByAppKey', async appKey => {
return {
id: 10,
appKey,
interface_type_id: 2,
selfAppSecret: 'secret-1',
};
});
const res = await app.httpRequest()
.post(`/openapi?method=${query.method}&v=${query.v}&timestamp=${query.timestamp}&app_key=${query.app_key}&sign=${signed.sign}`)
.send(body)
.expect('Content-Type', /json/)
.expect(200);
assert.equal(res.body.flag, 'success');
assert.equal(res.body.code, 1);
assert.equal(res.body.message, 'ok');
assert.deepEqual(res.body.data, {
pong: true,
method: 'ping.echo',
body,
});
assert.equal(typeof res.body.timestamp, 'number');
} finally {
mm.restore();
}
});
it('调用 index.ping 成功时应返回 code/message/data', async () => {
const query = {
method: 'index.ping',
v: '1.0',
timestamp: '1711111111',
app_key: 'app-key-1',
};
const body = {
hello: 'world',
};
const signed = apiSign.signRequest({
query,
body,
selfAppSecret: 'secret-1',
});
try {
mm(ApiSignService.prototype, 'getApiAppSecretByAppKey', async appKey => {
return {
id: 15,
appKey,
interface_type_id: 2,
selfAppSecret: 'secret-1',
};
});
const res = await app.httpRequest()
.post(`/openapi?method=${query.method}&v=${query.v}&timestamp=${query.timestamp}&app_key=${query.app_key}&sign=${signed.sign}`)
.send(body)
.expect('Content-Type', /json/)
.expect(200);
assert.equal(res.body.flag, 'success');
assert.equal(res.body.code, 1);
assert.equal(res.body.message, 'ok');
assert.ok(Object.prototype.hasOwnProperty.call(res.body, 'data'));
assert.equal(res.body.data.pong, true);
assert.equal(res.body.data.method, 'index.ping');
} finally {
mm.restore();
}
});
it('无特定请求头时签名正确也应返回成功', async () => {
const query = {
method: 'ping.echo',
v: '1.0',
timestamp: '1711111111',
app_key: 'app-key-1',
};
const body = {
orderNo: 'X-2',
};
const signed = apiSign.signRequest({
query,
body,
selfAppSecret: 'secret-1',
});
try {
mm(ApiSignService.prototype, 'getApiAppSecretByAppKey', async appKey => {
return {
id: 11,
appKey,
interface_type_id: 2,
selfAppSecret: 'secret-1',
};
});
const res = await app.httpRequest()
.post(`/openapi?method=${query.method}&v=${query.v}&timestamp=${query.timestamp}&app_key=${query.app_key}&sign=${signed.sign}`)
.send(JSON.stringify(body))
.expect('Content-Type', /json/)
.expect(200);
assert.equal(res.body.flag, 'success');
assert.equal(res.body.code, 1);
assert.equal(res.body.message, 'ok');
assert.equal(res.body.data.pong, true);
assert.equal(res.body.data.method, query.method);
} finally {
mm.restore();
}
});
it('openapi method 不存在时应返回可读错误且非 system 风格', async () => {
const query = {
method: 'ping.notFound',
v: '1.0',
timestamp: '1711111111',
app_key: 'app-key-1',
};
const body = {
orderNo: 'X-404',
};
const signed = apiSign.signRequest({
query,
body,
selfAppSecret: 'secret-1',
});
try {
mm(ApiSignService.prototype, 'getApiAppSecretByAppKey', async appKey => {
return {
id: 14,
appKey,
interface_type_id: 2,
selfAppSecret: 'secret-1',
};
});
const res = await app.httpRequest()
.post(`/openapi?method=${query.method}&v=${query.v}&timestamp=${query.timestamp}&app_key=${query.app_key}&sign=${signed.sign}`)
.send(body)
.expect('Content-Type', /json/)
.expect(400);
assert.equal(res.body.flag, 'failure');
assert.equal(res.body.code, 400);
assert.match(String(res.body.message || ''), /openapi method 不存在/);
assert.doesNotMatch(String(res.body.message || ''), /服务 .* 不存在|方法 .* 不存在/);
} finally {
mm.restore();
}
});
it('openapi 在缺少 platform 请求头时应继续走自身校验链路', async () => {
const res = await app.httpRequest()
.post('/openapi?method=order.create&v=1.0&timestamp=1711111111&sign=abc')
.send({ orderNo: 'X-3' })
.expect('Content-Type', /json/)
.expect(400);
assert.equal(res.body.flag, 'failure');
assert.equal(res.body.code, 400);
assert.match(String(res.body.message || ''), /app_key/);
assert.doesNotMatch(String(res.body.message || ''), /缺少请求头 platform/);
});
it('openapi 路由不应命中 home.system', async () => {
try {
mm(HomeController.prototype, 'system', async function() {
this.ctx.status = 500;
this.ctx.body = {
flag: 'failure',
message: '误命中 home.system',
};
});
const res = await app.httpRequest()
.post('/openapi?method=order.create&v=1.0&timestamp=1711111111&sign=abc')
.send({ orderNo: 'X-4' })
.expect('Content-Type', /json/)
.expect(400);
assert.equal(res.body.flag, 'failure');
assert.match(String(res.body.message || ''), /app_key/);
assert.doesNotMatch(String(res.body.message || ''), /误命中 home.system/);
} finally {
mm.restore();
}
});
it('system 缺少 platform 请求头时仍应返回原有错误', async () => {
const res = await app.httpRequest()
.post('/system?method=user.info.web&v=1.0&timestamp=1711111111')
.send({})
.expect('Content-Type', /json/)
.expect(400);
assert.equal(res.body.flag, 'failure');
assert.equal(res.body.code, 400);
assert.equal(res.body.message, '缺少请求头 platform');
});
});