import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
import { applyActiveAuditBizSign, AUDIT_BIZ_SIGN_HEADER, getActiveAuditBizSign, runWithAuditBizSignEvent, shouldAttachAuditBizSign } from './auditBizSignEvent';
|
|
afterEach(() => {
|
vi.useRealTimers();
|
});
|
|
describe('audit biz sign event scope', () => {
|
it('keeps one signature active for every write request in the submitted promise', async () => {
|
const headersList: Record<string, string>[] = [];
|
|
await runWithAuditBizSignEvent(' sign-1 ', async () => {
|
for (const method of ['post', 'put', 'patch', 'delete']) {
|
const headers = {};
|
applyActiveAuditBizSign(headers, '/api/dms/resources', method);
|
headersList.push(headers);
|
await Promise.resolve();
|
}
|
expect(getActiveAuditBizSign()).toBe('sign-1');
|
});
|
|
expect(headersList).toEqual(Array.from({ length: 4 }, () => ({ [AUDIT_BIZ_SIGN_HEADER]: 'sign-1' })));
|
expect(getActiveAuditBizSign()).toBe('');
|
});
|
|
it('does not attach to reads, excluded endpoints, reports, or external APIs', async () => {
|
await runWithAuditBizSignEvent('sign-1', () => {
|
expect(shouldAttachAuditBizSign('/api/dms/resources', 'get')).toBe(false);
|
expect(shouldAttachAuditBizSign('/api/lims/lims/biz/sign/verify', 'post')).toBe(false);
|
expect(shouldAttachAuditBizSign('/api/oauth/Login', 'post')).toBe(false);
|
expect(shouldAttachAuditBizSign('/api/audit/events/page', 'post')).toBe(false);
|
expect(shouldAttachAuditBizSign('/report/render', 'post')).toBe(false);
|
expect(shouldAttachAuditBizSign('https://external.example/api/write', 'post')).toBe(false);
|
});
|
});
|
|
it('recognizes business API paths after the development proxy prefix is applied', async () => {
|
await runWithAuditBizSignEvent('sign-1', () => {
|
const headers = {};
|
applyActiveAuditBizSign(headers, '/dev/api/lims/custom/submit', 'post', '/dev');
|
expect(headers).toEqual({ [AUDIT_BIZ_SIGN_HEADER]: 'sign-1' });
|
expect(shouldAttachAuditBizSign('/dev/api/oauth/Login', 'post', '/dev')).toBe(false);
|
});
|
});
|
|
it('preserves an explicitly supplied audit header', async () => {
|
await runWithAuditBizSignEvent('automatic-sign', () => {
|
const headers = { 'x-audit-biz-sign': 'explicit-sign' };
|
applyActiveAuditBizSign(headers, '/api/dms/resources', 'post');
|
expect(headers).toEqual({ 'x-audit-biz-sign': 'explicit-sign' });
|
});
|
});
|
|
it('serializes overlapping signed submit events', async () => {
|
let releaseFirst!: () => void;
|
const firstGate = new Promise<void>((resolve) => {
|
releaseFirst = resolve;
|
});
|
const observed: string[] = [];
|
|
const first = runWithAuditBizSignEvent('sign-1', async () => {
|
observed.push(getActiveAuditBizSign());
|
await firstGate;
|
observed.push(getActiveAuditBizSign());
|
});
|
const second = runWithAuditBizSignEvent('sign-2', () => {
|
observed.push(getActiveAuditBizSign());
|
});
|
|
await Promise.resolve();
|
expect(observed).toEqual(['sign-1']);
|
releaseFirst();
|
await Promise.all([first, second]);
|
expect(observed).toEqual(['sign-1', 'sign-1', 'sign-2']);
|
expect(getActiveAuditBizSign()).toBe('');
|
});
|
|
it('rejects invalid signature values before submitting the business action', async () => {
|
const submit = vi.fn();
|
await expect(runWithAuditBizSignEvent('invalid sign', submit)).rejects.toThrow('电子签名认证结果无效');
|
expect(submit).not.toHaveBeenCalled();
|
});
|
});
|