Files
canina/backend/src/orchestration/owner-review.http.spec.ts
T
2026-10-24 06:16:20 +00:00

49 lines
4.7 KiB
TypeScript

import {Test} from '@nestjs/testing';
import {INestApplication} from '@nestjs/common';
import request from 'supertest';
import {OrchestrationController} from './orchestration.controller';
import {OrchestrationService} from './orchestration.service';
import {PrepareWorkflowService} from './prepare-workflow.service';
import {JwtAuthGuard} from '../auth/jwt-auth.guard';
import {RolesGuard} from '../common/guards/roles.guard';
describe('configured owner real workflow HTTP authorization',()=>{
const owner='12345678-1234-1234-1234-123456789012';
let app:INestApplication,task:any,tx:any;
beforeAll(async()=>{
tx={prepareWorkflowOutbox:{create:jest.fn()},$queryRaw:jest.fn(async()=>[]),orchestrationTask:{findUnique:async()=>task,update:jest.fn(async({data}:any)=>(task={...task,state:data.state,version:task.version+1}))},orchestrationEvent:{findUnique:jest.fn(),create:jest.fn()},orchestrationApprovalDecision:{create:jest.fn()}};
const workflow=new PrepareWorkflowService({$transaction:async(cb:any)=>cb(tx)},{workerIds:[],stageCiIds:[],ownerReviewerIds:[owner]});
const m=await Test.createTestingModule({controllers:[OrchestrationController],providers:[{provide:OrchestrationService,useValue:{findAll:async()=>[task],findOne:async()=>task}},{provide:PrepareWorkflowService,useValue:workflow},RolesGuard]})
.overrideGuard(JwtAuthGuard).useValue({canActivate(c:any){const r=c.switchToHttp().getRequest();if(!r.headers.authorization)return false;r.user={sub:r.headers.authorization,role:'ADMIN'};return true;}}).compile();
app=m.createNestApplication();await app.init();
});
beforeEach(()=>{jest.clearAllMocks();task={id:'t',state:'GATE1_PENDING',version:2,operationMode:'PREPARE',metadata:{workflowScope:'STAGE_PREPARE',createdBy:'pmo'}};});
afterAll(async()=>{await app.close();});
it.each(['APPROVED','REJECTED'])('advertises and executes %s only for verified granted JWT subject',async decision=>{
const r=await request(app.getHttpServer()).get('/admin/orchestration/tasks').set('Authorization',owner).expect(200);
expect(r.body[0].availableActions).toEqual(['APPROVE_GATE1','REJECT_GATE1','HOLD_GATE1']);
await request(app.getHttpServer()).post('/admin/orchestration/tasks/t/gates/1/decide').set('Authorization',owner).send({decision,expectedVersion:2,idempotencyKey:'decision',expiresAt:new Date(Date.now()+60000).toISOString(),notes:'Delegated system testing by independent owner'}).expect(201);
expect(tx.orchestrationApprovalDecision.create).toHaveBeenCalledWith({data:expect.objectContaining({actorId:owner,decision})});
expect(tx.orchestrationEvent.create).toHaveBeenCalledWith({data:expect.objectContaining({actorId:owner,payload:expect.objectContaining({fingerprint:expect.stringContaining('Delegated system testing')})})});
});
it('holds via canonicalized ADMIN without granting proposer powers',async()=>{
await request(app.getHttpServer()).put('/admin/orchestration/tasks/t/transition').set('Authorization',owner).send({toState:'GATE1_HELD',expectedVersion:2,idempotencyKey:'hold',reason:'Independent owner needs evidence'}).expect(200);
expect(tx.orchestrationApprovalDecision.create).not.toHaveBeenCalled();
});
it.each(['ordinary-admin','pmo'])('denies ungranted subject %s and body impersonation',async subject=>{
const r=await request(app.getHttpServer()).get('/admin/orchestration/tasks').set('Authorization',subject).expect(200);
expect(r.body[0].availableActions).toEqual([]);
const body={decision:'APPROVED',expectedVersion:2,idempotencyKey:'deny',expiresAt:new Date(Date.now()+60000).toISOString()};
await request(app.getHttpServer()).post('/admin/orchestration/tasks/t/gates/1/decide').set('Authorization',subject).send(body).expect(403);
await request(app.getHttpServer()).post('/admin/orchestration/tasks/t/gates/1/decide').set('Authorization',subject).send({...body,actorId:owner}).expect(400);
await request(app.getHttpServer()).put('/admin/orchestration/tasks/t/transition').set('Authorization',subject).send({toState:'GATE1_HELD',expectedVersion:2,idempotencyKey:'hold',reason:'hold'}).expect(403);
expect(tx.orchestrationTask.update).not.toHaveBeenCalled();
});
it('denies grant holder self review, Gate2 and release',async()=>{
task.metadata.createdBy=owner;
await request(app.getHttpServer()).post('/admin/orchestration/tasks/t/gates/1/decide').set('Authorization',owner).send({decision:'APPROVED',expectedVersion:2,idempotencyKey:'self',expiresAt:new Date(Date.now()+60000).toISOString()}).expect(403);
await request(app.getHttpServer()).post('/admin/orchestration/tasks/t/gates/2/decide').set('Authorization',owner).send({}).expect(403);
await request(app.getHttpServer()).post('/admin/orchestration/tasks/t/commands/RELEASE_AUTHORIZE').set('Authorization',owner).send({}).expect(403);
expect(tx.orchestrationTask.update).not.toHaveBeenCalled();
});
});