gitea源码

source_test.go 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package auth_test
  4. import (
  5. "strings"
  6. "testing"
  7. auth_model "code.gitea.io/gitea/models/auth"
  8. "code.gitea.io/gitea/models/unittest"
  9. "code.gitea.io/gitea/modules/json"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. "xorm.io/xorm/schemas"
  13. )
  14. type TestSource struct {
  15. auth_model.ConfigBase
  16. Provider string
  17. ClientID string
  18. ClientSecret string
  19. OpenIDConnectAutoDiscoveryURL string
  20. IconURL string
  21. }
  22. // FromDB fills up a LDAPConfig from serialized format.
  23. func (source *TestSource) FromDB(bs []byte) error {
  24. return json.Unmarshal(bs, &source)
  25. }
  26. // ToDB exports a LDAPConfig to a serialized format.
  27. func (source *TestSource) ToDB() ([]byte, error) {
  28. return json.Marshal(source)
  29. }
  30. func TestDumpAuthSource(t *testing.T) {
  31. assert.NoError(t, unittest.PrepareTestDatabase())
  32. authSourceSchema, err := unittest.GetXORMEngine().TableInfo(new(auth_model.Source))
  33. assert.NoError(t, err)
  34. auth_model.RegisterTypeConfig(auth_model.OAuth2, new(TestSource))
  35. auth_model.CreateSource(t.Context(), &auth_model.Source{
  36. Type: auth_model.OAuth2,
  37. Name: "TestSource",
  38. IsActive: false,
  39. Cfg: &TestSource{
  40. Provider: "ConvertibleSourceName",
  41. ClientID: "42",
  42. },
  43. })
  44. sb := new(strings.Builder)
  45. // TODO: this test is quite hacky, it should use a low-level "select" (without model processors) but not a database dump
  46. engine := unittest.GetXORMEngine()
  47. require.NoError(t, engine.DumpTables([]*schemas.Table{authSourceSchema}, sb))
  48. assert.Contains(t, sb.String(), `"Provider":"ConvertibleSourceName"`)
  49. }