| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestRunnerName(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.Runner.Name != "Steve" { |
| 23 | t.Fatalf("standaard %q", cfg.Runner.Name) |
| 24 | } |
| 25 | body = []byte("base_url: https://git.example.test\nrunner:\n name: \" Ada \"\nauth:\n mode: oauth\n client_id: git.example.test\n") |
| 26 | if err := os.WriteFile(path, body, 0o600); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | cfg, err = Load(path) |
| 30 | if err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | if cfg.Runner.Name != "Ada" { |
| 34 | t.Fatalf("naam %q", cfg.Runner.Name) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func TestStoragePathOptional(t *testing.T) { |
| 39 | dir := t.TempDir() |
| 40 | path := filepath.Join(dir, "config.yml") |
| 41 | body := []byte("base_url: https://git.example.test\nauth:\n mode: oauth\n client_id: git.example.test\n") |
| 42 | if err := os.WriteFile(path, body, 0o600); err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "geheim") |
| 46 | cfg, err := Load(path) |
| 47 | if err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | if cfg.StoragePath != "" { |
| 51 | t.Fatalf("geen standaardpad verwacht, kreeg %q", cfg.StoragePath) |
| 52 | } |
| 53 | if cfg.StatePath != "/var/lib/bluegit" { |
| 54 | t.Fatal(cfg.StatePath) |
| 55 | } |
| 56 | if strings.Contains(Example(), "/mnt/storage") { |
| 57 | t.Fatal("voorbeeldconfiguratie bevat nog een vast schijfpad") |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func TestDevModeRefusedOnProductionHost(t *testing.T) { |
| 62 | dir := t.TempDir() |
| 63 | path := filepath.Join(dir, "config.yml") |
| 64 | body := []byte("base_url: https://git.timfalken.com\nstorage_path: /mnt/storage/bluegit\nauth:\n mode: dev\n") |
| 65 | if err := os.WriteFile(path, body, 0o600); err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | _, err := Load(path) |
| 69 | if err == nil || !strings.Contains(err.Error(), "dev") { |
| 70 | t.Fatalf("verwacht weigering, kreeg %v", err) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func TestSecretFromEnv(t *testing.T) { |
| 75 | dir := t.TempDir() |
| 76 | path := filepath.Join(dir, "config.yml") |
| 77 | body := []byte("base_url: https://git.example.test\nstorage_path: " + dir + "\nauth:\n mode: oauth\n client_id: git.example.test\n") |
| 78 | if err := os.WriteFile(path, body, 0o600); err != nil { |
| 79 | t.Fatal(err) |
| 80 | } |
| 81 | t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "uit-de-omgeving") |
| 82 | cfg, err := Load(path) |
| 83 | if err != nil { |
| 84 | t.Fatal(err) |
| 85 | } |
| 86 | if cfg.Auth.ClientSecret != "uit-de-omgeving" || cfg.Git.DefaultBranch != "master" { |
| 87 | t.Fatalf("branch %s geheim %s", cfg.Git.DefaultBranch, cfg.Auth.ClientSecret) |
| 88 | } |
| 89 | if cfg.RedirectURI() != "https://git.example.test/auth/callback" { |
| 90 | t.Fatal(cfg.RedirectURI()) |
| 91 | } |
| 92 | if cfg.SSHPublicHost() != "git.example.test" || cfg.SSHPublicPort() != 2222 { |
| 93 | t.Fatalf("ssh %s:%d", cfg.SSHPublicHost(), cfg.SSHPublicPort()) |
| 94 | } |
| 95 | if cfg.CloneSSH("tim/demo") != "" { |
| 96 | t.Fatal("ssh staat uit, dus geen clone-URL") |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func TestPublicSSHAndTrustedProxy(t *testing.T) { |
| 101 | dir := t.TempDir() |
| 102 | path := filepath.Join(dir, "config.yml") |
| 103 | body := []byte(`base_url: https://git.timfalken.com |
| 104 | storage_path: ` + dir + ` |
| 105 | listen: 0.0.0.0:8080 |
| 106 | trusted_proxies: ["10.0.0.2", "192.168.0.0/16"] |
| 107 | auth: |
| 108 | mode: oauth |
| 109 | client_id: git.timfalken.com |
| 110 | ssh: |
| 111 | enabled: true |
| 112 | listen: 0.0.0.0:2222 |
| 113 | public_host: git.timfalken.com |
| 114 | public_port: 22 |
| 115 | `) |
| 116 | if err := os.WriteFile(path, body, 0o600); err != nil { |
| 117 | t.Fatal(err) |
| 118 | } |
| 119 | t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "geheim") |
| 120 | cfg, err := Load(path) |
| 121 | if err != nil { |
| 122 | t.Fatal(err) |
| 123 | } |
| 124 | if cfg.CloneSSH("tim/demo") != "ssh://git@git.timfalken.com:22/tim/demo.git" { |
| 125 | t.Fatal(cfg.CloneSSH("tim/demo")) |
| 126 | } |
| 127 | if cfg.ProxySet() == nil || cfg.Listen != "0.0.0.0:8080" { |
| 128 | t.Fatalf("%+v", cfg) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | func TestBehindProxyFlagRejected(t *testing.T) { |
| 133 | dir := t.TempDir() |
| 134 | path := filepath.Join(dir, "config.yml") |
| 135 | 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") |
| 136 | if err := os.WriteFile(path, body, 0o600); err != nil { |
| 137 | t.Fatal(err) |
| 138 | } |
| 139 | t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "geheim") |
| 140 | _, err := Load(path) |
| 141 | if err == nil || !strings.Contains(err.Error(), "trusted_proxies") { |
| 142 | t.Fatalf("verwacht weigering van behind_proxy, kreeg %v", err) |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | func TestBadProxyRejected(t *testing.T) { |
| 147 | dir := t.TempDir() |
| 148 | path := filepath.Join(dir, "config.yml") |
| 149 | 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") |
| 150 | if err := os.WriteFile(path, body, 0o600); err != nil { |
| 151 | t.Fatal(err) |
| 152 | } |
| 153 | t.Setenv("BLUEGIT_AUTH_CLIENT_SECRET", "geheim") |
| 154 | _, err := Load(path) |
| 155 | if err == nil || !strings.Contains(err.Error(), "trusted_proxies") { |
| 156 | t.Fatalf("verwacht weigering, kreeg %v", err) |
| 157 | } |
| 158 | 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") |
| 159 | path = filepath.Join(dir, "port.yml") |
| 160 | if err := os.WriteFile(path, body, 0o600); err != nil { |
| 161 | t.Fatal(err) |
| 162 | } |
| 163 | _, err = Load(path) |
| 164 | if err == nil || !strings.Contains(err.Error(), "public_host") { |
| 165 | t.Fatalf("verwacht weigering van een poort in de host, kreeg %v", err) |
| 166 | } |
| 167 | } |