文件
shanghaipeiwan/server/tests/ownership-hardening.spec.ts

75 行
3.3 KiB
TypeScript

// 已读取AGENTS.md
import * as fs from 'fs';
import * as path from 'path';
/**
* 第七批回归闸门:评价回复归属 / 服务上下架归属 / 通用核销归属
* 以「源码契约断言」锁定安全边界。
*/
describe('第七批安全加固闸门 (评价回复 / 服务上下架 / 通用核销归属)', () => {
const rootDir = path.resolve(__dirname, '../../');
const serverRepoPath = path.join(rootDir, 'server/src/db/repository.ts');
const serverIndexPath = path.join(rootDir, 'server/src/index.ts');
const apiPath = path.join(rootDir, 'pwuniapp/common/api.js');
const indexCode = fs.readFileSync(serverIndexPath, 'utf-8');
const repoCode = fs.readFileSync(serverRepoPath, 'utf-8');
const apiCode = fs.readFileSync(apiPath, 'utf-8');
const routeBlock = (marker: string, len = 1400) => {
const start = indexCode.indexOf(marker);
expect(start).toBeGreaterThan(-1);
return indexCode.slice(start, start + len);
};
describe('1. 评价回复越权', () => {
it('仓储层必须提供 getReviewOwnership (基于 order_reviews.expert_id)', () => {
expect(repoCode).toContain('async getReviewOwnership(');
const start = repoCode.indexOf('async getReviewOwnership(');
const block = repoCode.slice(start, start + 400);
expect(block).toContain('SELECT expert_id FROM order_reviews');
expect(block).toContain('getCompanionOwnership');
});
it('回复路由必须校验归属', () => {
const block = routeBlock("app.post('/api/reviews/:id/reply'");
expect(block).toContain('repo.getReviewOwnership(reviewId)');
expect(block).toContain("assertCompanionActor(req, own, '回复评价')");
});
});
describe('2. 服务上下架越权', () => {
it('仓储层必须提供 getExpertServiceOwnership (基于 expert_services.expert_id)', () => {
expect(repoCode).toContain('async getExpertServiceOwnership(');
const start = repoCode.indexOf('async getExpertServiceOwnership(');
const block = repoCode.slice(start, start + 800);
expect(block).toContain('SELECT expert_id FROM expert_services');
expect(block).toContain('getCompanionOwnership');
});
it('上下架路由必须校验归属', () => {
const block = routeBlock("app.put('/api/companions/:id/services/:serviceId/status'");
expect(block).toContain('repo.getExpertServiceOwnership(serviceId)');
expect(block).toContain("assertCompanionActor(req, own, '调整服务上下架')");
});
});
describe('3. 通用核销归属 (生产闸门)', () => {
it('生产环境必须校验核销人:活动=发起人,订单=承接大神', () => {
const block = routeBlock("app.post('/api/verify/universal'");
expect(block).toContain('IS_PRODUCTION && !isVerifiedAdminRequest(req)');
expect(block).toContain("info.type === 'ACTIVITY'");
expect(block).toContain('Number(info.creatorId) === operator');
expect(block).toContain('own!.companionIds.includes(operator)');
expect(block).toContain('无权核销该门票');
});
});
describe('4. 小程序端写接口携带真实身份', () => {
it('回复评价/切换服务状态必须携带当前用户身份', () => {
expect(apiCode).toContain('{ replyContent, userId: normalizeUserId() }');
expect(apiCode).toContain('{ status, userId: normalizeUserId() }');
});
});
});