1package config
 2
 3import (
 4	"os"
 5	"testing"
 6
 7	"github.com/matryer/is"
 8)
 9
10func TestParseMultipleKeys(t *testing.T) {
11	is := is.New(t)
12	td := t.TempDir()
13	is.NoErr(os.Setenv("SOFT_SERVE_INITIAL_ADMIN_KEYS", "testdata/k1.pub\nssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFxIobhwtfdwN7m1TFt9wx3PsfvcAkISGPxmbmbauST8 a@b"))
14	is.NoErr(os.Setenv("SOFT_SERVE_DATA_PATH", td))
15	t.Cleanup(func() {
16		is.NoErr(os.Unsetenv("SOFT_SERVE_INITIAL_ADMIN_KEYS"))
17		is.NoErr(os.Unsetenv("SOFT_SERVE_DATA_PATH"))
18	})
19	cfg := DefaultConfig()
20	is.NoErr(cfg.ParseEnv())
21	is.Equal(cfg.InitialAdminKeys, []string{
22		"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINMwLvyV3ouVrTysUYGoJdl5Vgn5BACKov+n9PlzfPwH",
23		"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFxIobhwtfdwN7m1TFt9wx3PsfvcAkISGPxmbmbauST8",
24	})
25}
26
27func TestMergeInitAdminKeys(t *testing.T) {
28	is := is.New(t)
29	is.NoErr(os.Setenv("SOFT_SERVE_INITIAL_ADMIN_KEYS", "testdata/k1.pub"))
30	t.Cleanup(func() { is.NoErr(os.Unsetenv("SOFT_SERVE_INITIAL_ADMIN_KEYS")) })
31	cfg := &Config{
32		DataPath:         t.TempDir(),
33		InitialAdminKeys: []string{"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFxIobhwtfdwN7m1TFt9wx3PsfvcAkISGPxmbmbauST8 a@b"},
34	}
35	is.NoErr(cfg.WriteConfig())
36	is.NoErr(cfg.Parse())
37	is.Equal(cfg.InitialAdminKeys, []string{
38		"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINMwLvyV3ouVrTysUYGoJdl5Vgn5BACKov+n9PlzfPwH",
39		"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFxIobhwtfdwN7m1TFt9wx3PsfvcAkISGPxmbmbauST8",
40	})
41}
42
43func TestValidateInitAdminKeys(t *testing.T) {
44	is := is.New(t)
45	cfg := &Config{
46		DataPath: t.TempDir(),
47		InitialAdminKeys: []string{
48			"testdata/k1.pub",
49			"abc",
50			"",
51		},
52	}
53	is.NoErr(cfg.WriteConfig())
54	is.NoErr(cfg.Parse())
55	is.Equal(cfg.InitialAdminKeys, []string{
56		"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINMwLvyV3ouVrTysUYGoJdl5Vgn5BACKov+n9PlzfPwH",
57	})
58}
59
60func TestCustomConfigLocation(t *testing.T) {
61	is := is.New(t)
62	td := t.TempDir()
63	t.Cleanup(func() {
64		is.NoErr(os.Unsetenv("SOFT_SERVE_CONFIG_LOCATION"))
65	})
66
67	// Test that we get data from the custom file location, and not from the data dir.
68	is.NoErr(os.Setenv("SOFT_SERVE_CONFIG_LOCATION", "testdata/config.yaml"))
69	is.NoErr(os.Setenv("SOFT_SERVE_DATA_PATH", td))
70	cfg := DefaultConfig()
71	is.NoErr(cfg.Parse())
72	is.Equal(cfg.Name, "Test server name")
73	// If we unset the custom location, then use the default location.
74	is.NoErr(os.Unsetenv("SOFT_SERVE_CONFIG_LOCATION"))
75	cfg = DefaultConfig()
76	is.Equal(cfg.Name, "Soft Serve")
77	// Test that if the custom config location doesn't exist, default to datapath config.
78	is.NoErr(os.Setenv("SOFT_SERVE_CONFIG_LOCATION", "testdata/config_nonexistent.yaml"))
79	cfg = DefaultConfig()
80	is.Equal(cfg.Name, "Soft Serve")
81}