rickub/clipublic Fork 0
main
Commits
Clone
git clone https://git.rickub.com/rickub/cli.git
git clone ssh://git@rickub.com/rickub/cli.git
Initial import of the rickub CLI as a standalone public project 1a1d430Unverified · on main · Olivier Girardot · 9h ago
config_test.go · 203 lines · 5.7 KBGo Blame HistoryRaw
  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
200
201
202
203
package config

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

func TestSaveLoadRoundTrip(t *testing.T) {
	dir := t.TempDir()
	path := filepath.Join(dir, "config.yaml")

	in := &Config{}
	in.SetToken("http://localhost:3998", "rickub_pat_abc")
	if err := in.SaveTo(path); err != nil {
		t.Fatalf("SaveTo: %v", err)
	}

	// File must be 0600.
	info, err := os.Stat(path)
	if err != nil {
		t.Fatalf("stat: %v", err)
	}
	if perm := info.Mode().Perm(); perm != 0o600 {
		t.Errorf("perm = %o, want 600", perm)
	}

	out, err := LoadFrom(path)
	if err != nil {
		t.Fatalf("LoadFrom: %v", err)
	}
	if out.Host != "http://localhost:3998" {
		t.Errorf("host round-trip: got %q", out.Host)
	}
	if got := out.TokenFor("http://localhost:3998"); got != "rickub_pat_abc" {
		t.Errorf("token round-trip: got %q", got)
	}
}

func TestLoadMissingIsEmpty(t *testing.T) {
	out, err := LoadFrom(filepath.Join(t.TempDir(), "nope.yaml"))
	if err != nil {
		t.Fatalf("LoadFrom missing: %v", err)
	}
	if out.Host != "" || len(out.Hosts) != 0 {
		t.Errorf("expected empty config, got %+v", out)
	}
}

func TestResolveHostPrecedence(t *testing.T) {
	cfg := &Config{Host: "http://config-host"}

	// config only
	t.Setenv(EnvHost, "")
	if got := ResolveHost("", cfg); got != "http://config-host" {
		t.Errorf("config host: got %q", got)
	}
	// env overrides config
	t.Setenv(EnvHost, "http://env-host/")
	if got := ResolveHost("", cfg); got != "http://env-host" {
		t.Errorf("env host (trailing slash trimmed): got %q", got)
	}
	// flag overrides env
	if got := ResolveHost("http://flag-host", cfg); got != "http://flag-host" {
		t.Errorf("flag host: got %q", got)
	}
	// default when nothing set
	t.Setenv(EnvHost, "")
	if got := ResolveHost("", &Config{}); got != DefaultHost {
		t.Errorf("default host: got %q", got)
	}
}

func TestResolveTokenPrecedence(t *testing.T) {
	const host = "https://rickub.com"
	cfg := &Config{}
	cfg.SetToken(host, "cfg-token")

	t.Setenv(EnvToken, "")
	if got := ResolveToken("", cfg, host); got != "cfg-token" {
		t.Errorf("config token: got %q", got)
	}
	t.Setenv(EnvToken, "env-token")
	if got := ResolveToken("", cfg, host); got != "env-token" {
		t.Errorf("env token: got %q", got)
	}
	if got := ResolveToken("flag-token", cfg, host); got != "flag-token" {
		t.Errorf("flag token: got %q", got)
	}
}

// The stored token must never follow the host around: this is the leak the
// per-host binding exists to prevent.
func TestResolveTokenIsBoundToItsHost(t *testing.T) {
	t.Setenv(EnvToken, "")
	cfg := &Config{}
	cfg.SetToken("https://rickub.com", "rickub_pat_prod")

	if got := ResolveToken("", cfg, "https://evil.example"); got != "" {
		t.Errorf("stored token leaked to another host: got %q", got)
	}
	if got := ResolveToken("", cfg, "http://localhost:3000"); got != "" {
		t.Errorf("stored token leaked to a local host: got %q", got)
	}
	if got := ResolveToken("", cfg, "https://rickub.com"); got != "rickub_pat_prod" {
		t.Errorf("token not returned for its own host: got %q", got)
	}
	// An explicit token is the caller's own choice and works anywhere.
	if got := ResolveToken("explicit", cfg, "https://evil.example"); got != "explicit" {
		t.Errorf("explicit token: got %q", got)
	}
	t.Setenv(EnvToken, "env-token")
	if got := ResolveToken("", cfg, "https://evil.example"); got != "env-token" {
		t.Errorf("env token: got %q", got)
	}
}

func TestMultipleHostsKeepSeparateTokens(t *testing.T) {
	cfg := &Config{}
	cfg.SetToken("https://rickub.com", "prod")
	cfg.SetToken("http://localhost:3000/", "dev")

	if got := cfg.TokenFor("https://rickub.com"); got != "prod" {
		t.Errorf("prod token: got %q", got)
	}
	if got := cfg.TokenFor("http://localhost:3000"); got != "dev" {
		t.Errorf("dev token: got %q", got)
	}
	// SetToken makes the host it saved active.
	if cfg.Host != "http://localhost:3000" {
		t.Errorf("active host: got %q", cfg.Host)
	}

	// Logout only affects the named host.
	if !cfg.ClearToken("http://localhost:3000") {
		t.Error("ClearToken reported nothing removed")
	}
	if got := cfg.TokenFor("http://localhost:3000"); got != "" {
		t.Errorf("dev token survived logout: got %q", got)
	}
	if got := cfg.TokenFor("https://rickub.com"); got != "prod" {
		t.Errorf("prod token removed by dev logout: got %q", got)
	}
	if cfg.ClearToken("http://localhost:3000") {
		t.Error("ClearToken reported a removal on an empty host")
	}
}

func TestNormalizeHost(t *testing.T) {
	cases := map[string]string{
		"https://RickUB.com/":   "https://rickub.com",
		"HTTPS://rickub.com":    "https://rickub.com",
		" https://rickub.com  ": "https://rickub.com",
		"http://localhost:3000": "http://localhost:3000",
		"":                      "",
	}
	for in, want := range cases {
		if got := NormalizeHost(in); got != want {
			t.Errorf("NormalizeHost(%q) = %q, want %q", in, got, want)
		}
	}
}

func TestInsecureHostDetection(t *testing.T) {
	insecure := []string{"http://evil.example", "http://192.0.2.10:3000", "HTTP://Evil.Example/"}
	for _, h := range insecure {
		if !IsInsecureHost(h) {
			t.Errorf("IsInsecureHost(%q) = false, want true", h)
		}
	}
	secure := []string{
		"https://rickub.com",
		"http://localhost:3000",
		"http://127.0.0.1:3000",
		"http://[::1]:3000",
		"",
	}
	for _, h := range secure {
		if IsInsecureHost(h) {
			t.Errorf("IsInsecureHost(%q) = true, want false", h)
		}
	}
}

func TestWarnIfInsecure(t *testing.T) {
	var buf bytes.Buffer
	if !WarnIfInsecure(&buf, "http://evil.example") {
		t.Fatal("expected a warning for a plain-HTTP remote host")
	}
	if !strings.Contains(buf.String(), "evil.example") {
		t.Errorf("warning does not name the host: %q", buf.String())
	}

	buf.Reset()
	if WarnIfInsecure(&buf, "http://localhost:3000") {
		t.Error("warned about loopback")
	}
	if buf.Len() != 0 {
		t.Errorf("unexpected output: %q", buf.String())
	}
}