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 <jbd@google.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Ross Light 2017-05-17 10:14:19 -07:00
parent ad516a297a
commit f047394b6d
1 changed files with 26 additions and 3 deletions

View File

@ -403,15 +403,15 @@ func TestFetchWithNoRefreshToken(t *testing.T) {
func TestRefreshToken_RefreshTokenReplacement(t *testing.T) { func TestRefreshToken_RefreshTokenReplacement(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") 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 return
})) }))
defer ts.Close() defer ts.Close()
conf := newConf(ts.URL) conf := newConf(ts.URL)
tkr := tokenRefresher{ tkr := &tokenRefresher{
conf: conf, conf: conf,
ctx: context.Background(), ctx: context.Background(),
refreshToken: "OLD REFRESH TOKEN", refreshToken: "OLD_REFRESH_TOKEN",
} }
tk, err := tkr.Token() tk, err := tkr.Token()
if err != nil { 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) { func TestConfigClientWithToken(t *testing.T) {
tok := &Token{ tok := &Token{
AccessToken: "abc123", AccessToken: "abc123",