ldap_login_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. }
  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. ldap.ServerConfig,
  80. error,
  81. ) {
  82. return nil, ldap.ServerConfig{}, nil
  83. }
  84. func (auth *mockAuth) Add(dn string, values map[string][]string) error {
  85. return nil
  86. }
  87. func (auth *mockAuth) Remove(dn string) error {
  88. return nil
  89. }
  90. func mockLDAPAuthenticator(valid bool) *mockAuth {
  91. mock := &mockAuth{
  92. validLogin: valid,
  93. }
  94. newLDAP = func(servers []*ldap.ServerConfig) multildap.IMultiLDAP {
  95. return mock
  96. }
  97. return mock
  98. }
  99. type LDAPLoginScenarioContext struct {
  100. loginUserQuery *models.LoginUserQuery
  101. LDAPAuthenticatorMock *mockAuth
  102. }
  103. type LDAPLoginScenarioFunc func(c *LDAPLoginScenarioContext)
  104. func LDAPLoginScenario(desc string, fn LDAPLoginScenarioFunc) {
  105. Convey(desc, func() {
  106. mock := &mockAuth{}
  107. sc := &LDAPLoginScenarioContext{
  108. loginUserQuery: &models.LoginUserQuery{
  109. Username: "user",
  110. Password: "pwd",
  111. IpAddress: "192.168.1.1:56433",
  112. },
  113. LDAPAuthenticatorMock: mock,
  114. }
  115. getLDAPConfig = func() (*ldap.Config, error) {
  116. config := &ldap.Config{
  117. Servers: []*ldap.ServerConfig{
  118. {
  119. Host: "",
  120. },
  121. },
  122. }
  123. return config, nil
  124. }
  125. newLDAP = func(server []*ldap.ServerConfig) multildap.IMultiLDAP {
  126. return mock
  127. }
  128. defer func() {
  129. newLDAP = multildap.New
  130. getLDAPConfig = multildap.GetConfig
  131. }()
  132. fn(sc)
  133. })
  134. }
  135. func (sc *LDAPLoginScenarioContext) withLoginResult(valid bool) {
  136. sc.LDAPAuthenticatorMock = mockLDAPAuthenticator(valid)
  137. }