kundencenter/apps/e2e/setup-db.ts
Claude (KI) 9138d5dcb9 Browser-Abnahme mit Playwright und axe (#39, #15)
- apps/e2e: echte API + Web gegen kundencenter_e2e, Superadmin mit 2FA wird
  vorab angelegt. Abläufe: Login-Seite (Tastatur, falsches Passwort),
  Personal-Login mit 2FA + Abmelden (Sitzung beendet, geschützte Seite
  leitet um), Kundenanlage → Einladung → Kundenlogin, Mandantentrennung
  (fremde Organisation/Mitglieder 403/404, Verwaltung 401/403), 2FA-Replay.
- Projekte: Desktop hell und 360 px dunkel (touch); axe nach WCAG 2.2 AA auf
  Login, Dashboard, Kundenliste, Kunden-Dashboard – schwere/kritische
  Verstöße = rot. Lokal: 8/8 grün.
- ops/test/run-e2e.sh (Playwright-Image) und zweiter CI-Job.

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

36 lines
2.5 KiB
TypeScript
Raw 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)]);
await c.end();
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();