From f047394b6d14284165300fd82dad67edb3a4d7f6 Mon Sep 17 00:00:00 2001 From: Ross Light Date: Wed, 17 May 2017 10:14:19 -0700 Subject: [PATCH] oauth2: add test for preserving refresh token if none is received This passes right now, but it's not obvious from looking at tokenRefresher why it works. It depends on logic in internal.RetrieveToken. I'm working on a larger refactor, but I want to keep the test in place to avoid future regressions from seemingly harmless code movements. Change-Id: I742ccb952fbc069ca0887a556e362a0e59bef79b Reviewed-on: https://go-review.googlesource.com/43573 Reviewed-by: Jaana Burcu Dogan Reviewed-by: Brad Fitzpatrick --- oauth2_test.go | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/oauth2_test.go b/oauth2_test.go index e757b0f..584084b 100644 --- a/oauth2_test.go +++ b/oauth2_test.go @@ -403,15 +403,15 @@ func TestFetchWithNoRefreshToken(t *testing.T) { func TestRefreshToken_RefreshTokenReplacement(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") - w.Write([]byte(`{"access_token":"ACCESS TOKEN", "scope": "user", "token_type": "bearer", "refresh_token": "NEW REFRESH TOKEN"}`)) + w.Write([]byte(`{"access_token":"ACCESS_TOKEN", "scope": "user", "token_type": "bearer", "refresh_token": "NEW_REFRESH_TOKEN"}`)) return })) defer ts.Close() conf := newConf(ts.URL) - tkr := tokenRefresher{ + tkr := &tokenRefresher{ conf: conf, ctx: context.Background(), - refreshToken: "OLD REFRESH TOKEN", + refreshToken: "OLD_REFRESH_TOKEN", } tk, err := tkr.Token() if err != nil { @@ -423,6 +423,29 @@ func TestRefreshToken_RefreshTokenReplacement(t *testing.T) { } } +func TestRefreshToken_RefreshTokenPreservation(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.Write([]byte(`{"access_token":"ACCESS_TOKEN", "scope": "user", "token_type": "bearer"}`)) + return + })) + defer ts.Close() + conf := newConf(ts.URL) + const oldRefreshToken = "OLD_REFRESH_TOKEN" + tkr := &tokenRefresher{ + conf: conf, + ctx: context.Background(), + refreshToken: oldRefreshToken, + } + _, err := tkr.Token() + if err != nil { + t.Fatalf("got err = %v; want none", err) + } + if tkr.refreshToken != oldRefreshToken { + t.Errorf("tokenRefresher.refreshToken = %q; want %q", tkr.refreshToken, oldRefreshToken) + } +} + func TestConfigClientWithToken(t *testing.T) { tok := &Token{ AccessToken: "abc123",