Files
rust-discovery/tests/browser/customer-management.spec.ts
T

109 lines
5.2 KiB
TypeScript

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', os: 'Linux'
}]) });
}
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('row').filter({ hasText: 'NORD-PC' })).toContainText('Nord GmbH');
expect(mutations).toEqual([
{ method: 'POST', path: '/api/v1/tenants', body: { name: 'Nord' } },
{ method: 'PATCH', path: `/api/v1/tenants/${customerId}`, body: { name: 'Nord GmbH' } }
]);
});