BlueGit

timfa bluegit

Files

bluegit / internal / config / config_test.go

config_test.go

config_test.go 10.3 KiB Download Raw
1package config
2
3import (
4 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8)
9
10func TestPresetConfig(t *testing.T) {
11 dir := t.TempDir()
12 path := filepath.Join(dir, "config.yml")
13 docker := filepath.Join(dir, "Dockerfile")
14 if err := os.WriteFile(docker, []byte("FROM alpine:3.20\nRUN apk add --no-cache git\n"), 0o644); err != nil {
15 t.Fatal(err)
16 }
17 body := []byte("base_url: https://git.example.test\nauth:\n mode: oauth\n client_id: git.example.test\nci:\n presets:\n bluegit/node:\n from: node:22-bookworm\n team/tools:\n dockerfile: Dockerfile\n team/plain:\n image: alpine:3.20\n")
18 if err := os.WriteFile(path, body, 0o600); err != nil {
19 t.Fatal(err)
20 }
21 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "geheim")
22 cfg, err := Load(path)
23 if err != nil {
24 t.Fatal(err)
25 }
26 if cfg.CI.Presets["team/tools"].Dockerfile != docker {
27 t.Fatalf("pad %s", cfg.CI.Presets["team/tools"].Dockerfile)
28 }
29 if cfg.CI.Presets["bluegit/node"].From != "node:22-bookworm" || cfg.CI.Presets["team/plain"].Image != "alpine:3.20" {
30 t.Fatalf("%+v", cfg.CI.Presets)
31 }
32 bad := []byte("base_url: https://git.example.test\nauth:\n mode: oauth\n client_id: git.example.test\nci:\n presets:\n node:22:\n image: alpine\n")
33 if err := os.WriteFile(path, bad, 0o600); err != nil {
34 t.Fatal(err)
35 }
36 if _, err := Load(path); err == nil {
37 t.Fatal("een tag in de aliasnaam hoort te falen")
38 }
39}
40
41func TestRunnerName(t *testing.T) {
42 dir := t.TempDir()
43 path := filepath.Join(dir, "config.yml")
44 body := []byte("base_url: https://git.example.test\nauth:\n mode: oauth\n client_id: git.example.test\n")
45 if err := os.WriteFile(path, body, 0o600); err != nil {
46 t.Fatal(err)
47 }
48 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "geheim")
49 cfg, err := Load(path)
50 if err != nil {
51 t.Fatal(err)
52 }
53 if cfg.Runner.Name != "Steve" {
54 t.Fatalf("standaard %q", cfg.Runner.Name)
55 }
56 body = []byte("base_url: https://git.example.test\nrunner:\n name: \" Ada \"\nauth:\n mode: oauth\n client_id: git.example.test\n")
57 if err := os.WriteFile(path, body, 0o600); err != nil {
58 t.Fatal(err)
59 }
60 cfg, err = Load(path)
61 if err != nil {
62 t.Fatal(err)
63 }
64 if cfg.Runner.Name != "Ada" {
65 t.Fatalf("naam %q", cfg.Runner.Name)
66 }
67}
68
69func TestStoragePathOptional(t *testing.T) {
70 dir := t.TempDir()
71 path := filepath.Join(dir, "config.yml")
72 body := []byte("base_url: https://git.example.test\nauth:\n mode: oauth\n client_id: git.example.test\n")
73 if err := os.WriteFile(path, body, 0o600); err != nil {
74 t.Fatal(err)
75 }
76 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "geheim")
77 cfg, err := Load(path)
78 if err != nil {
79 t.Fatal(err)
80 }
81 if cfg.StoragePath != "" {
82 t.Fatalf("geen standaardpad verwacht, kreeg %q", cfg.StoragePath)
83 }
84 if cfg.StatePath != "/var/lib/bluegit" {
85 t.Fatal(cfg.StatePath)
86 }
87 if strings.Contains(Example(), "/mnt/storage") {
88 t.Fatal("voorbeeldconfiguratie bevat nog een vast schijfpad")
89 }
90}
91
92func TestDevModeRefusedOnProductionHost(t *testing.T) {
93 dir := t.TempDir()
94 path := filepath.Join(dir, "config.yml")
95 body := []byte("base_url: https://git.timfalken.com\nstorage_path: /mnt/storage/bluegit\nauth:\n mode: dev\n")
96 if err := os.WriteFile(path, body, 0o600); err != nil {
97 t.Fatal(err)
98 }
99 _, err := Load(path)
100 if err == nil || !strings.Contains(err.Error(), "dev") {
101 t.Fatalf("verwacht weigering, kreeg %v", err)
102 }
103}
104
105func TestOAuthDefaultsUnchanged(t *testing.T) {
106 dir := t.TempDir()
107 path := filepath.Join(dir, "config.yml")
108 body := []byte("base_url: https://git.example.test\nauth:\n mode: oauth\n client_id: git.example.test\n")
109 if err := os.WriteFile(path, body, 0o600); err != nil {
110 t.Fatal(err)
111 }
112 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "geheim")
113 cfg, err := Load(path)
114 if err != nil {
115 t.Fatal(err)
116 }
117 if cfg.Auth.Mode != "oauth" || cfg.Auth.BaseURL != "https://auth.timfalken.com" {
118 t.Fatalf("%+v", cfg.Auth)
119 }
120 if cfg.Auth.RedirectPath != "/auth/callback" || cfg.Auth.UserAgent != "BlueGitAccountClient/0.1" {
121 t.Fatalf("%+v", cfg.Auth)
122 }
123 if cfg.Auth.TenantID != "" || cfg.Auth.Issuer != "" {
124 t.Fatalf("entra-velden horen leeg: %+v", cfg.Auth)
125 }
126}
127
128func TestEntraConfig(t *testing.T) {
129 dir := t.TempDir()
130 path := filepath.Join(dir, "config.yml")
131 body := []byte(`base_url: https://git.example.test
132auth:
133 mode: entra
134 tenant_id: 00000000-0000-0000-0000-000000000001
135 client_id: 11111111-1111-1111-1111-111111111111
136`)
137 if err := os.WriteFile(path, body, 0o600); err != nil {
138 t.Fatal(err)
139 }
140 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "entra-geheim")
141 cfg, err := Load(path)
142 if err != nil {
143 t.Fatal(err)
144 }
145 if cfg.Auth.BaseURL != "" {
146 t.Fatalf("entra zet geen auth.timfalken-default, kreeg %q", cfg.Auth.BaseURL)
147 }
148 if cfg.EntraIssuer() != "https://login.microsoftonline.com/00000000-0000-0000-0000-000000000001/v2.0" {
149 t.Fatal(cfg.EntraIssuer())
150 }
151 if cfg.Auth.ClientSecret != "entra-geheim" || cfg.RedirectURI() != "https://git.example.test/auth/callback" {
152 t.Fatalf("%+v", cfg.Auth)
153 }
154
155 body = []byte(`base_url: https://git.example.test
156auth:
157 mode: entra
158 tenant_id: 00000000-0000-0000-0000-000000000001
159 client_id: 11111111-1111-1111-1111-111111111111
160 issuer: https://login.test.example/t/v2.0
161`)
162 if err := os.WriteFile(path, body, 0o600); err != nil {
163 t.Fatal(err)
164 }
165 cfg, err = Load(path)
166 if err != nil {
167 t.Fatal(err)
168 }
169 if cfg.EntraIssuer() != "https://login.test.example/t/v2.0" {
170 t.Fatal(cfg.EntraIssuer())
171 }
172}
173
174func TestEntraValidation(t *testing.T) {
175 dir := t.TempDir()
176 path := filepath.Join(dir, "config.yml")
177 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "geheim")
178 cases := []struct {
179 body string
180 want string
181 }{
182 {
183 body: "base_url: https://git.example.test\nauth:\n mode: entra\n client_id: app\n",
184 want: "tenant_id",
185 },
186 {
187 body: "base_url: https://git.example.test\nauth:\n mode: entra\n tenant_id: t1\n",
188 want: "client_id",
189 },
190 {
191 body: "base_url: https://git.example.test\nauth:\n mode: entra\n tenant_id: t1\n client_id: app\n issuer: niet-een-url\n",
192 want: "issuer",
193 },
194 }
195 for _, tc := range cases {
196 if err := os.WriteFile(path, []byte(tc.body), 0o600); err != nil {
197 t.Fatal(err)
198 }
199 _, err := Load(path)
200 if err == nil || !strings.Contains(err.Error(), tc.want) {
201 t.Fatalf("body %q: verwacht %q, kreeg %v", tc.body, tc.want, err)
202 }
203 }
204 // Zonder geheim:zelfde fouttekst als oauth.
205 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "")
206 body := []byte("base_url: https://git.example.test\nauth:\n mode: entra\n tenant_id: t1\n client_id: app\n")
207 if err := os.WriteFile(path, body, 0o600); err != nil {
208 t.Fatal(err)
209 }
210 _, err := Load(path)
211 if err == nil || !strings.Contains(err.Error(), "clientgeheim") {
212 t.Fatalf("verwacht clientgeheim-fout, kreeg %v", err)
213 }
214}
215
216func TestSecretFromEnv(t *testing.T) {
217 dir := t.TempDir()
218 path := filepath.Join(dir, "config.yml")
219 body := []byte("base_url: https://git.example.test\nstorage_path: " + dir + "\nauth:\n mode: oauth\n client_id: git.example.test\n")
220 if err := os.WriteFile(path, body, 0o600); err != nil {
221 t.Fatal(err)
222 }
223 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "uit-de-omgeving")
224 cfg, err := Load(path)
225 if err != nil {
226 t.Fatal(err)
227 }
228 if cfg.Auth.ClientSecret != "uit-de-omgeving" || cfg.Git.DefaultBranch != "master" {
229 t.Fatalf("branch %s geheim %s", cfg.Git.DefaultBranch, cfg.Auth.ClientSecret)
230 }
231 if cfg.RedirectURI() != "https://git.example.test/auth/callback" {
232 t.Fatal(cfg.RedirectURI())
233 }
234 if cfg.SSHPublicHost() != "git.example.test" || cfg.SSHPublicPort() != 2222 {
235 t.Fatalf("ssh %s:%d", cfg.SSHPublicHost(), cfg.SSHPublicPort())
236 }
237 if cfg.CloneSSH("tim/demo") != "" {
238 t.Fatal("ssh staat uit, dus geen clone-URL")
239 }
240}
241
242func TestPublicSSHAndTrustedProxy(t *testing.T) {
243 dir := t.TempDir()
244 path := filepath.Join(dir, "config.yml")
245 body := []byte(`base_url: https://git.timfalken.com
246storage_path: ` + dir + `
247listen: 0.0.0.0:8080
248trusted_proxies: ["10.0.0.2", "192.168.0.0/16"]
249auth:
250 mode: oauth
251 client_id: git.timfalken.com
252ssh:
253 enabled: true
254 listen: 0.0.0.0:2222
255 public_host: git.timfalken.com
256 public_port: 22
257`)
258 if err := os.WriteFile(path, body, 0o600); err != nil {
259 t.Fatal(err)
260 }
261 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "geheim")
262 cfg, err := Load(path)
263 if err != nil {
264 t.Fatal(err)
265 }
266 if cfg.CloneSSH("tim/demo") != "git@git.timfalken.com:tim/demo.git" {
267 t.Fatal(cfg.CloneSSH("tim/demo"))
268 }
269 if cfg.ProxySet() == nil || cfg.Listen != "0.0.0.0:8080" {
270 t.Fatalf("%+v", cfg)
271 }
272}
273
274func TestBehindProxyFlagRejected(t *testing.T) {
275 dir := t.TempDir()
276 path := filepath.Join(dir, "config.yml")
277 body := []byte("base_url: https://git.example.test\nstorage_path: " + dir + "\nbehind_proxy: true\nauth:\n mode: oauth\n client_id: git.example.test\n")
278 if err := os.WriteFile(path, body, 0o600); err != nil {
279 t.Fatal(err)
280 }
281 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "geheim")
282 _, err := Load(path)
283 if err == nil || !strings.Contains(err.Error(), "trusted_proxies") {
284 t.Fatalf("verwacht weigering van behind_proxy, kreeg %v", err)
285 }
286}
287
288func TestBadProxyRejected(t *testing.T) {
289 dir := t.TempDir()
290 path := filepath.Join(dir, "config.yml")
291 body := []byte("base_url: https://git.example.test\nstorage_path: " + dir + "\ntrusted_proxies: [ingress.local]\nauth:\n mode: oauth\n client_id: git.example.test\n")
292 if err := os.WriteFile(path, body, 0o600); err != nil {
293 t.Fatal(err)
294 }
295 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "geheim")
296 _, err := Load(path)
297 if err == nil || !strings.Contains(err.Error(), "trusted_proxies") {
298 t.Fatalf("verwacht weigering, kreeg %v", err)
299 }
300 body = []byte("base_url: https://git.example.test\nstorage_path: " + dir + "\nauth:\n mode: oauth\n client_id: git.example.test\nssh:\n enabled: true\n public_host: git.example.test:22\n")
301 path = filepath.Join(dir, "port.yml")
302 if err := os.WriteFile(path, body, 0o600); err != nil {
303 t.Fatal(err)
304 }
305 _, err = Load(path)
306 if err == nil || !strings.Contains(err.Error(), "public_host") {
307 t.Fatalf("verwacht weigering van een poort in de host, kreeg %v", err)
308 }
309}
310
311func TestCloneSSHForms(t *testing.T) {
312 c := Config{SSH: SSH{Enabled: true, PublicHost: "git.example.test", PublicPort: 22}}
313 if got := c.CloneSSH("tim/demo"); got != "git@git.example.test:tim/demo.git" {
314 t.Fatal(got)
315 }
316 c.SSH.PublicPort = 2222
317 if got := c.CloneSSH("tim/demo"); got != "ssh://git@git.example.test:2222/tim/demo.git" {
318 t.Fatal(got)
319 }
320 c.SSH.PublicHost, c.SSH.PublicPort = "::1", 22
321 if got := c.CloneSSH("tim/demo"); got != "git@[::1]:tim/demo.git" {
322 t.Fatal(got)
323 }
324}