BlueGit

timfa bluegit

Files

bluegit / internal / config / config_test.go

config_test.go

feat: webhooks weren privénetten en de beheerder kiest de datadisk
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
1ab44366 History
config_test.go 4.4 KiB Download Raw
1package config
2
3import (
4 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8)
9
10func TestStoragePathOptional(t *testing.T) {
11 dir := t.TempDir()
12 path := filepath.Join(dir, "config.yml")
13 body := []byte("base_url: https://git.example.test\nauth:\n mode: oauth\n client_id: git.example.test\n")
14 if err := os.WriteFile(path, body, 0o600); err != nil {
15 t.Fatal(err)
16 }
17 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "geheim")
18 cfg, err := Load(path)
19 if err != nil {
20 t.Fatal(err)
21 }
22 if cfg.StoragePath != "" {
23 t.Fatalf("geen standaardpad verwacht, kreeg %q", cfg.StoragePath)
24 }
25 if cfg.StatePath != "/var/lib/bluegit" {
26 t.Fatal(cfg.StatePath)
27 }
28 if strings.Contains(Example(), "/mnt/storage") {
29 t.Fatal("voorbeeldconfiguratie bevat nog een vast schijfpad")
30 }
31}
32
33func TestDevModeRefusedOnProductionHost(t *testing.T) {
34 dir := t.TempDir()
35 path := filepath.Join(dir, "config.yml")
36 body := []byte("base_url: https://git.timfalken.com\nstorage_path: /mnt/storage/bluegit\nauth:\n mode: dev\n")
37 if err := os.WriteFile(path, body, 0o600); err != nil {
38 t.Fatal(err)
39 }
40 _, err := Load(path)
41 if err == nil || !strings.Contains(err.Error(), "dev") {
42 t.Fatalf("verwacht weigering, kreeg %v", err)
43 }
44}
45
46func TestSecretFromEnv(t *testing.T) {
47 dir := t.TempDir()
48 path := filepath.Join(dir, "config.yml")
49 body := []byte("base_url: https://git.example.test\nstorage_path: " + dir + "\nauth:\n mode: oauth\n client_id: git.example.test\n")
50 if err := os.WriteFile(path, body, 0o600); err != nil {
51 t.Fatal(err)
52 }
53 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "uit-de-omgeving")
54 cfg, err := Load(path)
55 if err != nil {
56 t.Fatal(err)
57 }
58 if cfg.Auth.ClientSecret != "uit-de-omgeving" || cfg.Git.DefaultBranch != "master" {
59 t.Fatalf("branch %s geheim %s", cfg.Git.DefaultBranch, cfg.Auth.ClientSecret)
60 }
61 if cfg.RedirectURI() != "https://git.example.test/auth/callback" {
62 t.Fatal(cfg.RedirectURI())
63 }
64 if cfg.SSHPublicHost() != "git.example.test" || cfg.SSHPublicPort() != 2222 {
65 t.Fatalf("ssh %s:%d", cfg.SSHPublicHost(), cfg.SSHPublicPort())
66 }
67 if cfg.CloneSSH("tim/demo") != "" {
68 t.Fatal("ssh staat uit, dus geen clone-URL")
69 }
70}
71
72func TestPublicSSHAndTrustedProxy(t *testing.T) {
73 dir := t.TempDir()
74 path := filepath.Join(dir, "config.yml")
75 body := []byte(`base_url: https://git.timfalken.com
76storage_path: ` + dir + `
77listen: 0.0.0.0:8080
78trusted_proxies: ["10.0.0.2", "192.168.0.0/16"]
79auth:
80 mode: oauth
81 client_id: git.timfalken.com
82ssh:
83 enabled: true
84 listen: 0.0.0.0:2222
85 public_host: git.timfalken.com
86 public_port: 22
87`)
88 if err := os.WriteFile(path, body, 0o600); err != nil {
89 t.Fatal(err)
90 }
91 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "geheim")
92 cfg, err := Load(path)
93 if err != nil {
94 t.Fatal(err)
95 }
96 if cfg.CloneSSH("tim/demo") != "ssh://git@git.timfalken.com:22/tim/demo.git" {
97 t.Fatal(cfg.CloneSSH("tim/demo"))
98 }
99 if cfg.ProxySet() == nil || cfg.Listen != "0.0.0.0:8080" {
100 t.Fatalf("%+v", cfg)
101 }
102}
103
104func TestBehindProxyFlagRejected(t *testing.T) {
105 dir := t.TempDir()
106 path := filepath.Join(dir, "config.yml")
107 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")
108 if err := os.WriteFile(path, body, 0o600); err != nil {
109 t.Fatal(err)
110 }
111 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "geheim")
112 _, err := Load(path)
113 if err == nil || !strings.Contains(err.Error(), "trusted_proxies") {
114 t.Fatalf("verwacht weigering van behind_proxy, kreeg %v", err)
115 }
116}
117
118func TestBadProxyRejected(t *testing.T) {
119 dir := t.TempDir()
120 path := filepath.Join(dir, "config.yml")
121 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")
122 if err := os.WriteFile(path, body, 0o600); err != nil {
123 t.Fatal(err)
124 }
125 t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "geheim")
126 _, err := Load(path)
127 if err == nil || !strings.Contains(err.Error(), "trusted_proxies") {
128 t.Fatalf("verwacht weigering, kreeg %v", err)
129 }
130 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")
131 path = filepath.Join(dir, "port.yml")
132 if err := os.WriteFile(path, body, 0o600); err != nil {
133 t.Fatal(err)
134 }
135 _, err = Load(path)
136 if err == nil || !strings.Contains(err.Error(), "public_host") {
137 t.Fatalf("verwacht weigering van een poort in de host, kreeg %v", err)
138 }
139}