oauth2: rewrite google package, fix the broken build
Change-Id: I2753a88d7be483bdbc0cac09a1beccc4806ea4bc Reviewed-on: https://go-review.googlesource.com/1361 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Andrew Gerrand <adg@golang.org>
This commit is contained in:
committed by
Brad Fitzpatrick
parent
a568078818
commit
9b6b7610ad
110
oauth2_test.go
110
oauth2_test.go
@@ -10,6 +10,8 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
type mockTransport struct {
|
||||
@@ -33,31 +35,37 @@ func (c *mockCache) WriteToken(*Token) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
func newOpts(url string) *Options {
|
||||
opts, _ := New(
|
||||
Client("CLIENT_ID", "CLIENT_SECRET"),
|
||||
RedirectURL("REDIRECT_URL"),
|
||||
Scope("scope1", "scope2"),
|
||||
Endpoint(url+"/auth", url+"/token"),
|
||||
)
|
||||
return opts
|
||||
func newConf(url string) *Config {
|
||||
return &Config{
|
||||
ClientID: "CLIENT_ID",
|
||||
ClientSecret: "CLIENT_SECRET",
|
||||
RedirectURL: "REDIRECT_URL",
|
||||
Scopes: []string{"scope1", "scope2"},
|
||||
Endpoint: Endpoint{
|
||||
AuthURL: url + "/auth",
|
||||
TokenURL: url + "/token",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthCodeURL(t *testing.T) {
|
||||
opts := newOpts("server")
|
||||
url := opts.AuthCodeURL("foo", "offline", "force")
|
||||
conf := newConf("server")
|
||||
url := conf.AuthCodeURL("foo", AccessTypeOffline, ApprovalForce)
|
||||
if url != "server/auth?access_type=offline&approval_prompt=force&client_id=CLIENT_ID&redirect_uri=REDIRECT_URL&response_type=code&scope=scope1+scope2&state=foo" {
|
||||
t.Errorf("Auth code URL doesn't match the expected, found: %v", url)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthCodeURL_Optional(t *testing.T) {
|
||||
opts, _ := New(
|
||||
Client("CLIENT_ID", ""),
|
||||
Endpoint("auth-url", "token-token"),
|
||||
)
|
||||
url := opts.AuthCodeURL("", "", "")
|
||||
if url != "auth-url?client_id=CLIENT_ID&response_type=code" {
|
||||
conf := &Config{
|
||||
ClientID: "CLIENT_ID",
|
||||
Endpoint: Endpoint{
|
||||
AuthURL: "/auth-url",
|
||||
TokenURL: "/token-url",
|
||||
},
|
||||
}
|
||||
url := conf.AuthCodeURL("")
|
||||
if url != "/auth-url?client_id=CLIENT_ID&response_type=code" {
|
||||
t.Fatalf("Auth code URL doesn't match the expected, found: %v", url)
|
||||
}
|
||||
}
|
||||
@@ -86,12 +94,11 @@ func TestExchangeRequest(t *testing.T) {
|
||||
w.Write([]byte("access_token=90d64460d14870c08c81352a05dedd3465940a7c&scope=user&token_type=bearer"))
|
||||
}))
|
||||
defer ts.Close()
|
||||
opts := newOpts(ts.URL)
|
||||
tr, err := opts.NewTransportFromCode("exchange-code")
|
||||
conf := newConf(ts.URL)
|
||||
tok, err := conf.Exchange(NoContext, "exchange-code")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
tok := tr.Token()
|
||||
if tok.Expired() {
|
||||
t.Errorf("Token shouldn't be expired.")
|
||||
}
|
||||
@@ -131,15 +138,11 @@ func TestExchangeRequest_JSONResponse(t *testing.T) {
|
||||
w.Write([]byte(`{"access_token": "90d64460d14870c08c81352a05dedd3465940a7c", "scope": "user", "token_type": "bearer", "expires_in": 86400}`))
|
||||
}))
|
||||
defer ts.Close()
|
||||
opts := newOpts(ts.URL)
|
||||
tr, err := opts.NewTransportFromCode("exchange-code")
|
||||
conf := newConf(ts.URL)
|
||||
tok, err := conf.Exchange(NoContext, "exchange-code")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
tok := tr.Token()
|
||||
if tok.Expiry.IsZero() {
|
||||
t.Errorf("Token expiry should not be zero.")
|
||||
}
|
||||
if tok.Expired() {
|
||||
t.Errorf("Token shouldn't be expired.")
|
||||
}
|
||||
@@ -161,12 +164,11 @@ func TestExchangeRequest_BadResponse(t *testing.T) {
|
||||
w.Write([]byte(`{"scope": "user", "token_type": "bearer"}`))
|
||||
}))
|
||||
defer ts.Close()
|
||||
opts := newOpts(ts.URL)
|
||||
tr, err := opts.NewTransportFromCode("exchange-code")
|
||||
conf := newConf(ts.URL)
|
||||
tok, err := conf.Exchange(NoContext, "code")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
tok := tr.Token()
|
||||
if tok.AccessToken != "" {
|
||||
t.Errorf("Unexpected access token, %#v.", tok.AccessToken)
|
||||
}
|
||||
@@ -178,12 +180,11 @@ func TestExchangeRequest_BadResponseType(t *testing.T) {
|
||||
w.Write([]byte(`{"access_token":123, "scope": "user", "token_type": "bearer"}`))
|
||||
}))
|
||||
defer ts.Close()
|
||||
opts := newOpts(ts.URL)
|
||||
tr, err := opts.NewTransportFromCode("exchange-code")
|
||||
conf := newConf(ts.URL)
|
||||
tok, err := conf.Exchange(NoContext, "exchange-code")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
tok := tr.Token()
|
||||
if tok.AccessToken != "" {
|
||||
t.Errorf("Unexpected access token, %#v.", tok.AccessToken)
|
||||
}
|
||||
@@ -200,15 +201,16 @@ func TestExchangeRequest_NonBasicAuth(t *testing.T) {
|
||||
},
|
||||
}
|
||||
c := &http.Client{Transport: tr}
|
||||
opts, err := New(
|
||||
Client("CLIENT_ID", ""),
|
||||
Endpoint("https://accounts.google.com/auth", "https://accounts.google.com/token"),
|
||||
HTTPClient(c),
|
||||
)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
conf := &Config{
|
||||
ClientID: "CLIENT_ID",
|
||||
Endpoint: Endpoint{
|
||||
AuthURL: "https://accounts.google.com/auth",
|
||||
TokenURL: "https://accounts.google.com/token",
|
||||
},
|
||||
}
|
||||
opts.NewTransportFromCode("code")
|
||||
|
||||
ctx := context.WithValue(context.Background(), HTTPClient, c)
|
||||
conf.Exchange(ctx, "code")
|
||||
}
|
||||
|
||||
func TestTokenRefreshRequest(t *testing.T) {
|
||||
@@ -229,10 +231,8 @@ func TestTokenRefreshRequest(t *testing.T) {
|
||||
}
|
||||
}))
|
||||
defer ts.Close()
|
||||
opts := newOpts(ts.URL)
|
||||
tr := opts.NewTransport()
|
||||
tr.token = &Token{RefreshToken: "REFRESH_TOKEN"}
|
||||
c := http.Client{Transport: tr}
|
||||
conf := newConf(ts.URL)
|
||||
c := conf.Client(NoContext, &Token{RefreshToken: "REFRESH_TOKEN"})
|
||||
c.Get(ts.URL + "/somethingelse")
|
||||
}
|
||||
|
||||
@@ -254,28 +254,10 @@ func TestFetchWithNoRefreshToken(t *testing.T) {
|
||||
}
|
||||
}))
|
||||
defer ts.Close()
|
||||
opts := newOpts(ts.URL)
|
||||
tr := opts.NewTransport()
|
||||
c := http.Client{Transport: tr}
|
||||
conf := newConf(ts.URL)
|
||||
c := conf.Client(NoContext, nil)
|
||||
_, err := c.Get(ts.URL + "/somethingelse")
|
||||
if err == nil {
|
||||
t.Errorf("Fetch should return an error if no refresh token is set")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCacheNoToken(t *testing.T) {
|
||||
opts, err := New(
|
||||
Client("CLIENT_ID", "CLIENT_SECRET"),
|
||||
Endpoint("/auth", "/token"),
|
||||
)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
tr, err := opts.NewTransportFromTokenStore(&mockCache{token: nil, readErr: nil})
|
||||
if err != nil {
|
||||
t.Errorf("No error expected, %v is found", err)
|
||||
}
|
||||
if tr != nil {
|
||||
t.Errorf("No transport should have been initiated, tr is found to be %v", tr)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user