go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/auth/identity/identity_test.go (about)

     1  // Copyright 2015 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 identity
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/smartystreets/goconvey/convey"
    21  )
    22  
    23  func TestIdentity(t *testing.T) {
    24  	Convey("MakeIdentity works", t, func() {
    25  		id, err := MakeIdentity("anonymous:anonymous")
    26  		So(err, ShouldBeNil)
    27  		So(id, ShouldEqual, Identity("anonymous:anonymous"))
    28  		So(id.Kind(), ShouldEqual, Anonymous)
    29  		So(id.Value(), ShouldEqual, "anonymous")
    30  		So(id.Email(), ShouldEqual, "")
    31  
    32  		_, err = MakeIdentity("bad ident")
    33  		So(err, ShouldNotBeNil)
    34  	})
    35  
    36  	Convey("Validate works", t, func() {
    37  		So(Identity("user:abc@example.com").Validate(), ShouldBeNil)
    38  		So(Identity("user:").Validate(), ShouldNotBeNil)
    39  		So(Identity(":abc").Validate(), ShouldNotBeNil)
    40  		So(Identity("abc@example.com").Validate(), ShouldNotBeNil)
    41  		So(Identity("user:abc").Validate(), ShouldNotBeNil)
    42  	})
    43  
    44  	Convey("Kind works", t, func() {
    45  		So(Identity("user:abc@example.com").Kind(), ShouldEqual, User)
    46  		So(Identity("???").Kind(), ShouldEqual, Anonymous)
    47  	})
    48  
    49  	Convey("Value works", t, func() {
    50  		So(Identity("service:abc").Value(), ShouldEqual, "abc")
    51  		So(Identity("???").Value(), ShouldEqual, "anonymous")
    52  	})
    53  
    54  	Convey("Email works", t, func() {
    55  		So(Identity("user:abc@example.com").Email(), ShouldEqual, "abc@example.com")
    56  		So(Identity("service:abc").Email(), ShouldEqual, "")
    57  		So(Identity("???").Email(), ShouldEqual, "")
    58  	})
    59  }