182 lines
5.0 KiB
JavaScript
182 lines
5.0 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const ApiSignService = require('../../../../app/service/systems/api_sign');
|
|
const apiSign = require('../../../../app/lib/api_sign');
|
|
|
|
function createHarness(findOneImpl, appOverrides = {}) {
|
|
const app = {
|
|
getCacheAsync: async () => null,
|
|
setCacheAsync: async () => null,
|
|
...appOverrides,
|
|
};
|
|
const ctx = {
|
|
app,
|
|
logger: {
|
|
warn() {},
|
|
},
|
|
model: {
|
|
ApiApp: {
|
|
async findOne(query) {
|
|
return await findOneImpl(query);
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
const service = Object.create(ApiSignService.prototype);
|
|
service.ctx = ctx;
|
|
service.app = app;
|
|
return service;
|
|
}
|
|
|
|
describe('test/app/service/systems/api_sign.test.js', () => {
|
|
it('应按 app_key 查询 secret 并完成验签', async () => {
|
|
const service = createHarness(async query => {
|
|
assert.deepEqual(query.attributes, [ 'id', 'self_app_key', 'self_app_secret', 'is_enabled', 'interface_type_id' ]);
|
|
assert.equal(query.where.self_app_key, 'app-key-1');
|
|
return {
|
|
id: 10,
|
|
self_app_key: 'app-key-1',
|
|
self_app_secret: 'secret-1',
|
|
interface_type_id: 2,
|
|
is_enabled: 1,
|
|
};
|
|
});
|
|
|
|
const signResult = apiSign.signRequest({
|
|
query: {
|
|
app_key: 'app-key-1',
|
|
method: 'order.create',
|
|
timestamp: '1711111111',
|
|
},
|
|
body: {
|
|
orderNo: 'X-1',
|
|
},
|
|
selfAppSecret: 'secret-1',
|
|
});
|
|
|
|
const verifyResult = await service.verifyRequestSignByAppKey({
|
|
appKey: 'app-key-1',
|
|
query: {
|
|
method: 'order.create',
|
|
timestamp: '1711111111',
|
|
app_key: 'app-key-1',
|
|
},
|
|
body: {
|
|
orderNo: 'X-1',
|
|
},
|
|
providedSign: signResult.sign,
|
|
});
|
|
|
|
assert.equal(verifyResult.ok, true);
|
|
assert.equal(verifyResult.code, 'OK');
|
|
assert.equal(verifyResult.appId, 10);
|
|
});
|
|
|
|
it('app_key 不存在时应返回可读错误', async () => {
|
|
const service = createHarness(async () => null);
|
|
|
|
const verifyResult = await service.verifyRequestSignByAppKey({
|
|
appKey: 'missing-app-key',
|
|
query: {
|
|
app_key: 'missing-app-key',
|
|
},
|
|
body: {},
|
|
providedSign: 'abc',
|
|
});
|
|
|
|
assert.equal(verifyResult.ok, false);
|
|
assert.equal(verifyResult.code, 'APP_CONFIG_INVALID');
|
|
assert.equal(verifyResult.message, '未找到对应的 API 应用配置');
|
|
});
|
|
|
|
it('缓存未命中时应查库并写入缓存', async () => {
|
|
let queryCount = 0;
|
|
const cacheStore = new Map();
|
|
const setCacheCalls = [];
|
|
const service = createHarness(async () => {
|
|
queryCount += 1;
|
|
return {
|
|
id: 12,
|
|
self_app_key: 'app-key-cache-miss',
|
|
self_app_secret: 'secret-cache-miss',
|
|
interface_type_id: 2,
|
|
is_enabled: 1,
|
|
};
|
|
}, {
|
|
async getCacheAsync(cacheKey) {
|
|
return cacheStore.get(cacheKey) || null;
|
|
},
|
|
async setCacheAsync(cacheKey, value, ttl) {
|
|
setCacheCalls.push({ cacheKey, value, ttl });
|
|
cacheStore.set(cacheKey, value);
|
|
},
|
|
});
|
|
|
|
const result = await service.getApiAppSecretByAppKey('app-key-cache-miss');
|
|
|
|
assert.equal(queryCount, 1);
|
|
assert.equal(result.appKey, 'app-key-cache-miss');
|
|
assert.equal(result.selfAppSecret, 'secret-cache-miss');
|
|
assert.equal(setCacheCalls.length, 1);
|
|
assert.equal(setCacheCalls[0].cacheKey, 'openapi:api-app:app-key-cache-miss');
|
|
assert.equal(setCacheCalls[0].ttl, 300);
|
|
});
|
|
|
|
it('缓存命中时应直接返回且不查库', async () => {
|
|
let queryCount = 0;
|
|
const service = createHarness(async () => {
|
|
queryCount += 1;
|
|
return null;
|
|
}, {
|
|
async getCacheAsync(cacheKey) {
|
|
if (cacheKey === 'openapi:api-app:app-key-cache-hit') {
|
|
return {
|
|
id: 20,
|
|
appKey: 'app-key-cache-hit',
|
|
selfAppSecret: 'secret-cache-hit',
|
|
interface_type_id: 2,
|
|
};
|
|
}
|
|
return null;
|
|
},
|
|
async setCacheAsync() {
|
|
throw new Error('缓存命中路径不应写缓存');
|
|
},
|
|
});
|
|
|
|
const result = await service.getApiAppSecretByAppKey('app-key-cache-hit');
|
|
|
|
assert.equal(queryCount, 0);
|
|
assert.equal(result.id, 20);
|
|
assert.equal(result.appKey, 'app-key-cache-hit');
|
|
assert.equal(result.selfAppSecret, 'secret-cache-hit');
|
|
});
|
|
|
|
it('interface_type_id 不等于 2 时应视为 app_key 无效或不可用', async () => {
|
|
const service = createHarness(async () => {
|
|
return {
|
|
id: 21,
|
|
self_app_key: 'app-key-invalid-type',
|
|
self_app_secret: 'secret-invalid-type',
|
|
interface_type_id: 1,
|
|
is_enabled: 1,
|
|
};
|
|
});
|
|
|
|
const verifyResult = await service.verifyRequestSignByAppKey({
|
|
appKey: 'app-key-invalid-type',
|
|
query: {
|
|
app_key: 'app-key-invalid-type',
|
|
},
|
|
body: {},
|
|
providedSign: 'abc',
|
|
});
|
|
|
|
assert.equal(verifyResult.ok, false);
|
|
assert.equal(verifyResult.code, 'APP_CONFIG_INVALID');
|
|
assert.equal(verifyResult.message, 'app_key 无效或不可用');
|
|
});
|
|
});
|