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() { func (ss *SQLStore) addLoginAttemptQueryAndCommandHandlers() {
bus.AddHandler("sql", ss.CreateLoginAttempt) bus.AddHandler("sql", ss.CreateLoginAttempt)
bus.AddHandler("sql", ss.DeleteOldLoginAttempts) 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 { 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 { func (ss *SQLStore) GetUserLoginAttemptCount(ctx context.Context, query *models.GetUserLoginAttemptCountQuery) error {
loginAttempt := new(models.LoginAttempt) return ss.WithDbSession(ctx, func(dbSession *DBSession) error {
total, err := x. loginAttempt := new(models.LoginAttempt)
Where("username = ?", query.Username). total, err := dbSession.
And("created >= ?", query.Since.Unix()). Where("username = ?", query.Username).
Count(loginAttempt) And("created >= ?", query.Since.Unix()).
Count(loginAttempt)
if err != nil { if err != nil {
return err return err
} }
query.Result = total query.Result = total
return nil return nil
})
} }
func toInt64(i interface{}) int64 { func toInt64(i interface{}) int64 {

View File

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