feat(identity): store users, email tokens, and sessions

This commit is contained in:
shira committed 2026-10-06 00:15:22 +03:00
1 parent 15e2e50561
commit e7d0d92ef9
30 files changed
+4792 -32

No files matched your search

File diff suppressed because it is too large. Load diff
@@ -7,8 +7,10 @@ import (
"crypto/rand"
"io/fs"
"net/url"
"strconv"
"strings"
"testing"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
@@ -95,6 +97,23 @@ func (env *testEnvironment) newPoolWithMaxConns(ctx context.Context, t *testing.
return pool
}
// newPoolWithLockTimeout opens a pool whose sessions fail a lock wait after
// timeout, so a test can prove that a statement never waits for a lock.
func (env *testEnvironment) newPoolWithLockTimeout(ctx context.Context, t *testing.T, timeout time.Duration) *pgxpool.Pool {
t.Helper()
poolConfig, err := pgxpool.ParseConfig(env.Config.DatabaseURL)
if err != nil {
t.Fatalf("parse bootstrap database URL: %v", err)
}
poolConfig.ConnConfig.RuntimeParams["lock_timeout"] = strconv.FormatInt(timeout.Milliseconds(), 10)
pool, err := pgxpool.NewWithConfig(ctx, poolConfig)
if err != nil {
t.Fatalf("create PostgreSQL pool with a lock timeout: %v", err)
}
t.Cleanup(pool.Close)
return pool
}
// createIsolatedDatabase creates an empty database in the bootstrap container
// for tests that must change the whole schema, and drops it with t.
func (env *testEnvironment) createIsolatedDatabase(ctx context.Context, t *testing.T) string {
@@ -19,6 +19,15 @@ const (
concurrentMigrators = 4
)
// identityMigrationVersion creates identityTableNames.
const identityMigrationVersion = 2
var (
// Tables created by 00001 and by 00002.
outboxTables = []string{"outbox_events"}
identityTableNames = []string{"users", "email_tokens", "sessions"}
)
func TestPostgresMigrations(t *testing.T) {
env := bootstrap(t, postgresService)
ctx, cancel := context.WithTimeout(t.Context(), migrationTestTimeout)
@@ -41,10 +50,12 @@ func TestPostgresMigrations(t *testing.T) {
if len(applied) != total {
t.Fatalf("first Up() applied %d migrations, want %d", len(applied), total)
}
assertOutboxTableExists(ctx, t, pool, true)
assertTablesExist(ctx, t, pool, true, outboxTables...)
assertTablesExist(ctx, t, pool, true, identityTableNames...)
identityRolledBack := false
for rolledBack := 0; ; rolledBack++ {
_, err := migrator.DownOne(ctx)
result, err := migrator.DownOne(ctx)
if errors.Is(err, postgresadapter.ErrNoMigrationToRollBack) {
if rolledBack != total {
t.Fatalf("rolled back %d migrations, want %d", rolledBack, total)
@@ -54,8 +65,19 @@ func TestPostgresMigrations(t *testing.T) {
if err != nil {
t.Fatalf("DownOne() after %d rollbacks error = %v", rolledBack, err)
}
if result.Version == identityMigrationVersion {
// The identity migration rolls back on its own and leaves the
// outbox migration before it.
assertTablesExist(ctx, t, pool, false, identityTableNames...)
assertTablesExist(ctx, t, pool, true, outboxTables...)
identityRolledBack = true
}
}
assertOutboxTableExists(ctx, t, pool, false)
if !identityRolledBack {
t.Fatalf("migration %d was not rolled back", identityMigrationVersion)
}
assertTablesExist(ctx, t, pool, false, outboxTables...)
assertTablesExist(ctx, t, pool, false, identityTableNames...)
var leftovers int
err = pool.QueryRow(ctx, `
SELECT count(*) FROM pg_catalog.pg_class AS c
@@ -75,7 +97,8 @@ func TestPostgresMigrations(t *testing.T) {
if len(applied) != total {
t.Fatalf("second Up() applied %d migrations, want %d", len(applied), total)
}
assertOutboxTableExists(ctx, t, pool, true)
assertTablesExist(ctx, t, pool, true, outboxTables...)
assertTablesExist(ctx, t, pool, true, identityTableNames...)
assertAllApplied(ctx, t, migrator, total)
})
@@ -129,13 +152,15 @@ func assertAllApplied(ctx context.Context, t *testing.T, migrator *postgresadapt
}
}
func assertOutboxTableExists(ctx context.Context, t *testing.T, pool *pgxpool.Pool, want bool) {
func assertTablesExist(ctx context.Context, t *testing.T, pool *pgxpool.Pool, want bool, tables ...string) {
t.Helper()
var exists bool
if err := pool.QueryRow(ctx, `SELECT to_regclass('public.outbox_events') IS NOT NULL`).Scan(&exists); err != nil {
t.Fatalf("check outbox table: %v", err)
}
if exists != want {
t.Fatalf("outbox_events exists = %t, want %t", exists, want)
for _, table := range tables {
var exists bool
if err := pool.QueryRow(ctx, `SELECT to_regclass($1) IS NOT NULL`, "public."+table).Scan(&exists); err != nil {
t.Fatalf("check table %s: %v", table, err)
}
if exists != want {
t.Fatalf("%s exists = %t, want %t", table, exists, want)
}
}
}