kundencenter/apps/e2e/setup-db.ts
Claude (KI) f475e8bd36
Some checks failed
CI / test (push) Has been cancelled
CI / e2e (push) Has been cancelled
Browser-Abnahme: Ticket- und Rechnungsablauf (#119)
- Setup legt feste Ausgangsdaten an (Firmendaten, Testkunde, Rechnungs-
  entwurf) in der Wegwerf-DB kundencenter_e2e.
- Ticket: Kunde eröffnet über die Oberfläche, Personal antwortet, Kunde
  sieht die Antwort (Desktop + 360 px, axe).
- Rechnung: Entwurf für Kunden unsichtbar (404), Personal stellt aus,
  Kunde sieht Nummer und lädt das PDF (axe auf beiden Seiten).
Ergebnis: 15 passed, 1 bewusst übersprungen (Ausstellen nur einmal).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 16:06:41 +02:00

46 lines
4 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import mysql from 'mysql2/promise';
import { readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { randomUUID } from 'node:crypto';
import { hash } from '@node-rs/argon2';
import * as OTPAuth from 'otpauth';
/** Frische Datenbank kundencenter_e2e mit allen Migrationen und einem Superadmin mit eingerichteter 2FA.
* Läuft VOR Playwright (dessen webServer starten vor einem globalSetup). */
async function setup() {
if (process.env.KC_TEST_DB_RESET !== '1') throw new Error('E2E-Tests löschen kundencenter_e2e – nur über ops/test/run-e2e.sh starten.');
const base = { host: process.env.DB_HOST ?? '127.0.0.1', user: process.env.DB_USER!, password: process.env.DB_PASSWORD! };
const admin = await mysql.createConnection(base);
await admin.query('DROP DATABASE IF EXISTS kundencenter_e2e');
await admin.query('CREATE DATABASE kundencenter_e2e CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci');
await admin.end();
const c = await mysql.createConnection({ ...base, database: 'kundencenter_e2e', multipleStatements: false });
await c.query('CREATE TABLE schema_migrations (name VARCHAR(200) PRIMARY KEY, checksum CHAR(64) NULL, applied_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3))');
const dir = join(process.cwd(), '../../migrations');
for (const f of readdirSync(dir).filter((n) => n.endsWith('.sql')).sort()) {
await c.query('INSERT INTO schema_migrations (name) VALUES (?)', [f]);
for (const st of readFileSync(join(dir, f), 'utf8').replace(/^\s*--.*$/gm, '').split(/;\s*\n/).map((s) => s.trim()).filter(Boolean)) await c.query(st);
}
// Superadmin + bestätigte 2FA (Geheimnis mit dem Dummy-Schlüssel der Testumgebung verschlüsselt)
process.env.DB_NAME = 'kundencenter_e2e';
const { encrypt } = await import('@kc/platform/crypto');
const secret = new OTPAuth.Secret({ size: 20 }).base32;
const id = randomUUID();
await c.query("INSERT INTO users (id, email, name, password_hash, kind, staff_role, status, email_verified_at) VALUES (?,?,?,?, 'staff', 'superadmin', 'active', UTC_TIMESTAMP(3))", [id, 'admin@e2e.example', 'E2E Admin', await hash('e2e-passwort-admin-123')]);
await c.query('INSERT INTO mfa_totp (user_id, secret_enc, confirmed_at) VALUES (?,?,UTC_TIMESTAMP(3))', [id, encrypt(secret)]);
// Feste Ausgangsdaten für Ticket- und Rechnungsablauf (#119): vollständige Firmendaten, Testkunde mit Inhaber, Rechnungsentwurf
await c.query("UPDATE company_settings SET name='E2E Firma GmbH', street='Teststr. 1', zip='12345', city='Teststadt', tax_number='12/345/67890' WHERE id=1");
const org = randomUUID(); const owner = randomUUID(); const draft = randomUUID();
await c.query("INSERT INTO organizations (id, customer_number, name, customer_type) VALUES (?, 'K-E2E1', 'E2E Ablauf GmbH', 'business')", [org]);
await c.query("INSERT INTO billing_profiles (org_id, company, country, billing_email) VALUES (?, 'E2E Ablauf GmbH', 'DE', 'ablauf@e2e.example')", [org]);
await c.query("INSERT INTO users (id, email, name, password_hash, kind, status) VALUES (?, 'ablauf@e2e.example', 'Ablauf Kunde', ?, 'customer', 'active')", [owner, await hash('e2e-passwort-kunde-1')]);
await c.query("INSERT INTO memberships (org_id, user_id, role) VALUES (?,?,'owner')", [org, owner]);
await c.query("INSERT INTO invoices (id, org_id, status, total_net_cents, total_tax_cents, total_gross_cents, created_by) VALUES (?, ?, 'draft', 1000, 190, 1190, ?)", [draft, org, id]);
await c.query("INSERT INTO invoice_items (id, invoice_id, description, quantity, unit_price_net_cents, tax_bp, net_cents, tax_cents, gross_cents, sort_order) VALUES (?, ?, 'E2E Webhosting', 1, 1000, 1900, 1000, 190, 1190, 0)", [randomUUID(), draft]);
await c.end();
writeFileSync('/tmp/kc-e2e-data.json', JSON.stringify({ draftInvoiceId: draft, customerEmail: 'ablauf@e2e.example', customerPassword: 'e2e-passwort-kunde-1' }));
rmSync('/tmp/kc-e2e-totp-step', { force: true });
writeFileSync('/tmp/kc-e2e-admin.json', JSON.stringify({ email: 'admin@e2e.example', password: 'e2e-passwort-admin-123', secret }));
}
await setup();