-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathhandlers_settings_test.go
More file actions
224 lines (196 loc) · 6.99 KB
/
Copy pathhandlers_settings_test.go
File metadata and controls
224 lines (196 loc) · 6.99 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package web
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestSettingsGET(t *testing.T) {
srv := NewServer(Config{
ListenAddr: "127.0.0.1:0",
Profile: "work",
ReadOnly: true,
WebMutations: false,
})
srv.menuData = &fakeMenuDataLoader{snapshot: &MenuSnapshot{}}
req := httptest.NewRequest(http.MethodGet, "/api/settings", nil)
rr := httptest.NewRecorder()
srv.Handler().ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected status %d, got %d: %s", http.StatusOK, rr.Code, rr.Body.String())
}
body := rr.Body.String()
if !strings.Contains(body, `"profile"`) {
t.Errorf("expected 'profile' key, got: %s", body)
}
if !strings.Contains(body, `"readOnly"`) {
t.Errorf("expected 'readOnly' key, got: %s", body)
}
if !strings.Contains(body, `"webMutations"`) {
t.Errorf("expected 'webMutations' key, got: %s", body)
}
if !strings.Contains(body, `"version"`) {
t.Errorf("expected 'version' key, got: %s", body)
}
if !strings.Contains(body, `"work"`) {
t.Errorf("expected profile value 'work', got: %s", body)
}
}
func TestSettingsMethodNotAllowed(t *testing.T) {
srv := NewServer(Config{
ListenAddr: "127.0.0.1:0",
})
srv.menuData = &fakeMenuDataLoader{snapshot: &MenuSnapshot{}}
req := httptest.NewRequest(http.MethodPost, "/api/settings", nil)
rr := httptest.NewRecorder()
srv.Handler().ServeHTTP(rr, req)
if rr.Code != http.StatusMethodNotAllowed {
t.Fatalf("expected status %d, got %d: %s", http.StatusMethodNotAllowed, rr.Code, rr.Body.String())
}
if !strings.Contains(rr.Body.String(), ErrCodeMethodNotAllowed) {
t.Errorf("expected METHOD_NOT_ALLOWED error, got: %s", rr.Body.String())
}
}
func TestSettingsUnauthorized(t *testing.T) {
srv := NewServer(Config{
ListenAddr: "127.0.0.1:0",
Token: "secret-token",
})
srv.menuData = &fakeMenuDataLoader{snapshot: &MenuSnapshot{}}
req := httptest.NewRequest(http.MethodGet, "/api/settings", nil)
rr := httptest.NewRecorder()
srv.Handler().ServeHTTP(rr, req)
if rr.Code != http.StatusUnauthorized {
t.Fatalf("expected status %d, got %d: %s", http.StatusUnauthorized, rr.Code, rr.Body.String())
}
if !strings.Contains(rr.Body.String(), ErrCodeUnauthorized) {
t.Errorf("expected UNAUTHORIZED error, got: %s", rr.Body.String())
}
}
func TestSettingsWebMutationsTrue(t *testing.T) {
srv := NewServer(Config{
ListenAddr: "127.0.0.1:0",
WebMutations: true,
})
srv.menuData = &fakeMenuDataLoader{snapshot: &MenuSnapshot{}}
req := httptest.NewRequest(http.MethodGet, "/api/settings", nil)
rr := httptest.NewRecorder()
srv.Handler().ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected status %d, got %d", http.StatusOK, rr.Code)
}
if !strings.Contains(rr.Body.String(), `"webMutations":true`) {
t.Errorf("expected webMutations:true, got: %s", rr.Body.String())
}
}
func TestProfilesGET(t *testing.T) {
srv := NewServer(Config{
ListenAddr: "127.0.0.1:0",
Profile: "work",
})
srv.menuData = &fakeMenuDataLoader{snapshot: &MenuSnapshot{}}
req := httptest.NewRequest(http.MethodGet, "/api/profiles", nil)
rr := httptest.NewRecorder()
srv.Handler().ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected status %d, got %d: %s", http.StatusOK, rr.Code, rr.Body.String())
}
body := rr.Body.String()
if !strings.Contains(body, `"current"`) {
t.Errorf("expected 'current' key, got: %s", body)
}
if !strings.Contains(body, `"profiles"`) {
t.Errorf("expected 'profiles' key, got: %s", body)
}
if !strings.Contains(body, `"work"`) {
t.Errorf("expected profile value 'work', got: %s", body)
}
}
func TestProfilesMethodNotAllowed(t *testing.T) {
srv := NewServer(Config{
ListenAddr: "127.0.0.1:0",
})
srv.menuData = &fakeMenuDataLoader{snapshot: &MenuSnapshot{}}
req := httptest.NewRequest(http.MethodPost, "/api/profiles", nil)
rr := httptest.NewRecorder()
srv.Handler().ServeHTTP(rr, req)
if rr.Code != http.StatusMethodNotAllowed {
t.Fatalf("expected status %d, got %d: %s", http.StatusMethodNotAllowed, rr.Code, rr.Body.String())
}
if !strings.Contains(rr.Body.String(), ErrCodeMethodNotAllowed) {
t.Errorf("expected METHOD_NOT_ALLOWED error, got: %s", rr.Body.String())
}
}
func TestProfilesUnauthorized(t *testing.T) {
srv := NewServer(Config{
ListenAddr: "127.0.0.1:0",
Token: "secret-token",
})
srv.menuData = &fakeMenuDataLoader{snapshot: &MenuSnapshot{}}
req := httptest.NewRequest(http.MethodGet, "/api/profiles", nil)
rr := httptest.NewRecorder()
srv.Handler().ServeHTTP(rr, req)
if rr.Code != http.StatusUnauthorized {
t.Fatalf("expected status %d, got %d: %s", http.StatusUnauthorized, rr.Code, rr.Body.String())
}
if !strings.Contains(rr.Body.String(), ErrCodeUnauthorized) {
t.Errorf("expected UNAUTHORIZED error, got: %s", rr.Body.String())
}
}
// Issue #1682: GET /api/settings carries the terminal link-open policy so the
// web UI can skip the confirm for trusted hosts. A Config that never set
// ConfirmLinkOpen must still report the confirm ON — a false there would
// silently disable the prompt for every host.
func TestSettingsLinkPolicy_DefaultsToConfirmOn(t *testing.T) {
srv := NewServer(Config{ListenAddr: "127.0.0.1:0", Profile: "default"})
srv.menuData = &fakeMenuDataLoader{snapshot: &MenuSnapshot{}}
req := httptest.NewRequest(http.MethodGet, "/api/settings", nil)
rr := httptest.NewRecorder()
srv.Handler().ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected status %d, got %d: %s", http.StatusOK, rr.Code, rr.Body.String())
}
var got SettingsResponse
if err := json.Unmarshal(rr.Body.Bytes(), &got); err != nil {
t.Fatalf("decode settings: %v", err)
}
if !got.ConfirmLinkOpen {
t.Error("confirmLinkOpen = false with ConfirmLinkOpen unset, want true (confirm stays on)")
}
if len(got.TrustedDomains) != 0 {
t.Errorf("trustedDomains = %v, want empty when unconfigured", got.TrustedDomains)
}
}
func TestSettingsLinkPolicy_ServesConfiguredAllowlistAndToggle(t *testing.T) {
off := false
srv := NewServer(Config{
ListenAddr: "127.0.0.1:0",
Profile: "default",
TrustedDomains: []string{"gitlab.corp.example", "*.ci.corp.example"},
ConfirmLinkOpen: &off,
})
srv.menuData = &fakeMenuDataLoader{snapshot: &MenuSnapshot{}}
req := httptest.NewRequest(http.MethodGet, "/api/settings", nil)
rr := httptest.NewRecorder()
srv.Handler().ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected status %d, got %d: %s", http.StatusOK, rr.Code, rr.Body.String())
}
var got SettingsResponse
if err := json.Unmarshal(rr.Body.Bytes(), &got); err != nil {
t.Fatalf("decode settings: %v", err)
}
if got.ConfirmLinkOpen {
t.Error("confirmLinkOpen = true, want false when ConfirmLinkOpen is explicitly false")
}
want := []string{"gitlab.corp.example", "*.ci.corp.example"}
if len(got.TrustedDomains) != len(want) {
t.Fatalf("trustedDomains = %v, want %v", got.TrustedDomains, want)
}
for i := range want {
if got.TrustedDomains[i] != want[i] {
t.Errorf("trustedDomains[%d] = %q, want %q", i, got.TrustedDomains[i], want[i])
}
}
}