code.gitea.io/gitea@v1.22.3/models/migrations/v1_16/v189_test.go (about) 1 // Copyright 2021 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package v1_16 //nolint 5 6 import ( 7 "testing" 8 9 "code.gitea.io/gitea/models/migrations/base" 10 "code.gitea.io/gitea/modules/json" 11 12 "github.com/stretchr/testify/assert" 13 ) 14 15 // LoginSource represents an external way for authorizing users. 16 type LoginSourceOriginalV189 struct { 17 ID int64 `xorm:"pk autoincr"` 18 Type int 19 IsActived bool `xorm:"INDEX NOT NULL DEFAULT false"` 20 Cfg string `xorm:"TEXT"` 21 Expected string `xorm:"TEXT"` 22 } 23 24 func (ls *LoginSourceOriginalV189) TableName() string { 25 return "login_source" 26 } 27 28 func Test_UnwrapLDAPSourceCfg(t *testing.T) { 29 // Prepare and load the testing database 30 x, deferable := base.PrepareTestEnv(t, 0, new(LoginSourceOriginalV189)) 31 if x == nil || t.Failed() { 32 defer deferable() 33 return 34 } 35 defer deferable() 36 37 // LoginSource represents an external way for authorizing users. 38 type LoginSource struct { 39 ID int64 `xorm:"pk autoincr"` 40 Type int 41 IsActive bool `xorm:"INDEX NOT NULL DEFAULT false"` 42 Cfg string `xorm:"TEXT"` 43 Expected string `xorm:"TEXT"` 44 } 45 46 // Run the migration 47 if err := UnwrapLDAPSourceCfg(x); err != nil { 48 assert.NoError(t, err) 49 return 50 } 51 52 const batchSize = 100 53 for start := 0; ; start += batchSize { 54 sources := make([]*LoginSource, 0, batchSize) 55 if err := x.Table("login_source").Limit(batchSize, start).Find(&sources); err != nil { 56 assert.NoError(t, err) 57 return 58 } 59 60 if len(sources) == 0 { 61 break 62 } 63 64 for _, source := range sources { 65 converted := map[string]any{} 66 expected := map[string]any{} 67 68 if err := json.Unmarshal([]byte(source.Cfg), &converted); err != nil { 69 assert.NoError(t, err) 70 return 71 } 72 73 if err := json.Unmarshal([]byte(source.Expected), &expected); err != nil { 74 assert.NoError(t, err) 75 return 76 } 77 78 assert.EqualValues(t, expected, converted, "UnwrapLDAPSourceCfg failed for %d", source.ID) 79 assert.EqualValues(t, source.ID%2 == 0, source.IsActive, "UnwrapLDAPSourceCfg failed for %d", source.ID) 80 } 81 } 82 }