forked from lunanting/cangku
39 lines
1.4 KiB
JavaScript
39 lines
1.4 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const crypto = require('node:crypto');
|
|
const test = require('node:test');
|
|
|
|
const {
|
|
buildYydNotificationSignature,
|
|
validateNotificationPayload,
|
|
} = require('../src/services/yydNotificationCenterClient');
|
|
|
|
test('notification center signature follows the documented canonical request', () => {
|
|
const rawBody = JSON.stringify({ title: '测试' });
|
|
const bodyHash = crypto.createHash('sha256').update(rawBody, 'utf8').digest('hex');
|
|
const canonical = `1789092000\n0123456789abcdef\nPOST\n/v1/notifications\n${bodyHash}`;
|
|
const expected = crypto.createHmac('sha256', 'project-secret').update(canonical, 'utf8').digest('hex');
|
|
|
|
assert.equal(buildYydNotificationSignature({
|
|
projectSecret: 'project-secret',
|
|
timestamp: '1789092000',
|
|
nonce: '0123456789abcdef',
|
|
rawBody,
|
|
}), expected);
|
|
});
|
|
|
|
test('notification payload requires exactly one recipient identity', () => {
|
|
const base = { idempotencyKey: 'event:1:v1', title: '到期提醒', content: '请替换' };
|
|
assert.throws(() => validateNotificationPayload(base), /必须且只能/);
|
|
assert.throws(() => validateNotificationPayload({
|
|
...base,
|
|
recipientAlias: '苏适',
|
|
recipientUserId: 'ding-user-id',
|
|
}), /必须且只能/);
|
|
assert.deepEqual(validateNotificationPayload({ ...base, recipientAlias: '苏适' }), {
|
|
...base,
|
|
recipientAlias: '苏适',
|
|
});
|
|
});
|