import { ComplianceGdprService, maskIpAddress, anonymizePiiData, GdprUserDataBundle, } from '../../../src/modules/crm/service/compliance'; describe('Point 53: 海外合规 GDPR / CCPA 访客数据匿名化脱敏与被遗忘权导出', () => { let service: ComplianceGdprService; beforeEach(() => { service = new ComplianceGdprService(); }); describe('1. maskIpAddress (IP 地址合规脱敏掩码规范)', () => { it('IPv4 地址末尾一段应被严格掩码为 xxx', () => { expect(maskIpAddress('192.168.1.100')).toBe('192.168.1.xxx'); expect(maskIpAddress('8.8.8.8')).toBe('8.8.8.xxx'); }); it('IPv6 地址后半段应被规范掩码', () => { expect(maskIpAddress('2001:0db8:85a3:0000:0000:8a2e:0370:7334')).toBe( '2001:0db8:85a3:xxxx:xxxx:xxxx:xxxx:xxxx' ); }); it('非法或空 IP 应安全返回未知掩码', () => { expect(maskIpAddress('')).toBe('unknown'); expect(maskIpAddress(undefined)).toBe('unknown'); }); }); describe('2. anonymizePiiData (可识别个人隐私数据匿名化清洗)', () => { it('应将姓名、手机号、邮箱与备注彻底清除并标记为已脱敏', () => { const rawLead = { id: 101, name: 'Hans Zimmer', email: 'hans@berlin-music.de', phone: '+49 170 1234567', company: 'Berlin Music AG', notes: 'Interested in 500 VIP NFC cards', }; const cleaned = anonymizePiiData(rawLead); expect(cleaned.id).toBe(101); expect(cleaned.name).toBe('[GDPR_DELETED]'); expect(cleaned.email).toBe('deleted-101@privacy.anonymized'); expect(cleaned.phone).toBe(''); expect(cleaned.notes).toBe(''); expect(cleaned.company).toBe('Berlin Music AG'); // 保留无识别性企业属性用于统计 }); }); describe('3. ComplianceGdprService 数据导出包与被遗忘权销毁', () => { it('数据主体请求导出时应打包符合 GDPR Article 20 的 JSON 便携数据包', async () => { const bundle = await service.exportPersonalDataBundle('visitor-de-001', { profile: { name: 'Klaus Mueller', email: 'klaus@munich.de' }, visitLogs: [{ page: '/card/alex', staySeconds: 45 }], }); expect(bundle.subjectId).toBe('visitor-de-001'); expect(bundle.exportedAt).toBeDefined(); expect(bundle.complianceStandard).toBe('GDPR_ARTICLE_20'); expect(bundle.dataPayload.profile.name).toBe('Klaus Mueller'); }); it('执行被遗忘权销毁应返回销毁证书与合规哈希凭据', async () => { const receipt = await service.executeRightToBeForgotten('visitor-de-001'); expect(receipt.subjectId).toBe('visitor-de-001'); expect(receipt.action).toBe('PURGED_AND_ANONYMIZED'); expect(receipt.certificateHash).toHaveLength(64); }); }); });