import { test, expect } from '../../web/node_modules/@playwright/test/index.js'; import { mkdirSync, writeFileSync } from 'node:fs'; import { resolve } from 'node:path'; import { mkdtempSync, rmSync } from 'node:fs'; import { chromium } from '../../web/node_modules/@playwright/test/index.js'; import { login } from './helpers'; test('real TLS UI: login, tenant, fixed preview, pending, reload, CSRF denial and logout', async ({ page, context }) => { test.skip(process.env.RMM_E2E_PROFILE !== 'dev/test' || process.env.RMM_E2E_MODE !== 'pending-only', 'Explicit pending-only dev/test run required'); const asset = process.env.RMM_E2E_ASSET_ID; if (!asset) throw new Error('Authenticated local fixture asset required'); const statuses: { path: string; status: number }[] = []; page.on('response', r => { const path = new URL(r.url()).pathname; if (path === '/auth/dev/login' || path.endsWith('/operation-plans') || path.endsWith('/submit')) statuses.push({ path: path === '/auth/dev/login' ? 'login' : path.endsWith('/submit') ? 'submit' : 'plan', status: r.status() }); }); let authorizationRequests = 0; page.on('request', r => { if (new URL(r.url()).pathname.endsWith('/authorizations')) authorizationRequests++; }); await login(page); await expect(page.getByRole('heading', { name: 'Geräte', exact: true })).toBeVisible(); const cookies = await context.cookies(); const authCookie = cookies.find(c => c.name === '__Host-rmm_session'); expect(authCookie?.httpOnly).toBe(true); expect(authCookie?.secure).toBe(true); expect(authCookie?.sameSite).toBe('Strict'); await page.getByRole('checkbox', { name: `Ziel ${asset}`, exact: true }).check(); await page.getByRole('link', { name: 'Aufträge', exact: true }).click(); await page.getByRole('link', { name: 'Neuer Auftrag', exact: true }).click(); await page.getByRole('button', { name: 'Weiter zum Skript' }).click(); await page.getByRole('radio', { name: 'Einmaliges Skript' }).check(); await page.getByLabel('Skriptinhalt', { exact: true }).fill("printf '%s\\n' 'Hello World'\n"); await page.getByText('Zeitfenster und Ausführungsoptionen', { exact: true }).click(); await page.getByLabel('Laufzeit je Gerät in Sekunden').fill('60'); await page.getByLabel('Erwartete Standardausgabe (optional)', { exact: true }).fill('Hello World\n'); await page.getByLabel('Begründung', { exact: true }).fill('Authenticated synthetic local fixture · pending-only browser contract; do not execute'); await page.getByRole('button', { name: 'Zusammenfassung ansehen' }).click(); const approve = page.getByRole('button', { name: 'SIMULIERT: Genehmigen' }); await expect(approve).toHaveCount(0); await expect(page.getByRole('button', { name: 'Zur Genehmigung einreichen' })).toBeEnabled(); await page.getByRole('button', { name: 'Zur Genehmigung einreichen' }).click(); const op = page.locator('section[data-operation-id]'); await op.getByText('Technische Auftragsdaten', { exact: true }).click(); await expect(op.getByLabel('Technische Auftragsdaten', { exact: true })).toContainText('"approval_state": "pending_approval"'); await expect(op.getByLabel('Technische Auftragsdaten', { exact: true })).toContainText('"job_count": 0'); await expect(op.getByLabel('Technische Auftragsdaten', { exact: true })).toContainText('"execution_state": null'); const operationId = await op.getAttribute('data-operation-id'); if (!operationId) throw new Error('Operation ID missing'); expect(operationId).toMatch(/^[0-9a-f-]{36}$/); await page.reload(); await expect(page.getByRole('status')).toContainText('SIMULIERT'); await expect(op).toHaveAttribute('data-operation-id', operationId); await op.getByText('Technische Auftragsdaten', { exact: true }).click(); await expect(op.getByLabel('Technische Auftragsdaten', { exact: true })).toContainText('"approval_state": "pending_approval"'); await page.getByRole('button', { name: 'Antrag prüfen' }).click(); await expect(approve).toBeDisabled(); await page.getByRole('checkbox', { name: 'Ich habe die Geräte, den Inhalt und die Ausführungsrechte geprüft.', exact: false }).check(); await expect(approve).toBeEnabled(); // We intentionally do NOT click approval in this run. expect(authorizationRequests).toBe(0); // Negative CSRF check uses a real UI logout click, never an authorization request. await page.route('**/auth/logout', async route => { const headers = { ...route.request().headers() }; delete headers['x-csrf-token']; await route.continue({ headers }); }); const denied = page.waitForResponse(r => new URL(r.url()).pathname === '/auth/logout'); await page.getByRole('button', { name: 'Abmelden', exact: true }).click(); expect((await denied).status()).toBe(403); await expect(page.getByRole('alert').first()).toBeVisible(); await expect(page.getByRole('status')).toContainText('SIMULIERT'); await page.unroute('**/auth/logout'); await page.getByRole('button', { name: 'Abmelden', exact: true }).click(); await expect(page.getByRole('button', { name: 'Anmelden', exact: true })).toBeVisible(); expect(statuses.some(s => s.path === 'login' && s.status === 200)).toBe(true); expect(statuses.some(s => s.path === 'plan' && s.status === 200)).toBe(true); expect(statuses.some(s => s.path === 'submit' && s.status === 200)).toBe(true); const directory = resolve('node_modules/.cache/browser-evidence'); mkdirSync(directory, { recursive: true, mode: 0o700 }); writeFileSync(resolve(directory, 'pending.json'), JSON.stringify({ profile: 'dev/test', testsimulation: true, tls_verification_enabled: true, synthetic_authenticated_local_fixture: true, simulation_banner: true, secure_cookie: true, pending_job_count: 0, pending_execution_state: null, reload_pending_review: true, csrf_logout_denied_status: 403, authorization_requests: authorizationRequests, executed_jobs: false, vm_evidence: false, statuses, checked_at: new Date().toISOString() }, null, 2), { mode: 0o600 }); }); test('real browser TLS rejects a certificate for a different DNS name', async ({ page }) => { test.skip(process.env.RMM_E2E_PROFILE !== 'dev/test' || process.env.RMM_E2E_MODE !== 'pending-only'); let rejected = false; try { await page.goto('https://rmm-dev-wrong-name.internal:18443/'); } catch (e) { rejected = e instanceof Error && /ERR_CERT_COMMON_NAME_INVALID/.test(e.message); } expect(rejected).toBe(true); }); test('real browser without the isolated development CA rejects the server chain', async () => { test.skip(process.env.RMM_E2E_PROFILE !== 'dev/test' || process.env.RMM_E2E_MODE !== 'pending-only'); const home = mkdtempSync(resolve('node_modules/.cache/browser-untrusted-')); const browser = await chromium.launch({ env: { ...process.env, HOME: home }, args: ['--host-resolver-rules=MAP rmm-dev.internal 127.0.0.1', '--no-proxy-server'] }); try { const page = await browser.newPage(); let rejected = false; try { await page.goto('https://rmm-dev.internal:18443/'); } catch (e) { rejected = e instanceof Error && /ERR_CERT_AUTHORITY_INVALID/.test(e.message); } expect(rejected).toBe(true); } finally { await browser.close(); rmSync(home, { recursive: true, force: true }); } });