WMS_MAIN/test/app/lib/polo_api_client.test.js
liufumingren dc4894cb6d 1
2026-05-25 17:08:56 +08:00

68 lines
2.1 KiB
JavaScript

'use strict';
const assert = require('node:assert/strict');
const crypto = require('crypto');
const PoloApiClient = require('../../../app/lib/polo_api_client');
describe('test/app/lib/polo_api_client.test.js', () => {
it('签名应对整串转小写后再做 UTF-8 MD5', async () => {
let requestOptions = null;
const ctx = {
async curl(url, options) {
requestOptions = { url, options };
return {
data: {
code: 0,
msg: 'ok',
},
};
},
};
const client = new PoloApiClient(ctx);
client._formatDate = () => '2026-05-25 10:00:00';
await client.request({
method: 'Differ.JH.BuildeAuthorizeUrl',
bizcontent: {
appKey: 'AbC123',
callbackUrl: 'http://Example.com/CallBack',
},
config: {
interface_url: 'http://aliyun.polyapi.com/OpenAPI/do',
appkey: 'AppKeyXyZ',
app_secret: 'SeCrEt',
platid: '633',
version: '1.5',
},
});
const requestBody = new URLSearchParams(requestOptions.options.content);
const signMap = {
method: 'Differ.JH.BuildeAuthorizeUrl',
appkey: 'AppKeyXyZ',
platid: '633',
version: '1.5',
contenttype: 'json',
bizcontent: JSON.stringify({
appKey: 'AbC123',
callbackUrl: 'http://Example.com/CallBack',
}),
timestamp: '2026-05-25 10:00:00',
};
const rawSignText = `SeCrEtappkeyAppKeyXyZbizcontent${JSON.stringify({
appKey: 'AbC123',
callbackUrl: 'http://Example.com/CallBack',
})}contenttypejsonmethodDiffer.JH.BuildeAuthorizeUrlplatid633timestamp2026-05-25 10:00:00version1.5SeCrEt`;
const signText = client._getSignText(signMap, 'SeCrEt');
const expectedSignText = rawSignText.toLowerCase();
const expectedSign = crypto.createHash('md5').update(expectedSignText, 'utf8').digest('hex');
const originalCaseSign = crypto.createHash('md5').update(rawSignText, 'utf8').digest('hex');
assert.equal(signText, expectedSignText);
assert.equal(requestBody.get('sign'), expectedSign);
assert.notEqual(requestBody.get('sign'), originalCaseSign);
});
});