go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/auth/realms/realms_test.go (about)

     1  // Copyright 2020 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 realms
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/smartystreets/goconvey/convey"
    21  	. "go.chromium.org/luci/common/testing/assertions"
    22  )
    23  
    24  func TestValidateProjectName(t *testing.T) {
    25  	t.Parallel()
    26  
    27  	Convey("Works", t, func() {
    28  		So(ValidateProjectName("something-blah"), ShouldBeNil)
    29  		So(ValidateProjectName("@internal"), ShouldBeNil)
    30  
    31  		So(ValidateProjectName("something-BLAH"), ShouldNotBeNil)
    32  		So(ValidateProjectName(""), ShouldNotBeNil)
    33  		So(ValidateProjectName(":"), ShouldNotBeNil)
    34  		So(ValidateProjectName("@blah"), ShouldNotBeNil)
    35  	})
    36  }
    37  
    38  func TestValidateRealmName(t *testing.T) {
    39  	t.Parallel()
    40  
    41  	Convey("GlobalScope", t, func() {
    42  		call := func(r string) error {
    43  			return ValidateRealmName(r, GlobalScope)
    44  		}
    45  
    46  		So(call("something-blah:realm"), ShouldBeNil)
    47  		So(call("something-blah:a/b/c"), ShouldBeNil)
    48  		So(call("something-blah:@root"), ShouldBeNil)
    49  		So(call("something-blah:@legacy"), ShouldBeNil)
    50  		So(call("something-blah:@project"), ShouldBeNil)
    51  
    52  		So(call("@internal:realm"), ShouldBeNil)
    53  		So(call("@internal:a/b/c"), ShouldBeNil)
    54  		So(call("@internal:@root"), ShouldBeNil)
    55  		So(call("@internal:@legacy"), ShouldBeNil)
    56  		So(call("@internal:@project"), ShouldBeNil)
    57  
    58  		So(call("realm"), ShouldErrLike, "should be <project>:<realm>")
    59  		So(call("BLAH:realm"), ShouldErrLike, "bad project name")
    60  		So(call("@blah:zzz"), ShouldErrLike, "bad project name")
    61  		So(call("blah:"), ShouldErrLike, "the realm name should match")
    62  		So(call("blah:@zzz"), ShouldErrLike, "the realm name should match")
    63  	})
    64  
    65  	Convey("ProjectScope", t, func() {
    66  		call := func(r string) error {
    67  			return ValidateRealmName(r, ProjectScope)
    68  		}
    69  
    70  		So(call("realm"), ShouldBeNil)
    71  		So(call("a/b/c"), ShouldBeNil)
    72  		So(call("@root"), ShouldBeNil)
    73  		So(call("@legacy"), ShouldBeNil)
    74  		So(call("@project"), ShouldBeNil)
    75  
    76  		So(call("blah:realm"), ShouldNotBeNil)
    77  		So(call(":realm"), ShouldNotBeNil)
    78  		So(call("realm:"), ShouldNotBeNil)
    79  		So(call("@zzz"), ShouldNotBeNil)
    80  	})
    81  }
    82  
    83  func TestRealmSplitJoin(t *testing.T) {
    84  	t.Parallel()
    85  
    86  	Convey("Works", t, func() {
    87  		a, b := Split("a:b")
    88  		So(a, ShouldEqual, "a")
    89  		So(b, ShouldEqual, "b")
    90  
    91  		a, b = Split(":")
    92  		So(a, ShouldEqual, "")
    93  		So(b, ShouldEqual, "")
    94  
    95  		So(func() { Split("ab") }, ShouldPanic)
    96  
    97  		So(Join("a", "b"), ShouldEqual, "a:b")
    98  	})
    99  }