auth_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package login
  2. import (
  3. "errors"
  4. "testing"
  5. m "github.com/grafana/grafana/pkg/models"
  6. . "github.com/smartystreets/goconvey/convey"
  7. )
  8. func TestAuthenticateUser(t *testing.T) {
  9. Convey("Authenticate user", t, func() {
  10. authScenario("When a user authenticates having too many login attempts", func(sc *authScenarioContext) {
  11. mockLoginAttemptValidation(ErrTooManyLoginAttempts, sc)
  12. mockLoginUsingGrafanaDB(nil, sc)
  13. mockLoginUsingLdap(true, nil, sc)
  14. mockSaveInvalidLoginAttempt(sc)
  15. err := AuthenticateUser(sc.loginUserQuery)
  16. Convey("it should result in", func() {
  17. So(err, ShouldEqual, ErrTooManyLoginAttempts)
  18. So(sc.loginAttemptValidationWasCalled, ShouldBeTrue)
  19. So(sc.grafanaLoginWasCalled, ShouldBeFalse)
  20. So(sc.ldapLoginWasCalled, ShouldBeFalse)
  21. So(sc.saveInvalidLoginAttemptWasCalled, ShouldBeFalse)
  22. })
  23. })
  24. authScenario("When grafana user authenticate with valid credentials", func(sc *authScenarioContext) {
  25. mockLoginAttemptValidation(nil, sc)
  26. mockLoginUsingGrafanaDB(nil, sc)
  27. mockLoginUsingLdap(true, ErrInvalidCredentials, sc)
  28. mockSaveInvalidLoginAttempt(sc)
  29. err := AuthenticateUser(sc.loginUserQuery)
  30. Convey("it should result in", func() {
  31. So(err, ShouldEqual, nil)
  32. So(sc.loginAttemptValidationWasCalled, ShouldBeTrue)
  33. So(sc.grafanaLoginWasCalled, ShouldBeTrue)
  34. So(sc.ldapLoginWasCalled, ShouldBeFalse)
  35. So(sc.saveInvalidLoginAttemptWasCalled, ShouldBeFalse)
  36. })
  37. })
  38. authScenario("When grafana user authenticate and unexpected error occurs", func(sc *authScenarioContext) {
  39. customErr := errors.New("custom")
  40. mockLoginAttemptValidation(nil, sc)
  41. mockLoginUsingGrafanaDB(customErr, sc)
  42. mockLoginUsingLdap(true, ErrInvalidCredentials, sc)
  43. mockSaveInvalidLoginAttempt(sc)
  44. err := AuthenticateUser(sc.loginUserQuery)
  45. Convey("it should result in", func() {
  46. So(err, ShouldEqual, customErr)
  47. So(sc.loginAttemptValidationWasCalled, ShouldBeTrue)
  48. So(sc.grafanaLoginWasCalled, ShouldBeTrue)
  49. So(sc.ldapLoginWasCalled, ShouldBeFalse)
  50. So(sc.saveInvalidLoginAttemptWasCalled, ShouldBeFalse)
  51. })
  52. })
  53. authScenario("When a non-existing grafana user authenticate and ldap disabled", func(sc *authScenarioContext) {
  54. mockLoginAttemptValidation(nil, sc)
  55. mockLoginUsingGrafanaDB(m.ErrUserNotFound, sc)
  56. mockLoginUsingLdap(false, nil, sc)
  57. mockSaveInvalidLoginAttempt(sc)
  58. err := AuthenticateUser(sc.loginUserQuery)
  59. Convey("it should result in", func() {
  60. So(err, ShouldEqual, ErrInvalidCredentials)
  61. So(sc.loginAttemptValidationWasCalled, ShouldBeTrue)
  62. So(sc.grafanaLoginWasCalled, ShouldBeTrue)
  63. So(sc.ldapLoginWasCalled, ShouldBeTrue)
  64. So(sc.saveInvalidLoginAttemptWasCalled, ShouldBeFalse)
  65. })
  66. })
  67. authScenario("When a non-existing grafana user authenticate and invalid ldap credentials", func(sc *authScenarioContext) {
  68. mockLoginAttemptValidation(nil, sc)
  69. mockLoginUsingGrafanaDB(m.ErrUserNotFound, sc)
  70. mockLoginUsingLdap(true, ErrInvalidCredentials, sc)
  71. mockSaveInvalidLoginAttempt(sc)
  72. err := AuthenticateUser(sc.loginUserQuery)
  73. Convey("it should result in", func() {
  74. So(err, ShouldEqual, ErrInvalidCredentials)
  75. So(sc.loginAttemptValidationWasCalled, ShouldBeTrue)
  76. So(sc.grafanaLoginWasCalled, ShouldBeTrue)
  77. So(sc.ldapLoginWasCalled, ShouldBeTrue)
  78. So(sc.saveInvalidLoginAttemptWasCalled, ShouldBeTrue)
  79. })
  80. })
  81. authScenario("When a non-existing grafana user authenticate and valid ldap credentials", func(sc *authScenarioContext) {
  82. mockLoginAttemptValidation(nil, sc)
  83. mockLoginUsingGrafanaDB(m.ErrUserNotFound, sc)
  84. mockLoginUsingLdap(true, nil, sc)
  85. mockSaveInvalidLoginAttempt(sc)
  86. err := AuthenticateUser(sc.loginUserQuery)
  87. Convey("it should result in", func() {
  88. So(err, ShouldBeNil)
  89. So(sc.loginAttemptValidationWasCalled, ShouldBeTrue)
  90. So(sc.grafanaLoginWasCalled, ShouldBeTrue)
  91. So(sc.ldapLoginWasCalled, ShouldBeTrue)
  92. So(sc.saveInvalidLoginAttemptWasCalled, ShouldBeFalse)
  93. })
  94. })
  95. authScenario("When a non-existing grafana user authenticate and ldap returns unexpected error", func(sc *authScenarioContext) {
  96. customErr := errors.New("custom")
  97. mockLoginAttemptValidation(nil, sc)
  98. mockLoginUsingGrafanaDB(m.ErrUserNotFound, sc)
  99. mockLoginUsingLdap(true, customErr, sc)
  100. mockSaveInvalidLoginAttempt(sc)
  101. err := AuthenticateUser(sc.loginUserQuery)
  102. Convey("it should result in", func() {
  103. So(err, ShouldEqual, customErr)
  104. So(sc.loginAttemptValidationWasCalled, ShouldBeTrue)
  105. So(sc.grafanaLoginWasCalled, ShouldBeTrue)
  106. So(sc.ldapLoginWasCalled, ShouldBeTrue)
  107. So(sc.saveInvalidLoginAttemptWasCalled, ShouldBeFalse)
  108. })
  109. })
  110. authScenario("When grafana user authenticate with invalid credentials and invalid ldap credentials", func(sc *authScenarioContext) {
  111. mockLoginAttemptValidation(nil, sc)
  112. mockLoginUsingGrafanaDB(ErrInvalidCredentials, sc)
  113. mockLoginUsingLdap(true, ErrInvalidCredentials, sc)
  114. mockSaveInvalidLoginAttempt(sc)
  115. err := AuthenticateUser(sc.loginUserQuery)
  116. Convey("it should result in", func() {
  117. So(err, ShouldEqual, ErrInvalidCredentials)
  118. So(sc.loginAttemptValidationWasCalled, ShouldBeTrue)
  119. So(sc.grafanaLoginWasCalled, ShouldBeTrue)
  120. So(sc.ldapLoginWasCalled, ShouldBeTrue)
  121. So(sc.saveInvalidLoginAttemptWasCalled, ShouldBeTrue)
  122. })
  123. })
  124. })
  125. }
  126. type authScenarioContext struct {
  127. loginUserQuery *m.LoginUserQuery
  128. grafanaLoginWasCalled bool
  129. ldapLoginWasCalled bool
  130. loginAttemptValidationWasCalled bool
  131. saveInvalidLoginAttemptWasCalled bool
  132. }
  133. type authScenarioFunc func(sc *authScenarioContext)
  134. func mockLoginUsingGrafanaDB(err error, sc *authScenarioContext) {
  135. loginUsingGrafanaDB = func(query *m.LoginUserQuery) error {
  136. sc.grafanaLoginWasCalled = true
  137. return err
  138. }
  139. }
  140. func mockLoginUsingLdap(enabled bool, err error, sc *authScenarioContext) {
  141. loginUsingLdap = func(query *m.LoginUserQuery) (bool, error) {
  142. sc.ldapLoginWasCalled = true
  143. return enabled, err
  144. }
  145. }
  146. func mockLoginAttemptValidation(err error, sc *authScenarioContext) {
  147. validateLoginAttempts = func(username string) error {
  148. sc.loginAttemptValidationWasCalled = true
  149. return err
  150. }
  151. }
  152. func mockSaveInvalidLoginAttempt(sc *authScenarioContext) {
  153. saveInvalidLoginAttempt = func(query *m.LoginUserQuery) {
  154. sc.saveInvalidLoginAttemptWasCalled = true
  155. }
  156. }
  157. func authScenario(desc string, fn authScenarioFunc) {
  158. Convey(desc, func() {
  159. origLoginUsingGrafanaDB := loginUsingGrafanaDB
  160. origLoginUsingLdap := loginUsingLdap
  161. origValidateLoginAttempts := validateLoginAttempts
  162. origSaveInvalidLoginAttempt := saveInvalidLoginAttempt
  163. sc := &authScenarioContext{
  164. loginUserQuery: &m.LoginUserQuery{
  165. Username: "user",
  166. Password: "pwd",
  167. IpAddress: "192.168.1.1:56433",
  168. },
  169. }
  170. defer func() {
  171. loginUsingGrafanaDB = origLoginUsingGrafanaDB
  172. loginUsingLdap = origLoginUsingLdap
  173. validateLoginAttempts = origValidateLoginAttempts
  174. saveInvalidLoginAttempt = origSaveInvalidLoginAttempt
  175. }()
  176. fn(sc)
  177. })
  178. }