刘光辉
4 小时以前 cf11e63b659afcc9792ab3e395f1b45d844431a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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();
  });
});