package updates import ( "testing" "git.golem15.com/golem15/summercms/modules/lagoon" "github.com/go-gormigrate/gormigrate/v2" ) func TestExtendUsersMigration(t *testing.T) { sqlDB := dedicatedDB(t, "user_extend") gdb, err := lagoon.Use(t.Context(), sqlDB) if err != nil { t.Fatal(err) } m := gormigrate.New(gdb, &gormigrate.Options{ TableName: "summer_migrations_golem15_user", UseTransaction: true, }, All()) if err := m.Migrate(); err != nil { t.Fatal(err) } cols := []string{ "name", "surname", "is_activated", "activated_at", "activation_code", "activation_code_issued_at", "reset_password_code", "reset_password_code_issued_at", "has_self_set_password", "marketing_consent", "is_onboarded", "organisation_id", "organisation_role", "preferred_locale", "tokens_valid_after", "created_ip_address", "last_ip_address", } for _, col := range cols { if !gdb.Migrator().HasColumn("users", col) { t.Fatalf("missing column %s", col) } } if err := gdb.Exec(`INSERT INTO users (email) VALUES ('defaults@example.test')`).Error; err != nil { t.Fatal(err) } var row struct { HasSelfSetPassword bool IsActivated bool MarketingConsent bool IsOnboarded bool } if err := gdb.Raw(`SELECT has_self_set_password, is_activated, marketing_consent, is_onboarded FROM users WHERE email = ?`, "defaults@example.test").Scan(&row).Error; err != nil { t.Fatal(err) } if !row.HasSelfSetPassword || row.IsActivated || row.MarketingConsent || row.IsOnboarded { t.Fatalf("defaults = %+v", row) } // The three admin migrations, API tokens, user groups, jwt blacklist, // throttle, then extend-users. for range 8 { if err := m.RollbackLast(); err != nil { t.Fatal(err) } } for _, col := range cols { if gdb.Migrator().HasColumn("users", col) { t.Fatalf("column %s survived rollback", col) } } if gdb.Migrator().HasTable("user_throttle") || gdb.Migrator().HasTable("jwt_blacklist") { t.Fatal("new tables survived rollback") } if !gdb.Migrator().HasTable("users") { t.Fatal("users dropped") } } func TestCreateUserThrottleMigration(t *testing.T) { sqlDB := dedicatedDB(t, "user_throttle") gdb, err := lagoon.Use(t.Context(), sqlDB) if err != nil { t.Fatal(err) } m := gormigrate.New(gdb, &gormigrate.Options{ TableName: "summer_migrations_golem15_user", UseTransaction: true, }, All()) if err := m.Migrate(); err != nil { t.Fatal(err) } if !gdb.Migrator().HasTable("user_throttle") { t.Fatal("user_throttle missing") } for _, col := range []string{"id", "user_id", "ip_address", "attempts", "is_suspended", "suspended_at", "is_banned", "created_at", "updated_at"} { if !gdb.Migrator().HasColumn("user_throttle", col) { t.Fatalf("missing column %s", col) } } // The three admin migrations, API tokens, user groups, jwt blacklist, then throttle. for range 7 { if err := m.RollbackLast(); err != nil { t.Fatal(err) } } if gdb.Migrator().HasTable("user_throttle") { t.Fatal("user_throttle survived rollback") } } func TestCreateJwtBlacklistMigration(t *testing.T) { sqlDB := dedicatedDB(t, "user_jwt_blacklist") gdb, err := lagoon.Use(t.Context(), sqlDB) if err != nil { t.Fatal(err) } m := gormigrate.New(gdb, &gormigrate.Options{ TableName: "summer_migrations_golem15_user", UseTransaction: true, }, All()) if err := m.Migrate(); err != nil { t.Fatal(err) } if !gdb.Migrator().HasTable("jwt_blacklist") { t.Fatal("jwt_blacklist missing") } for _, col := range []string{"jti", "expires_at", "valid_until"} { if !gdb.Migrator().HasColumn("jwt_blacklist", col) { t.Fatalf("missing column %s", col) } } // The three admin migrations, API tokens, user groups, then jwt blacklist. for range 6 { if err := m.RollbackLast(); err != nil { t.Fatal(err) } } if gdb.Migrator().HasTable("jwt_blacklist") { t.Fatal("jwt_blacklist survived rollback") } }