go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/lucictx/local_auth_test.go (about)

     1  // Copyright 2017 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 lucictx
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	. "github.com/smartystreets/goconvey/convey"
    22  )
    23  
    24  func TestLocalAuth(t *testing.T) {
    25  	t.Parallel()
    26  
    27  	Convey("SwitchLocalAccount works", t, func() {
    28  		c := context.Background()
    29  
    30  		Convey("No local_auth at all", func() {
    31  			ctx, err := SwitchLocalAccount(c, "some")
    32  			So(ctx, ShouldBeNil)
    33  			So(err, ShouldEqual, ErrNoLocalAuthAccount)
    34  		})
    35  
    36  		Convey("Noop change", func() {
    37  			c := SetLocalAuth(c, &LocalAuth{
    38  				DefaultAccountId: "some",
    39  				Accounts: []*LocalAuthAccount{
    40  					{Id: "some"},
    41  				},
    42  			})
    43  			ctx, err := SwitchLocalAccount(c, "some")
    44  			So(ctx, ShouldEqual, c)
    45  			So(err, ShouldBeNil)
    46  		})
    47  
    48  		Convey("Switching into existing account", func() {
    49  			c := SetLocalAuth(c, &LocalAuth{
    50  				DefaultAccountId: "one",
    51  				Accounts: []*LocalAuthAccount{
    52  					{Id: "one"},
    53  					{Id: "two"},
    54  				},
    55  			})
    56  			ctx, err := SwitchLocalAccount(c, "two")
    57  			So(ctx, ShouldNotBeNil)
    58  			So(err, ShouldBeNil)
    59  			So(GetLocalAuth(ctx).DefaultAccountId, ShouldEqual, "two")
    60  		})
    61  
    62  		Convey("Switching into non-existing account", func() {
    63  			c := SetLocalAuth(c, &LocalAuth{
    64  				DefaultAccountId: "one",
    65  				Accounts: []*LocalAuthAccount{
    66  					{Id: "one"},
    67  				},
    68  			})
    69  			ctx, err := SwitchLocalAccount(c, "two")
    70  			So(ctx, ShouldBeNil)
    71  			So(err, ShouldEqual, ErrNoLocalAuthAccount)
    72  		})
    73  
    74  		Convey("Clearing local auth", func() {
    75  			c := SetLocalAuth(c, &LocalAuth{
    76  				DefaultAccountId: "one",
    77  				Accounts: []*LocalAuthAccount{
    78  					{Id: "some"},
    79  				},
    80  			})
    81  			c = SetLocalAuth(c, nil)
    82  
    83  			So(GetLocalAuth(c), ShouldBeNil)
    84  
    85  			ctx, err := SwitchLocalAccount(c, "some")
    86  			So(ctx, ShouldBeNil)
    87  			So(err, ShouldEqual, ErrNoLocalAuthAccount)
    88  		})
    89  	})
    90  }