CI / Web-Barrierefreiheitspruefung (axe, Tastatur, Reflow, Textabstaende) (pull_request) Canceled after 0s
CI / Core-Modul (Build/Vet/Test) (pull_request) Failing after 1m0s
CI / DMS Security-Test-Gate (AC5, Merge-Gate) (pull_request) Successful in 5s
CI / Connect Security-Test-Gate (QA-01 AC4, Merge-Gate) (pull_request) Successful in 4s
CI / DMS-Modul Supply-Chain-Scan (govulncheck) (pull_request) Failing after 21s
CI / Web (dms-browser) (pull_request) Failing after 1m27s
CI / Web (pdf-viewer) (pull_request) Failing after 1m2s
CI / Web (shl) (pull_request) Failing after 11s
CI / Core-Modul (Build/Vet/Test) (push) Failing after 1m0s
CI / DMS Security-Test-Gate (AC5, Merge-Gate) (push) Successful in 5s
CI / Connect Security-Test-Gate (QA-01 AC4, Merge-Gate) (push) Successful in 4s
CI / DMS-Modul Supply-Chain-Scan (govulncheck) (push) Failing after 19s
CI / Web (dms-browser) (push) Failing after 1m26s
CI / Web (pdf-viewer) (push) Failing after 1m3s
CI / Web (shl) (push) Failing after 11s
Core-Schnittstellen-Vertragstests / contract-tests (pull_request) Successful in 13m12s
CI / DMS-Modul (Lint/Build/Test/Coverage/Artefakt) (pull_request) Canceled after 0s
CI / DMS-Modul (Lint/Build/Test/Coverage/Artefakt) (push) Canceled after 0s
CI / Web-Barrierefreiheitspruefung (axe, Tastatur, Reflow, Textabstaende) (push) Canceled after 0s
DB-Integrationstests / db-tests (pull_request) Canceled after 0s
Supply-Chain-Scan (Go) / govulncheck (push) Canceled after 0s
Supply-Chain-Scan (Go) / govulncheck (pull_request) Canceled after 0s
Supply-Chain-Scan (npm) / npm-audit (pull_request) Canceled after 0s
Core-Pflichttest-Gate / pflichttest-gate (pull_request) Canceled after 0s
RBAC-Rollenhierarchie-Pflichtgate / rbac-policy-invariant (pull_request) Canceled after 0s
Core-Schnittstellen-Vertragstests / contract-tests (push) Successful in 12m23s
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
181 lines
5.7 KiB
Go
181 lines
5.7 KiB
Go
package migrationtenant
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"gitea.perlbach24.de/scripte/nexarch/internal/tenant"
|
|
"gitea.perlbach24.de/scripte/nexarch/internal/testdb"
|
|
)
|
|
|
|
// Integrationstest gegen eine echte Postgres-Instanz mit synthetischen
|
|
// Tenant-Map-Eintraegen (fiktive Slugs/Namen, keine echten archivmail-/
|
|
// archivdms-Kundendaten) — siehe Prüfprotokoll COR-01 fuer die
|
|
// dokumentierte Begruendung. Ohne TEST_ADMIN_DSN uebersprungen.
|
|
func setupSchema(t *testing.T, ctx context.Context, pool *pgxpool.Pool) {
|
|
t.Helper()
|
|
if _, err := pool.Exec(ctx, `
|
|
CREATE EXTENSION IF NOT EXISTS pgcrypto;
|
|
CREATE TABLE IF NOT EXISTS tenants (
|
|
id UUID PRIMARY KEY,
|
|
slug TEXT NOT NULL UNIQUE,
|
|
name TEXT NOT NULL,
|
|
db_name TEXT NOT NULL UNIQUE,
|
|
db_dsn TEXT NOT NULL,
|
|
status TEXT NOT NULL DEFAULT 'active',
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
previous_status TEXT,
|
|
deletion_scheduled_at TIMESTAMPTZ,
|
|
retention_block_reason TEXT,
|
|
retention_checked_at TIMESTAMPTZ
|
|
);
|
|
CREATE TABLE IF NOT EXISTS tenant_migration_map (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
archivmail_tenant_id BIGINT,
|
|
archivdms_tenant_id BIGINT,
|
|
nexarch_tenant_id UUID NOT NULL UNIQUE,
|
|
slug TEXT NOT NULL,
|
|
name TEXT NOT NULL,
|
|
approved_by TEXT,
|
|
approved_at TIMESTAMPTZ,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
CONSTRAINT tenant_migration_map_source_present
|
|
CHECK (archivmail_tenant_id IS NOT NULL OR archivdms_tenant_id IS NOT NULL)
|
|
);
|
|
`); err != nil {
|
|
t.Fatalf("schema: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestMigrateApproved(t *testing.T) {
|
|
adminDSN := os.Getenv("TEST_ADMIN_DSN")
|
|
if adminDSN == "" {
|
|
t.Skip("TEST_ADMIN_DSN nicht gesetzt, Integrationstest uebersprungen")
|
|
}
|
|
ctx := context.Background()
|
|
|
|
adminPool, err := pgxpool.New(ctx, adminDSN)
|
|
if err != nil {
|
|
t.Fatalf("admin pool: %v", err)
|
|
}
|
|
defer adminPool.Close()
|
|
|
|
registryPool := testdb.Pool(t, adminDSN)
|
|
defer registryPool.Close()
|
|
setupSchema(t, ctx, registryPool)
|
|
|
|
dsnTemplate := strings.Replace(adminDSN, "/postgres?", "/%s?", 1)
|
|
registry := tenant.NewRegistry(registryPool)
|
|
provisioner := tenant.NewProvisioner(adminPool, registry, dsnTemplate, dsnTemplate)
|
|
mapStore := NewMapStore(registryPool)
|
|
migrator := NewMigrator(mapStore, provisioner)
|
|
|
|
// Fuenf synthetische, freigegebene Tenant-Zusammenfuehrungen und ein
|
|
// bewusst NICHT freigegebener Eintrag (Akzeptanzkriterium 2).
|
|
type synthEntry struct {
|
|
id string
|
|
slug string
|
|
name string
|
|
approved bool
|
|
}
|
|
suffix := testdb.Suffix()
|
|
entries := make([]synthEntry, 0, 6)
|
|
for i := 1; i <= 5; i++ {
|
|
entries = append(entries, synthEntry{
|
|
id: newUUIDv4(),
|
|
slug: testdb.Slug("synth"),
|
|
name: "Synthetische Test GmbH " + suffix,
|
|
approved: true,
|
|
})
|
|
}
|
|
unapproved := synthEntry{id: newUUIDv4(), slug: testdb.Slug("unapp"), name: "Unfreigegeben " + suffix, approved: false}
|
|
entries = append(entries, unapproved)
|
|
|
|
for i, e := range entries {
|
|
approvedAt := "NULL"
|
|
approvedBy := "NULL"
|
|
if e.approved {
|
|
approvedAt = "now()"
|
|
approvedBy = "'qa-tester'"
|
|
}
|
|
_, err := registryPool.Exec(ctx, `
|
|
INSERT INTO tenant_migration_map (archivmail_tenant_id, archivdms_tenant_id, nexarch_tenant_id, slug, name, approved_by, approved_at)
|
|
VALUES ($1, NULL, $2, $3, $4, `+approvedBy+`, `+approvedAt+`)
|
|
`, int64(1000+i), e.id, e.slug, e.name)
|
|
if err != nil {
|
|
t.Fatalf("map-eintrag %d anlegen: %v", i, err)
|
|
}
|
|
}
|
|
|
|
t.Cleanup(func() {
|
|
for _, e := range entries {
|
|
_, _ = registryPool.Exec(ctx, `DELETE FROM tenants WHERE slug = $1`, e.slug)
|
|
_, _ = adminPool.Exec(ctx, `DROP DATABASE IF EXISTS tenant_`+e.slug)
|
|
}
|
|
})
|
|
|
|
// AC 1: genau ein Lauf uebernimmt jeden freigegebenen Tenant.
|
|
res, err := migrator.MigrateApproved(ctx)
|
|
if err != nil {
|
|
t.Fatalf("MigrateApproved: %v", err)
|
|
}
|
|
if len(res.Migrated) != 5 {
|
|
t.Fatalf("erwarte 5 migrierte Tenants, habe %d", len(res.Migrated))
|
|
}
|
|
|
|
// AC 2: der unfreigegebene Eintrag wurde nicht uebernommen.
|
|
if _, err := registry.GetByID(ctx, unapproved.id); err != tenant.ErrTenantNotFound {
|
|
t.Fatalf("unfreigegebener tenant %q haette nicht angelegt werden duerfen, err=%v", unapproved.id, err)
|
|
}
|
|
|
|
// Stichprobe: tenant_id in Core entspricht exakt der Map-Vorgabe.
|
|
for _, e := range entries[:5] {
|
|
got, err := registry.GetByID(ctx, e.id)
|
|
if err != nil {
|
|
t.Fatalf("tenant %q nach migration nicht gefunden: %v", e.id, err)
|
|
}
|
|
if got.ID != e.id || got.Slug != e.slug {
|
|
t.Fatalf("tenant_id nicht korrekt umgeschrieben: erwartet id=%s slug=%s, habe id=%s slug=%s", e.id, e.slug, got.ID, got.Slug)
|
|
}
|
|
}
|
|
|
|
// AC 3: erneuter Lauf gegen dieselbe (bereits migrierte) Quelle erzeugt
|
|
// keine Duplikate.
|
|
res2, err := migrator.MigrateApproved(ctx)
|
|
if err != nil {
|
|
t.Fatalf("zweiter MigrateApproved-Lauf: %v", err)
|
|
}
|
|
if len(res2.Migrated) != 5 {
|
|
t.Fatalf("erwarte weiterhin 5 Tenants nach erneutem Lauf, habe %d", len(res2.Migrated))
|
|
}
|
|
for _, e := range entries[:5] {
|
|
var count int
|
|
if err := registryPool.QueryRow(ctx, `SELECT count(*) FROM tenants WHERE id = $1`, e.id).Scan(&count); err != nil {
|
|
t.Fatalf("duplikat-pruefung: %v", err)
|
|
}
|
|
if count != 1 {
|
|
t.Fatalf("tenant %q ist nach erneutem Lauf %d mal vorhanden, erwartet 1", e.id, count)
|
|
}
|
|
}
|
|
}
|
|
|
|
// newUUIDv4 erzeugt eine zufaellige UUIDv4 fuer synthetische Testdaten, ohne
|
|
// eine zusaetzliche Abhaengigkeit einzufuehren (Postgres selbst nutzt
|
|
// gen_random_uuid() in Produktionscode ueber ProvisionWithID / die
|
|
// tenant_migration_map).
|
|
func newUUIDv4() string {
|
|
b := make([]byte, 16)
|
|
if _, err := rand.Read(b); err != nil {
|
|
panic(err)
|
|
}
|
|
b[6] = (b[6] & 0x0f) | 0x40
|
|
b[8] = (b[8] & 0x3f) | 0x80
|
|
return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:16])
|
|
}
|