go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/gcloud/googleoauth/info_test.go (about) 1 // Copyright 2017 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package googleoauth 16 17 import ( 18 "context" 19 "net/http" 20 "net/http/httptest" 21 "testing" 22 23 . "github.com/smartystreets/goconvey/convey" 24 ) 25 26 func TestGetTokenInfo(t *testing.T) { 27 Convey("Works", t, func() { 28 goodToken := true 29 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 30 if r.Method != "GET" { 31 panic("not a GET") 32 } 33 if r.RequestURI != "/?access_token=zzz%3D%3D%3Dtoken" { 34 panic("wrong URI " + r.RequestURI) 35 } 36 if goodToken { 37 w.Header().Set("Content-Type", "application/json") 38 w.WriteHeader(200) 39 w.Write([]byte(`{"aud":"blah", "expires_in":"12345", "email_verified": "true"}`)) 40 } else { 41 w.Header().Set("Content-Type", "application/json") 42 w.WriteHeader(400) 43 w.Write([]byte(`{"error_description":"Invalid value"}`)) 44 } 45 })) 46 defer ts.Close() 47 48 info, err := GetTokenInfo(context.Background(), TokenInfoParams{ 49 AccessToken: "zzz===token", 50 Endpoint: ts.URL, 51 }) 52 So(err, ShouldBeNil) 53 So(info, ShouldResemble, &TokenInfo{ 54 Aud: "blah", 55 ExpiresIn: 12345, 56 EmailVerified: true, 57 }) 58 59 goodToken = false 60 info, err = GetTokenInfo(context.Background(), TokenInfoParams{ 61 AccessToken: "zzz===token", 62 Endpoint: ts.URL, 63 }) 64 So(info, ShouldBeNil) 65 So(err, ShouldEqual, ErrBadToken) 66 }) 67 }