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
package shared

import (
	"os"
	"path/filepath"
	"strings"
	"testing"
)

// A key that no longer does anything is ignored, like any unknown key, but
// with a warning naming it.
func TestRemovedKeyWarnings(t *testing.T) {
	if w := removedKeyWarnings([]byte(`{"host": "x", "unknown": 1}`)); len(w) != 0 {
		t.Errorf("no removed key set: %v", w)
	}
	w := removedKeyWarnings([]byte(`{"host": "x", "blocked_feeds_file": "/b", "allowed_requesters_file": ""}`))
	if len(w) != 2 ||
		!strings.Contains(w[0], "allowed_requesters_file") ||
		!strings.Contains(w[1], "blocked_feeds_file") {
		t.Errorf("warnings = %q; want one per removed key, sorted", w)
	}
}

// The keys that moved out of the birb object are inside it, so they are
// warned about under their "birb." names rather than passing unnoticed.
func TestRemovedKeyWarningsInsideBirb(t *testing.T) {
	w := removedKeyWarnings([]byte(`{
		"host": "x",
		"birb": {"user": "birb", "pub_key": "p", "priv_key": "q", "published": "2026-01-01T00:00:00Z"}
	}`))
	if len(w) != 3 ||
		!strings.Contains(w[0], "birb.priv_key") ||
		!strings.Contains(w[1], "birb.pub_key") ||
		!strings.Contains(w[2], "birb.published") {
		t.Errorf("warnings = %q; want one per removed birb key, sorted", w)
	}
	if w := removedKeyWarnings([]byte(`{"birb": {"user": "birb"}}`)); len(w) != 0 {
		t.Errorf("a birb that sets no removed key: %v", w)
	}
}

// Nothing in the secrets file is needed to serve, so a missing one is not an
// error: there are then no api_keys, and /api refuses every request.
func TestLoadConfigWithoutSecretsFile(t *testing.T) {
	dir := t.TempDir()
	cfgPath := filepath.Join(dir, "config.json")
	if err := os.WriteFile(cfgPath, []byte(`{"host": "kakapo.example.org"}`), 0o644); err != nil {
		t.Fatal(err)
	}
	t.Setenv(configVarName, cfgPath)
	t.Setenv(secretsVarName, filepath.Join(dir, "no-such-secrets.json"))

	cfg := LoadConfig()
	if cfg.Host != "kakapo.example.org" {
		t.Errorf("host = %q", cfg.Host)
	}
	if len(cfg.Secrets.ApiKeys) != 0 {
		t.Errorf("api keys = %v; want none", cfg.Secrets.ApiKeys)
	}
}

// The default secrets path is relative to the working directory, so a service
// started anywhere but a source tree does not find it — and from a working
// directory it may not read, the error is "permission denied" rather than "no
// such file". Neither is an error when SECRETS was not set: the path was only
// a guess at a development tree.
func TestLoadConfigUnreadableDefaultSecretsPath(t *testing.T) {
	dir := t.TempDir()
	cfgPath := filepath.Join(dir, "config.json")
	if err := os.WriteFile(cfgPath, []byte(`{"host": "kakapo.example.org"}`), 0o644); err != nil {
		t.Fatal(err)
	}
	t.Setenv(configVarName, cfgPath)
	t.Setenv(secretsVarName, "")

	// A working directory whose "dev" is unreadable, so Stat of the default
	// dev/secrets.dev.json fails with EACCES.
	wd := filepath.Join(dir, "wd")
	if err := os.MkdirAll(filepath.Join(wd, "dev"), 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(wd, "dev", "secrets.dev.json"), []byte(`{}`), 0o644); err != nil {
		t.Fatal(err)
	}
	if err := os.Chmod(filepath.Join(wd, "dev"), 0o000); err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { _ = os.Chmod(filepath.Join(wd, "dev"), 0o755) })
	t.Chdir(wd)

	cfg := LoadConfig()
	if len(cfg.Secrets.ApiKeys) != 0 {
		t.Errorf("api keys = %v; want none", cfg.Secrets.ApiKeys)
	}
}