go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/tokenserver/appengine/impl/projectscope/config_test.go (about)

     1  // Copyright 2019 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 projectscope
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"go.chromium.org/luci/appengine/gaetesting"
    22  	configset "go.chromium.org/luci/config"
    23  	"go.chromium.org/luci/config/cfgclient"
    24  	"go.chromium.org/luci/config/impl/memory"
    25  	"go.chromium.org/luci/server/auth"
    26  	"go.chromium.org/luci/server/auth/authtest"
    27  
    28  	"go.chromium.org/luci/tokenserver/appengine/impl/utils/projectidentity"
    29  
    30  	. "github.com/smartystreets/goconvey/convey"
    31  )
    32  
    33  const fakeConfig = `
    34  	projects {
    35  		id: "id1"
    36  		gitiles_location {
    37  			repo: "https://some/repo"
    38  			ref: "refs/heads/main"
    39  		}
    40  		identity_config {
    41  			service_account_email: "foo@bar.com"
    42  		}
    43  	}
    44  	projects {
    45  		id: "id2"
    46  		gitiles_location {
    47  			repo: "https://some/other/repo"
    48  			ref: "refs/heads/main"
    49  		}
    50  		identity_config {
    51  			service_account_email: "bar@bar.com"
    52  		}
    53  	}
    54  `
    55  
    56  func TestRules(t *testing.T) {
    57  	t.Parallel()
    58  
    59  	ctx := auth.WithState(gaetesting.TestingContext(), &authtest.FakeState{
    60  		Identity: "user:unused@example.com",
    61  	})
    62  	storage := projectidentity.ProjectIdentities(ctx)
    63  
    64  	Convey("Loads", t, func() {
    65  		ctx = prepareCfg(ctx, fakeConfig)
    66  		_, err := ImportConfigs(ctx)
    67  		So(err, ShouldBeNil)
    68  
    69  		expected := map[string]string{
    70  			"id1": "foo@bar.com",
    71  			"id2": "bar@bar.com",
    72  		}
    73  
    74  		for project, email := range expected {
    75  			identity, err := storage.LookupByProject(ctx, project)
    76  			So(err, ShouldBeNil)
    77  			So(identity, ShouldResemble, &projectidentity.ProjectIdentity{
    78  				Project: project,
    79  				Email:   email,
    80  			})
    81  		}
    82  
    83  	})
    84  
    85  }
    86  
    87  // prepareCfg injects config.Backend implementation with a bunch of
    88  // config files.
    89  func prepareCfg(c context.Context, configFile string) context.Context {
    90  	return cfgclient.Use(c, memory.New(map[configset.Set]memory.Files{
    91  		"services/${config_service_appid}": {
    92  			"projects.cfg": configFile,
    93  		},
    94  	}))
    95  }