File
Blob: cmd/client/cli_test.go
| 1 | package client |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | tunclient "go.miragespace.co/specter/tun/client" |
| 11 | |
| 12 | "github.com/stretchr/testify/require" |
| 13 | "github.com/urfave/cli/v3" |
| 14 | ) |
| 15 | |
| 16 | func TestConfigExampleUsesRootWriter(t *testing.T) { |
| 17 | var output bytes.Buffer |
| 18 | app := &cli.Command{Name: "specter", Writer: &output, Commands: []*cli.Command{Generate()}} |
| 19 | require.NoError(t, app.Run(t.Context(), []string{"specter", "client", "config-example"})) |
| 20 | require.Equal(t, tunclient.ExampleConfigYAML(), output.String()) |
| 21 | } |
| 22 | |
| 23 | func TestLightweightRejectsOwnerFlags(t *testing.T) { |
| 24 | for _, name := range []string{"expose", "serve"} { |
| 25 | app := &cli.Command{ |
| 26 | Name: "specter", |
| 27 | Commands: []*cli.Command{Generate()}, |
| 28 | } |
| 29 | require.ErrorContains(t, app.Run(t.Context(), []string{"specter", "client", name, "--apex", "example.com", "--server", "a", "http://localhost"}), "flag provided but not defined: -server") |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func TestLoadTunnelToken(t *testing.T) { |
| 34 | for _, tc := range []struct { |
| 35 | name string |
| 36 | file string |
| 37 | env string |
| 38 | fileSet bool |
| 39 | envSet bool |
| 40 | valid bool |
| 41 | }{ |
| 42 | { |
| 43 | name: "file", |
| 44 | file: " secret\n", |
| 45 | fileSet: true, |
| 46 | valid: true, |
| 47 | }, |
| 48 | { |
| 49 | name: "env", |
| 50 | env: " secret\n", |
| 51 | envSet: true, |
| 52 | valid: true, |
| 53 | }, |
| 54 | { |
| 55 | name: "both", |
| 56 | file: "secret", |
| 57 | env: "secret", |
| 58 | fileSet: true, |
| 59 | envSet: true, |
| 60 | }, |
| 61 | {name: "neither"}, |
| 62 | { |
| 63 | name: "large file", |
| 64 | file: strings.Repeat("x", 4097), |
| 65 | fileSet: true, |
| 66 | }, |
| 67 | { |
| 68 | name: "large env", |
| 69 | env: strings.Repeat("x", 4097), |
| 70 | envSet: true, |
| 71 | }, |
| 72 | { |
| 73 | name: "empty", |
| 74 | env: " \n", |
| 75 | envSet: true, |
| 76 | }, |
| 77 | } { |
| 78 | t.Run(tc.name, func(t *testing.T) { |
| 79 | path := "" |
| 80 | if tc.fileSet { |
| 81 | path = filepath.Join(t.TempDir(), "token") |
| 82 | require.NoError(t, os.WriteFile(path, []byte(tc.file), 0600)) |
| 83 | } |
| 84 | token, err := loadTunnelToken(path, tc.env, tc.envSet) |
| 85 | if tc.valid { |
| 86 | require.NoError(t, err) |
| 87 | require.Equal(t, "secret", token) |
| 88 | } else { |
| 89 | require.Error(t, err) |
| 90 | } |
| 91 | }) |
| 92 | } |
| 93 | } |