internal/config/config_test.go
145 lines · 3439 bytes
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 "notify_admin without smtp",
55 minimal + "\n[registration]\nnotify_admin = true\n",
56 "notify_admin = true requires [mail] smtp_host",
57 },
58 {
59 "system ssh with open registration",
60 minimal + "\n[ssh]\nmode = \"system\"\n[registration]\nmode = \"open\"\n[mail]\nsmtp_host = \"mx.example\"\nfrom = \"gitbay@example\"\n",
61 "requires registration.mode = \"closed\"",
62 },
63 {
64 "password auth in view_only",
65 minimal + "\n[web]\nmode = \"view_only\"\npassword_auth = true\n",
66 "password_auth",
67 },
68 {
69 "password auth not implemented",
70 minimal + "\n[web]\nmode = \"accounts\"\npassword_auth = true\n",
71 "not implemented",
72 },
73 {
74 "bad ssh mode",
75 minimal + "\n[ssh]\nmode = \"tcp\"\n",
76 "ssh.mode",
77 },
78 {
79 "unknown key",
80 "[server]\nroot = \"/var/lib/gitbay\"\nsite_url = \"https://gitbay.example\"\nbogus = 1\n",
81 "unknown config key",
82 },
83 {
84 "missing site_url",
85 "[server]\nroot = \"/var/lib/gitbay\"\n",
86 "site_url",
87 },
88 {
89 "negative repo limit",
90 minimal + "\n[limits]\nmax_repos_per_user = -1\n",
91 "must not be negative",
92 },
93 {
94 "negative snippet limit",
95 minimal + "\n[limits]\nmax_snippets_per_user = -1\n",
96 "max_snippets_per_user",
97 },
98 }
99 for _, tc := range cases {
100 t.Run(tc.name, func(t *testing.T) {
101 _, err := Load(writeConfig(t, tc.body))
102 if err == nil {
103 t.Fatalf("expected error containing %q, got nil", tc.wantErr)
104 }
105 if !strings.Contains(err.Error(), tc.wantErr) {
106 t.Fatalf("error %q does not contain %q", err, tc.wantErr)
107 }
108 })
109 }
110}
111
112func TestValidCombinations(t *testing.T) {
113 cases := []struct {
114 name string
115 body string
116 }{
117 {
118 "invite with smtp",
119 minimal + "\n[registration]\nmode = \"invite\"\n[mail]\nsmtp_host = \"mx.example\"\nfrom = \"gitbay@example\"\n",
120 },
121 {
122 "system ssh closed registration",
123 minimal + "\n[ssh]\nmode = \"system\"\n",
124 },
125 {
126 "accounts web without password auth",
127 minimal + "\n[web]\nmode = \"accounts\"\n",
128 },
129 {
130 "closed registration, no smtp at all",
131 minimal,
132 },
133 {
134 "acme with public https host",
135 "[server]\nroot = \"/var/lib/gitbay\"\nsite_url = \"https://gitbay.org\"\n[http]\ntls = \"acme\"\nacme_email = \"noreply@gitbay.org\"\n",
136 },
137 }
138 for _, tc := range cases {
139 t.Run(tc.name, func(t *testing.T) {
140 if _, err := Load(writeConfig(t, tc.body)); err != nil {
141 t.Fatal(err)
142 }
143 })
144 }
145}