go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/rpc/get_project_cfg_test.go (about)

     1  // Copyright 2021 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 rpc
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/smartystreets/goconvey/convey"
    21  	"go.chromium.org/luci/appengine/gaetesting"
    22  	"go.chromium.org/luci/auth/identity"
    23  	. "go.chromium.org/luci/common/testing/assertions"
    24  	"go.chromium.org/luci/gae/service/datastore"
    25  	"go.chromium.org/luci/grpc/grpcutil"
    26  	"go.chromium.org/luci/milo/internal/projectconfig"
    27  	projectconfigpb "go.chromium.org/luci/milo/proto/projectconfig"
    28  	milopb "go.chromium.org/luci/milo/proto/v1"
    29  	"go.chromium.org/luci/server/auth"
    30  	"go.chromium.org/luci/server/auth/authtest"
    31  	"google.golang.org/grpc/codes"
    32  	"google.golang.org/protobuf/proto"
    33  )
    34  
    35  func TestGetProjectCfg(t *testing.T) {
    36  	t.Parallel()
    37  	Convey(`TestGetProjectCfg`, t, func() {
    38  		c := gaetesting.TestingContextWithAppID("luci-milo-dev")
    39  		datastore.GetTestable(c).Consistent(true)
    40  		srv := &MiloInternalService{}
    41  
    42  		mc := &projectconfigpb.MetadataConfig{
    43  			TestMetadataProperties: []*projectconfigpb.DisplayRule{
    44  				{
    45  					Schema: "package.name",
    46  					DisplayItems: []*projectconfigpb.DisplayItem{{
    47  						DisplayName: "owner",
    48  						Path:        "owner.email",
    49  					}},
    50  				},
    51  			},
    52  		}
    53  		mcbytes, err := proto.Marshal(mc)
    54  		So(err, ShouldBeNil)
    55  		err = datastore.Put(c, &projectconfig.Project{
    56  			ID:             "fake_project",
    57  			ACL:            projectconfig.ACL{Identities: []identity.Identity{"user_with_access"}},
    58  			LogoURL:        "https://logo.com",
    59  			MetadataConfig: mcbytes,
    60  		})
    61  		So(err, ShouldBeNil)
    62  
    63  		Convey(`reject users with no access`, func() {
    64  			c = auth.WithState(c, &authtest.FakeState{Identity: "user_without_access"})
    65  			req := &milopb.GetProjectCfgRequest{
    66  				Project: "fake_project",
    67  			}
    68  
    69  			_, err := srv.GetProjectCfg(c, req)
    70  			So(err, ShouldNotBeNil)
    71  			So(grpcutil.Code(err), ShouldEqual, codes.PermissionDenied)
    72  		})
    73  
    74  		Convey(`accept users access`, func() {
    75  			c = auth.WithState(c, &authtest.FakeState{Identity: "user_with_access"})
    76  			req := &milopb.GetProjectCfgRequest{
    77  				Project: "fake_project",
    78  			}
    79  
    80  			cfg, err := srv.GetProjectCfg(c, req)
    81  			So(err, ShouldBeNil)
    82  			So(cfg.GetLogoUrl(), ShouldEqual, "https://logo.com")
    83  			So(cfg.MetadataConfig, ShouldResembleProto, mc)
    84  		})
    85  
    86  		Convey(`reject invalid request`, func() {
    87  			c = auth.WithState(c, &authtest.FakeState{Identity: "user_with_access"})
    88  			req := &milopb.GetProjectCfgRequest{}
    89  
    90  			_, err := srv.GetProjectCfg(c, req)
    91  			So(err, ShouldNotBeNil)
    92  			So(grpcutil.Code(err), ShouldEqual, codes.InvalidArgument)
    93  		})
    94  	})
    95  }