code.gitea.io/gitea@v1.21.7/models/auth/source_test.go (about)

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