|
| 1 | +// Copyright 2026 Verda Cloud Oy |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package auth |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "os" |
| 20 | + "path/filepath" |
| 21 | + "runtime" |
| 22 | + "strings" |
| 23 | + "testing" |
| 24 | + |
| 25 | + cmdutil "github.com/verda-cloud/verda-cli/internal/verda-cli/cmd/util" |
| 26 | + "github.com/verda-cloud/verda-cli/internal/verda-cli/options" |
| 27 | +) |
| 28 | + |
| 29 | +// These tests cover the flag-driven write path only. Supplying both |
| 30 | +// --client-id and --client-secret is what skips the wizard (login.go checks |
| 31 | +// them with OR), and a wizard here would start a real tea.Program against the |
| 32 | +// developer's stdin — a hang when `go test` runs from a terminal. The |
| 33 | +// post-wizard "still empty" validation gate is therefore unreachable from a |
| 34 | +// test; covering it needs the engine injected rather than constructed inline. |
| 35 | + |
| 36 | +// sandboxHome points the whole config dir at a temp tree. VERDA_HOME, not |
| 37 | +// VERDA_SHARED_CREDENTIALS_FILE: login calls options.EnsureVerdaDir, which |
| 38 | +// resolves through VerdaDir and would mkdir the developer's real ~/.verda even |
| 39 | +// with the credentials path redirected elsewhere. |
| 40 | +func sandboxHome(t *testing.T) string { |
| 41 | + t.Helper() |
| 42 | + dir := t.TempDir() |
| 43 | + t.Setenv("VERDA_HOME", dir) |
| 44 | + t.Setenv("VERDA_SHARED_CREDENTIALS_FILE", filepath.Join(dir, "credentials")) |
| 45 | + return dir |
| 46 | +} |
| 47 | + |
| 48 | +func runAuthLoginForTest(t *testing.T, args ...string) error { |
| 49 | + t.Helper() |
| 50 | + streams := cmdutil.IOStreams{Out: &bytes.Buffer{}, ErrOut: &bytes.Buffer{}} |
| 51 | + cmd := NewCmdLogin(cmdutil.NewTestFactory(nil), streams) |
| 52 | + cmd.SetArgs(args) |
| 53 | + cmd.SetOut(streams.Out) |
| 54 | + cmd.SetErr(streams.ErrOut) |
| 55 | + cmd.SilenceUsage = true |
| 56 | + cmd.SilenceErrors = true |
| 57 | + return cmd.Execute() |
| 58 | +} |
| 59 | + |
| 60 | +func loadProfile(t *testing.T, path, profile string) *options.SharedCredentials { |
| 61 | + t.Helper() |
| 62 | + creds, err := options.LoadSharedCredentialsForProfile(path, profile) |
| 63 | + if err != nil { |
| 64 | + t.Fatalf("LoadSharedCredentialsForProfile(%q, %q): %v", path, profile, err) |
| 65 | + } |
| 66 | + return creds |
| 67 | +} |
| 68 | + |
| 69 | +func TestLoginWritesNewProfile(t *testing.T) { |
| 70 | + dir := sandboxHome(t) |
| 71 | + path := filepath.Join(dir, "credentials") |
| 72 | + |
| 73 | + if err := runAuthLoginForTest(t, "--client-id", "id-1", "--client-secret", "secret-1"); err != nil { |
| 74 | + t.Fatalf("login: %v", err) |
| 75 | + } |
| 76 | + |
| 77 | + got := loadProfile(t, path, "default") |
| 78 | + if got.ClientID != "id-1" { |
| 79 | + t.Errorf("ClientID = %q, want id-1", got.ClientID) |
| 80 | + } |
| 81 | + if got.ClientSecret != "secret-1" { |
| 82 | + t.Errorf("ClientSecret = %q, want secret-1", got.ClientSecret) |
| 83 | + } |
| 84 | + if got.BaseURL != defaultBaseURL { |
| 85 | + t.Errorf("BaseURL = %q, want %q", got.BaseURL, defaultBaseURL) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// A leaked secret is not recoverable, so the 0600 is load-bearing rather than |
| 90 | +// cosmetic. Windows has no mode bits to assert. |
| 91 | +func TestLoginRestrictsFilePermissions(t *testing.T) { |
| 92 | + if runtime.GOOS == "windows" { |
| 93 | + t.Skip("no Unix mode bits on Windows") |
| 94 | + } |
| 95 | + dir := sandboxHome(t) |
| 96 | + path := filepath.Join(dir, "credentials") |
| 97 | + |
| 98 | + if err := runAuthLoginForTest(t, "--client-id", "id", "--client-secret", "secret"); err != nil { |
| 99 | + t.Fatalf("login: %v", err) |
| 100 | + } |
| 101 | + |
| 102 | + info, err := os.Stat(path) |
| 103 | + if err != nil { |
| 104 | + t.Fatalf("stat: %v", err) |
| 105 | + } |
| 106 | + if perm := info.Mode().Perm(); perm != 0o600 { |
| 107 | + t.Errorf("mode = %#o, want 0600", perm) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func TestLoginWritesNamedProfileAndBaseURL(t *testing.T) { |
| 112 | + dir := sandboxHome(t) |
| 113 | + path := filepath.Join(dir, "credentials") |
| 114 | + |
| 115 | + err := runAuthLoginForTest(t, |
| 116 | + "--profile", "staging", |
| 117 | + "--base-url", "https://staging-api.verda.com/v1", |
| 118 | + "--client-id", "stg-id", |
| 119 | + "--client-secret", "stg-secret", |
| 120 | + ) |
| 121 | + if err != nil { |
| 122 | + t.Fatalf("login: %v", err) |
| 123 | + } |
| 124 | + |
| 125 | + got := loadProfile(t, path, "staging") |
| 126 | + if got.BaseURL != "https://staging-api.verda.com/v1" { |
| 127 | + t.Errorf("BaseURL = %q", got.BaseURL) |
| 128 | + } |
| 129 | + if got.ClientID != "stg-id" { |
| 130 | + t.Errorf("ClientID = %q, want stg-id", got.ClientID) |
| 131 | + } |
| 132 | + |
| 133 | + if _, err := options.LoadSharedCredentialsForProfile(path, "default"); err == nil { |
| 134 | + t.Error("a [default] section appeared; --profile must write only the named section") |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +// The writer merges into the existing INI. Dropping unrelated profiles would |
| 139 | +// destroy credentials the user cannot recover from the API. |
| 140 | +func TestLoginPreservesOtherProfiles(t *testing.T) { |
| 141 | + dir := sandboxHome(t) |
| 142 | + path := filepath.Join(dir, "credentials") |
| 143 | + |
| 144 | + seed := "[other]\n" + |
| 145 | + "verda_base_url = https://other.verda.com/v1\n" + |
| 146 | + "verda_client_id = other-id\n" + |
| 147 | + "verda_client_secret = other-secret\n" |
| 148 | + if err := os.WriteFile(path, []byte(seed), 0o600); err != nil { |
| 149 | + t.Fatalf("seed: %v", err) |
| 150 | + } |
| 151 | + |
| 152 | + if err := runAuthLoginForTest(t, "--client-id", "new-id", "--client-secret", "new-secret"); err != nil { |
| 153 | + t.Fatalf("login: %v", err) |
| 154 | + } |
| 155 | + |
| 156 | + other := loadProfile(t, path, "other") |
| 157 | + if other.ClientID != "other-id" || other.ClientSecret != "other-secret" { |
| 158 | + t.Errorf("[other] was modified: %+v", other) |
| 159 | + } |
| 160 | + if added := loadProfile(t, path, "default"); added.ClientID != "new-id" { |
| 161 | + t.Errorf("[default] ClientID = %q, want new-id", added.ClientID) |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +// Re-running login against a profile is the documented re-auth path: rotating a |
| 166 | +// secret must replace the stored one, not append or refuse. |
| 167 | +func TestLoginOverwritesSameProfile(t *testing.T) { |
| 168 | + dir := sandboxHome(t) |
| 169 | + path := filepath.Join(dir, "credentials") |
| 170 | + |
| 171 | + if err := runAuthLoginForTest(t, "--client-id", "old", "--client-secret", "old-secret"); err != nil { |
| 172 | + t.Fatalf("first login: %v", err) |
| 173 | + } |
| 174 | + if err := runAuthLoginForTest(t, "--client-id", "new", "--client-secret", "new-secret"); err != nil { |
| 175 | + t.Fatalf("second login: %v", err) |
| 176 | + } |
| 177 | + |
| 178 | + got := loadProfile(t, path, "default") |
| 179 | + if got.ClientID != "new" || got.ClientSecret != "new-secret" { |
| 180 | + t.Errorf("re-login did not replace credentials: %+v", got) |
| 181 | + } |
| 182 | + |
| 183 | + data, err := os.ReadFile(path) //nolint:gosec // test-owned temp file |
| 184 | + if err != nil { |
| 185 | + t.Fatalf("read: %v", err) |
| 186 | + } |
| 187 | + if n := strings.Count(string(data), "[default]"); n != 1 { |
| 188 | + t.Errorf("found %d [default] sections, want 1", n) |
| 189 | + } |
| 190 | + if strings.Contains(string(data), "old-secret") { |
| 191 | + t.Error("the replaced secret is still present in the file") |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +// --credentials-file outranks VERDA_SHARED_CREDENTIALS_FILE; sandboxHome sets |
| 196 | +// the env var, so a write landing at the flag path proves the precedence. |
| 197 | +func TestLoginCredentialsFileFlagWinsOverEnv(t *testing.T) { |
| 198 | + dir := sandboxHome(t) |
| 199 | + flagPath := filepath.Join(dir, "explicit-credentials") |
| 200 | + |
| 201 | + err := runAuthLoginForTest(t, |
| 202 | + "--credentials-file", flagPath, |
| 203 | + "--client-id", "flag-id", |
| 204 | + "--client-secret", "flag-secret", |
| 205 | + ) |
| 206 | + if err != nil { |
| 207 | + t.Fatalf("login: %v", err) |
| 208 | + } |
| 209 | + |
| 210 | + if got := loadProfile(t, flagPath, "default"); got.ClientID != "flag-id" { |
| 211 | + t.Errorf("ClientID = %q, want flag-id", got.ClientID) |
| 212 | + } |
| 213 | + if _, err := os.Stat(filepath.Join(dir, "credentials")); !os.IsNotExist(err) { |
| 214 | + t.Error("the env-var path was written despite --credentials-file") |
| 215 | + } |
| 216 | +} |
0 commit comments