summaryrefslogtreecommitdiffstats
path: root/routers/web/admin/admin_test.go
blob: 3518869ede7bdad3f6326cc6e8b747f85dd08227 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2019 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package admin

import (
	"testing"

	activities_model "code.gitea.io/gitea/models/activities"
	"code.gitea.io/gitea/models/unittest"
	"code.gitea.io/gitea/modules/setting"
	"code.gitea.io/gitea/modules/test"
	"code.gitea.io/gitea/services/contexttest"

	"github.com/stretchr/testify/assert"
)

func TestShadowPassword(t *testing.T) {
	kases := []struct {
		Provider string
		CfgItem  string
		Result   string
	}{
		{
			Provider: "redis",
			CfgItem:  "network=tcp,addr=:6379,password=gitea,db=0,pool_size=100,idle_timeout=180",
			Result:   "network=tcp,addr=:6379,password=******,db=0,pool_size=100,idle_timeout=180",
		},
		{
			Provider: "mysql",
			CfgItem:  "root:@tcp(localhost:3306)/gitea?charset=utf8",
			Result:   "root:******@tcp(localhost:3306)/gitea?charset=utf8",
		},
		{
			Provider: "mysql",
			CfgItem:  "/gitea?charset=utf8",
			Result:   "/gitea?charset=utf8",
		},
		{
			Provider: "mysql",
			CfgItem:  "user:mypassword@/dbname",
			Result:   "user:******@/dbname",
		},
		{
			Provider: "postgres",
			CfgItem:  "user=pqgotest dbname=pqgotest sslmode=verify-full",
			Result:   "user=pqgotest dbname=pqgotest sslmode=verify-full",
		},
		{
			Provider: "postgres",
			CfgItem:  "user=pqgotest password= dbname=pqgotest sslmode=verify-full",
			Result:   "user=pqgotest password=****** dbname=pqgotest sslmode=verify-full",
		},
		{
			Provider: "postgres",
			CfgItem:  "postgres://user:pass@hostname/dbname",
			Result:   "postgres://user:******@hostname/dbname",
		},
		{
			Provider: "couchbase",
			CfgItem:  "http://dev-couchbase.example.com:8091/",
			Result:   "http://dev-couchbase.example.com:8091/",
		},
		{
			Provider: "couchbase",
			CfgItem:  "http://user:the_password@dev-couchbase.example.com:8091/",
			Result:   "http://user:******@dev-couchbase.example.com:8091/",
		},
	}

	for _, k := range kases {
		assert.EqualValues(t, k.Result, shadowPassword(k.Provider, k.CfgItem))
	}
}

func TestMonitorStats(t *testing.T) {
	unittest.PrepareTestEnv(t)

	t.Run("Normal", func(t *testing.T) {
		defer test.MockVariableValue(&setting.Metrics.EnabledIssueByLabel, false)()
		defer test.MockVariableValue(&setting.Metrics.EnabledIssueByRepository, false)()

		ctx, _ := contexttest.MockContext(t, "admin/stats")
		MonitorStats(ctx)

		// Test some of the stats manually.
		mappedStats := ctx.Data["Stats"].(map[string]any)
		stats := activities_model.GetStatistic(ctx).Counter

		assert.EqualValues(t, stats.Comment, mappedStats["Comment"])
		assert.EqualValues(t, stats.Issue, mappedStats["Issue"])
		assert.EqualValues(t, stats.User, mappedStats["User"])
		assert.EqualValues(t, stats.Milestone, mappedStats["Milestone"])

		// Ensure that these aren't set.
		assert.Empty(t, stats.IssueByLabel)
		assert.Empty(t, stats.IssueByRepository)
		assert.Nil(t, mappedStats["IssueByLabel"])
		assert.Nil(t, mappedStats["IssueByRepository"])
	})

	t.Run("IssueByX", func(t *testing.T) {
		defer test.MockVariableValue(&setting.Metrics.EnabledIssueByLabel, true)()
		defer test.MockVariableValue(&setting.Metrics.EnabledIssueByRepository, true)()

		ctx, _ := contexttest.MockContext(t, "admin/stats")
		MonitorStats(ctx)

		mappedStats := ctx.Data["Stats"].(map[string]any)
		stats := activities_model.GetStatistic(ctx).Counter

		assert.NotEmpty(t, stats.IssueByLabel)
		assert.NotEmpty(t, stats.IssueByRepository)
		assert.EqualValues(t, stats.IssueByLabel, mappedStats["IssueByLabel"])
		assert.EqualValues(t, stats.IssueByRepository, mappedStats["IssueByRepository"])
	})
}