login_attempt_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package sqlstore
  2. import (
  3. "testing"
  4. "time"
  5. m "github.com/grafana/grafana/pkg/models"
  6. . "github.com/smartystreets/goconvey/convey"
  7. )
  8. func mockTime(mock time.Time) time.Time {
  9. getTimeNow = func() time.Time { return mock }
  10. return mock
  11. }
  12. func TestLoginAttempts(t *testing.T) {
  13. Convey("Testing Login Attempts DB Access", t, func() {
  14. InitTestDB(t)
  15. user := "user"
  16. beginningOfTime := mockTime(time.Date(2017, 10, 22, 8, 0, 0, 0, time.Local))
  17. err := CreateLoginAttempt(&m.CreateLoginAttemptCommand{
  18. Username: user,
  19. IpAddress: "192.168.0.1",
  20. })
  21. So(err, ShouldBeNil)
  22. timePlusOneMinute := mockTime(beginningOfTime.Add(time.Minute * 1))
  23. err = CreateLoginAttempt(&m.CreateLoginAttemptCommand{
  24. Username: user,
  25. IpAddress: "192.168.0.1",
  26. })
  27. So(err, ShouldBeNil)
  28. timePlusTwoMinutes := mockTime(beginningOfTime.Add(time.Minute * 2))
  29. err = CreateLoginAttempt(&m.CreateLoginAttemptCommand{
  30. Username: user,
  31. IpAddress: "192.168.0.1",
  32. })
  33. So(err, ShouldBeNil)
  34. Convey("Should return a total count of zero login attempts when comparing since beginning of time + 2min and 1s", func() {
  35. query := m.GetUserLoginAttemptCountQuery{
  36. Username: user,
  37. Since: timePlusTwoMinutes.Add(time.Second * 1),
  38. }
  39. err := GetUserLoginAttemptCount(&query)
  40. So(err, ShouldBeNil)
  41. So(query.Result, ShouldEqual, 0)
  42. })
  43. Convey("Should return the total count of login attempts since beginning of time", func() {
  44. query := m.GetUserLoginAttemptCountQuery{
  45. Username: user,
  46. Since: beginningOfTime,
  47. }
  48. err := GetUserLoginAttemptCount(&query)
  49. So(err, ShouldBeNil)
  50. So(query.Result, ShouldEqual, 3)
  51. })
  52. Convey("Should return the total count of login attempts since beginning of time + 1min", func() {
  53. query := m.GetUserLoginAttemptCountQuery{
  54. Username: user,
  55. Since: timePlusOneMinute,
  56. }
  57. err := GetUserLoginAttemptCount(&query)
  58. So(err, ShouldBeNil)
  59. So(query.Result, ShouldEqual, 2)
  60. })
  61. Convey("Should return the total count of login attempts since beginning of time + 2min", func() {
  62. query := m.GetUserLoginAttemptCountQuery{
  63. Username: user,
  64. Since: timePlusTwoMinutes,
  65. }
  66. err := GetUserLoginAttemptCount(&query)
  67. So(err, ShouldBeNil)
  68. So(query.Result, ShouldEqual, 1)
  69. })
  70. Convey("Should return deleted rows older than beginning of time", func() {
  71. cmd := m.DeleteOldLoginAttemptsCommand{
  72. OlderThan: beginningOfTime,
  73. }
  74. err := DeleteOldLoginAttempts(&cmd)
  75. So(err, ShouldBeNil)
  76. So(cmd.DeletedRows, ShouldEqual, 0)
  77. })
  78. Convey("Should return deleted rows older than beginning of time + 1min", func() {
  79. cmd := m.DeleteOldLoginAttemptsCommand{
  80. OlderThan: timePlusOneMinute,
  81. }
  82. err := DeleteOldLoginAttempts(&cmd)
  83. So(err, ShouldBeNil)
  84. So(cmd.DeletedRows, ShouldEqual, 1)
  85. })
  86. Convey("Should return deleted rows older than beginning of time + 2min", func() {
  87. cmd := m.DeleteOldLoginAttemptsCommand{
  88. OlderThan: timePlusTwoMinutes,
  89. }
  90. err := DeleteOldLoginAttempts(&cmd)
  91. So(err, ShouldBeNil)
  92. So(cmd.DeletedRows, ShouldEqual, 2)
  93. })
  94. Convey("Should return deleted rows older than beginning of time + 2min and 1s", func() {
  95. cmd := m.DeleteOldLoginAttemptsCommand{
  96. OlderThan: timePlusTwoMinutes.Add(time.Second * 1),
  97. }
  98. err := DeleteOldLoginAttempts(&cmd)
  99. So(err, ShouldBeNil)
  100. So(cmd.DeletedRows, ShouldEqual, 3)
  101. })
  102. })
  103. }