krz/gitbay
A CLI-first git forge.
clone: git clone https://gitbay.org/krz/gitbay.git
main: internal/config/config_test.go · raw
1package config
2
3import (
4 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8)
9
10func writeConfig(t *testing.T, body string) string {
11 t.Helper()
12 p := filepath.Join(t.TempDir(), "config.toml")
13 if err := os.WriteFile(p, []byte(body), 0o600); err != nil {
14 t.Fatal(err)
15 }
16 return p
17}
18
19const minimal = `
20[server]
21root = "/var/lib/gitbay"
22site_url = "https://gitbay.example"
23`
24
25func TestLoadMinimal(t *testing.T) {
26 cfg, err := Load(writeConfig(t, minimal))
27 if err != nil {
28 t.Fatal(err)
29 }
30 // Defaults applied.
31 if cfg.SSH.Mode != "embedded" || cfg.SSH.Port != 22 {
32 t.Errorf("ssh defaults wrong: %+v", cfg.SSH)
33 }
34 if cfg.Web.Mode != "view_only" {
35 t.Errorf("web default wrong: %+v", cfg.Web)
36 }
37 if cfg.Registration.Mode != "closed" {
38 t.Errorf("registration default wrong: %+v", cfg.Registration)
39 }
40}
41
42func TestContradictions(t *testing.T) {
43 cases := []struct {
44 name string
45 body string
46 wantErr string
47 }{
48 {
49 "registration open without smtp",
50 minimal + "\n[registration]\nmode = \"open\"\n",
51 "requires [mail] smtp_host",
52 },
53 {
54 "system ssh with open registration",
55 minimal + "\n[ssh]\nmode = \"system\"\n[registration]\nmode = \"open\"\n[mail]\nsmtp_host = \"mx.example\"\nfrom = \"gitbay@example\"\n",
56 "requires registration.mode = \"closed\"",
57 },
58 {
59 "password auth in view_only",
60 minimal + "\n[web]\nmode = \"view_only\"\npassword_auth = true\n",
61 "password_auth",
62 },
63 {
64 "password auth not implemented",
65 minimal + "\n[web]\nmode = \"accounts\"\npassword_auth = true\n",
66 "not implemented",
67 },
68 {
69 "bad ssh mode",
70 minimal + "\n[ssh]\nmode = \"tcp\"\n",
71 "ssh.mode",
72 },
73 {
74 "unknown key",
75 "[server]\nroot = \"/var/lib/gitbay\"\nsite_url = \"https://gitbay.example\"\nbogus = 1\n",
76 "unknown config key",
77 },
78 {
79 "missing site_url",
80 "[server]\nroot = \"/var/lib/gitbay\"\n",
81 "site_url",
82 },
83 }
84 for _, tc := range cases {
85 t.Run(tc.name, func(t *testing.T) {
86 _, err := Load(writeConfig(t, tc.body))
87 if err == nil {
88 t.Fatalf("expected error containing %q, got nil", tc.wantErr)
89 }
90 if !strings.Contains(err.Error(), tc.wantErr) {
91 t.Fatalf("error %q does not contain %q", err, tc.wantErr)
92 }
93 })
94 }
95}
96
97func TestValidCombinations(t *testing.T) {
98 cases := []struct {
99 name string
100 body string
101 }{
102 {
103 "invite with smtp",
104 minimal + "\n[registration]\nmode = \"invite\"\n[mail]\nsmtp_host = \"mx.example\"\nfrom = \"gitbay@example\"\n",
105 },
106 {
107 "system ssh closed registration",
108 minimal + "\n[ssh]\nmode = \"system\"\n",
109 },
110 {
111 "accounts web without password auth",
112 minimal + "\n[web]\nmode = \"accounts\"\n",
113 },
114 {
115 "closed registration, no smtp at all",
116 minimal,
117 },
118 {
119 "acme with public https host",
120 "[server]\nroot = \"/var/lib/gitbay\"\nsite_url = \"https://gitbay.org\"\n[http]\ntls = \"acme\"\nacme_email = \"noreply@gitbay.org\"\n",
121 },
122 }
123 for _, tc := range cases {
124 t.Run(tc.name, func(t *testing.T) {
125 if _, err := Load(writeConfig(t, tc.body)); err != nil {
126 t.Fatal(err)
127 }
128 })
129 }
130}