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 { resolve } from 'node:path'; 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('Missing local fixture address'); origin = `http://127.0.0.1:${address.port}`; }); test.afterAll(async () => { await server?.close(); }); test('creates and renames a customer, opens its inventory, and hides editing for a reader', async ({ page }) => { const home = { tenant_id: '11111111-1111-4111-8111-111111111111', name: 'Testzentrale' }; const customerId = '22222222-2222-4222-8222-222222222222'; const customers = [home]; const csrf = 'local-customer-test'; let canManage = true; const mutations: { method: string; path: string; body: unknown }[] = []; const listing = (items: unknown[]) => ({ items, next_cursor: null, truncated: false, completeness: 'complete', as_of: '2026-11-17T12:00:00Z' }); await page.route(`${origin}/auth/config`, route => route.fulfill({ json: { local_login_enabled: true, oidc_configured: false, oidc_login_url: null, dev_login_enabled: false } })); await page.route(`${origin}/auth/session`, route => route.fulfill({ json: { principal: canManage ? 'customer-admin-fixture' : 'inventory-reader-fixture', testsimulation: false, tenant_id: route.request().headers()['x-rmm-tenant'] ?? home.tenant_id, csrf_token: csrf, permissions: ['inventory:read'] } })); await page.route(`${origin}/api/v1/**`, async route => { const request = route.request(); const path = new URL(request.url()).pathname; const method = request.method(); const collection = '/api/v1/tenants'; if (method === 'GET' && path === collection) { return route.fulfill({ json: { ...listing(customers), can_manage_customers: canManage } }); } if (method === 'GET' && path === `${collection}/${customerId}/devices`) { const customer = customers.find(c => c.tenant_id === customerId); if (!customer) throw new Error('Kunde fehlt im Testbestand'); return route.fulfill({ json: listing([{ tenant_id: customerId, customer_name: customer.name, asset_id: 'device-1', agent_id: 'agent-1', lifecycle: 'current', status: 'online', hostname: 'NORD-PC', device_type: 'hypervisor', os: 'Proxmox VE', ram_available_bytes: 6 * 2 ** 30, ram_total_bytes: 16 * 2 ** 30, cpu_usage_percent: 0 }]) }); } if (method === 'POST' || method === 'PATCH') { const body = request.postDataJSON(); mutations.push({ method, path, body }); expect(canManage).toBe(true); expect(request.headers()['x-csrf-token']).toBe(csrf); if (method === 'POST' && path === collection) { const created = { tenant_id: customerId, name: body.name }; customers.push(created); return route.fulfill({ status: 201, json: created }); } if (method === 'PATCH' && path === `${collection}/${customerId}`) { expect(request.headers()['x-rmm-tenant']).toBe(customerId); const customer = customers.find(c => c.tenant_id === customerId); if (!customer) throw new Error('Unbekannter Testkunde'); customer.name = body.name; return route.fulfill({ json: customer }); } } return route.fulfill({ status: 404, json: { code: 'UNEXPECTED_FIXTURE_REQUEST', message: path } }); }); await page.goto(`${origin}/#kunden`); await page.getByRole('button', { name: 'Kunde anlegen' }).click(); await page.getByLabel('Kundenname').fill('Nord'); await page.getByRole('button', { name: 'Speichern' }).click(); await expect(page.getByRole('status')).toHaveText('Kunde „Nord“ angelegt.'); await page.getByRole('button', { name: 'Nord umbenennen' }).click(); await expect(page.getByLabel('Kundenname')).toHaveValue('Nord'); await page.getByLabel('Kundenname').fill('Nord GmbH'); await page.getByRole('button', { name: 'Speichern' }).click(); await expect(page.getByRole('status')).toHaveText('Kundenname in „Nord GmbH“ geändert.'); await expect(page.getByRole('cell', { name: 'Nord', exact: true })).toHaveCount(0); canManage = false; await page.reload(); await expect(page.getByRole('row', { name: /Nord GmbH/ })).toBeVisible(); await expect(page.getByRole('button', { name: 'Kunde anlegen' })).toHaveCount(0); await expect(page.getByRole('button', { name: /umbenennen/i })).toHaveCount(0); await page.getByRole('button', { name: 'Geräte von Nord GmbH anzeigen' }).click(); await expect(page.getByRole('heading', { name: 'Geräte', exact: true })).toBeVisible(); await expect(page.getByRole('combobox', { name: 'Kunde', exact: true })).toHaveValue(customerId); await expect(page.getByRole('combobox', { name: 'Kunde im Gerätebestand', exact: true })).toHaveValue(customerId); const device = page.getByRole('row').filter({ hasText: 'NORD-PC' }); await expect(device).toContainText('Nord GmbH'); const headers = page.locator('.fleet-table thead th[scope="col"]'); await expect(headers.nth(0)).toHaveText('Hostname'); await expect(headers.nth(1)).toHaveText('Type'); await expect(device.getByRole('cell').nth(2)).toHaveText('hypervisor'); await expect(device.getByRole('cell').nth(3)).toHaveText('Proxmox VE'); await expect(device.locator('[data-label="RAM frei / gesamt"]')).toHaveText('6 / 16 GiB'); await expect(device.getByText('Auslastung 0 %', { exact: true })).toBeVisible(); const navigation = page.getByRole('navigation', { name: 'Hauptnavigation' }); await navigation.getByRole('link', { name: 'Aufträge', exact: true }).click(); await expect(page.getByRole('combobox', { name: 'Kunde', exact: true })).toHaveValue(customerId); await navigation.getByRole('link', { name: 'Skripte', exact: true }).click(); await expect(page.getByRole('combobox', { name: 'Kunde', exact: true })).toHaveValue(customerId); await navigation.getByRole('link', { name: 'Genehmigungen', exact: true }).click(); await expect(page.locator('header').getByText('Nord GmbH')).toBeVisible(); await navigation.getByRole('link', { name: 'Geräte', exact: true }).click(); await expect(page.getByRole('combobox', { name: 'Kunde im Gerätebestand', exact: true })).toHaveValue(customerId); expect(mutations).toEqual([ { method: 'POST', path: '/api/v1/tenants', body: { name: 'Nord' } }, { method: 'PATCH', path: `/api/v1/tenants/${customerId}`, body: { name: 'Nord GmbH' } } ]); }); test('removes the confirmed historical installation and keeps its successor', async ({ page }) => { const customer = { tenant_id: '22222222-2222-4222-8222-222222222222', name: 'Nord GmbH' }; const asset = '33333333-3333-4333-8333-333333333333'; const old = '44444444-4444-4444-8444-444444444444'; const active = '55555555-5555-4555-8555-555555555555'; const path = '/api/v1/tenants/' + customer.tenant_id + '/devices/' + asset; const device = { tenant_id: customer.tenant_id, customer_name: customer.name, asset_id: asset, hostname: 'NORD-PC', os: 'Windows', recent_jobs: [], volumes: [] }; const listing = (items: unknown[]) => ({ items, next_cursor: null, truncated: false, completeness: 'complete', as_of: new Date().toISOString() }); let attempts = 0, revoked = false, deleted = false, canManage = true; await page.route(origin + '/auth/config', r => r.fulfill({ json: { local_login_enabled: true, oidc_configured: false, oidc_login_url: null, dev_login_enabled: false } })); await page.route(origin + '/auth/session', r => r.fulfill({ json: { principal: 'operator', tenant_id: customer.tenant_id, csrf_token: 'fixture-csrf', testsimulation: false, permissions: canManage ? ['inventory:read', 'devices:revoke'] : ['inventory:read'] } })); await page.route(origin + '/api/v1/**', async r => { const req = r.request(), url = new URL(req.url()); const historical = { ...device, agent_id: old, lifecycle: revoked ? 'revoked' : 'replaced', status: 'offline' }; const successor = { ...device, agent_id: active, lifecycle: 'current', status: 'online', source_id: 'windows.base', capabilities: ['windows.base/1', 'rmm.sync.v1', 'run_script', 'script.powershell/1', 'script.powershell.process_remote_signed/1', 'script.full_maintenance/1', 'helper.system.v1', 'system:run_script'] }; if (url.pathname === '/api/v1/tenants') return r.fulfill({ json: listing([customer]) }); if (url.pathname === '/api/v1/devices' || url.pathname === path.slice(0, path.lastIndexOf('/'))) { return r.fulfill({ json: listing(url.searchParams.get('view') === 'history' ? (deleted ? [] : [historical]) : [successor]) }); } if (url.pathname === path && req.method() === 'GET') { if (url.searchParams.get('agent_id') === old && !deleted) return r.fulfill({ json: historical }); if (url.searchParams.get('agent_id') === active) return r.fulfill({ json: successor }); } if (url.pathname === path + '/revocation' && req.method() === 'POST') { expect(url.searchParams.get('agent_id')).toBe(old); expect(req.headers()['x-csrf-token']).toBe('fixture-csrf'); expect(req.postDataJSON()).toEqual({ reason: 'Neuinstallation bestätigt' }); revoked = true; return r.fulfill({ json: { asset_id: asset, revoked: true } }); } if (url.pathname === path && req.method() === 'DELETE') { expect(url.searchParams.get('agent_id')).toBe(old); expect(req.headers()['x-rmm-tenant']).toBe(customer.tenant_id); expect(req.headers()['x-csrf-token']).toBe('fixture-csrf'); expect(req.postData()).toBeNull(); if (++attempts === 1) return r.fulfill({ status: 409, json: { code: 'DEVICE_HAS_OPEN_WORK' } }); deleted = true; return r.fulfill({ status: 204 }); } return r.fulfill({ status: 404, json: { code: 'NOT_FOUND' } }); }); await page.goto(origin + '/#geraete'); await page.getByRole('button', { name: 'Historie' }).click(); await page.getByRole('button', { name: 'NORD-PC' }).click(); await page.getByText('Gerät verwalten', { exact: true }).click(); await expect(page.getByRole('button', { name: 'Kopplung löschen' })).toHaveCount(0); await page.getByText('Geräteidentität widerrufen', { exact: true }).click(); await page.getByLabel('Grund für Geräteidentität widerrufen').fill('Neuinstallation bestätigt'); const nonce = await page.getByText('Aktuelle Bestätigungskennung:').locator('code').innerText(); await page.getByLabel('Bestätigungskennung eingeben').fill(nonce); await page.getByRole('checkbox', { name: 'Ich bestätige' }).check(); await page.getByRole('button', { name: 'Geräteidentität widerrufen · ausdrücklich ausführen' }).click(); await page.getByRole('button', { name: 'Kopplung löschen' }).click(); await expect(page.locator('.warning')).toContainText('NORD-PC'); await expect(page.locator('.warning')).toContainText(customer.name); await expect(page.getByText('Kopplungs-ID')).toContainText(old); await page.getByRole('button', { name: 'Abbrechen' }).click(); expect(attempts).toBe(0); await page.getByRole('button', { name: 'Kopplung löschen' }).click(); await page.getByRole('button', { name: 'Endgültig löschen' }).click(); await expect(page.getByRole('alert')).toContainText('offene Aufträge'); await page.getByRole('button', { name: 'Endgültig löschen' }).click(); await expect(page.getByRole('status')).toContainText('Kopplung von „NORD-PC“ bei „Nord GmbH“ gelöscht.'); await expect(page.getByRole('button', { name: 'NORD-PC' })).toHaveCount(0); await page.getByRole('button', { name: 'Bestand' }).click(); await page.getByRole('button', { name: 'NORD-PC' }).click(); await expect(page).toHaveURL(new RegExp(active + '$')); await expect(page.getByText('PowerShell als Dienstkonto und als Administrator (SYSTEM)')).toBeVisible(); await page.getByText('Gerät verwalten', { exact: true }).click(); await expect(page.getByRole('button', { name: 'Kopplung löschen' })).toHaveCount(0); await expect(page.getByText('Geräteidentität widerrufen', { exact: true })).toBeVisible(); canManage = false; await page.reload(); await expect(page.getByRole('heading', { name: 'NORD-PC' })).toBeVisible(); await expect(page.getByText('Gerät verwalten', { exact: true })).toHaveCount(0); expect(attempts).toBe(2); });