| 42 | } |
| 43 | |
| 44 | func TestTokenRefresherRefresh(t *testing.T) { |
| 45 | // Pin a non-UTC clock so the test proves the derived expiry is normalized to UTC rather than inheriting the |
| 46 | // local zone of the anchor. |
| 47 | loc, err := time.LoadLocation("America/New_York") |
| 48 | require.NoError(t, err) |
| 49 | fixedNow := time.Date(2026, 1, 2, 3, 4, 5, 0, loc) |
| 50 | timeNow = func() time.Time { return fixedNow } |
| 51 | t.Cleanup(func() { timeNow = time.Now }) |
| 52 | |
| 53 | stub := &stubRoundTripper{ |
| 54 | body: "access_token=gho_NEW&refresh_token=ghr_NEW&token_type=bearer&scope=repo&expires_in=28800&refresh_token_expires_in=15897600", |
| 55 | } |
| 56 | refresher := NewTokenRefresher(&http.Client{Transport: stub}) |
| 57 | |
| 58 | credential, err := refresher.Refresh("ghr_OLD", "github.com") |
| 59 | require.NoError(t, err) |
| 60 | |
| 61 | // The renewed token pair is mapped through verbatim. |
| 62 | require.Equal(t, "gho_NEW", credential.AccessToken) |
| 63 | require.Equal(t, "ghr_NEW", credential.RefreshToken) |
| 64 | require.Equal(t, 28800, credential.ExpiresIn) |
| 65 | require.Equal(t, 15897600, credential.RefreshTokenExpiresIn) |
| 66 | // Absolute expiries are derived from the server lifetimes, anchored at the instant the exchange was initiated, |
| 67 | // and expressed in UTC so they match the canonical form gh stores and displays. |
| 68 | require.NotNil(t, credential.ExpiresAt) |
| 69 | require.Equal(t, time.UTC, credential.ExpiresAt.Location()) |
| 70 | require.Equal(t, fixedNow.Add(28800*time.Second).UTC(), *credential.ExpiresAt) |
| 71 | require.NotNil(t, credential.RefreshTokenExpiresAt) |
| 72 | require.Equal(t, time.UTC, credential.RefreshTokenExpiresAt.Location()) |
| 73 | require.Equal(t, fixedNow.Add(15897600*time.Second).UTC(), *credential.RefreshTokenExpiresAt) |
| 74 | |
| 75 | // The request is a refresh_token grant carrying the supplied refresh token, sent to the host's token endpoint. |
| 76 | require.Equal(t, "https://github.com/login/oauth/access_token", stub.lastReq.URL.String()) |
| 77 | values, err := url.ParseQuery(stub.lastBody) |
| 78 | require.NoError(t, err) |
| 79 | require.Equal(t, "refresh_token", values.Get("grant_type")) |
| 80 | require.Equal(t, "ghr_OLD", values.Get("refresh_token")) |
| 81 | require.Equal(t, oauthClientID, values.Get("client_id")) |
| 82 | } |
| 83 | |
| 84 | func TestTokenRefresherRefreshNoExpiry(t *testing.T) { |
| 85 | stub := &stubRoundTripper{ |