feat(round-13): 潜客企业域名富化/雷达CSV报表导出/商机Webhook签名分发/销售管辖权限隔离 (91套件1033测试全绿)
这个提交包含在:
@@ -0,0 +1,101 @@
|
||||
import {
|
||||
CardAnalyticsExportService,
|
||||
escapeCsvCell,
|
||||
generateCsvContent,
|
||||
RadarExportRow,
|
||||
} from '../../../src/modules/crm/service/analytics-export';
|
||||
|
||||
describe('Point 50: 访客雷达多维数据报表 CSV 导出引擎 (RFC 4180)', () => {
|
||||
let service: CardAnalyticsExportService;
|
||||
|
||||
beforeEach(() => {
|
||||
service = new CardAnalyticsExportService();
|
||||
});
|
||||
|
||||
describe('1. escapeCsvCell (RFC 4180 单元格转义规范)', () => {
|
||||
it('包含逗号、换行或双引号的单元格必须用双引号包裹且内部双引号翻倍', () => {
|
||||
expect(escapeCsvCell('Hello, World')).toBe('"Hello, World"');
|
||||
expect(escapeCsvCell('Line 1\nLine 2')).toBe('"Line 1\nLine 2"');
|
||||
expect(escapeCsvCell('She said "Hi"')).toBe('"She said ""Hi"""');
|
||||
});
|
||||
|
||||
it('普通纯文本单元格无需包裹双引号', () => {
|
||||
expect(escapeCsvCell('Arthur Pendelton')).toBe('Arthur Pendelton');
|
||||
expect(escapeCsvCell('Dubai')).toBe('Dubai');
|
||||
expect(escapeCsvCell(100)).toBe('100');
|
||||
});
|
||||
|
||||
it('空值、null 或 undefined 应返回空字符串', () => {
|
||||
expect(escapeCsvCell('')).toBe('');
|
||||
expect(escapeCsvCell(null)).toBe('');
|
||||
expect(escapeCsvCell(undefined)).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
describe('2. generateCsvContent (UTF-8 BOM 与多语言表头自适应)', () => {
|
||||
const mockRows: RadarExportRow[] = [
|
||||
{
|
||||
visitorId: 'vis-001',
|
||||
name: 'Sheikh Mansour',
|
||||
country: 'AE',
|
||||
city: 'Abu Dhabi',
|
||||
stayDurationSeconds: 154,
|
||||
radarScore: 92,
|
||||
trafficChannel: 'nfc_tap',
|
||||
visitedAt: '2026-09-20 14:30:00',
|
||||
},
|
||||
{
|
||||
visitorId: 'vis-002',
|
||||
name: 'Elena, Rostova',
|
||||
country: 'SE',
|
||||
city: 'Stockholm',
|
||||
stayDurationSeconds: 65,
|
||||
radarScore: 68,
|
||||
trafficChannel: 'whatsapp_chat',
|
||||
visitedAt: '2026-09-20 15:10:00',
|
||||
},
|
||||
];
|
||||
|
||||
it('导出的 CSV 必须以 UTF-8 BOM (\\uFEFF) 开头以防止 Excel 乱码', () => {
|
||||
const csv = generateCsvContent(mockRows, 'en');
|
||||
expect(csv.startsWith('\uFEFF')).toBe(true);
|
||||
});
|
||||
|
||||
it('英语表头应包含准确的 RFC 4180 列名', () => {
|
||||
const csv = generateCsvContent(mockRows, 'en');
|
||||
expect(csv).toContain('Visitor ID,Name,Country,City,Stay (Seconds),Radar Score,Channel,Visited Time');
|
||||
expect(csv).toContain('"Elena, Rostova"'); // 包含逗号被正确转义
|
||||
});
|
||||
|
||||
it('阿拉伯语表头应输出规范阿语文案', () => {
|
||||
const csv = generateCsvContent(mockRows, 'ar');
|
||||
expect(csv).toContain('معرف الزائر,الاسم,الدولة');
|
||||
});
|
||||
|
||||
it('中文表头应输出清晰中文列名', () => {
|
||||
const csv = generateCsvContent(mockRows, 'zh');
|
||||
expect(csv).toContain('访客标识,姓名,国家,城市,停留时长(秒),意向评分,访问渠道,访问时间');
|
||||
});
|
||||
});
|
||||
|
||||
describe('3. CardAnalyticsExportService 报表导出元数据', () => {
|
||||
it('应生成包含文件名与行数统计的导出包', async () => {
|
||||
const result = await service.exportRadarDataset([
|
||||
{
|
||||
visitorId: 'vis-101',
|
||||
name: 'David',
|
||||
country: 'GB',
|
||||
city: 'London',
|
||||
stayDurationSeconds: 200,
|
||||
radarScore: 85,
|
||||
trafficChannel: 'linkedin_share',
|
||||
visitedAt: '2026-09-20 16:00:00',
|
||||
},
|
||||
], 'en');
|
||||
|
||||
expect(result.fileName).toMatch(/radar_export_.*\.csv/);
|
||||
expect(result.rowCount).toBe(1);
|
||||
expect(result.csvContent.length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,78 @@
|
||||
import {
|
||||
LeadCompanyEnrichmentService,
|
||||
extractDomainFromEmail,
|
||||
isPublicFreeMailbox,
|
||||
enrichCompanyProfile,
|
||||
CompanyProfileResult,
|
||||
} from '../../../src/modules/crm/service/enrichment';
|
||||
|
||||
describe('Point 49: 出海潜客企业邮箱域名解析与商业主页自动富化', () => {
|
||||
let service: LeadCompanyEnrichmentService;
|
||||
|
||||
beforeEach(() => {
|
||||
service = new LeadCompanyEnrichmentService();
|
||||
});
|
||||
|
||||
describe('1. 邮箱域名解析与公用免费邮箱过滤 (extractDomainFromEmail & isPublicFreeMailbox)', () => {
|
||||
it('应正确提取企业邮箱域名', () => {
|
||||
expect(extractDomainFromEmail('alex@siemens.com')).toBe('siemens.com');
|
||||
expect(extractDomainFromEmail('tariq@aramco.com.sa')).toBe('aramco.com.sa');
|
||||
expect(extractDomainFromEmail(' JOHN.DOE@VOLVO.SE ')).toBe('volvo.se');
|
||||
});
|
||||
|
||||
it('非法或无效邮箱应安全返回空字符串', () => {
|
||||
expect(extractDomainFromEmail('')).toBe('');
|
||||
expect(extractDomainFromEmail('invalid-email-format')).toBe('');
|
||||
expect(extractDomainFromEmail(null as any)).toBe('');
|
||||
});
|
||||
|
||||
it('应准确判定常见公用免费邮箱 (Gmail, Hotmail, 163, QQ, Yahoo 等)', () => {
|
||||
expect(isPublicFreeMailbox('gmail.com')).toBe(true);
|
||||
expect(isPublicFreeMailbox('yahoo.com')).toBe(true);
|
||||
expect(isPublicFreeMailbox('outlook.com')).toBe(true);
|
||||
expect(isPublicFreeMailbox('163.com')).toBe(true);
|
||||
expect(isPublicFreeMailbox('qq.com')).toBe(true);
|
||||
|
||||
// 企业独立域名应判定为 false
|
||||
expect(isPublicFreeMailbox('bmw.de')).toBe(false);
|
||||
expect(isPublicFreeMailbox('omnicard.ai')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('2. enrichCompanyProfile (企业画像与社交网络自动补全)', () => {
|
||||
it('针对企业域名应提取标准官网、LinkedIn 公司页与品牌展示名称', () => {
|
||||
const profile = enrichCompanyProfile('faisal@emiratessteel.ae', 'Emirates Steel Arkan');
|
||||
|
||||
expect(profile.isEnterpriseDomain).toBe(true);
|
||||
expect(profile.domain).toBe('emiratessteel.ae');
|
||||
expect(profile.websiteUrl).toBe('https://emiratessteel.ae');
|
||||
expect(profile.linkedinCompanyUrl).toBe('https://www.linkedin.com/company/emiratessteel');
|
||||
expect(profile.brandName).toBe('Emirates Steel Arkan');
|
||||
});
|
||||
|
||||
it('针对免费个人邮箱应标记非企业域名,并提取备用展示', () => {
|
||||
const profile = enrichCompanyProfile('freelancer2026@gmail.com');
|
||||
|
||||
expect(profile.isEnterpriseDomain).toBe(false);
|
||||
expect(profile.domain).toBe('gmail.com');
|
||||
expect(profile.websiteUrl).toBeUndefined();
|
||||
expect(profile.linkedinCompanyUrl).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('3. LeadCompanyEnrichmentService 综合线索富化', () => {
|
||||
it('应能为潜客线索生成完整的企业富化数据对象', async () => {
|
||||
const enriched = await service.enrichLead({
|
||||
leadId: 301,
|
||||
name: 'Carlos Mendez',
|
||||
email: 'carlos@iberia.es',
|
||||
company: 'Iberia Airlines',
|
||||
});
|
||||
|
||||
expect(enriched.leadId).toBe(301);
|
||||
expect(enriched.companyProfile.isEnterpriseDomain).toBe(true);
|
||||
expect(enriched.companyProfile.domain).toBe('iberia.es');
|
||||
expect(enriched.suggestedIndustry).toBeDefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,97 @@
|
||||
import {
|
||||
LeadWebhookDispatcher,
|
||||
computeWebhookSignature,
|
||||
verifyWebhookSignature,
|
||||
LeadWebhookPayload,
|
||||
} from '../../../src/modules/crm/service/webhook-dispatcher';
|
||||
|
||||
describe('Point 51: 出海商机实时 Webhook 触发器与 HMAC-SHA256 签名分发', () => {
|
||||
let dispatcher: LeadWebhookDispatcher;
|
||||
const mockSecret = 'whsec_8f9a2b4c6e1d7g5h3j8k9l0m';
|
||||
const mockPayload: LeadWebhookPayload = {
|
||||
event: 'lead.created',
|
||||
timestamp: Date.now(),
|
||||
tenantId: 'tenant-uae-101',
|
||||
data: {
|
||||
leadId: 501,
|
||||
name: 'Sheikh Khalid',
|
||||
company: 'Dubai Financial Group',
|
||||
score: 95,
|
||||
channel: 'nfc_tap',
|
||||
},
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
mockPayload.timestamp = Date.now();
|
||||
dispatcher = new LeadWebhookDispatcher();
|
||||
});
|
||||
|
||||
describe('1. computeWebhookSignature & verifyWebhookSignature (HMAC-SHA256 签名验签)', () => {
|
||||
it('应计算规范的 sha256 签名串', () => {
|
||||
const payloadStr = JSON.stringify(mockPayload);
|
||||
const sig = computeWebhookSignature(payloadStr, mockSecret, mockPayload.timestamp);
|
||||
|
||||
expect(sig).toHaveLength(64);
|
||||
expect(/^[0-9a-f]{64}$/i.test(sig)).toBe(true);
|
||||
|
||||
// 验签通过
|
||||
const isValid = verifyWebhookSignature(
|
||||
payloadStr,
|
||||
sig,
|
||||
mockSecret,
|
||||
mockPayload.timestamp
|
||||
);
|
||||
expect(isValid).toBe(true);
|
||||
});
|
||||
|
||||
it('篡改 Payload 任何内容时验签应失败', () => {
|
||||
const payloadStr = JSON.stringify(mockPayload);
|
||||
const sig = computeWebhookSignature(payloadStr, mockSecret, mockPayload.timestamp);
|
||||
|
||||
const tamperedStr = JSON.stringify({ ...mockPayload, tenantId: 'tampered-tenant' });
|
||||
expect(verifyWebhookSignature(tamperedStr, sig, mockSecret, mockPayload.timestamp)).toBe(false);
|
||||
});
|
||||
|
||||
it('超出 5 分钟 (300,000ms) 时钟窗口的请求应被拒绝', () => {
|
||||
const oldTs = Date.now() - 6 * 60 * 1000;
|
||||
const payloadStr = JSON.stringify({ ...mockPayload, timestamp: oldTs });
|
||||
const sig = computeWebhookSignature(payloadStr, mockSecret, oldTs);
|
||||
|
||||
expect(verifyWebhookSignature(payloadStr, sig, mockSecret, oldTs)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('2. LeadWebhookDispatcher 异步分发与重试逻辑', () => {
|
||||
it('目标端成功接收 (200 OK) 时标记成功', async () => {
|
||||
const mockHttpPoster = jest.fn(async () => ({ statusCode: 200 }));
|
||||
dispatcher.setHttpPoster(mockHttpPoster);
|
||||
|
||||
const result = await dispatcher.dispatch({
|
||||
targetUrl: 'https://api.partner.com/omnicard/webhook',
|
||||
secret: mockSecret,
|
||||
payload: mockPayload,
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.attempts).toBe(1);
|
||||
expect(mockHttpPoster).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('遇到 500 异常时应自动重试,全部失败后记入死信', async () => {
|
||||
const mockHttpPoster = jest.fn(async () => ({ statusCode: 500 }));
|
||||
dispatcher.setHttpPoster(mockHttpPoster);
|
||||
|
||||
const result = await dispatcher.dispatch({
|
||||
targetUrl: 'https://api.partner.com/failing/webhook',
|
||||
secret: mockSecret,
|
||||
payload: mockPayload,
|
||||
maxRetries: 2,
|
||||
initialBackoffMs: 10,
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.attempts).toBe(2);
|
||||
expect(result.isDeadLetter).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,124 @@
|
||||
import {
|
||||
TeamDataScopeService,
|
||||
UserDataContext,
|
||||
buildLeadDataScopeFilter,
|
||||
assertLeadAccessPermission,
|
||||
} from '../../../src/modules/team/service/data-scope';
|
||||
|
||||
describe('Point 52: 出海多租户团队成员细粒度数据隔离与销售管辖范围保护', () => {
|
||||
let service: TeamDataScopeService;
|
||||
|
||||
beforeEach(() => {
|
||||
service = new TeamDataScopeService();
|
||||
});
|
||||
|
||||
describe('1. buildLeadDataScopeFilter (数据权限范围查询条件构建)', () => {
|
||||
it('租户拥有者 (owner) 与管理员 (admin) 应具备租户级全量权限', () => {
|
||||
const ownerCtx: UserDataContext = {
|
||||
userId: 1,
|
||||
tenantId: 'tenant-100',
|
||||
role: 'owner',
|
||||
};
|
||||
const adminCtx: UserDataContext = {
|
||||
userId: 2,
|
||||
tenantId: 'tenant-100',
|
||||
role: 'admin',
|
||||
};
|
||||
|
||||
const ownerFilter = buildLeadDataScopeFilter(ownerCtx);
|
||||
expect(ownerFilter.tenantId).toBe('tenant-100');
|
||||
expect(ownerFilter.assignedUserId).toBeUndefined(); // 全员无限制
|
||||
expect(ownerFilter.departmentId).toBeUndefined();
|
||||
|
||||
const adminFilter = buildLeadDataScopeFilter(adminCtx);
|
||||
expect(adminFilter.tenantId).toBe('tenant-100');
|
||||
expect(adminFilter.assignedUserId).toBeUndefined();
|
||||
});
|
||||
|
||||
it('部门区域销售总监 (sales_manager) 应锁定为本部门管辖范围', () => {
|
||||
const managerCtx: UserDataContext = {
|
||||
userId: 10,
|
||||
tenantId: 'tenant-100',
|
||||
role: 'sales_manager',
|
||||
departmentId: 'dept-middle-east',
|
||||
};
|
||||
|
||||
const filter = buildLeadDataScopeFilter(managerCtx);
|
||||
expect(filter.tenantId).toBe('tenant-100');
|
||||
expect(filter.departmentId).toBe('dept-middle-east');
|
||||
expect(filter.assignedUserId).toBeUndefined();
|
||||
});
|
||||
|
||||
it('一线销售 (sales_rep/member) 必须严格隔离只能查看本人线索', () => {
|
||||
const repCtx: UserDataContext = {
|
||||
userId: 88,
|
||||
tenantId: 'tenant-100',
|
||||
role: 'sales_rep',
|
||||
departmentId: 'dept-middle-east',
|
||||
};
|
||||
|
||||
const filter = buildLeadDataScopeFilter(repCtx);
|
||||
expect(filter.tenantId).toBe('tenant-100');
|
||||
expect(filter.assignedUserId).toBe(88);
|
||||
});
|
||||
});
|
||||
|
||||
describe('2. assertLeadAccessPermission (单条线索详情读取防越权校验)', () => {
|
||||
const mockLead = {
|
||||
id: 999,
|
||||
tenantId: 'tenant-100',
|
||||
assignedUserId: 88,
|
||||
departmentId: 'dept-middle-east',
|
||||
};
|
||||
|
||||
it('允许线索归属人本人 (userId=88) 访问', () => {
|
||||
const repCtx: UserDataContext = {
|
||||
userId: 88,
|
||||
tenantId: 'tenant-100',
|
||||
role: 'sales_rep',
|
||||
};
|
||||
expect(() => assertLeadAccessPermission(repCtx, mockLead)).not.toThrow();
|
||||
});
|
||||
|
||||
it('允许所属部门主管 (dept-middle-east) 与企业 Owner 跨权限访问', () => {
|
||||
const managerCtx: UserDataContext = {
|
||||
userId: 10,
|
||||
tenantId: 'tenant-100',
|
||||
role: 'sales_manager',
|
||||
departmentId: 'dept-middle-east',
|
||||
};
|
||||
const ownerCtx: UserDataContext = {
|
||||
userId: 1,
|
||||
tenantId: 'tenant-100',
|
||||
role: 'owner',
|
||||
};
|
||||
|
||||
expect(() => assertLeadAccessPermission(managerCtx, mockLead)).not.toThrow();
|
||||
expect(() => assertLeadAccessPermission(ownerCtx, mockLead)).not.toThrow();
|
||||
});
|
||||
|
||||
it('同租户其他销售 (userId=89) 越权访问时应抛出 403 明确拒绝', () => {
|
||||
const otherRepCtx: UserDataContext = {
|
||||
userId: 89,
|
||||
tenantId: 'tenant-100',
|
||||
role: 'sales_rep',
|
||||
};
|
||||
|
||||
expect(() => assertLeadAccessPermission(otherRepCtx, mockLead)).toThrow(
|
||||
/无权访问该客户线索|Forbidden|权限不足/i
|
||||
);
|
||||
});
|
||||
|
||||
it('跨租户访问应坚决阻断', () => {
|
||||
const crossTenantCtx: UserDataContext = {
|
||||
userId: 1,
|
||||
tenantId: 'tenant-200', // 跨租户
|
||||
role: 'owner',
|
||||
};
|
||||
|
||||
expect(() => assertLeadAccessPermission(crossTenantCtx, mockLead)).toThrow(
|
||||
/无权访问该客户线索|Forbidden|权限不足/i
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
在新工单中引用
屏蔽一个用户