forked from baron/baron-sso
조직 연동 오류 해결
This commit is contained in:
@@ -56,7 +56,7 @@ func TestEnsureSuperAdminPromotesExistingLocalUser(t *testing.T) {
|
||||
Email: "existing@example.com",
|
||||
Name: "Existing",
|
||||
Role: domain.RoleUser,
|
||||
Status: domain.UserStatusInactive,
|
||||
Status: domain.UserStatusPreboarding,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,9 @@ func Run(db *gorm.DB) error {
|
||||
}
|
||||
|
||||
// 3. Normalize staging seed/read-model data
|
||||
if err := CanonicalizeLegacyUserStatuses(db); err != nil {
|
||||
return fmt.Errorf("legacy user status canonicalization failed: %w", err)
|
||||
}
|
||||
if err := SanitizeLegacyUserMetadata(db); err != nil {
|
||||
return fmt.Errorf("legacy user metadata sanitize failed: %w", err)
|
||||
}
|
||||
@@ -64,6 +67,25 @@ func migrateSchemas(db *gorm.DB) error {
|
||||
)
|
||||
}
|
||||
|
||||
func CanonicalizeLegacyUserStatuses(db *gorm.DB) error {
|
||||
if db == nil || !db.Migrator().HasTable(&domain.User{}) {
|
||||
return nil
|
||||
}
|
||||
updates := map[string]string{
|
||||
"inactive": domain.UserStatusPreboarding,
|
||||
"leave_of_absence": domain.UserStatusTemporaryLeave,
|
||||
"baron_only": domain.UserStatusBaronGuest,
|
||||
}
|
||||
for legacy, canonical := range updates {
|
||||
if err := db.Model(&domain.User{}).
|
||||
Where("status = ?", legacy).
|
||||
Update("status", canonical).Error; err != nil {
|
||||
return fmt.Errorf("failed to canonicalize users.status %s to %s: %w", legacy, canonical, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func dropLegacyUserCompanyColumns(db *gorm.DB) error {
|
||||
if !db.Migrator().HasTable(&domain.User{}) {
|
||||
return nil
|
||||
|
||||
@@ -68,6 +68,52 @@ func TestSanitizeLegacyUserMetadataRemovesClassificationFlags(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCanonicalizeLegacyUserStatuses(t *testing.T) {
|
||||
db := openBootstrapPostgresTestDB(t)
|
||||
if err := db.AutoMigrate(&domain.User{}); err != nil {
|
||||
t.Fatalf("failed to migrate users table: %v", err)
|
||||
}
|
||||
|
||||
users := []domain.User{
|
||||
{ID: "11000000-0000-0000-0000-000000000001", Email: "inactive@example.com", Name: "Inactive", Role: domain.RoleUser, Status: "inactive"},
|
||||
{ID: "11000000-0000-0000-0000-000000000002", Email: "leave@example.com", Name: "Leave", Role: domain.RoleUser, Status: "leave_of_absence"},
|
||||
{ID: "11000000-0000-0000-0000-000000000003", Email: "baron-only@example.com", Name: "Baron Only", Role: domain.RoleUser, Status: "baron_only"},
|
||||
{ID: "11000000-0000-0000-0000-000000000004", Email: "active@example.com", Name: "Active", Role: domain.RoleUser, Status: domain.UserStatusActive},
|
||||
}
|
||||
if err := db.Create(&users).Error; err != nil {
|
||||
t.Fatalf("failed to create users: %v", err)
|
||||
}
|
||||
|
||||
if err := CanonicalizeLegacyUserStatuses(db); err != nil {
|
||||
t.Fatalf("CanonicalizeLegacyUserStatuses returned error: %v", err)
|
||||
}
|
||||
if err := CanonicalizeLegacyUserStatuses(db); err != nil {
|
||||
t.Fatalf("CanonicalizeLegacyUserStatuses must be idempotent: %v", err)
|
||||
}
|
||||
|
||||
got := map[string]string{}
|
||||
var loaded []domain.User
|
||||
if err := db.Find(&loaded).Error; err != nil {
|
||||
t.Fatalf("failed to load users: %v", err)
|
||||
}
|
||||
for _, user := range loaded {
|
||||
got[user.Email] = user.Status
|
||||
}
|
||||
|
||||
if got["inactive@example.com"] != domain.UserStatusPreboarding {
|
||||
t.Fatalf("inactive status = %q, want %q", got["inactive@example.com"], domain.UserStatusPreboarding)
|
||||
}
|
||||
if got["leave@example.com"] != domain.UserStatusTemporaryLeave {
|
||||
t.Fatalf("leave status = %q, want %q", got["leave@example.com"], domain.UserStatusTemporaryLeave)
|
||||
}
|
||||
if got["baron-only@example.com"] != domain.UserStatusBaronGuest {
|
||||
t.Fatalf("baron_only status = %q, want %q", got["baron-only@example.com"], domain.UserStatusBaronGuest)
|
||||
}
|
||||
if got["active@example.com"] != domain.UserStatusActive {
|
||||
t.Fatalf("active status = %q, want %q", got["active@example.com"], domain.UserStatusActive)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunSanitizesLegacyUserMetadata(t *testing.T) {
|
||||
db := openBootstrapPostgresTestDB(t)
|
||||
if err := db.AutoMigrate(&domain.User{}); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user