feat(identity): add server sessions and audited mail recovery

This commit is contained in:
TakuyaYagam1 committed 2026-10-07 11:03:46 +03:00
1 parent 20c03288d6
commit 4ee53d9a74
58 files changed
+5359 -567

No files matched your search

@@ -8,9 +8,15 @@ import (
"time"
"github.com/jackc/pgx/v5/pgxpool"
postgresadapter "lincodex/backend/internal/adapter/secondary/postgres"
)
const registrationConsentMigrationName = "00016_bind_registration_consent.sql"
const (
registrationConsentMigrationVersion = 16
registrationConsentMigrationName = "00016_bind_registration_consent.sql"
mailRetryMigrationName = "00017_retry_unknown_mail.sql"
)
func TestRegistrationConsentMigration(t *testing.T) {
env := bootstrap(t, postgresService)
@@ -24,10 +30,7 @@ func TestRegistrationConsentMigration(t *testing.T) {
if _, err := migrator.Up(ctx); err != nil {
t.Fatalf("apply migrations before preparing legacy data: %v", err)
}
rolledBack, err := migrator.DownOne(ctx)
if err != nil || rolledBack.Name != registrationConsentMigrationName {
t.Fatalf("roll back the registration-consent migration = %+v, %v", rolledBack, err)
}
rollbackThroughRegistrationConsent(ctx, t, migrator)
assertRegistrationConsentColumn(ctx, t, pool, false)
const userID = "018f0000-0000-7000-8000-000000000001"
@@ -41,8 +44,8 @@ func TestRegistrationConsentMigration(t *testing.T) {
seedRegistrationConsentFixture(ctx, t, pool, userID, createdAt, registrationAt, registrationConsentID, settingsConsentID, otherConsentID, c1SettingsVersion, settingsAt)
applied, err := migrator.Up(ctx)
if err != nil || len(applied) != 1 || applied[0].Name != registrationConsentMigrationName {
t.Fatalf("apply registration-consent migration = %+v, %v", applied, err)
if err != nil || len(applied) < 2 || applied[0].Name != registrationConsentMigrationName || applied[1].Name != mailRetryMigrationName {
t.Fatalf("apply registration-consent and later migrations = %+v, %v", applied, err)
}
assertConsentRegistrationVersion(ctx, t, pool, registrationConsentID, 2)
assertConsentRegistrationVersionNull(ctx, t, pool, settingsConsentID, otherConsentID)
@@ -89,17 +92,14 @@ func TestRegistrationConsentMigration(t *testing.T) {
t.Fatal("a refused registration C1 version change altered its version or revocation state")
}
rolledBack, err = migrator.DownOne(ctx)
if err != nil || rolledBack.Name != registrationConsentMigrationName {
t.Fatalf("roll back registration-consent migration = %+v, %v", rolledBack, err)
}
rollbackThroughRegistrationConsent(ctx, t, migrator)
assertRegistrationConsentColumn(ctx, t, pool, false)
if countConsentRecords(ctx, t, pool) != beforeMismatch {
t.Fatal("rolling back the registration-consent migration changed the consent ledger")
}
applied, err = migrator.Up(ctx)
if err != nil || len(applied) != 1 || applied[0].Name != registrationConsentMigrationName {
t.Fatalf("reapply registration-consent migration = %+v, %v", applied, err)
if err != nil || len(applied) < 2 || applied[0].Name != registrationConsentMigrationName || applied[1].Name != mailRetryMigrationName {
t.Fatalf("reapply registration-consent and later migrations = %+v, %v", applied, err)
}
assertRegistrationConsentColumn(ctx, t, pool, true)
assertConsentRegistrationVersion(ctx, t, pool, compatibilityConsentID, 3)
@@ -113,10 +113,7 @@ func TestRegistrationConsentMigration(t *testing.T) {
if _, err := migrator.Up(ctx); err != nil {
t.Fatalf("apply migrations before preparing stale legacy data: %v", err)
}
rolledBack, err := migrator.DownOne(ctx)
if err != nil || rolledBack.Name != registrationConsentMigrationName {
t.Fatalf("roll back the registration-consent migration = %+v, %v", rolledBack, err)
}
rollbackThroughRegistrationConsent(ctx, t, migrator)
assertRegistrationConsentColumn(ctx, t, pool, false)
const userID = "018f0000-0000-7000-8000-000000000101"
@@ -147,14 +144,36 @@ func TestRegistrationConsentMigration(t *testing.T) {
if _, err := pool.Exec(ctx, `UPDATE users SET last_registered_at = $2, updated_at = $2 WHERE id = $1::uuid`, userID, grantAt); err != nil {
t.Fatalf("correct the synthetic legacy registration timestamp: %v", err)
}
if _, err := migrator.Up(ctx); err != nil {
t.Fatalf("migration did not succeed after the legacy data was corrected: %v", err)
applied, err := migrator.Up(ctx)
if err != nil || len(applied) < 2 || applied[0].Name != registrationConsentMigrationName || applied[1].Name != mailRetryMigrationName {
t.Fatalf("migrations did not succeed after the legacy data was corrected: %+v, %v", applied, err)
}
assertRegistrationConsentColumn(ctx, t, pool, true)
assertConsentRegistrationVersion(ctx, t, pool, consentID, 1)
})
}
// rollbackThroughRegistrationConsent removes later migrations as well, so
// each fixture is truly at the schema immediately before migration 00016.
func rollbackThroughRegistrationConsent(ctx context.Context, t *testing.T, migrator *postgresadapter.Migrator) {
t.Helper()
for {
rolledBack, err := migrator.DownOne(ctx)
if err != nil {
t.Fatalf("roll back through registration-consent migration: %v", err)
}
if rolledBack.Version < registrationConsentMigrationVersion {
t.Fatalf("rolled back migration %d before reaching migration %d", rolledBack.Version, registrationConsentMigrationVersion)
}
if rolledBack.Version == registrationConsentMigrationVersion {
if rolledBack.Name != registrationConsentMigrationName {
t.Fatalf("rolled back migration %d named %q, want %q", rolledBack.Version, rolledBack.Name, registrationConsentMigrationName)
}
return
}
}
}
func seedRegistrationConsentFixture(
ctx context.Context,
t *testing.T,