internal/config/config_test.go

v1.26.0
gitbay/internal/config/config_test.go history · blame · raw

140 lines · 3290 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			"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			"negative repo limit",
 85			minimal + "\n[limits]\nmax_repos_per_user = -1\n",
 86			"must not be negative",
 87		},
 88		{
 89			"negative snippet limit",
 90			minimal + "\n[limits]\nmax_snippets_per_user = -1\n",
 91			"max_snippets_per_user",
 92		},
 93	}
 94	for _, tc := range cases {
 95		t.Run(tc.name, func(t *testing.T) {
 96			_, err := Load(writeConfig(t, tc.body))
 97			if err == nil {
 98				t.Fatalf("expected error containing %q, got nil", tc.wantErr)
 99			}
100			if !strings.Contains(err.Error(), tc.wantErr) {
101				t.Fatalf("error %q does not contain %q", err, tc.wantErr)
102			}
103		})
104	}
105}
106
107func TestValidCombinations(t *testing.T) {
108	cases := []struct {
109		name string
110		body string
111	}{
112		{
113			"invite with smtp",
114			minimal + "\n[registration]\nmode = \"invite\"\n[mail]\nsmtp_host = \"mx.example\"\nfrom = \"gitbay@example\"\n",
115		},
116		{
117			"system ssh closed registration",
118			minimal + "\n[ssh]\nmode = \"system\"\n",
119		},
120		{
121			"accounts web without password auth",
122			minimal + "\n[web]\nmode = \"accounts\"\n",
123		},
124		{
125			"closed registration, no smtp at all",
126			minimal,
127		},
128		{
129			"acme with public https host",
130			"[server]\nroot = \"/var/lib/gitbay\"\nsite_url = \"https://gitbay.org\"\n[http]\ntls = \"acme\"\nacme_email = \"noreply@gitbay.org\"\n",
131		},
132	}
133	for _, tc := range cases {
134		t.Run(tc.name, func(t *testing.T) {
135			if _, err := Load(writeConfig(t, tc.body)); err != nil {
136				t.Fatal(err)
137			}
138		})
139	}
140}