Browse Source

Always return most recently used auth_module from GetAuthInfo

Sean Lafferty 6 years ago
parent
commit
f17307bb97
2 changed files with 33 additions and 1 deletions
  1. 1 1
      pkg/services/sqlstore/user_auth.go
  2. 32 0
      pkg/services/sqlstore/user_auth_test.go

+ 1 - 1
pkg/services/sqlstore/user_auth.go

@@ -119,7 +119,7 @@ func GetAuthInfo(query *m.GetAuthInfoQuery) error {
 		AuthModule: query.AuthModule,
 		AuthId:     query.AuthId,
 	}
-	has, err := x.Get(userAuth)
+	has, err := x.Desc("created").Get(userAuth)
 	if err != nil {
 		return err
 	}

+ 32 - 0
pkg/services/sqlstore/user_auth_test.go

@@ -169,5 +169,37 @@ func TestUserAuth(t *testing.T) {
 			So(getAuthQuery.Result.OAuthTokenType, ShouldEqual, token.TokenType)
 
 		})
+
+		Convey("Always return the most recently used auth_module", func() {
+			// Find a user to set tokens on
+			login := "loginuser0"
+
+			// Calling GetUserByAuthInfoQuery on an existing user will populate an entry in the user_auth table
+			query := &m.GetUserByAuthInfoQuery{Login: login, AuthModule: "test", AuthId: "test"}
+			err = GetUserByAuthInfo(query)
+
+			So(err, ShouldBeNil)
+			So(query.Result.Login, ShouldEqual, login)
+
+			// Add a second auth module for this user
+			// resolution of `Created` column is 1sec, so we need a delay
+			time.Sleep(time.Second)
+			query = &m.GetUserByAuthInfoQuery{Login: login, AuthModule: "test2", AuthId: "test2"}
+			err = GetUserByAuthInfo(query)
+
+			So(err, ShouldBeNil)
+			So(query.Result.Login, ShouldEqual, login)
+
+			// Get the latest entry by not supply an authmodule or authid
+			getAuthQuery := &m.GetAuthInfoQuery{
+				UserId: query.Result.Id,
+			}
+
+			err = GetAuthInfo(getAuthQuery)
+
+			So(err, ShouldBeNil)
+			So(getAuthQuery.Result.AuthModule, ShouldEqual, "test2")
+
+		})
 	})
 }