ci: expand CI checks and make consumer sync opt-in
- Add Go, npm, Python, and Java validation to CI - Run Go tests against PostgreSQL - Record performance fixes in the report
This commit is contained in:
1 parent
199a5efaef
commit
22887fef03
100 files changed
+9740
-429
No files matched your search
@@ -2,10 +2,13 @@ package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"repos.gfranz.ch/Gabriel20xx/shared/backend/postgres"
|
||||
)
|
||||
|
||||
@@ -119,3 +122,135 @@ func TestStoreIntegration(t *testing.T) {
|
||||
t.Fatalf("FindByID() after UpdateLastLogin = %+v, want LastLoginAt set", afterLogin)
|
||||
}
|
||||
}
|
||||
|
||||
// openScratchStore opens DATABASE_URL (a scratch database, see TestStoreIntegration), applies the
|
||||
// schema and removes every account before and after the test.
|
||||
func openScratchStore(t *testing.T) (context.Context, *pgxpool.Pool, *Store) {
|
||||
t.Helper()
|
||||
url := os.Getenv("DATABASE_URL")
|
||||
if url == "" {
|
||||
t.Skip("DATABASE_URL not set; skipping Postgres integration test")
|
||||
}
|
||||
ctx := context.Background()
|
||||
pool, err := postgres.Open(ctx, postgres.DefaultConfig(url))
|
||||
if err != nil {
|
||||
t.Fatalf("open pool: %v", err)
|
||||
}
|
||||
t.Cleanup(pool.Close)
|
||||
if err := EnsureSchema(ctx, pool, SchemaOptions{SingleUser: false}); err != nil {
|
||||
t.Fatalf("EnsureSchema() error = %v", err)
|
||||
}
|
||||
store := NewStore(pool)
|
||||
if err := store.DeleteAllUsers(ctx); err != nil {
|
||||
t.Fatalf("DeleteAllUsers() (pre-test cleanup) error = %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if err := store.DeleteAllUsers(context.Background()); err != nil {
|
||||
t.Logf("cleanup: DeleteAllUsers() error = %v", err)
|
||||
}
|
||||
})
|
||||
return ctx, pool, store
|
||||
}
|
||||
|
||||
// TestStoreRefusesUsernamesThatDifferOnlyByCase covers the unique index on lower(username).
|
||||
func TestStoreRefusesUsernamesThatDifferOnlyByCase(t *testing.T) {
|
||||
ctx, _, store := openScratchStore(t)
|
||||
hash, err := HashPassword("correct horse battery staple")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := store.CreateUser(ctx, "Admin", "", hash); err != nil {
|
||||
t.Fatalf("CreateUser(Admin) error = %v", err)
|
||||
}
|
||||
if _, err := store.CreateUser(ctx, "admin", "", hash); !errors.Is(err, ErrUserExists) {
|
||||
t.Fatalf("CreateUser(admin) error = %v, want ErrUserExists", err)
|
||||
}
|
||||
if _, err := store.CreateManagedUser(ctx, "ADMIN", "", hash, RoleUser); !errors.Is(err, ErrUserExists) {
|
||||
t.Fatalf("CreateManagedUser(ADMIN) error = %v, want ErrUserExists", err)
|
||||
}
|
||||
bob, err := store.CreateUser(ctx, "bob", "", hash)
|
||||
if err != nil {
|
||||
t.Fatalf("CreateUser(bob) error = %v", err)
|
||||
}
|
||||
if err := store.UpdateCredentials(ctx, bob.ID, "admin", "", hash); !errors.Is(err, ErrUserExists) {
|
||||
t.Fatalf("UpdateCredentials(admin) error = %v, want ErrUserExists", err)
|
||||
}
|
||||
role := RoleUser
|
||||
if err := store.UpdateUser(ctx, bob.ID, "Admin", "", hash, &role); !errors.Is(err, ErrUserExists) {
|
||||
t.Fatalf("UpdateUser(Admin) error = %v, want ErrUserExists", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestEnsureSchemaStartsWithExistingCaseVariantUsernames checks that a database already holding
|
||||
// "Admin" and "admin" still starts, gets no index, and gets one after the pair is resolved.
|
||||
func TestEnsureSchemaStartsWithExistingCaseVariantUsernames(t *testing.T) {
|
||||
ctx, pool, store := openScratchStore(t)
|
||||
if _, err := pool.Exec(ctx, `DROP INDEX IF EXISTS auth_users_username_lower_idx`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO auth_users (username, password_hash) VALUES ('Admin', 'x'), ('admin', 'x')`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
indexExists := func() bool {
|
||||
var exists bool
|
||||
if err := pool.QueryRow(ctx, `SELECT EXISTS (SELECT 1 FROM pg_indexes WHERE indexname = 'auth_users_username_lower_idx')`).Scan(&exists); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return exists
|
||||
}
|
||||
if err := EnsureSchema(ctx, pool, SchemaOptions{SingleUser: false}); err != nil {
|
||||
t.Fatalf("EnsureSchema() with clashing usernames error = %v, want it to start", err)
|
||||
}
|
||||
if indexExists() {
|
||||
t.Fatal("the case-insensitive index was created over clashing usernames")
|
||||
}
|
||||
if err := store.DeleteAllUsers(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := EnsureSchema(ctx, pool, SchemaOptions{SingleUser: false}); err != nil {
|
||||
t.Fatalf("EnsureSchema() after cleanup error = %v", err)
|
||||
}
|
||||
if !indexExists() {
|
||||
t.Fatal("the case-insensitive index was not created once the clash was gone")
|
||||
}
|
||||
}
|
||||
|
||||
// TestStoreTOTPAndIdentifierPrimitives covers SetTOTPSecret refusing to replace an enabled second
|
||||
// factor and FindByIdentifier trying usernames before emails.
|
||||
func TestStoreTOTPAndIdentifierPrimitives(t *testing.T) {
|
||||
ctx, pool, store := openScratchStore(t)
|
||||
hash, err := HashPassword("correct horse battery staple")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
owner, err := store.CreateUser(ctx, "owner", "owner@example.com", hash)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := store.SetTOTPSecret(ctx, owner.ID, "JBSWY3DPEHPK3PXP"); err != nil {
|
||||
t.Fatalf("SetTOTPSecret() while disabled error = %v", err)
|
||||
}
|
||||
if err := store.EnableTOTP(ctx, owner.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := store.SetTOTPSecret(ctx, owner.ID, "OTHERSECRET"); !errors.Is(err, ErrTOTPAlreadyEnabled) {
|
||||
t.Fatalf("SetTOTPSecret() while enabled error = %v, want ErrTOTPAlreadyEnabled", err)
|
||||
}
|
||||
if state, err := store.GetTOTPState(ctx, owner.ID); err != nil || !state.Enabled || state.Secret != "JBSWY3DPEHPK3PXP" {
|
||||
t.Fatalf("TOTP state after a refused replace = %+v, %v; want the original secret still enabled", state, err)
|
||||
}
|
||||
|
||||
// A username equal to another account's email (a row written outside the store's validation)
|
||||
// resolves to the username's own account, not to the email's owner.
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO auth_users (username, email, password_hash) VALUES ('owner@example.com', 'second@example.com', 'x')`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
found, err := store.FindByIdentifier(ctx, "owner@example.com")
|
||||
if err != nil || found == nil || found.Username != "owner@example.com" {
|
||||
t.Fatalf("FindByIdentifier() = %+v, %v; want the account whose username matches", found, err)
|
||||
}
|
||||
|
||||
if err := store.UpdateCredentials(ctx, owner.ID, "owner", "owner@example.com", ""); !errors.Is(err, ErrEmptyPasswordHash) {
|
||||
t.Fatalf("UpdateCredentials() with an empty hash error = %v, want ErrEmptyPasswordHash", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user