code.gitea.io/gitea@v1.22.3/routers/web/admin/admin_test.go (about)

     1  // Copyright 2019 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package admin
     5  
     6  import (
     7  	"net/http"
     8  	"testing"
     9  
    10  	"code.gitea.io/gitea/modules/json"
    11  	"code.gitea.io/gitea/modules/setting"
    12  	"code.gitea.io/gitea/modules/test"
    13  	"code.gitea.io/gitea/services/contexttest"
    14  
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestShadowPassword(t *testing.T) {
    19  	kases := []struct {
    20  		Provider string
    21  		CfgItem  string
    22  		Result   string
    23  	}{
    24  		{
    25  			Provider: "redis",
    26  			CfgItem:  "network=tcp,addr=:6379,password=gitea,db=0,pool_size=100,idle_timeout=180",
    27  			Result:   "network=tcp,addr=:6379,password=******,db=0,pool_size=100,idle_timeout=180",
    28  		},
    29  		{
    30  			Provider: "mysql",
    31  			CfgItem:  "root:@tcp(localhost:3306)/gitea?charset=utf8",
    32  			Result:   "root:******@tcp(localhost:3306)/gitea?charset=utf8",
    33  		},
    34  		{
    35  			Provider: "mysql",
    36  			CfgItem:  "/gitea?charset=utf8",
    37  			Result:   "/gitea?charset=utf8",
    38  		},
    39  		{
    40  			Provider: "mysql",
    41  			CfgItem:  "user:mypassword@/dbname",
    42  			Result:   "user:******@/dbname",
    43  		},
    44  		{
    45  			Provider: "postgres",
    46  			CfgItem:  "user=pqgotest dbname=pqgotest sslmode=verify-full",
    47  			Result:   "user=pqgotest dbname=pqgotest sslmode=verify-full",
    48  		},
    49  		{
    50  			Provider: "postgres",
    51  			CfgItem:  "user=pqgotest password= dbname=pqgotest sslmode=verify-full",
    52  			Result:   "user=pqgotest password=****** dbname=pqgotest sslmode=verify-full",
    53  		},
    54  		{
    55  			Provider: "postgres",
    56  			CfgItem:  "postgres://user:pass@hostname/dbname",
    57  			Result:   "postgres://user:******@hostname/dbname",
    58  		},
    59  		{
    60  			Provider: "couchbase",
    61  			CfgItem:  "http://dev-couchbase.example.com:8091/",
    62  			Result:   "http://dev-couchbase.example.com:8091/",
    63  		},
    64  		{
    65  			Provider: "couchbase",
    66  			CfgItem:  "http://user:the_password@dev-couchbase.example.com:8091/",
    67  			Result:   "http://user:******@dev-couchbase.example.com:8091/",
    68  		},
    69  	}
    70  
    71  	for _, k := range kases {
    72  		assert.EqualValues(t, k.Result, shadowPassword(k.Provider, k.CfgItem))
    73  	}
    74  }
    75  
    76  func TestSelfCheckPost(t *testing.T) {
    77  	defer test.MockVariableValue(&setting.AppURL, "http://config/sub/")()
    78  	defer test.MockVariableValue(&setting.AppSubURL, "/sub")()
    79  
    80  	ctx, resp := contexttest.MockContext(t, "GET http://host/sub/admin/self_check?location_origin=http://frontend")
    81  	SelfCheckPost(ctx)
    82  	assert.EqualValues(t, http.StatusOK, resp.Code)
    83  
    84  	data := struct {
    85  		Problems []string `json:"problems"`
    86  	}{}
    87  	err := json.Unmarshal(resp.Body.Bytes(), &data)
    88  	assert.NoError(t, err)
    89  	assert.Equal(t, []string{
    90  		ctx.Locale.TrString("admin.self_check.location_origin_mismatch", "http://frontend/sub/", "http://config/sub/"),
    91  	}, data.Problems)
    92  }