Merge branch 'main' into 17748-extra-claims
[arvados.git] / lib / controller / localdb / login_oidc_test.go
index 4be7d58f699c455ee67e31a206bcebfc889ed0ad..f5b7e2e91cb7bef788bf7dc73f4363bd1ef8a25e 100644 (file)
@@ -51,11 +51,20 @@ func (s *OIDCLoginSuite) TearDownSuite(c *check.C) {
        c.Check(arvados.NewClientFromEnv().RequestAndDecode(nil, "POST", "database/reset", nil, nil), check.IsNil)
 }
 
+// TODO: In the interest of reducing redundant tests: It looks like if we change
+// Given/Family names to {"Fake", "User Name"} in SetUpTest, and update
+// TestGoogleLogin_Success accordingly, then we wouldn't even need a separate
+// TestGoogleLogin_OIDCClamisWithGivenNames test to check the normal case, and
+// the old/renamed OIDCNameWithoutGivenAndFamilyNames test would check the case
+// where we fall back to splitting on whitespace.
+
 func (s *OIDCLoginSuite) SetUpTest(c *check.C) {
        s.fakeProvider = arvadostest.NewOIDCProvider(c)
        s.fakeProvider.AuthEmail = "active-user@arvados.local"
        s.fakeProvider.AuthEmailVerified = true
        s.fakeProvider.AuthName = "Fake User Name"
+       s.fakeProvider.AuthGivenName = "Fake User"
+       s.fakeProvider.AuthFamilyName = "Name"
        s.fakeProvider.ValidCode = fmt.Sprintf("abcdefgh-%d", time.Now().Unix())
        s.fakeProvider.PeopleAPIResponse = map[string]interface{}{}
 
@@ -446,6 +455,7 @@ func (s *OIDCLoginSuite) TestGoogleLogin_Success(c *check.C) {
 
 func (s *OIDCLoginSuite) TestGoogleLogin_RealName(c *check.C) {
        s.fakeProvider.AuthEmail = "joe.smith@primary.example.com"
+       s.fakeProvider.AuthEmailVerified = true
        s.fakeProvider.PeopleAPIResponse = map[string]interface{}{
                "names": []map[string]interface{}{
                        {
@@ -471,8 +481,10 @@ func (s *OIDCLoginSuite) TestGoogleLogin_RealName(c *check.C) {
        c.Check(authinfo.LastName, check.Equals, "Psmith")
 }
 
-func (s *OIDCLoginSuite) TestGoogleLogin_OIDCRealName(c *check.C) {
+func (s *OIDCLoginSuite) TestGoogleLogin_OIDCNameWithoutGivenAndFamilyNames(c *check.C) {
        s.fakeProvider.AuthName = "Joe P. Smith"
+       s.fakeProvider.AuthGivenName = ""
+       s.fakeProvider.AuthFamilyName = ""
        s.fakeProvider.AuthEmail = "joe.smith@primary.example.com"
        state := s.startLogin(c)
        s.localdb.Login(context.Background(), arvados.LoginOptions{
@@ -485,6 +497,22 @@ func (s *OIDCLoginSuite) TestGoogleLogin_OIDCRealName(c *check.C) {
        c.Check(authinfo.LastName, check.Equals, "Smith")
 }
 
+func (s *OIDCLoginSuite) TestGoogleLogin_OIDCClaimsWithGivenNames(c *check.C) {
+       s.fakeProvider.AuthName = "Federico Garcia Lorca"
+       s.fakeProvider.AuthGivenName = "Federico"
+       s.fakeProvider.AuthFamilyName = "Garcia Lorca"
+       s.fakeProvider.AuthEmail = "federico.garcia.lorca@primary.example.com"
+       state := s.startLogin(c)
+       s.localdb.Login(context.Background(), arvados.LoginOptions{
+               Code:  s.fakeProvider.ValidCode,
+               State: state,
+       })
+
+       authinfo := getCallbackAuthInfo(c, s.railsSpy)
+       c.Check(authinfo.FirstName, check.Equals, "Federico")
+       c.Check(authinfo.LastName, check.Equals, "Garcia Lorca")
+}
+
 // People API returns some additional email addresses.
 func (s *OIDCLoginSuite) TestGoogleLogin_AlternateEmailAddresses(c *check.C) {
        s.fakeProvider.AuthEmail = "joe.smith@primary.example.com"