go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/auth/authtest/config.go (about) 1 // Copyright 2016 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 "net/http" 20 "time" 21 22 "golang.org/x/oauth2" 23 24 "go.chromium.org/luci/common/clock" 25 26 "go.chromium.org/luci/server/auth" 27 "go.chromium.org/luci/server/auth/authdb" 28 ) 29 30 // MockAuthConfig configures the auth library for unit tests environment. 31 // 32 // You need this *only* if your tests call auth.Authenticate(...) or 33 // auth.GetRPCTransport(...). If your tests only check groups or permissions 34 // (for example when testing bodies of request handlers), use FakeState instead. 35 // See its docs for some examples. 36 func MockAuthConfig(ctx context.Context, mocks ...MockedDatum) context.Context { 37 return auth.ModifyConfig(ctx, func(cfg auth.Config) auth.Config { 38 fakeDB := NewFakeDB(mocks...) 39 cfg.DBProvider = func(context.Context) (authdb.DB, error) { 40 return fakeDB, nil 41 } 42 cfg.AnonymousTransport = func(context.Context) http.RoundTripper { 43 return http.DefaultTransport 44 } 45 cfg.AccessTokenProvider = func(ctx context.Context, scopes []string) (*oauth2.Token, error) { 46 return &oauth2.Token{ 47 AccessToken: "fake_token", 48 TokenType: "Bearer", 49 Expiry: clock.Now(ctx).Add(time.Hour).UTC(), 50 }, nil 51 } 52 return cfg 53 }) 54 }