ldap_login_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package login
  2. import (
  3. "errors"
  4. "testing"
  5. . "github.com/smartystreets/goconvey/convey"
  6. "github.com/grafana/grafana/pkg/models"
  7. "github.com/grafana/grafana/pkg/services/ldap"
  8. "github.com/grafana/grafana/pkg/services/multildap"
  9. "github.com/grafana/grafana/pkg/setting"
  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. }
  61. func (auth *mockAuth) Login(query *models.LoginUserQuery) (
  62. *models.ExternalUserInfo,
  63. error,
  64. ) {
  65. auth.loginCalled = true
  66. if !auth.validLogin {
  67. return nil, errTest
  68. }
  69. return nil, nil
  70. }
  71. func (auth *mockAuth) Users(logins []string) (
  72. []*models.ExternalUserInfo,
  73. error,
  74. ) {
  75. return nil, nil
  76. }
  77. func (auth *mockAuth) User(login string) (
  78. *models.ExternalUserInfo,
  79. error,
  80. ) {
  81. return nil, nil
  82. }
  83. func (auth *mockAuth) Add(dn string, values map[string][]string) error {
  84. return nil
  85. }
  86. func (auth *mockAuth) Remove(dn string) error {
  87. return nil
  88. }
  89. func mockLDAPAuthenticator(valid bool) *mockAuth {
  90. mock := &mockAuth{
  91. validLogin: valid,
  92. }
  93. newLDAP = func(servers []*ldap.ServerConfig) multildap.IMultiLDAP {
  94. return mock
  95. }
  96. return mock
  97. }
  98. type LDAPLoginScenarioContext struct {
  99. loginUserQuery *models.LoginUserQuery
  100. LDAPAuthenticatorMock *mockAuth
  101. }
  102. type LDAPLoginScenarioFunc func(c *LDAPLoginScenarioContext)
  103. func LDAPLoginScenario(desc string, fn LDAPLoginScenarioFunc) {
  104. Convey(desc, func() {
  105. mock := &mockAuth{}
  106. sc := &LDAPLoginScenarioContext{
  107. loginUserQuery: &models.LoginUserQuery{
  108. Username: "user",
  109. Password: "pwd",
  110. IpAddress: "192.168.1.1:56433",
  111. },
  112. LDAPAuthenticatorMock: mock,
  113. }
  114. getLDAPConfig = func() (*ldap.Config, error) {
  115. config := &ldap.Config{
  116. Servers: []*ldap.ServerConfig{
  117. {
  118. Host: "",
  119. },
  120. },
  121. }
  122. return config, nil
  123. }
  124. newLDAP = func(server []*ldap.ServerConfig) multildap.IMultiLDAP {
  125. return mock
  126. }
  127. defer func() {
  128. newLDAP = multildap.New
  129. getLDAPConfig = multildap.GetConfig
  130. }()
  131. fn(sc)
  132. })
  133. }
  134. func (sc *LDAPLoginScenarioContext) withLoginResult(valid bool) {
  135. sc.LDAPAuthenticatorMock = mockLDAPAuthenticator(valid)
  136. }