go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/internal/buildsource/buildbucket/buckets_test.go (about)

     1  // Copyright 2022 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 buildbucket
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	. "github.com/smartystreets/goconvey/convey"
    22  	"go.chromium.org/luci/auth/identity"
    23  	"go.chromium.org/luci/buildbucket/bbperms"
    24  	buildbucketpb "go.chromium.org/luci/buildbucket/proto"
    25  	"go.chromium.org/luci/server/auth"
    26  	"go.chromium.org/luci/server/auth/authtest"
    27  )
    28  
    29  const testIdentity = identity.Identity("user:test@example.com")
    30  
    31  func TestFilterVisibleBuilders(t *testing.T) {
    32  	t.Parallel()
    33  
    34  	Convey(`FilterVisibleBuilders`, t, func() {
    35  		s := &authtest.FakeState{
    36  			FakeDB: authtest.NewFakeDB(
    37  				authtest.MockPermission(testIdentity, "proj1:bucket1", bbperms.BuildersList),
    38  				authtest.MockPermission(testIdentity, "proj2:bucket1", bbperms.BuildersList),
    39  			),
    40  			Identity: testIdentity,
    41  		}
    42  		ctx := auth.WithState(context.Background(), s)
    43  
    44  		builders := []*buildbucketpb.BuilderID{
    45  			{
    46  				Project: "proj1",
    47  				Bucket:  "bucket1",
    48  				Builder: "builder1",
    49  			},
    50  			{
    51  				Project: "proj1",
    52  				Bucket:  "bucket2",
    53  				Builder: "builder1",
    54  			},
    55  			{
    56  				Project: "proj2",
    57  				Bucket:  "bucket1",
    58  				Builder: "builder1",
    59  			},
    60  			{
    61  				Project: "proj2",
    62  				Bucket:  "bucket1",
    63  				Builder: "builder2",
    64  			},
    65  			{
    66  				Project: "proj2",
    67  				Bucket:  "bucket2",
    68  				Builder: "builder1",
    69  			},
    70  		}
    71  
    72  		visibleBuilders, err := FilterVisibleBuilders(ctx, builders, "")
    73  		So(err, ShouldBeNil)
    74  		So(visibleBuilders, ShouldResemble, []*buildbucketpb.BuilderID{
    75  			{
    76  				Project: "proj1",
    77  				Bucket:  "bucket1",
    78  				Builder: "builder1",
    79  			},
    80  			{
    81  				Project: "proj2",
    82  				Bucket:  "bucket1",
    83  				Builder: "builder1",
    84  			},
    85  			{
    86  				Project: "proj2",
    87  				Bucket:  "bucket1",
    88  				Builder: "builder2",
    89  			},
    90  		})
    91  
    92  		visibleBuildersInProj2, err := FilterVisibleBuilders(ctx, builders, "proj2")
    93  		So(err, ShouldBeNil)
    94  		So(visibleBuildersInProj2, ShouldResemble, []*buildbucketpb.BuilderID{
    95  			{
    96  				Project: "proj2",
    97  				Bucket:  "bucket1",
    98  				Builder: "builder1",
    99  			},
   100  			{
   101  				Project: "proj2",
   102  				Bucket:  "bucket1",
   103  				Builder: "builder2",
   104  			},
   105  		})
   106  	})
   107  }