diff --git a/pkg/services/sqlstore/user_auth.go b/pkg/services/sqlstore/user_auth.go index fa4e5938207..fd8ec3d057f 100644 --- a/pkg/services/sqlstore/user_auth.go +++ b/pkg/services/sqlstore/user_auth.go @@ -10,6 +10,8 @@ import ( "github.com/grafana/grafana/pkg/util" ) +var getTime = time.Now + func init() { bus.AddHandler("sql", GetUserByAuthInfo) bus.AddHandler("sql", GetAuthInfo) @@ -153,7 +155,7 @@ func SetAuthInfo(cmd *m.SetAuthInfoCommand) error { UserId: cmd.UserId, AuthModule: cmd.AuthModule, AuthId: cmd.AuthId, - Created: time.Now(), + Created: getTime(), } if cmd.OAuthToken != nil { @@ -187,7 +189,7 @@ func UpdateAuthInfo(cmd *m.UpdateAuthInfoCommand) error { UserId: cmd.UserId, AuthModule: cmd.AuthModule, AuthId: cmd.AuthId, - Created: time.Now(), + Created: getTime(), } if cmd.OAuthToken != nil { diff --git a/pkg/services/sqlstore/user_auth_test.go b/pkg/services/sqlstore/user_auth_test.go index dca3da0be41..8a8213b8f87 100644 --- a/pkg/services/sqlstore/user_auth_test.go +++ b/pkg/services/sqlstore/user_auth_test.go @@ -175,17 +175,21 @@ func TestUserAuth(t *testing.T) { login := "loginuser0" // Calling GetUserByAuthInfoQuery on an existing user will populate an entry in the user_auth table + // Make the first log-in during the past + getTime = func() time.Time { return time.Now().AddDate(0, 0, -2) } query := &m.GetUserByAuthInfoQuery{Login: login, AuthModule: "test1", AuthId: "test1"} err = GetUserByAuthInfo(query) + getTime = time.Now 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) + // Have this module's last log-in be more recent + getTime = func() time.Time { return time.Now().AddDate(0, 0, -1) } query = &m.GetUserByAuthInfoQuery{Login: login, AuthModule: "test2", AuthId: "test2"} err = GetUserByAuthInfo(query) + getTime = time.Now So(err, ShouldBeNil) So(query.Result.Login, ShouldEqual, login) @@ -201,8 +205,6 @@ func TestUserAuth(t *testing.T) { So(getAuthQuery.Result.AuthModule, ShouldEqual, "test2") // "log in" again with the first auth module - // resolution of `Created` column is 1sec, so we need a delay - time.Sleep(time.Second) updateAuthCmd := &m.UpdateAuthInfoCommand{UserId: query.Result.Id, AuthModule: "test1", AuthId: "test1"} err = UpdateAuthInfo(updateAuthCmd)