-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathuserconfig_web_test.go
More file actions
164 lines (151 loc) · 5.49 KB
/
Copy pathuserconfig_web_test.go
File metadata and controls
164 lines (151 loc) · 5.49 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package session
import (
"os"
"path/filepath"
"testing"
)
// withTempHomeAndConfig sets HOME to a temp dir, writes the given config.toml
// contents (or no file when contents is empty), clears the user-config cache,
// and registers cleanup that restores HOME and clears the cache again. It
// returns the temp dir for tests that need to inspect the path.
func withTempHomeAndConfig(t *testing.T, contents string) string {
t.Helper()
tempDir := t.TempDir()
originalHome := os.Getenv("HOME")
os.Setenv("HOME", tempDir)
// Keep XDG_CONFIG_HOME inside this temp HOME too. TestMain clears XDG so
// HOME-only isolation usually works, but this helper writes legacy config
// files and should stay isolated even if a caller adds an XDG override.
// An empty XDG config dir makes reads fall back to the legacy file below.
t.Setenv("XDG_CONFIG_HOME", filepath.Join(tempDir, ".config"))
t.Cleanup(func() {
os.Setenv("HOME", originalHome)
ClearUserConfigCache()
})
ClearUserConfigCache()
if contents != "" {
agentDeckDir := filepath.Join(tempDir, ".agent-deck")
if err := os.MkdirAll(agentDeckDir, 0o700); err != nil {
t.Fatalf("mkdir: %v", err)
}
if err := os.WriteFile(filepath.Join(agentDeckDir, "config.toml"), []byte(contents), 0o600); err != nil {
t.Fatalf("write config: %v", err)
}
}
return tempDir
}
func TestWebSettings_DefaultsTrueWhenAbsent(t *testing.T) {
withTempHomeAndConfig(t, `
[claude]
config_dir = "~/.claude"
`)
if !GetWebMutationsEnabled() {
t.Errorf("GetWebMutationsEnabled() = false, want true when [web] is absent")
}
}
func TestWebSettings_DefaultsTrueWhenNoConfigFile(t *testing.T) {
withTempHomeAndConfig(t, "")
if !GetWebMutationsEnabled() {
t.Errorf("GetWebMutationsEnabled() = false, want true when config.toml is missing")
}
}
func TestWebSettings_ExplicitTrue(t *testing.T) {
withTempHomeAndConfig(t, `
[web]
mutations_enabled = true
`)
if !GetWebMutationsEnabled() {
t.Errorf("GetWebMutationsEnabled() = false, want true when explicitly enabled")
}
}
func TestWebSettings_ExplicitFalse(t *testing.T) {
withTempHomeAndConfig(t, `
[web]
mutations_enabled = false
`)
if GetWebMutationsEnabled() {
t.Errorf("GetWebMutationsEnabled() = true, want false when explicitly disabled")
}
}
// --- [web] trusted_domains / confirm_link_open (issue #1682) ---------------
func TestWebTrustedDomains_EmptyWhenAbsent(t *testing.T) {
withTempHomeAndConfig(t, `
[web]
mutations_enabled = true
`)
if got := GetWebTrustedDomains(); len(got) != 0 {
t.Errorf("GetWebTrustedDomains() = %v, want empty when the key is absent", got)
}
}
func TestWebTrustedDomains_ReadsAndNormalizes(t *testing.T) {
withTempHomeAndConfig(t, `
[web]
trusted_domains = ["GitLab.Corp.Example", "https://gerrit.corp.example:8443/c/1", " ", "*.ci.corp.example", "gitlab.corp.example"]
`)
got := GetWebTrustedDomains()
want := []string{"gitlab.corp.example", "gerrit.corp.example", "*.ci.corp.example"}
if len(got) != len(want) {
t.Fatalf("GetWebTrustedDomains() = %v, want %v", got, want)
}
for i := range want {
if got[i] != want[i] {
t.Errorf("GetWebTrustedDomains()[%d] = %q, want %q", i, got[i], want[i])
}
}
}
func TestWebConfirmLinkOpen_DefaultsTrue(t *testing.T) {
withTempHomeAndConfig(t, `
[web]
trusted_domains = ["gitlab.corp.example"]
`)
if !GetWebConfirmLinkOpen() {
t.Error("GetWebConfirmLinkOpen() = false, want true when the key is omitted")
}
}
func TestWebConfirmLinkOpen_ExplicitFalse(t *testing.T) {
withTempHomeAndConfig(t, `
[web]
confirm_link_open = false
`)
if GetWebConfirmLinkOpen() {
t.Error("GetWebConfirmLinkOpen() = true, want false when explicitly disabled")
}
}
func TestNormalizeTrustedDomains(t *testing.T) {
tests := []struct {
name string
in []string
want []string
}{
{"nil stays nil", nil, nil},
{"plain host", []string{"gitlab.corp.example"}, []string{"gitlab.corp.example"}},
{"case folded", []string{"GitLab.CORP.example"}, []string{"gitlab.corp.example"}},
{"trims space", []string{" gitlab.corp.example "}, []string{"gitlab.corp.example"}},
{"strips scheme and path", []string{"https://gitlab.corp.example/group/repo/-/merge_requests/7"}, []string{"gitlab.corp.example"}},
{"strips port", []string{"gerrit.corp.example:8443"}, []string{"gerrit.corp.example"}},
{"strips userinfo", []string{"https://user:pw@gitlab.corp.example"}, []string{"gitlab.corp.example"}},
{"strips trailing dot", []string{"gitlab.corp.example."}, []string{"gitlab.corp.example"}},
{"keeps subdomain wildcard", []string{"*.corp.example"}, []string{"*.corp.example"}},
{"wildcard with port", []string{"*.corp.example:8443"}, []string{"*.corp.example"}},
{"ipv6 literal keeps brackets", []string{"[::1]:8443"}, []string{"[::1]"}},
{"drops blanks", []string{"", " ", "\t"}, nil},
{"drops bare wildcard", []string{"*", "*.", "*.example"}, nil},
{"drops embedded wildcard", []string{"git.*.corp.example"}, nil},
{"drops scheme-only", []string{"https://"}, nil},
{"dedupes after normalizing", []string{"gitlab.corp.example", "GITLAB.corp.example:443"}, []string{"gitlab.corp.example"}},
{"preserves order", []string{"b.example", "a.example"}, []string{"b.example", "a.example"}},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := NormalizeTrustedDomains(tc.in)
if len(got) != len(tc.want) {
t.Fatalf("NormalizeTrustedDomains(%v) = %v, want %v", tc.in, got, tc.want)
}
for i := range tc.want {
if got[i] != tc.want[i] {
t.Errorf("NormalizeTrustedDomains(%v)[%d] = %q, want %q", tc.in, i, got[i], tc.want[i])
}
}
})
}
}