Chore: Remove x from login attempt (#46853)

This commit is contained in:
Kat Yang 2022-03-24 13:00:41 -04:00 committed by GitHub
parent 36483eb6bf
commit 39f14a2ec2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 16 deletions

View File

@ -14,7 +14,7 @@ var getTimeNow = time.Now
func (ss *SQLStore) addLoginAttemptQueryAndCommandHandlers() {
bus.AddHandler("sql", ss.CreateLoginAttempt)
bus.AddHandler("sql", ss.DeleteOldLoginAttempts)
bus.AddHandler("sql", GetUserLoginAttemptCount)
bus.AddHandler("sql", ss.GetUserLoginAttemptCount)
}
func (ss *SQLStore) CreateLoginAttempt(ctx context.Context, cmd *models.CreateLoginAttemptCommand) error {
@ -65,19 +65,21 @@ func (ss *SQLStore) DeleteOldLoginAttempts(ctx context.Context, cmd *models.Dele
})
}
func GetUserLoginAttemptCount(ctx context.Context, query *models.GetUserLoginAttemptCountQuery) error {
loginAttempt := new(models.LoginAttempt)
total, err := x.
Where("username = ?", query.Username).
And("created >= ?", query.Since.Unix()).
Count(loginAttempt)
func (ss *SQLStore) GetUserLoginAttemptCount(ctx context.Context, query *models.GetUserLoginAttemptCountQuery) error {
return ss.WithDbSession(ctx, func(dbSession *DBSession) error {
loginAttempt := new(models.LoginAttempt)
total, err := dbSession.
Where("username = ?", query.Username).
And("created >= ?", query.Since.Unix()).
Count(loginAttempt)
if err != nil {
return err
}
if err != nil {
return err
}
query.Result = total
return nil
query.Result = total
return nil
})
}
func toInt64(i interface{}) int64 {

View File

@ -51,7 +51,7 @@ func TestLoginAttempts(t *testing.T) {
Username: user,
Since: timePlusTwoMinutes.Add(time.Second * 1),
}
err := GetUserLoginAttemptCount(context.Background(), &query)
err := sqlStore.GetUserLoginAttemptCount(context.Background(), &query)
require.Nil(t, err)
require.Equal(t, int64(0), query.Result)
})
@ -62,7 +62,7 @@ func TestLoginAttempts(t *testing.T) {
Username: user,
Since: beginningOfTime,
}
err := GetUserLoginAttemptCount(context.Background(), &query)
err := sqlStore.GetUserLoginAttemptCount(context.Background(), &query)
require.Nil(t, err)
require.Equal(t, int64(3), query.Result)
})
@ -73,7 +73,7 @@ func TestLoginAttempts(t *testing.T) {
Username: user,
Since: timePlusOneMinute,
}
err := GetUserLoginAttemptCount(context.Background(), &query)
err := sqlStore.GetUserLoginAttemptCount(context.Background(), &query)
require.Nil(t, err)
require.Equal(t, int64(2), query.Result)
})
@ -84,7 +84,7 @@ func TestLoginAttempts(t *testing.T) {
Username: user,
Since: timePlusTwoMinutes,
}
err := GetUserLoginAttemptCount(context.Background(), &query)
err := sqlStore.GetUserLoginAttemptCount(context.Background(), &query)
require.Nil(t, err)
require.Equal(t, int64(1), query.Result)
})