BlueGit

timfa bluegit

Files

bluegit / internal / config / config_test.go

config_test.go

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