brute_force_login_protection_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package login
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/bus"
  5. m "github.com/grafana/grafana/pkg/models"
  6. "github.com/grafana/grafana/pkg/setting"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. func TestLoginAttemptsValidation(t *testing.T) {
  10. Convey("Validate login attempts", t, func() {
  11. Convey("Given brute force login protection enabled", func() {
  12. setting.DisableBruteForceLoginProtection = false
  13. Convey("When user login attempt count equals max-1 ", func() {
  14. withLoginAttempts(maxInvalidLoginAttempts - 1)
  15. err := validateLoginAttempts("user")
  16. Convey("it should not result in error", func() {
  17. So(err, ShouldBeNil)
  18. })
  19. })
  20. Convey("When user login attempt count equals max ", func() {
  21. withLoginAttempts(maxInvalidLoginAttempts)
  22. err := validateLoginAttempts("user")
  23. Convey("it should result in too many login attempts error", func() {
  24. So(err, ShouldEqual, ErrTooManyLoginAttempts)
  25. })
  26. })
  27. Convey("When user login attempt count is greater than max ", func() {
  28. withLoginAttempts(maxInvalidLoginAttempts + 5)
  29. err := validateLoginAttempts("user")
  30. Convey("it should result in too many login attempts error", func() {
  31. So(err, ShouldEqual, ErrTooManyLoginAttempts)
  32. })
  33. })
  34. Convey("When saving invalid login attempt", func() {
  35. defer bus.ClearBusHandlers()
  36. createLoginAttemptCmd := &m.CreateLoginAttemptCommand{}
  37. bus.AddHandler("test", func(cmd *m.CreateLoginAttemptCommand) error {
  38. createLoginAttemptCmd = cmd
  39. return nil
  40. })
  41. saveInvalidLoginAttempt(&m.LoginUserQuery{
  42. Username: "user",
  43. Password: "pwd",
  44. IpAddress: "192.168.1.1:56433",
  45. })
  46. Convey("it should dispatch command", func() {
  47. So(createLoginAttemptCmd, ShouldNotBeNil)
  48. So(createLoginAttemptCmd.Username, ShouldEqual, "user")
  49. So(createLoginAttemptCmd.IpAddress, ShouldEqual, "192.168.1.1:56433")
  50. })
  51. })
  52. })
  53. Convey("Given brute force login protection disabled", func() {
  54. setting.DisableBruteForceLoginProtection = true
  55. Convey("When user login attempt count equals max-1 ", func() {
  56. withLoginAttempts(maxInvalidLoginAttempts - 1)
  57. err := validateLoginAttempts("user")
  58. Convey("it should not result in error", func() {
  59. So(err, ShouldBeNil)
  60. })
  61. })
  62. Convey("When user login attempt count equals max ", func() {
  63. withLoginAttempts(maxInvalidLoginAttempts)
  64. err := validateLoginAttempts("user")
  65. Convey("it should not result in error", func() {
  66. So(err, ShouldBeNil)
  67. })
  68. })
  69. Convey("When user login attempt count is greater than max ", func() {
  70. withLoginAttempts(maxInvalidLoginAttempts + 5)
  71. err := validateLoginAttempts("user")
  72. Convey("it should not result in error", func() {
  73. So(err, ShouldBeNil)
  74. })
  75. })
  76. Convey("When saving invalid login attempt", func() {
  77. defer bus.ClearBusHandlers()
  78. createLoginAttemptCmd := (*m.CreateLoginAttemptCommand)(nil)
  79. bus.AddHandler("test", func(cmd *m.CreateLoginAttemptCommand) error {
  80. createLoginAttemptCmd = cmd
  81. return nil
  82. })
  83. saveInvalidLoginAttempt(&m.LoginUserQuery{
  84. Username: "user",
  85. Password: "pwd",
  86. IpAddress: "192.168.1.1:56433",
  87. })
  88. Convey("it should not dispatch command", func() {
  89. So(createLoginAttemptCmd, ShouldBeNil)
  90. })
  91. })
  92. })
  93. })
  94. }
  95. func withLoginAttempts(loginAttempts int64) {
  96. bus.AddHandler("test", func(query *m.GetUserLoginAttemptCountQuery) error {
  97. query.Result = loginAttempts
  98. return nil
  99. })
  100. }