summaryrefslogtreecommitdiff
path: root/internal/config/config_test.go
blob: 39baa0b919b374c5a8a6bd65f4a84b3df6df03f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package config

import (
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"regexp"
	"strings"
	"testing"
)

func noSystemConfig(t *testing.T) {
	t.Helper()
	old := systemConfigPath
	systemConfigPath = filepath.Join(t.TempDir(), "absent.conf")
	t.Cleanup(func() { systemConfigPath = old })
}

func setConfKey(t *testing.T, key, value string) string {
	t.Helper()
	path := filepath.Join(t.TempDir(), "openstore.conf")
	buff, err := os.ReadFile("testdata/full.conf")
	if err != nil {
		t.Fatalf("read full.conf: %v", err)
	}

	expr := fmt.Sprintf("(%s)\\s*=.*", key)
	repl := fmt.Sprintf("${1} = %s", value)

	re := regexp.MustCompile(expr)
	result := re.ReplaceAll(buff, []byte(repl))

	if err = os.WriteFile(path, result, 0o644); err != nil {
		t.Fatalf("write conf: %v", err)
	}

	return path
}

func TestRejectLoadingMissingFile(t *testing.T) {
	noSystemConfig(t)

	_, err := Load(filepath.Join(t.TempDir(), "nonexistent.conf"))
	if err == nil {
		t.Fatalf("no error when loading missing config: %v", err)
	}
}

func TestLoadFullConfig(t *testing.T) {
	noSystemConfig(t)

	cfg, err := Load("testdata/full.conf")
	if err != nil {
		t.Fatalf("load: %v", err)
	}

	tests := []struct {
		field string
		got   string
		want  string
	}{
		{"addr", cfg.Serve.Addr, "127.0.0.1:8080"},
		{"name", cfg.Instance.Name, "Full Store"},
		{"contact-email", cfg.Instance.ContactEmail, "full@store.com"},
		{"base-url", cfg.Instance.BaseURL, "https://full.store.com"},
		{"database-name", cfg.Database.Name, "openstore"},
		{"database-user", cfg.Database.User, "openstore"},
		{"database-password", cfg.Database.Password, "12345678"},
	}

	for _, tt := range tests {
		if tt.got != tt.want {
			t.Errorf("%s: got %q, want %q", tt.field, tt.got, tt.want)
		}
	}
}

func TestLoadUserConfigWinsOverSystem(t *testing.T) {
	old := systemConfigPath
	systemConfigPath = "testdata/system.conf"
	t.Cleanup(func() { systemConfigPath = old })

	cfg, err := Load("testdata/partial.conf")
	if err != nil {
		t.Fatalf("load: %v", err)
	}

	if cfg.Instance.Name != "Only Name Set" {
		t.Errorf("user config should win, got name %q", cfg.Instance.Name)
	}
	if cfg.Instance.ContactEmail != "system@store.com" {
		t.Errorf("key absent from user config should fall back to system, got %q",
			cfg.Instance.ContactEmail)
	}
	if cfg.Serve.Addr != "127.0.0.1:9999" {
		t.Errorf("section absent from user config should fall back to system, got %q",
			cfg.Serve.Addr)
	}
}

func TestLoadMalformedFile(t *testing.T) {
	noSystemConfig(t)

	if _, err := Load("testdata/malformed.conf"); err == nil {
		t.Error("malformed config file loaded without error")
	}
}

func TestLoadRejectsBadBaseURL(t *testing.T) {
	noSystemConfig(t)

	tests := map[string]string{
		"not a url":      "garbage",
		"no scheme":      "example.com/path",
		"scheme only":    "https://",
		"path only":      "/listings",
		"bad url syntax": "://x",
	}

	for name, badURL := range tests {
		t.Run(name, func(t *testing.T) {
			path := setConfKey(t, "base-url", badURL)

			cfg, err := Load(path)
			if cfg != nil {
				t.Error("config with bad base-url created")
			}

			if !errors.Is(err, ErrBadConfigUrl) {
				t.Fatalf("bad base-url %q accepted", badURL)
			}

			if !strings.Contains(err.Error(), "base-url") {
				t.Errorf("error should mention base-url, got: %v", err)
			}
		})
	}
}

func TestLoadAcceptsGoodBaseURL(t *testing.T) {
	noSystemConfig(t)

	urls := []string{
		"https://example.com",
		"http://example.com",
		"https://example.com/subpath",
		"https://sub.example.com:8443",
	}

	for _, u := range urls {
		t.Run(u, func(t *testing.T) {
			path := setConfKey(t, "base-url", u)

			cfg, err := Load(path)
			if err != nil {
				t.Fatalf("good url %q rejected: %v", u, err)
			}
			if cfg.Instance.BaseURL != u {
				t.Errorf("base-url: got %q, want %q", cfg.Instance.BaseURL, u)
			}
		})
	}
}

func TetsRejectsConfigWithoutDatabase(t *testing.T) {
	noSystemConfig(t)
	_, err := Load("testdata/no-db.conf")
	if err == nil {
		t.Errorf("Load returned no error when database config is missing")
	}

	if !errors.Is(err, ErrBadConfig) {
		t.Errorf("Got error %v, wanted %v", err, ErrBadConfig)
	}
}

func TestDefaultPath(t *testing.T) {
	t.Setenv("XDG_CONFIG_HOME", "/tmp/xdg-test")

	path, err := DefaultPath()
	if err != nil {
		t.Fatalf("DefaultPath: %v", err)
	}

	want := filepath.Join("/tmp/xdg-test", "openstore", "openstore.conf")
	if path != want {
		t.Errorf("got %q, want %q", path, want)
	}
}

func TestDefaultPathWithoutConfigDir(t *testing.T) {
	t.Setenv("XDG_CONFIG_HOME", "")
	t.Setenv("HOME", "")

	if _, err := DefaultPath(); err == nil {
		t.Error("DefaultPath returned no error with no config dir available")
	}
}