| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- package sqlstore
- import (
- "testing"
- "time"
- m "github.com/grafana/grafana/pkg/models"
- . "github.com/smartystreets/goconvey/convey"
- )
- func mockTime(mock time.Time) time.Time {
- getTimeNow = func() time.Time { return mock }
- return mock
- }
- func TestLoginAttempts(t *testing.T) {
- Convey("Testing Login Attempts DB Access", t, func() {
- InitTestDB(t)
- user := "user"
- beginningOfTime := mockTime(time.Date(2017, 10, 22, 8, 0, 0, 0, time.Local))
- err := CreateLoginAttempt(&m.CreateLoginAttemptCommand{
- Username: user,
- IpAddress: "192.168.0.1",
- })
- So(err, ShouldBeNil)
- timePlusOneMinute := mockTime(beginningOfTime.Add(time.Minute * 1))
- err = CreateLoginAttempt(&m.CreateLoginAttemptCommand{
- Username: user,
- IpAddress: "192.168.0.1",
- })
- So(err, ShouldBeNil)
- timePlusTwoMinutes := mockTime(beginningOfTime.Add(time.Minute * 2))
- err = CreateLoginAttempt(&m.CreateLoginAttemptCommand{
- Username: user,
- IpAddress: "192.168.0.1",
- })
- So(err, ShouldBeNil)
- Convey("Should return a total count of zero login attempts when comparing since beginning of time + 2min and 1s", func() {
- query := m.GetUserLoginAttemptCountQuery{
- Username: user,
- Since: timePlusTwoMinutes.Add(time.Second * 1),
- }
- err := GetUserLoginAttemptCount(&query)
- So(err, ShouldBeNil)
- So(query.Result, ShouldEqual, 0)
- })
- Convey("Should return the total count of login attempts since beginning of time", func() {
- query := m.GetUserLoginAttemptCountQuery{
- Username: user,
- Since: beginningOfTime,
- }
- err := GetUserLoginAttemptCount(&query)
- So(err, ShouldBeNil)
- So(query.Result, ShouldEqual, 3)
- })
- Convey("Should return the total count of login attempts since beginning of time + 1min", func() {
- query := m.GetUserLoginAttemptCountQuery{
- Username: user,
- Since: timePlusOneMinute,
- }
- err := GetUserLoginAttemptCount(&query)
- So(err, ShouldBeNil)
- So(query.Result, ShouldEqual, 2)
- })
- Convey("Should return the total count of login attempts since beginning of time + 2min", func() {
- query := m.GetUserLoginAttemptCountQuery{
- Username: user,
- Since: timePlusTwoMinutes,
- }
- err := GetUserLoginAttemptCount(&query)
- So(err, ShouldBeNil)
- So(query.Result, ShouldEqual, 1)
- })
- Convey("Should return deleted rows older than beginning of time", func() {
- cmd := m.DeleteOldLoginAttemptsCommand{
- OlderThan: beginningOfTime,
- }
- err := DeleteOldLoginAttempts(&cmd)
- So(err, ShouldBeNil)
- So(cmd.DeletedRows, ShouldEqual, 0)
- })
- Convey("Should return deleted rows older than beginning of time + 1min", func() {
- cmd := m.DeleteOldLoginAttemptsCommand{
- OlderThan: timePlusOneMinute,
- }
- err := DeleteOldLoginAttempts(&cmd)
- So(err, ShouldBeNil)
- So(cmd.DeletedRows, ShouldEqual, 1)
- })
- Convey("Should return deleted rows older than beginning of time + 2min", func() {
- cmd := m.DeleteOldLoginAttemptsCommand{
- OlderThan: timePlusTwoMinutes,
- }
- err := DeleteOldLoginAttempts(&cmd)
- So(err, ShouldBeNil)
- So(cmd.DeletedRows, ShouldEqual, 2)
- })
- Convey("Should return deleted rows older than beginning of time + 2min and 1s", func() {
- cmd := m.DeleteOldLoginAttemptsCommand{
- OlderThan: timePlusTwoMinutes.Add(time.Second * 1),
- }
- err := DeleteOldLoginAttempts(&cmd)
- So(err, ShouldBeNil)
- So(cmd.DeletedRows, ShouldEqual, 3)
- })
- })
- }
|