|
| 1 | +package sql |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/google/uuid" |
| 13 | + "github.com/rs/zerolog" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | + |
| 17 | + "github.com/authorizerdev/authorizer/internal/config" |
| 18 | + "github.com/authorizerdev/authorizer/internal/constants" |
| 19 | + "github.com/authorizerdev/authorizer/internal/refs" |
| 20 | + "github.com/authorizerdev/authorizer/internal/storage/schemas" |
| 21 | +) |
| 22 | + |
| 23 | +func sqlTestDeps(t *testing.T) *Dependencies { |
| 24 | + t.Helper() |
| 25 | + logger := zerolog.New(zerolog.NewTestWriter(t)).With().Timestamp().Logger() |
| 26 | + return &Dependencies{Log: &logger} |
| 27 | +} |
| 28 | + |
| 29 | +// sqlMigrationTestDBTypes returns the SQL backends to exercise. Honors TEST_DBS |
| 30 | +// (comma-separated); defaults to sqlite so local runs stay light. CI sets |
| 31 | +// TEST_DBS=postgres to cover the engine where the original failure was reported. |
| 32 | +func sqlMigrationTestDBTypes() []string { |
| 33 | + supported := map[string]bool{ |
| 34 | + constants.DbTypePostgres: true, |
| 35 | + constants.DbTypeSqlite: true, |
| 36 | + constants.DbTypeMysql: true, |
| 37 | + constants.DbTypeMariaDB: true, |
| 38 | + } |
| 39 | + env := os.Getenv("TEST_DBS") |
| 40 | + if env == "" { |
| 41 | + return []string{constants.DbTypeSqlite} |
| 42 | + } |
| 43 | + var out []string |
| 44 | + for _, p := range strings.Split(env, ",") { |
| 45 | + p = strings.TrimSpace(p) |
| 46 | + if supported[p] { |
| 47 | + out = append(out, p) |
| 48 | + } |
| 49 | + } |
| 50 | + return out |
| 51 | +} |
| 52 | + |
| 53 | +// sqlMigrationTestConfig builds a config for dbType, skipping the test when the |
| 54 | +// backend is not reachable in this environment. |
| 55 | +func sqlMigrationTestConfig(t *testing.T, dbType string) *config.Config { |
| 56 | + t.Helper() |
| 57 | + cfg := &config.Config{ |
| 58 | + DatabaseType: dbType, |
| 59 | + DatabaseName: "authorizer_test", |
| 60 | + DefaultRoles: []string{"user"}, |
| 61 | + } |
| 62 | + switch dbType { |
| 63 | + case constants.DbTypeSqlite: |
| 64 | + cfg.DatabaseURL = filepath.Join(t.TempDir(), "migration_test.db") |
| 65 | + case constants.DbTypePostgres: |
| 66 | + cfg.DatabaseURL = "postgres://postgres:postgres@localhost:5434/postgres" |
| 67 | + skipIfTCPClosed(t, "localhost:5434") |
| 68 | + case constants.DbTypeMysql, constants.DbTypeMariaDB: |
| 69 | + cfg.DatabaseURL = "root:password@tcp(localhost:3306)/authorizer_test" |
| 70 | + skipIfTCPClosed(t, "localhost:3306") |
| 71 | + default: |
| 72 | + t.Skipf("unsupported SQL db type for migration test: %s", dbType) |
| 73 | + } |
| 74 | + return cfg |
| 75 | +} |
| 76 | + |
| 77 | +func skipIfTCPClosed(t *testing.T, addr string) { |
| 78 | + t.Helper() |
| 79 | + conn, err := net.DialTimeout("tcp", addr, 2*time.Second) |
| 80 | + if err != nil { |
| 81 | + t.Skipf("skipping: %s not reachable: %v", addr, err) |
| 82 | + } |
| 83 | + _ = conn.Close() |
| 84 | +} |
| 85 | + |
| 86 | +// TestProviderEmailPhoneUpdatesAndUniqueness covers v2's application-layer |
| 87 | +// uniqueness for users and OTPs (email and phone_number are non-unique indexes |
| 88 | +// in the DB). It asserts that a user can update their own email/phone number |
| 89 | +// seamlessly, that duplicates across different users are still rejected, and |
| 90 | +// that OTPs key independently on email and phone. |
| 91 | +func TestProviderEmailPhoneUpdatesAndUniqueness(t *testing.T) { |
| 92 | + for _, dbType := range sqlMigrationTestDBTypes() { |
| 93 | + t.Run(dbType, func(t *testing.T) { |
| 94 | + cfg := sqlMigrationTestConfig(t, dbType) |
| 95 | + p, err := NewProvider(cfg, sqlTestDeps(t)) |
| 96 | + require.NoError(t, err) |
| 97 | + require.NotNil(t, p) |
| 98 | + ctx := context.Background() |
| 99 | + |
| 100 | + uniq := strings.ReplaceAll(uuid.New().String(), "-", "") |
| 101 | + emailA := "a_" + uniq + "@test.com" |
| 102 | + phoneA := "+1100" + uniq[:9] |
| 103 | + |
| 104 | + userA := &schemas.User{ |
| 105 | + ID: uuid.New().String(), |
| 106 | + Email: refs.NewStringRef(emailA), |
| 107 | + PhoneNumber: refs.NewStringRef(phoneA), |
| 108 | + SignupMethods: "basic_auth", |
| 109 | + } |
| 110 | + userA, err = p.AddUser(ctx, userA) |
| 111 | + require.NoError(t, err) |
| 112 | + |
| 113 | + // Seamless self-update: change A's phone number to a new value. |
| 114 | + newPhoneA := "+1900" + uniq[:9] |
| 115 | + userA.PhoneNumber = refs.NewStringRef(newPhoneA) |
| 116 | + _, err = p.UpdateUser(ctx, userA) |
| 117 | + require.NoError(t, err, "updating own phone number should succeed") |
| 118 | + gotByPhone, err := p.GetUserByPhoneNumber(ctx, newPhoneA) |
| 119 | + require.NoError(t, err) |
| 120 | + assert.Equal(t, userA.ID, gotByPhone.ID) |
| 121 | + |
| 122 | + // Seamless self-update: change A's email, then re-save (idempotent). |
| 123 | + newEmailA := "a2_" + uniq + "@test.com" |
| 124 | + userA.Email = refs.NewStringRef(newEmailA) |
| 125 | + _, err = p.UpdateUser(ctx, userA) |
| 126 | + require.NoError(t, err, "updating own email should succeed") |
| 127 | + _, err = p.UpdateUser(ctx, userA) |
| 128 | + require.NoError(t, err, "re-saving the same user should succeed") |
| 129 | + gotByEmail, err := p.GetUserByEmail(ctx, newEmailA) |
| 130 | + require.NoError(t, err) |
| 131 | + assert.Equal(t, userA.ID, gotByEmail.ID) |
| 132 | + |
| 133 | + // A different user cannot claim A's email or phone via AddUser. |
| 134 | + _, err = p.AddUser(ctx, &schemas.User{ |
| 135 | + ID: uuid.New().String(), Email: refs.NewStringRef(newEmailA), SignupMethods: "basic_auth", |
| 136 | + }) |
| 137 | + assert.Error(t, err, "duplicate email should be rejected") |
| 138 | + _, err = p.AddUser(ctx, &schemas.User{ |
| 139 | + ID: uuid.New().String(), PhoneNumber: refs.NewStringRef(newPhoneA), SignupMethods: "basic_auth", |
| 140 | + }) |
| 141 | + assert.Error(t, err, "duplicate phone should be rejected") |
| 142 | + |
| 143 | + // OTPs key on email and phone independently; upsert by the same email |
| 144 | + // updates in place. |
| 145 | + otpEmail := &schemas.OTP{Email: newEmailA, Otp: "111111", ExpiresAt: time.Now().Add(5 * time.Minute).Unix()} |
| 146 | + _, err = p.UpsertOTP(ctx, otpEmail) |
| 147 | + require.NoError(t, err) |
| 148 | + otpEmail.Otp = "222222" |
| 149 | + _, err = p.UpsertOTP(ctx, otpEmail) |
| 150 | + require.NoError(t, err) |
| 151 | + fetchedOTP, err := p.GetOTPByEmail(ctx, newEmailA) |
| 152 | + require.NoError(t, err) |
| 153 | + assert.Equal(t, "222222", fetchedOTP.Otp) |
| 154 | + |
| 155 | + otpPhone := &schemas.OTP{PhoneNumber: newPhoneA, Otp: "333333", ExpiresAt: time.Now().Add(5 * time.Minute).Unix()} |
| 156 | + _, err = p.UpsertOTP(ctx, otpPhone) |
| 157 | + require.NoError(t, err) |
| 158 | + fetchedPhoneOTP, err := p.GetOTPByPhoneNumber(ctx, newPhoneA) |
| 159 | + require.NoError(t, err) |
| 160 | + assert.Equal(t, "333333", fetchedPhoneOTP.Otp) |
| 161 | + |
| 162 | + // Cleanup. |
| 163 | + _ = p.DeleteOTP(ctx, fetchedOTP) |
| 164 | + _ = p.DeleteOTP(ctx, fetchedPhoneOTP) |
| 165 | + _ = p.DeleteUser(ctx, userA) |
| 166 | + }) |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +// TestStaleUniqueConstraintMigration reproduces an upgraded v1 database whose |
| 171 | +// email/phone columns still carry UNIQUE objects, and asserts the provider |
| 172 | +// clears them on startup instead of aborting with SQLSTATE 42704 |
| 173 | +// (regression introduced by the GORM 1.25.10 bump in 2.3.0-rc.1). Postgres only: |
| 174 | +// it has named UNIQUE constraints and standalone unique indexes with stable |
| 175 | +// DDL; sqlite rebuilds tables and has no comparable failure mode. |
| 176 | +func TestStaleUniqueConstraintMigration(t *testing.T) { |
| 177 | + for _, dbType := range sqlMigrationTestDBTypes() { |
| 178 | + if dbType != constants.DbTypePostgres { |
| 179 | + continue |
| 180 | + } |
| 181 | + t.Run(dbType, func(t *testing.T) { |
| 182 | + cfg := sqlMigrationTestConfig(t, dbType) |
| 183 | + deps := sqlTestDeps(t) |
| 184 | + ctx := context.Background() |
| 185 | + |
| 186 | + // First boot creates the v2 schema (non-unique indexes). |
| 187 | + p1, err := NewProvider(cfg, deps) |
| 188 | + require.NoError(t, err) |
| 189 | + |
| 190 | + // Simulate a v1 database: a UNIQUE CONSTRAINT on users.email and a |
| 191 | + // standalone UNIQUE INDEX on otps.phone_number — the two real-world |
| 192 | + // forms reported in the field. Idempotent so reruns against the shared |
| 193 | + // test DB don't fail on pre-existing objects. |
| 194 | + require.NoError(t, p1.db.Exec(`ALTER TABLE authorizer_users DROP CONSTRAINT IF EXISTS authorizer_users_email_key`).Error) |
| 195 | + require.NoError(t, p1.db.Exec(`ALTER TABLE authorizer_users ADD CONSTRAINT authorizer_users_email_key UNIQUE (email)`).Error) |
| 196 | + require.NoError(t, p1.db.Exec(`DROP INDEX IF EXISTS idx_authorizer_otps_phone_number`).Error) |
| 197 | + require.NoError(t, p1.db.Exec(`CREATE UNIQUE INDEX idx_authorizer_otps_phone_number ON authorizer_otps (phone_number)`).Error) |
| 198 | + require.True(t, p1.db.Migrator().HasConstraint(&schemas.User{}, "authorizer_users_email_key"), |
| 199 | + "precondition: stale unique constraint seeded") |
| 200 | + |
| 201 | + // Second boot must clear the stale uniqueness and complete migration. |
| 202 | + p2, err := NewProvider(cfg, deps) |
| 203 | + require.NoError(t, err, "startup must not abort with SQLSTATE 42704") |
| 204 | + |
| 205 | + assert.False(t, p2.db.Migrator().HasConstraint(&schemas.User{}, "authorizer_users_email_key"), |
| 206 | + "stale unique constraint should be dropped") |
| 207 | + |
| 208 | + // The search index is preserved, just no longer unique: AutoMigrate |
| 209 | + // recreates idx_authorizer_otps_phone_number as a non-unique index. |
| 210 | + indexes, err := p2.db.Migrator().GetIndexes(&schemas.OTP{}) |
| 211 | + require.NoError(t, err) |
| 212 | + var phoneIdx string |
| 213 | + for _, idx := range indexes { |
| 214 | + if cols := idx.Columns(); len(cols) == 1 && cols[0] == "phone_number" { |
| 215 | + phoneIdx = idx.Name() |
| 216 | + if unique, ok := idx.Unique(); ok { |
| 217 | + assert.False(t, unique, "otps.phone_number index should no longer be unique") |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | + assert.NotEmpty(t, phoneIdx, "non-unique search index on otps.phone_number should still exist") |
| 222 | + |
| 223 | + // After migrating a legacy DB, a user can still update their phone |
| 224 | + // number seamlessly (no stale constraint in the way). |
| 225 | + uniq := strings.ReplaceAll(uuid.New().String(), "-", "") |
| 226 | + u := &schemas.User{ |
| 227 | + ID: uuid.New().String(), |
| 228 | + Email: refs.NewStringRef("m_" + uniq + "@test.com"), |
| 229 | + PhoneNumber: refs.NewStringRef("+1700" + uniq[:9]), |
| 230 | + SignupMethods: "basic_auth", |
| 231 | + } |
| 232 | + u, err = p2.AddUser(ctx, u) |
| 233 | + require.NoError(t, err) |
| 234 | + u.PhoneNumber = refs.NewStringRef("+1600" + uniq[:9]) |
| 235 | + _, err = p2.UpdateUser(ctx, u) |
| 236 | + require.NoError(t, err, "updating phone after migration should succeed") |
| 237 | + _ = p2.DeleteUser(ctx, u) |
| 238 | + }) |
| 239 | + } |
| 240 | +} |
0 commit comments