go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/auth/integration/firebase/firebase_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 firebase 16 17 import ( 18 "context" 19 "encoding/json" 20 "net/http" 21 "net/url" 22 "strings" 23 "testing" 24 "time" 25 26 "golang.org/x/oauth2" 27 28 "go.chromium.org/luci/common/clock" 29 "go.chromium.org/luci/common/clock/testclock" 30 31 . "github.com/smartystreets/goconvey/convey" 32 ) 33 34 func TestProtocol(t *testing.T) { 35 t.Parallel() 36 37 ctx := context.Background() 38 ctx, _ = testclock.UseTime(ctx, testclock.TestRecentTimeUTC) 39 token := "tok1" 40 41 Convey("With server", t, func(c C) { 42 s := Server{ 43 Source: oauth2.StaticTokenSource(&oauth2.Token{ 44 AccessToken: token, 45 Expiry: clock.Now(ctx).Add(30 * time.Minute), 46 }), 47 } 48 addr, err := s.Start(ctx) 49 So(err, ShouldBeNil) 50 defer s.Stop(ctx) 51 52 call := func(refreshTok string) (*http.Response, error) { 53 form := url.Values{} 54 form.Add("grant_type", "refresh_token") 55 form.Add("refresh_token", refreshTok) 56 form.Add("client_id", "fake_client_id") 57 form.Add("client_secret", "fake_client_secret") 58 return http.Post(addr+"/oauth2/v3/token", "application/x-www-form-urlencoded", strings.NewReader(form.Encode())) 59 } 60 61 Convey("Happy path", func() { 62 resp, err := call(token) 63 So(err, ShouldBeNil) 64 defer resp.Body.Close() 65 tok := map[string]any{} 66 So(resp.StatusCode, ShouldEqual, 200) 67 So(resp.Header.Get("Content-Type"), ShouldEqual, "application/json") 68 So(json.NewDecoder(resp.Body).Decode(&tok), ShouldBeNil) 69 So(tok, ShouldResemble, map[string]any{ 70 "access_token": "tok1", 71 "expires_in": 1800.0, 72 "token_type": "Bearer", 73 }) 74 }) 75 }) 76 }