import { test, expect } from '../../web/node_modules/@playwright/test/index.js'; import { createServer, type ViteDevServer } from '../../web/node_modules/vite/dist/node/index.js'; import { readFileSync } from 'node:fs'; import { resolve } from 'node:path'; import { createHash } from 'node:crypto'; let server: ViteDevServer; let origin: string; test.beforeAll(async () => { server = await createServer({ root: resolve(__dirname,'../../web'), configFile:false, server:{host:'127.0.0.1', port:0}, logLevel:'silent' }); await server.listen(); const address = server.httpServer!.address(); if (!address || typeof address === 'string') throw new Error('Lokaler Testserver fehlt'); origin = 'http://127.0.0.1:' + address.port; }); test.afterAll(async () => { await server?.close(); }); test('reviews the fixed MCP request, rejects it once and blocks a changed hash', async ({page}) => { const cases = JSON.parse(readFileSync(resolve(__dirname,'../script_console/codec-golden.json'),'utf8')); const saved = JSON.parse(Buffer.from(cases.find((entry: {name: string}) => entry.name === 'legacy_linux_agent').plan_payload_bytes).toString('utf8')); const job = saved.jobs[0], payload = job.payload; const created = new Date().toISOString(), expires = new Date(Date.now() + 60000).toISOString(); payload.not_before = created; payload.start_before = expires; const hash = (data: string) => createHash('sha256').update(data).digest('hex'); job.job_payload_hash = hash(JSON.stringify(payload)); saved.principal = 'mcp:example:readonly-client'; saved.reason = 'Linux-Version prüfen'; const raw = JSON.stringify(saved); const key = hash(raw); const customer = { tenant_id:saved.tenant_id, name:'Werkstatt' }; const other = { tenant_id:'aaaaaaa0-0000-0000-0000-000000000000', name:'Nur Inventar' }; const target = {asset_id:payload.asset_id,agent_id:payload.agent_id}; const summary = { title:'Freies Skript', action:'run_script', requester:saved.principal, reason:saved.reason, ...customer, customer_name:customer.name, plan_id:saved.plan_id, operation_id:'22222222-2222-4222-8222-222222222222', targets:[{...target,hostname:'WERK-LINUX'}], created_at:created, jobs:[], job_count:0, execution_state:null }; const path = '/api/v1/tenants/'+saved.tenant_id; let approval = 'pending_approval', corrupt = false, rights = true; const decision = { principal: 'Marta', at: created, testsimulation: true }; const decisions: unknown[] = []; const listing = (items: unknown[]) => ({items,next_cursor:null,completeness:'complete',truncated:false,as_of:created}); await page.route(origin+'/auth/config', r => r.fulfill({json:{dev_login_enabled:true,local_login_enabled:false,oidc_configured:false,oidc_login_url:null}})); await page.route(origin+'/auth/session', r => r.fulfill({json:{principal:'Marta',testsimulation:true,csrf_token:'fixture',tenant_id:customer.tenant_id,permissions:['inventory:read','operations:read','plans:approve','scripts:execute']}})); await page.route(origin+'/api/v1/**', async route => { const req = route.request(), url = new URL(req.url()); if (url.pathname === '/api/v1/tenants') return route.fulfill({json:listing([customer,other])}); if (url.pathname === '/api/v1/approvals') { const pending = approval === 'pending_approval'; const visible = rights && !url.searchParams.has('count_only') && (url.searchParams.get('status') === 'pending') === pending; return route.fulfill({json:{...listing(visible ? [{...summary,approval_state:approval,decision:pending ? null : decision}]:[]), customers:rights?[customer]:[], can_review:rights,pending_count:rights && pending?1:0}}); } if (url.pathname === path+'/operation-plans/'+saved.plan_id) return route.fulfill({json:{ ...summary, plan_payload:saved, payload:saved, plan_hash:corrupt?'f'.repeat(64):key,plan_payload_bytes:[...Buffer.from(raw)], resolved_targets:[target], excluded_targets:[], jobs:[{...job,...target,job_id:payload.job_id,attempt_id:payload.attempt_id}], expires_at:expires, approval_state:approval, authorization_status:'required', decision:approval === 'rejected' ? decision : null }}); if (url.pathname === path+'/operation-plans/'+saved.plan_id+'/authorizations' && req.method() === 'POST') { decisions.push(req.postDataJSON()); approval='rejected'; return route.fulfill({json:{...summary,approval_state:approval}}); } if (url.pathname === path+'/operations/'+summary.operation_id) return route.fulfill({json:{...summary,approval_state:approval}}); return route.fulfill({status:404,json:{code:'UNEXPECTED_FIXTURE_REQUEST'}}); }); await page.goto(origin+'/#genehmigungen'); const inbox = page.getByRole('region',{name:'Genehmigungseingang'}); await expect(inbox.getByRole('option',{name:other.name})).toHaveCount(0); await inbox.getByRole('button',{name:'Freies Skript'}).click(); await expect(page.locator('.review-targets')).toContainText('WERK-LINUX'); await expect(page.getByLabel('Vollständiger Skripttext')).toHaveText(payload.action.script_utf8); const reject = page.getByRole('button',{name:'SIMULIERT: Ablehnen',exact:true}); await expect(reject).toBeDisabled(); await page.getByRole('checkbox',{name:'Ich habe die Geräte',exact:false}).check(); await expect(page.getByRole('button',{name:'SIMULIERT: Genehmigen',exact:true})).toBeEnabled(); await reject.click(); await expect(page.locator('section[data-operation-id] .order-status')).toHaveText('Abgelehnt'); expect(decisions).toEqual([{plan_hash:key,decision:'reject'}]); await page.getByRole('link',{name:/^Genehmigungen/}).click(); await expect(inbox.getByText('Keine offenen Genehmigungen.')).toBeVisible(); await inbox.getByRole('button',{name:'Verlauf'}).click(); const entry = inbox.locator('td[data-label="Entschieden von und am"]'); await expect(entry).toContainText('Marta'); await expect(entry).toContainText('Simulierter Testentscheid'); await expect(entry.locator('time')).toHaveAttribute('datetime', created); await inbox.getByRole('button',{name:'Freies Skript'}).click(); await expect(page.locator('section[data-plan-id] time')).toHaveAttribute('datetime', created); await expect(page.locator('section[data-plan-id] .order-status')).toHaveText('Abgelehnt'); await expect(reject).toHaveCount(0); corrupt=true; approval='pending_approval'; await page.reload(); await expect(page.getByRole('alert')).toContainText('Planhash'); await expect(page.getByRole('checkbox',{name:'Ich habe die Geräte',exact:false})).toHaveCount(0); rights=false; await page.goto(origin+'/#genehmigungen'); await expect(inbox.getByText('Dieses Konto hat keine Berechtigung für Genehmigungen.')).toBeVisible(); expect(decisions).toHaveLength(1); });