gitea源码

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