//go:build !integration package oauth2 import ( "encoding/json" "net/http" "net/http/httptest" "net/url" "os" "path/filepath" "strings" "sync/atomic" "testing" "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/zalando/go-keyring" "golang.org/x/oauth2" "gitlab.com/gitlab-org/cli/internal/config" ) const testHost = "gitlab.com" // newKeyringConfig writes a keyring-backed OAuth config for testHost to a temp // directory and returns a config re-read from that directory, so its backing // dir is set and reload() works. The keyring is mocked per-test. func newKeyringConfig(t *testing.T, accessToken, refreshToken string, expiry time.Time) config.Config { t.Helper() return newKeyringConfigInDir(t, t.TempDir(), accessToken, refreshToken, expiry) } // tokenSourceForServer builds a configTokenSource whose token endpoint points at // srv, bypassing NewConfigTokenSource so the test controls the refresh response. func tokenSourceForServer(cfg config.Config, srv *httptest.Server) *configTokenSource { return &configTokenSource{ cfg: cfg, httpClient: srv.Client(), hostname: testHost, oauth2Config: &oauth2.Config{ ClientID: "test-client", Endpoint: oauth2.Endpoint{TokenURL: srv.URL + "/oauth/token"}, Scopes: scopes, }, } } // withoutAdoptBackoff removes the inter-attempt sleep in adopt() so a test that // exhausts the retries does not pause for adoptAttempts*adoptBackoff. func withoutAdoptBackoff(t *testing.T) { t.Helper() old := adoptBackoff adoptBackoff = 0 t.Cleanup(func() { adoptBackoff = old }) } func writeTokenResponse(t *testing.T, w http.ResponseWriter, accessToken, refreshToken string, expiresIn int) { t.Helper() w.Header().Set("Content-Type", "application/json") // assert, not require: this runs in the server's handler goroutine, where // require's FailNow (runtime.Goexit) would exit the wrong goroutine. assert.NoError(t, json.NewEncoder(w).Encode(map[string]any{ "access_token": accessToken, "refresh_token": refreshToken, "token_type": "bearer", "expires_in": expiresIn, })) } func writeInvalidGrant(t *testing.T, w http.ResponseWriter) { t.Helper() w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusBadRequest) // assert, not require: this runs in the server's handler goroutine. assert.NoError(t, json.NewEncoder(w).Encode(map[string]any{ "error": "invalid_grant", "error_description": "The provided authorization grant is invalid, expired, or revoked.", })) } func TestToken_SkipsRefreshWhenFreshestTokenIsValid(t *testing.T) { cfg := newKeyringConfig(t, "access-current", "refresh-current", time.Now().Add(time.Hour)) srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { t.Errorf("token endpoint should not be called when the token is still valid; got %s", r.URL.Path) writeInvalidGrant(t, w) })) defer srv.Close() ts := tokenSourceForServer(cfg, srv) token, err := ts.Token() require.NoError(t, err) assert.Equal(t, "access-current", token.AccessToken) } func TestToken_RefreshRotatesAndPersists(t *testing.T) { cfg := newKeyringConfig(t, "access-old", "refresh-old", time.Now().Add(-time.Hour)) srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if !assert.NoError(t, r.ParseForm()) { return } assert.Equal(t, "refresh_token", r.Form.Get("grant_type")) assert.Equal(t, "refresh-old", r.Form.Get("refresh_token")) writeTokenResponse(t, w, "access-new", "refresh-new", 7200) })) defer srv.Close() ts := tokenSourceForServer(cfg, srv) token, err := ts.Token() require.NoError(t, err) assert.Equal(t, "access-new", token.AccessToken) assert.Equal(t, "refresh-new", token.RefreshToken) // The rotated credentials are persisted to the keyring, not just returned. storedAccess, err := keyring.Get("glab:gitlab.com:token", "") require.NoError(t, err) assert.Equal(t, "access-new", storedAccess) storedRefresh, err := keyring.Get("glab:gitlab.com:oauth2_refresh_token", "") require.NoError(t, err) assert.Equal(t, "refresh-new", storedRefresh) } func TestToken_AdoptsConcurrentlyRotatedTokenOnInvalidGrant(t *testing.T) { dir := t.TempDir() cfg := newKeyringConfigInDir(t, dir, "access-old", "refresh-old", time.Now().Add(-time.Hour)) // The handler models a concurrent process that won the refresh race: it // commits a fresh, valid token to the keyring and config file, then returns // invalid_grant to us because our refresh token is now consumed. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // assert, not require: this runs in the server's handler goroutine. Return // early if the config cannot be re-read so we do not deref a nil winner. winner, err := config.ParseConfig(filepath.Join(dir, "config.yml")) if !assert.NoError(t, err) { writeInvalidGrant(t, w) return } assert.NoError(t, winner.Set(testHost, "token", "access-winner")) assert.NoError(t, winner.Set(testHost, "oauth2_refresh_token", "refresh-winner")) assert.NoError(t, winner.Set(testHost, "oauth2_expiry_date", time.Now().Add(time.Hour).Format(time.RFC3339))) assert.NoError(t, winner.Write()) writeInvalidGrant(t, w) })) defer srv.Close() ts := tokenSourceForServer(cfg, srv) token, err := ts.Token() require.NoError(t, err) assert.Equal(t, "access-winner", token.AccessToken, "should adopt the token the winning process rotated") } func TestToken_RefreshesInsideGracePeriod(t *testing.T) { // 4 minutes left: valid by x/oauth2's 10s default, stale under the grace. cfg := newKeyringConfig(t, "access-4min", "refresh-old", time.Now().Add(4*time.Minute)) var refreshes atomic.Int32 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { refreshes.Add(1) if !assert.NoError(t, r.ParseForm()) { return } assert.Equal(t, "refresh_token", r.Form.Get("grant_type")) assert.Equal(t, "refresh-old", r.Form.Get("refresh_token")) writeTokenResponse(t, w, "access-refreshed", "refresh-refreshed", 7200) })) defer srv.Close() ts := tokenSourceForServer(cfg, srv) token, err := ts.Token() require.NoError(t, err) assert.Equal(t, "access-refreshed", token.AccessToken, "a token inside the grace period must be refreshed, not returned as-is") assert.EqualValues(t, 1, refreshes.Load(), "the token endpoint must be called exactly once") } func TestToken_ReturnsCurrentTokenWhenProbeFailsInsideGracePeriod(t *testing.T) { // File-mode config (no keyring) whose directory cannot be written, so // CredentialWriteProbe fails on the directory check. Set in memory only: // nothing can be written here, and freshest() falls back to the in-memory // copy when the re-read fails. cfg := config.NewBlankConfigInDir(unwritableDir(t)) require.NoError(t, cfg.Set(testHost, "is_oauth2", "true")) require.NoError(t, cfg.Set(testHost, "oauth2_refresh_token", "refresh-old")) require.NoError(t, cfg.Set(testHost, "token", "access-4min")) require.NoError(t, cfg.Set(testHost, "oauth2_expiry_date", time.Now().Add(4*time.Minute).Format(time.RFC3339))) srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { t.Errorf("token endpoint must not be called when the refreshed credentials could not be saved; got %s", r.URL.Path) writeInvalidGrant(t, w) })) defer srv.Close() ts := tokenSourceForServer(cfg, srv) token, err := ts.Token() require.NoError(t, err, "a token that still passes the plain check must be returned, not turned into a failure") assert.Equal(t, "access-4min", token.AccessToken) } func TestToken_ReturnsProbeErrorWhenTokenIsExpired(t *testing.T) { // Same unwritable setup as the fallback test, but the token is already // dead. The fallback only applies to a token that still passes the plain // check; an expired one must surface the probe error, not be handed back. cfg := config.NewBlankConfigInDir(unwritableDir(t)) require.NoError(t, cfg.Set(testHost, "is_oauth2", "true")) require.NoError(t, cfg.Set(testHost, "oauth2_refresh_token", "refresh-old")) require.NoError(t, cfg.Set(testHost, "token", "access-expired")) require.NoError(t, cfg.Set(testHost, "oauth2_expiry_date", time.Now().Add(-time.Hour).Format(time.RFC3339))) srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { t.Errorf("token endpoint must not be called when the refreshed credentials could not be saved; got %s", r.URL.Path) writeInvalidGrant(t, w) })) defer srv.Close() ts := tokenSourceForServer(cfg, srv) _, err := ts.Token() require.Error(t, err) assert.ErrorContains(t, err, "could not be saved") } func TestToken_ReturnsAccessTokenOnlyTokenInsideGracePeriod(t *testing.T) { // is_oauth2 with an access token and no refresh token, as the Docker // credential helper builds it. Nothing can renew it, so inside the grace // window it must be returned while it is still usable, not refreshed. cfg := newKeyringConfig(t, "access-4min", "", time.Now().Add(4*time.Minute)) srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { t.Errorf("token endpoint must not be called with no refresh token; got %s", r.URL.Path) writeInvalidGrant(t, w) })) defer srv.Close() ts := tokenSourceForServer(cfg, srv) token, err := ts.Token() require.NoError(t, err) assert.Equal(t, "access-4min", token.AccessToken) } func TestToken_AdoptsWinnerTokenInsideGracePeriod(t *testing.T) { withoutAdoptBackoff(t) dir := t.TempDir() cfg := newKeyringConfigInDir(t, dir, "access-old", "refresh-old", time.Now().Add(-time.Hour)) // The winning process committed a token with 4 minutes left, then our // refresh fails with invalid_grant. adopt must accept that token: the // refresh token is spent, so there is nothing to renew with, and a // usable token in the config must not force a re-login. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { winner, err := config.ParseConfig(filepath.Join(dir, "config.yml")) if !assert.NoError(t, err) { writeInvalidGrant(t, w) return } assert.NoError(t, winner.Set(testHost, "token", "access-winner-4min")) assert.NoError(t, winner.Set(testHost, "oauth2_refresh_token", "refresh-winner")) assert.NoError(t, winner.Set(testHost, "oauth2_expiry_date", time.Now().Add(4*time.Minute).Format(time.RFC3339))) assert.NoError(t, winner.Write()) writeInvalidGrant(t, w) })) defer srv.Close() ts := tokenSourceForServer(cfg, srv) token, err := ts.Token() require.NoError(t, err) assert.Equal(t, "access-winner-4min", token.AccessToken, "adopt must keep the plain 10s check and accept the winner's token") } func TestToken_ReturnsErrorWhenInvalidGrantWithoutRecovery(t *testing.T) { withoutAdoptBackoff(t) cfg := newKeyringConfig(t, "access-old", "refresh-old", time.Now().Add(-time.Hour)) // A genuinely revoked/expired session: invalid_grant with no valid token ever // appearing on disk. We must surface the error rather than loop forever. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { writeInvalidGrant(t, w) })) defer srv.Close() ts := tokenSourceForServer(cfg, srv) _, err := ts.Token() require.Error(t, err) assert.True(t, isInvalidGrant(err), "expected the original invalid_grant error to be surfaced") } func TestToken_ForgetsRejectedRefreshToken(t *testing.T) { withoutAdoptBackoff(t) cfg := newKeyringConfig(t, "access-old", "refresh-old", time.Now().Add(-time.Hour)) var refreshes atomic.Int32 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { refreshes.Add(1) writeInvalidGrant(t, w) })) defer srv.Close() ts := tokenSourceForServer(cfg, srv) _, err := ts.Token() require.Error(t, err) assert.True(t, isInvalidGrant(err)) _, err = keyring.Get("glab:gitlab.com:oauth2_refresh_token", "") require.ErrorIs(t, err, keyring.ErrNotFound, "the rejected refresh token must be deleted") sent := refreshes.Load() _, err = ts.Token() require.Error(t, err) assert.Equal(t, sent, refreshes.Load(), "a forgotten refresh token must not be sent again") } func TestToken_ForgetsRejectedRefreshToken_FileBacked(t *testing.T) { withoutAdoptBackoff(t) keyring.MockInit() t.Cleanup(keyring.MockInit) dir := t.TempDir() writeOAuthConfig(t, dir, false, "access-old", "refresh-old", time.Now().Add(-time.Hour)) cfg, err := config.ParseConfig(filepath.Join(dir, "config.yml")) require.NoError(t, err) var refreshes atomic.Int32 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { refreshes.Add(1) writeInvalidGrant(t, w) })) defer srv.Close() ts := tokenSourceForServer(cfg, srv) _, err = ts.Token() require.Error(t, err) assert.True(t, isInvalidGrant(err)) onDisk, err := os.ReadFile(filepath.Join(dir, "config.yml")) require.NoError(t, err) assert.NotContains(t, string(onDisk), "refresh-old", "the rejected refresh token must be removed from config.yml") sent := refreshes.Load() _, err = tokenSourceForServer(cfg, srv).Token() require.Error(t, err) assert.Equal(t, sent, refreshes.Load(), "a forgotten refresh token must not be sent again") } func TestToken_KeepsRefreshTokenRotatedAfterRejection(t *testing.T) { withoutAdoptBackoff(t) dir := t.TempDir() cfg := newKeyringConfigInDir(t, dir, "access-old", "refresh-old", time.Now().Add(-time.Hour)) // Another process stores a different refresh token, with an access token // adopt won't take. forget must not delete a token it never presented. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { other, err := config.ParseConfig(filepath.Join(dir, "config.yml")) if !assert.NoError(t, err) { writeInvalidGrant(t, w) return } assert.NoError(t, other.Set(testHost, "oauth2_refresh_token", "refresh-other")) assert.NoError(t, other.Write()) writeInvalidGrant(t, w) })) defer srv.Close() ts := tokenSourceForServer(cfg, srv) _, err := ts.Token() require.Error(t, err) stored, err := keyring.Get("glab:gitlab.com:oauth2_refresh_token", "") require.NoError(t, err) assert.Equal(t, "refresh-other", stored) } func TestIsInvalidGrant(t *testing.T) { assert.True(t, isInvalidGrant(&oauth2.RetrieveError{ErrorCode: "invalid_grant"})) assert.False(t, isInvalidGrant(&oauth2.RetrieveError{ErrorCode: "invalid_client"})) assert.False(t, isInvalidGrant(&url.Error{Op: "Post", Err: assert.AnError})) assert.False(t, isInvalidGrant(nil)) } func TestValidWithGrace(t *testing.T) { tests := []struct { name string token *oauth2.Token want bool }{ {name: "nil token", token: nil, want: false}, {name: "empty access token", token: &oauth2.Token{Expiry: time.Now().Add(time.Hour)}, want: false}, {name: "no expiry recorded", token: &oauth2.Token{AccessToken: "a"}, want: true}, {name: "one hour left", token: &oauth2.Token{AccessToken: "a", Expiry: time.Now().Add(time.Hour)}, want: true}, {name: "four minutes left", token: &oauth2.Token{AccessToken: "a", Expiry: time.Now().Add(4 * time.Minute)}, want: false}, {name: "expired", token: &oauth2.Token{AccessToken: "a", Expiry: time.Now().Add(-time.Minute)}, want: false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { assert.Equal(t, tt.want, validWithGrace(tt.token)) }) } } // TestToken_ActsOnFreshestStateNotStartupCopy verifies that a token source // constructed with a stale startup config acts on the freshest persisted state: // when another process has rotated the credentials to a valid token, Token() // returns that token and never contacts the endpoint. Covered for both storage // modes because a keyring host reads its secrets live while a file-backed host // relies on the config re-read. func TestToken_ActsOnFreshestStateNotStartupCopy(t *testing.T) { for _, tc := range []struct { name string useKeyring bool }{ {"keyring-backed", true}, {"file-backed", false}, } { t.Run(tc.name, func(t *testing.T) { keyring.MockInit() t.Cleanup(keyring.MockInit) dir := t.TempDir() // Startup state persisted to disk: an already-expired token, then loaded // into a config that stands in for a long-lived process's in-memory copy. writeOAuthConfig(t, dir, tc.useKeyring, "access-startup", "refresh-startup", time.Now().Add(-time.Hour)) startup, err := config.ParseConfig(filepath.Join(dir, "config.yml")) require.NoError(t, err) // Another process rotates the credentials to a fresh, valid token. writeOAuthConfig(t, dir, tc.useKeyring, "access-fresh", "refresh-fresh", time.Now().Add(time.Hour)) // The freshest token is valid, so the endpoint must not be contacted. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { t.Errorf("token endpoint should not be called; the freshest token is valid") writeInvalidGrant(t, w) })) defer srv.Close() ts := tokenSourceForServer(startup, srv) token, err := ts.Token() require.NoError(t, err) assert.Equal(t, "access-fresh", token.AccessToken, "Token must act on the freshest persisted state, not the startup in-memory copy") }) } } // TestToken_RefreshDoesNotOverwriteConcurrentConfigWrites verifies that a // refresh persists through a freshly re-read config, so a write another process // made after this process loaded its config (here, rotating a second host's // token) is not rolled back when the refresh writes. The narrow window of a // write landing *during* the network refresh is closed by the cross-process // lock added in !3679 (mechanism A). func TestToken_RefreshDoesNotOverwriteConcurrentConfigWrites(t *testing.T) { keyring.MockInit() t.Cleanup(keyring.MockInit) dir := t.TempDir() const otherHost = "other.example.com" // Our host starts with an expired token (file-backed so credentials live in // the document being serialized). writeOAuthConfig(t, dir, false, "access-old", "refresh-old", time.Now().Add(-time.Hour)) startup, err := config.ParseConfig(filepath.Join(dir, "config.yml")) require.NoError(t, err) // Another process adds/rotates a second host's token on disk after we loaded. other, err := config.ParseConfig(filepath.Join(dir, "config.yml")) require.NoError(t, err) require.NoError(t, other.Set(otherHost, "token", "other-rotated")) require.NoError(t, other.Write()) srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { writeTokenResponse(t, w, "access-new", "refresh-new", 7200) })) defer srv.Close() ts := tokenSourceForServer(startup, srv) _, err = ts.Token() require.NoError(t, err) onDisk, err := config.ParseConfig(filepath.Join(dir, "config.yml")) require.NoError(t, err) ourToken, _, err := onDisk.GetWithSource(testHost, "token", false) require.NoError(t, err) assert.Equal(t, "access-new", ourToken, "our refreshed token should be persisted") otherToken, _, err := onDisk.GetWithSource(otherHost, "token", false) require.NoError(t, err) assert.Equal(t, "other-rotated", otherToken, "a config write from another process must survive the refresh") } // TestNewConfigTokenSource_IgnoresEnvironmentToken pins the fix for the gap // Timo flagged on !3704: api.WithoutTokenFromEnvironment set // searchEnvForIdentity=false for the token and is_oauth2 lookups in // api.NewClientFromConfig, but NewConfigTokenSource still read "token" through // cfg.Get, which always searches GITLAB_TOKEN/GITLAB_ACCESS_TOKEN/OAUTH_TOKEN // regardless of that option. A caller like the Docker credential helper, // which runs as a subprocess inheriting the user's shell environment, could // therefore mint a request using a stray environment token even though it // asked not to. func TestNewConfigTokenSource_IgnoresEnvironmentToken(t *testing.T) { for _, tc := range []struct { name string searchEnvForIdentity bool want string }{ {"searches env when asked to", true, "env-token"}, {"ignores env when told not to", false, "config-token"}, } { t.Run(tc.name, func(t *testing.T) { t.Setenv("GITLAB_TOKEN", "env-token") expiry := time.Now().Add(time.Hour).Format(time.RFC3339) cfg := config.NewFromString(` --- hosts: ` + testHost + `: is_oauth2: "true" client_id: abc token: config-token oauth2_refresh_token: refresh-token oauth2_expiry_date: ` + expiry + ` `) ts, err := NewConfigTokenSource(cfg, &http.Client{}, "https", testHost, tc.searchEnvForIdentity) require.NoError(t, err) token, err := ts.Token() require.NoError(t, err) assert.Equal(t, tc.want, token.AccessToken) }) } } // TestNewConfigTokenSource_RefreshesInsideGracePeriod goes through // NewConfigTokenSource; the other grace tests build configTokenSource directly. func TestNewConfigTokenSource_RefreshesInsideGracePeriod(t *testing.T) { var refreshes atomic.Int32 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { refreshes.Add(1) writeTokenResponse(t, w, "access-renewed", "refresh-renewed", 3600) })) defer srv.Close() host := strings.TrimPrefix(srv.URL, "http://") cfg := config.NewFromString(` --- hosts: ` + host + `: is_oauth2: "true" client_id: abc token: access-4min oauth2_refresh_token: refresh-old oauth2_expiry_date: ` + time.Now().Add(4*time.Minute).Format(time.RFC3339) + ` `) ts, err := NewConfigTokenSource(cfg, srv.Client(), "http", host, false) require.NoError(t, err) token, err := ts.Token() require.NoError(t, err) assert.Equal(t, "access-renewed", token.AccessToken) assert.EqualValues(t, 1, refreshes.Load()) } // newKeyringConfigInDir is newKeyringConfig with a caller-provided directory, so // a test can also open a second config pointing at the same files. func newKeyringConfigInDir(t *testing.T, dir, accessToken, refreshToken string, expiry time.Time) config.Config { t.Helper() keyring.MockInit() t.Cleanup(keyring.MockInit) seed := config.NewBlankConfigInDir(dir) require.NoError(t, seed.Set(testHost, "use_keyring", "true")) require.NoError(t, seed.Set(testHost, "is_oauth2", "true")) require.NoError(t, seed.Set(testHost, "oauth2_refresh_token", refreshToken)) require.NoError(t, seed.Set(testHost, "token", accessToken)) require.NoError(t, seed.Set(testHost, "oauth2_expiry_date", expiry.Format(time.RFC3339))) require.NoError(t, seed.Write()) cfg, err := config.ParseConfig(filepath.Join(dir, "config.yml")) require.NoError(t, err) return cfg } // writeOAuthConfig persists an OAuth config for testHost to dir in either // keyring or plaintext-file storage mode. Callers re-read it with // config.ParseConfig. The keyring must already be mocked by the caller. func writeOAuthConfig(t *testing.T, dir string, useKeyring bool, accessToken, refreshToken string, expiry time.Time) { t.Helper() seed := config.NewBlankConfigInDir(dir) if useKeyring { require.NoError(t, seed.Set(testHost, "use_keyring", "true")) } require.NoError(t, seed.Set(testHost, "is_oauth2", "true")) require.NoError(t, seed.Set(testHost, "oauth2_refresh_token", refreshToken)) require.NoError(t, seed.Set(testHost, "token", accessToken)) require.NoError(t, seed.Set(testHost, "oauth2_expiry_date", expiry.Format(time.RFC3339))) require.NoError(t, seed.Write()) } // The refresh endpoint is built from the host config, so a self-managed instance // installed under a subfolder must have that subfolder in the token URL. #8399 // fixed this for the authorization URL; the refresh path still went to the host // root, so refreshing on such an instance hit the wrong endpoint and failed. func TestNewConfigTokenSource_RefreshUsesConfiguredSubfolder(t *testing.T) { for _, tc := range []struct { name string subfolder string wantPath string }{ {"subfolder configured", "gitlab", "/gitlab/oauth/token"}, {"surrounding slashes are trimmed", "/gitlab/", "/gitlab/oauth/token"}, {"no subfolder", "", "/oauth/token"}, } { t.Run(tc.name, func(t *testing.T) { var gotPath string srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { gotPath = r.URL.Path writeTokenResponse(t, w, "new-access-token", "new-refresh-token", 3600) })) defer srv.Close() host := strings.TrimPrefix(srv.URL, "http://") expired := time.Now().Add(-time.Hour).Format(time.RFC3339) cfg := config.NewFromString(` --- hosts: "` + host + `": is_oauth2: "true" client_id: abc token: expired-token oauth2_refresh_token: refresh-token oauth2_expiry_date: ` + expired + ` subfolder: ` + `"` + tc.subfolder + `"` + ` `) ts, err := NewConfigTokenSource(cfg, srv.Client(), "http", host, false) require.NoError(t, err) token, err := ts.Token() require.NoError(t, err) assert.Equal(t, "new-access-token", token.AccessToken) assert.Equal(t, tc.wantPath, gotPath, "refresh must go to the configured subfolder") }) } } // unwritableDir returns a path whose parent is a regular file, so MkdirAll // fails with ENOTDIR. Chmod would not do: root ignores the permission bits, // and CI runs as root. func unwritableDir(t *testing.T) string { t.Helper() blocker := filepath.Join(t.TempDir(), "blocker") require.NoError(t, os.WriteFile(blocker, nil, 0o600)) return filepath.Join(blocker, "glab-cli") }