|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/entireio/cli/internal/entireclient/tokenstore" |
| 12 | +) |
| 13 | + |
| 14 | +// failingTokenStore installs a backend whose Set always fails, standing in |
| 15 | +// for the locked/absent OS keyring a headless machine hits (#1036). Fault |
| 16 | +// injection (rather than filesystem permissions) keeps the failure |
| 17 | +// deterministic even when tests run as root, where permission bits don't |
| 18 | +// block writes. |
| 19 | +func failingTokenStore(t *testing.T) { |
| 20 | + t.Helper() |
| 21 | + restore := tokenstore.UseFailingBackendForTesting( |
| 22 | + filepath.Join(t.TempDir(), "tokens.json"), |
| 23 | + func(string, string) bool { return true }, |
| 24 | + ) |
| 25 | + t.Cleanup(restore) |
| 26 | +} |
| 27 | + |
| 28 | +// loginTestJWT builds a token that passes validateReceivedToken and carries |
| 29 | +// the iss/handle claims RecordLoginContext keys on. |
| 30 | +func loginTestJWT(t *testing.T, issuer string) string { |
| 31 | + t.Helper() |
| 32 | + exp := time.Now().Add(time.Hour).Unix() |
| 33 | + return makeJWT(t, `{"alg":"RS256"}`, fmt.Sprintf(`{"iss":%q,"handle":"alice","exp":%d}`, issuer, exp)) |
| 34 | +} |
| 35 | + |
| 36 | +// A login that reaches token persistence and fails there must tell headless |
| 37 | +// users about the file token store: the default backend is the OS keyring, |
| 38 | +// and on keyring-less machines (CI, containers, minimal server VMs) the raw |
| 39 | +// store error gives no way forward (#1036). Both store-write sites are |
| 40 | +// covered: the refresh-token write (refreshToken != "") fails first when a |
| 41 | +// refresh token is present, and the login-token write is the first store |
| 42 | +// write when there is none. |
| 43 | +func TestPersistLogin_StoreWriteFailureIncludesHeadlessHint(t *testing.T) { |
| 44 | + for name, refreshToken := range map[string]string{ |
| 45 | + "refresh-token write fails": "refresh-token", |
| 46 | + "login-token write fails": "", |
| 47 | + } { |
| 48 | + t.Run(name, func(t *testing.T) { |
| 49 | + // Not parallel: mutates the process-global tokenstore backend and |
| 50 | + // env. TestMain sets ENTIRE_TOKEN_STORE=file process-wide for |
| 51 | + // spawned-binary isolation; blank it so this test sees the |
| 52 | + // default-keyring condition a real user hits. |
| 53 | + t.Setenv("ENTIRE_TOKEN_STORE", "") |
| 54 | + failingTokenStore(t) |
| 55 | + |
| 56 | + var out bytes.Buffer |
| 57 | + err := persistLogin(&out, "https://example.test", loginTestJWT(t, "https://example.test"), refreshToken) |
| 58 | + if err == nil { |
| 59 | + t.Fatal("persistLogin should fail when the token store rejects writes") |
| 60 | + } |
| 61 | + if !strings.Contains(err.Error(), "ENTIRE_TOKEN_STORE=file") { |
| 62 | + t.Fatalf("store-write failure should point headless users at the file token store, got:\n%v", err) |
| 63 | + } |
| 64 | + if !strings.Contains(err.Error(), "ENTIRE_TOKEN_STORE_PATH") { |
| 65 | + t.Fatalf("hint should mention the path override, got:\n%v", err) |
| 66 | + } |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// When the user is already on the file backend, suggesting |
| 72 | +// ENTIRE_TOKEN_STORE=file would be nonsense — the raw error must pass |
| 73 | +// through without the headless hint. |
| 74 | +func TestPersistLogin_StoreWriteFailureOnFileBackend_NoHint(t *testing.T) { |
| 75 | + // Not parallel: mutates the process-global tokenstore backend and env. |
| 76 | + t.Setenv("ENTIRE_TOKEN_STORE", "file") |
| 77 | + failingTokenStore(t) |
| 78 | + |
| 79 | + var out bytes.Buffer |
| 80 | + err := persistLogin(&out, "https://example.test", loginTestJWT(t, "https://example.test"), "refresh-token") |
| 81 | + if err == nil { |
| 82 | + t.Fatal("persistLogin should fail when the token store rejects writes") |
| 83 | + } |
| 84 | + // Assert on the hint's structural markers, not its prose: the underlying |
| 85 | + // store error can never contain these, so the assertion stays meaningful |
| 86 | + // if the hint wording changes. |
| 87 | + if strings.Contains(err.Error(), "=file entire login") || strings.Contains(err.Error(), "ENTIRE_TOKEN_STORE_PATH") { |
| 88 | + t.Fatalf("hint must not appear when the file backend is already configured, got:\n%v", err) |
| 89 | + } |
| 90 | + if !strings.Contains(err.Error(), "save login") { |
| 91 | + t.Fatalf("underlying save failure should still surface, got:\n%v", err) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// Failures unrelated to the credential store (here: a token whose issuer |
| 96 | +// doesn't match the login server) must not carry the keyring hint — the |
| 97 | +// file store wouldn't help. |
| 98 | +func TestPersistLogin_NonStoreFailure_NoHint(t *testing.T) { |
| 99 | + // Not parallel: mutates process-global env. |
| 100 | + t.Setenv("ENTIRE_TOKEN_STORE", "") |
| 101 | + restore := tokenstore.UseFileBackendForTesting(filepath.Join(t.TempDir(), "tokens.json")) |
| 102 | + t.Cleanup(restore) |
| 103 | + |
| 104 | + exp := time.Now().Add(time.Hour).Unix() |
| 105 | + // iss mismatch with baseURL fails validateReceivedToken before any store write. |
| 106 | + token := makeJWT(t, `{"alg":"RS256"}`, fmt.Sprintf(`{"iss":"https://other.test","handle":"alice","exp":%d}`, exp)) |
| 107 | + |
| 108 | + var out bytes.Buffer |
| 109 | + err := persistLogin(&out, "https://example.test", token, "refresh-token") |
| 110 | + if err == nil { |
| 111 | + t.Fatal("persistLogin should reject a token from the wrong issuer") |
| 112 | + } |
| 113 | + if strings.Contains(err.Error(), "ENTIRE_TOKEN_STORE") { |
| 114 | + t.Fatalf("non-store failure must not carry the token-store hint, got:\n%v", err) |
| 115 | + } |
| 116 | +} |
0 commit comments