ldap_login_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package login
  2. import (
  3. "errors"
  4. "testing"
  5. "github.com/grafana/grafana/pkg/models"
  6. "github.com/grafana/grafana/pkg/services/ldap"
  7. "github.com/grafana/grafana/pkg/services/multildap"
  8. "github.com/grafana/grafana/pkg/setting"
  9. . "github.com/smartystreets/goconvey/convey"
  10. )
  11. var errTest = errors.New("Test error")
  12. func TestLDAPLogin(t *testing.T) {
  13. Convey("Login using ldap", t, func() {
  14. Convey("Given ldap enabled and no server configured", func() {
  15. setting.LDAPEnabled = true
  16. LDAPLoginScenario("When login", func(sc *LDAPLoginScenarioContext) {
  17. sc.withLoginResult(false)
  18. getLDAPConfig = func() (*ldap.Config, error) {
  19. config := &ldap.Config{
  20. Servers: []*ldap.ServerConfig{},
  21. }
  22. return config, nil
  23. }
  24. enabled, err := loginUsingLDAP(sc.loginUserQuery)
  25. Convey("it should return true", func() {
  26. So(enabled, ShouldBeTrue)
  27. })
  28. Convey("it should return no LDAP servers error", func() {
  29. So(err, ShouldEqual, errTest)
  30. })
  31. Convey("it should not call ldap login", func() {
  32. So(sc.LDAPAuthenticatorMock.loginCalled, ShouldBeTrue)
  33. })
  34. })
  35. })
  36. Convey("Given ldap disabled", func() {
  37. setting.LDAPEnabled = false
  38. LDAPLoginScenario("When login", func(sc *LDAPLoginScenarioContext) {
  39. sc.withLoginResult(false)
  40. enabled, err := loginUsingLDAP(&models.LoginUserQuery{
  41. Username: "user",
  42. Password: "pwd",
  43. })
  44. Convey("it should return false", func() {
  45. So(enabled, ShouldBeFalse)
  46. })
  47. Convey("it should not return error", func() {
  48. So(err, ShouldBeNil)
  49. })
  50. Convey("it should not call ldap login", func() {
  51. So(sc.LDAPAuthenticatorMock.loginCalled, ShouldBeFalse)
  52. })
  53. })
  54. })
  55. })
  56. }
  57. type mockAuth struct {
  58. validLogin bool
  59. loginCalled bool
  60. pingCalled bool
  61. }
  62. func (auth *mockAuth) Ping() ([]*multildap.ServerStatus, error) {
  63. auth.pingCalled = true
  64. return nil, nil
  65. }
  66. func (auth *mockAuth) Login(query *models.LoginUserQuery) (
  67. *models.ExternalUserInfo,
  68. error,
  69. ) {
  70. auth.loginCalled = true
  71. if !auth.validLogin {
  72. return nil, errTest
  73. }
  74. return nil, nil
  75. }
  76. func (auth *mockAuth) Users(logins []string) (
  77. []*models.ExternalUserInfo,
  78. error,
  79. ) {
  80. return nil, nil
  81. }
  82. func (auth *mockAuth) User(login string) (
  83. *models.ExternalUserInfo,
  84. ldap.ServerConfig,
  85. error,
  86. ) {
  87. return nil, ldap.ServerConfig{}, nil
  88. }
  89. func (auth *mockAuth) Add(dn string, values map[string][]string) error {
  90. return nil
  91. }
  92. func (auth *mockAuth) Remove(dn string) error {
  93. return nil
  94. }
  95. func mockLDAPAuthenticator(valid bool) *mockAuth {
  96. mock := &mockAuth{
  97. validLogin: valid,
  98. }
  99. newLDAP = func(servers []*ldap.ServerConfig) multildap.IMultiLDAP {
  100. return mock
  101. }
  102. return mock
  103. }
  104. type LDAPLoginScenarioContext struct {
  105. loginUserQuery *models.LoginUserQuery
  106. LDAPAuthenticatorMock *mockAuth
  107. }
  108. type LDAPLoginScenarioFunc func(c *LDAPLoginScenarioContext)
  109. func LDAPLoginScenario(desc string, fn LDAPLoginScenarioFunc) {
  110. Convey(desc, func() {
  111. mock := &mockAuth{}
  112. sc := &LDAPLoginScenarioContext{
  113. loginUserQuery: &models.LoginUserQuery{
  114. Username: "user",
  115. Password: "pwd",
  116. IpAddress: "192.168.1.1:56433",
  117. },
  118. LDAPAuthenticatorMock: mock,
  119. }
  120. getLDAPConfig = func() (*ldap.Config, error) {
  121. config := &ldap.Config{
  122. Servers: []*ldap.ServerConfig{
  123. {
  124. Host: "",
  125. },
  126. },
  127. }
  128. return config, nil
  129. }
  130. newLDAP = func(server []*ldap.ServerConfig) multildap.IMultiLDAP {
  131. return mock
  132. }
  133. defer func() {
  134. newLDAP = multildap.New
  135. getLDAPConfig = multildap.GetConfig
  136. }()
  137. fn(sc)
  138. })
  139. }
  140. func (sc *LDAPLoginScenarioContext) withLoginResult(valid bool) {
  141. sc.LDAPAuthenticatorMock = mockLDAPAuthenticator(valid)
  142. }