go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gae/impl/memory/user_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 memory
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"go.chromium.org/luci/gae/service/user"
    22  
    23  	. "github.com/smartystreets/goconvey/convey"
    24  	. "go.chromium.org/luci/common/testing/assertions"
    25  )
    26  
    27  func TestUser(t *testing.T) {
    28  	t.Parallel()
    29  
    30  	Convey("user", t, func() {
    31  		c := Use(context.Background())
    32  
    33  		Convey("default state is anonymous", func() {
    34  			So(user.Current(c), ShouldBeNil)
    35  
    36  			usr, err := user.CurrentOAuth(c, "something")
    37  			So(err, ShouldBeNil)
    38  			So(usr, ShouldBeNil)
    39  
    40  			So(user.IsAdmin(c), ShouldBeFalse)
    41  		})
    42  
    43  		Convey("can login (normal)", func() {
    44  			user.GetTestable(c).Login("hello@world.com", "", false)
    45  			So(user.Current(c), ShouldResemble, &user.User{
    46  				Email:      "hello@world.com",
    47  				AuthDomain: "world.com",
    48  				ID:         "14628837901535854097",
    49  			})
    50  
    51  			usr, err := user.CurrentOAuth(c, "scope")
    52  			So(usr, ShouldBeNil)
    53  			So(err, ShouldBeNil)
    54  
    55  			Convey("and logout", func() {
    56  				user.GetTestable(c).Logout()
    57  				So(user.Current(c), ShouldBeNil)
    58  
    59  				usr, err := user.CurrentOAuth(c, "scope")
    60  				So(usr, ShouldBeNil)
    61  				So(err, ShouldBeNil)
    62  			})
    63  		})
    64  
    65  		Convey("can be admin", func() {
    66  			user.GetTestable(c).Login("hello@world.com", "", true)
    67  			So(user.Current(c), ShouldResemble, &user.User{
    68  				Email:      "hello@world.com",
    69  				AuthDomain: "world.com",
    70  				ID:         "14628837901535854097",
    71  				Admin:      true,
    72  			})
    73  			So(user.IsAdmin(c), ShouldBeTrue)
    74  		})
    75  
    76  		Convey("can login (oauth)", func() {
    77  			user.GetTestable(c).Login("hello@world.com", "clientID", false)
    78  			usr, err := user.CurrentOAuth(c, "scope")
    79  			So(err, ShouldBeNil)
    80  			So(usr, ShouldResemble, &user.User{
    81  				Email:      "hello@world.com",
    82  				AuthDomain: "world.com",
    83  				ID:         "14628837901535854097",
    84  				ClientID:   "clientID",
    85  			})
    86  
    87  			So(user.Current(c), ShouldBeNil)
    88  
    89  			Convey("and logout", func() {
    90  				user.GetTestable(c).Logout()
    91  				So(user.Current(c), ShouldBeNil)
    92  
    93  				usr, err := user.CurrentOAuth(c, "scope")
    94  				So(usr, ShouldBeNil)
    95  				So(err, ShouldBeNil)
    96  			})
    97  		})
    98  
    99  		Convey("panics on bad email", func() {
   100  			So(func() {
   101  				user.GetTestable(c).Login("bademail", "", false)
   102  			}, ShouldPanicLike, `mail:`)
   103  		})
   104  
   105  		Convey("fake URLs", func() {
   106  			url, err := user.LoginURL(c, "https://funky.example.com")
   107  			So(err, ShouldBeNil)
   108  			So(url, ShouldEqual, "https://fakeapp.example.com/_ah/login?redirect=https%3A%2F%2Ffunky.example.com")
   109  
   110  			url, err = user.LogoutURL(c, "https://funky.example.com")
   111  			So(err, ShouldBeNil)
   112  			So(url, ShouldEqual, "https://fakeapp.example.com/_ah/logout?redirect=https%3A%2F%2Ffunky.example.com")
   113  		})
   114  
   115  		Convey("Some stuff is deprecated", func() {
   116  			url, err := user.LoginURLFederated(c, "https://something", "something")
   117  			So(err, ShouldErrLike, "LoginURLFederated is deprecated")
   118  			So(url, ShouldEqual, "")
   119  
   120  			key, err := user.OAuthConsumerKey(c)
   121  			So(err, ShouldErrLike, "OAuthConsumerKey is deprecated")
   122  			So(key, ShouldEqual, "")
   123  		})
   124  
   125  	})
   126  }