go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/internal/git/gerrit_test.go (about) 1 // Copyright 2018 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 git 16 17 import ( 18 "context" 19 "testing" 20 21 "github.com/golang/mock/gomock" 22 23 "go.chromium.org/luci/auth/identity" 24 gerritpb "go.chromium.org/luci/common/proto/gerrit" 25 "go.chromium.org/luci/milo/internal/git/gitacls" 26 configpb "go.chromium.org/luci/milo/proto/config" 27 "go.chromium.org/luci/server/auth" 28 "go.chromium.org/luci/server/auth/authtest" 29 "go.chromium.org/luci/server/caching" 30 31 . "github.com/smartystreets/goconvey/convey" 32 ) 33 34 func TestCLEmail(t *testing.T) { 35 t.Parallel() 36 37 Convey("CLEmail", t, func() { 38 c := caching.WithEmptyProcessCache(context.Background()) 39 40 ctl := gomock.NewController(t) 41 defer ctl.Finish() 42 gerritMock := gerritpb.NewMockGerritClient(ctl) 43 44 host := "limited-review.googlesource.com" 45 acls, err := gitacls.FromConfig(c, []*configpb.Settings_SourceAcls{ 46 {Hosts: []string{"limited.googlesource.com"}, Readers: []string{"allowed@example.com"}}, 47 }) 48 So(err, ShouldBeNil) 49 impl := implementation{mockGerrit: gerritMock, acls: acls} 50 c = Use(c, &impl) 51 cAllowed := auth.WithState(c, &authtest.FakeState{Identity: "user:allowed@example.com"}) 52 cDenied := auth.WithState(c, &authtest.FakeState{Identity: identity.AnonymousIdentity}) 53 54 // Will be called exactly once. 55 gerritMock.EXPECT().GetChange(gomock.Any(), gomock.Any()).Return(&gerritpb.ChangeInfo{ 56 Owner: &gerritpb.AccountInfo{Email: "user@example.com"}, 57 Project: "project", 58 }, nil) 59 60 _, err = impl.CLEmail(cDenied, host, 123) 61 Convey("ACLs respected with cold cache", func() { 62 So(err.Error(), ShouldContainSubstring, "not logged in") 63 }) 64 65 // Now that we have cached change owner, no more GetChange calls should 66 // happen, ensured by gerritMock expectation above. 67 68 Convey("ACLs still respected with warm cache", func() { 69 _, err = impl.CLEmail(cDenied, host, 123) 70 So(err.Error(), ShouldContainSubstring, "not logged in") 71 }) 72 73 Convey("Happy cached path", func() { 74 email, err := impl.CLEmail(cAllowed, host, 123) 75 So(err, ShouldBeNil) 76 So(email, ShouldResemble, "user@example.com") 77 }) 78 }) 79 }