go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/auth/integration/authtest/faketokengen_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 authtest 16 17 import ( 18 "context" 19 "fmt" 20 "testing" 21 "time" 22 23 "go.chromium.org/luci/auth" 24 "go.chromium.org/luci/auth/integration/localauth" 25 "go.chromium.org/luci/common/clock/testclock" 26 "go.chromium.org/luci/lucictx" 27 28 . "github.com/smartystreets/goconvey/convey" 29 ) 30 31 func TestFakeTokenGenerator(t *testing.T) { 32 t.Parallel() 33 34 Convey("With fakes", t, func() { 35 ctx, _ := testclock.UseTime(context.Background(), testclock.TestRecentTimeUTC) 36 37 gen := FakeTokenGenerator{KeepRecord: true} 38 39 srv := localauth.Server{ 40 TokenGenerators: map[string]localauth.TokenGenerator{ 41 "authtest": &gen, 42 }, 43 DefaultAccountID: "authtest", 44 } 45 la, err := srv.Start(ctx) 46 So(err, ShouldBeNil) 47 Reset(func() { srv.Stop(ctx) }) 48 ctx = lucictx.SetLocalAuth(ctx, la) 49 50 Convey("Access tokens", func() { 51 for idx, scope := range []string{"A", "B"} { 52 auth := auth.NewAuthenticator(ctx, auth.SilentLogin, auth.Options{ 53 Scopes: []string{scope, "zzz"}, 54 }) 55 56 email, err := auth.GetEmail() 57 So(err, ShouldBeNil) 58 So(email, ShouldEqual, DefaultFakeEmail) 59 60 tok, err := auth.GetAccessToken(time.Minute) 61 So(err, ShouldBeNil) 62 So(tok.AccessToken, ShouldEqual, fmt.Sprintf("fake_token_%d", idx)) 63 64 // Expiry is rounded to integer number of seconds, since that's the 65 // granularity of OAuth token expiration. Compare int unix timestamps to 66 // account for that. 67 So(tok.Expiry.Unix(), ShouldEqual, 68 testclock.TestRecentTimeUTC.Add(DefaultFakeLifetime).Unix()) 69 } 70 71 So(gen.TokenScopes("fake_token_0"), ShouldResemble, []string{"A", "zzz"}) 72 So(gen.TokenScopes("fake_token_1"), ShouldResemble, []string{"B", "zzz"}) 73 }) 74 75 Convey("ID tokens", func() { 76 for idx, aud := range []string{"A", "B"} { 77 auth := auth.NewAuthenticator(ctx, auth.SilentLogin, auth.Options{ 78 UseIDTokens: true, 79 Audience: aud, 80 }) 81 82 email, err := auth.GetEmail() 83 So(err, ShouldBeNil) 84 So(email, ShouldEqual, DefaultFakeEmail) 85 86 tok, err := auth.GetAccessToken(time.Minute) 87 So(err, ShouldBeNil) 88 So(tok.AccessToken, ShouldEqual, fmt.Sprintf("fake_token_%d", idx)) 89 90 // Expiry is rounded to integer number of seconds, since that's the 91 // granularity of OAuth token expiration. Compare int unix timestamps to 92 // account for that. 93 So(tok.Expiry.Unix(), ShouldEqual, 94 testclock.TestRecentTimeUTC.Add(DefaultFakeLifetime).Unix()) 95 } 96 97 So(gen.TokenScopes("fake_token_0"), ShouldResemble, []string{"audience:A"}) 98 So(gen.TokenScopes("fake_token_1"), ShouldResemble, []string{"audience:B"}) 99 }) 100 }) 101 }