mirror of
https://github.com/grafana/grafana.git
synced 2025-02-14 01:23:32 -06:00
Chore: Use TimeNow instead of time.Now in user sql store to make it testable (#54983)
This commit is contained in:
parent
d5e28426ee
commit
c5be9605a2
@ -86,8 +86,9 @@ func TestSQLStore_GetOrgUsers(t *testing.T) {
|
||||
|
||||
func TestSQLStore_GetOrgUsers_PopulatesCorrectly(t *testing.T) {
|
||||
// The millisecond part is not stored in the DB
|
||||
constNow := time.Now().UTC().Truncate(time.Second)
|
||||
defer mockTimeNow(constNow)()
|
||||
constNow := time.Date(2022, 8, 17, 20, 34, 58, 0, time.UTC)
|
||||
MockTimeNow(constNow)
|
||||
defer ResetTimeNow()
|
||||
|
||||
store := InitTestDB(t, InitTestDBOpt{})
|
||||
_, err := store.CreateUser(context.Background(), user.CreateUserCommand{
|
||||
@ -334,14 +335,3 @@ func hasWildcardScope(user *user.SignedInUser, action string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func mockTimeNow(constTime time.Time) func() {
|
||||
timeNow = func() time.Time {
|
||||
return constTime.Truncate(time.Second)
|
||||
}
|
||||
return resetTimeNow
|
||||
}
|
||||
|
||||
func resetTimeNow() {
|
||||
timeNow = time.Now
|
||||
}
|
||||
|
16
pkg/services/sqlstore/time.go
Normal file
16
pkg/services/sqlstore/time.go
Normal file
@ -0,0 +1,16 @@
|
||||
package sqlstore
|
||||
|
||||
import "time"
|
||||
|
||||
// TimeNow makes it possible to test usage of time
|
||||
var TimeNow = time.Now
|
||||
|
||||
func MockTimeNow(constTime time.Time) {
|
||||
TimeNow = func() time.Time {
|
||||
return constTime
|
||||
}
|
||||
}
|
||||
|
||||
func ResetTimeNow() {
|
||||
TimeNow = time.Now
|
||||
}
|
@ -114,9 +114,9 @@ func (ss *SQLStore) createUser(ctx context.Context, sess *DBSession, args user.C
|
||||
IsDisabled: args.IsDisabled,
|
||||
OrgID: orgID,
|
||||
EmailVerified: args.EmailVerified,
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
LastSeenAt: time.Now().AddDate(-10, 0, 0),
|
||||
Created: TimeNow(),
|
||||
Updated: TimeNow(),
|
||||
LastSeenAt: TimeNow().AddDate(-10, 0, 0),
|
||||
IsServiceAccount: args.IsServiceAccount,
|
||||
}
|
||||
|
||||
@ -159,8 +159,8 @@ func (ss *SQLStore) createUser(ctx context.Context, sess *DBSession, args user.C
|
||||
OrgId: orgID,
|
||||
UserId: usr.ID,
|
||||
Role: org.RoleAdmin,
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
Created: TimeNow(),
|
||||
Updated: TimeNow(),
|
||||
}
|
||||
|
||||
if ss.Cfg.AutoAssignOrg && !usr.IsAdmin {
|
||||
@ -314,7 +314,7 @@ func (ss *SQLStore) UpdateUser(ctx context.Context, cmd *models.UpdateUserComman
|
||||
Email: cmd.Email,
|
||||
Login: cmd.Login,
|
||||
Theme: cmd.Theme,
|
||||
Updated: time.Now(),
|
||||
Updated: TimeNow(),
|
||||
}
|
||||
|
||||
if _, err := sess.ID(cmd.UserId).Where(notServiceAccountFilter(ss)).Update(&user); err != nil {
|
||||
@ -343,7 +343,7 @@ func (ss *SQLStore) ChangeUserPassword(ctx context.Context, cmd *models.ChangeUs
|
||||
return ss.WithTransactionalDbSession(ctx, func(sess *DBSession) error {
|
||||
user := user.User{
|
||||
Password: cmd.NewPassword,
|
||||
Updated: time.Now(),
|
||||
Updated: TimeNow(),
|
||||
}
|
||||
|
||||
_, err := sess.ID(cmd.UserId).Where(notServiceAccountFilter(ss)).Update(&user)
|
||||
@ -355,7 +355,7 @@ func (ss *SQLStore) UpdateUserLastSeenAt(ctx context.Context, cmd *models.Update
|
||||
return ss.WithTransactionalDbSession(ctx, func(sess *DBSession) error {
|
||||
user := user.User{
|
||||
ID: cmd.UserId,
|
||||
LastSeenAt: time.Now(),
|
||||
LastSeenAt: TimeNow(),
|
||||
}
|
||||
|
||||
_, err := sess.ID(cmd.UserId).Update(&user)
|
||||
@ -849,7 +849,7 @@ func (ss *SQLStore) SetUserHelpFlag(ctx context.Context, cmd *models.SetUserHelp
|
||||
user := user.User{
|
||||
ID: cmd.UserId,
|
||||
HelpFlags1: cmd.HelpFlags1,
|
||||
Updated: time.Now(),
|
||||
Updated: TimeNow(),
|
||||
}
|
||||
|
||||
_, err := sess.ID(cmd.UserId).Cols("help_flags1").Update(&user)
|
||||
|
Loading…
Reference in New Issue
Block a user