ldap_login_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package login
  2. import (
  3. "errors"
  4. "testing"
  5. . "github.com/smartystreets/goconvey/convey"
  6. m "github.com/grafana/grafana/pkg/models"
  7. LDAP "github.com/grafana/grafana/pkg/services/ldap"
  8. "github.com/grafana/grafana/pkg/setting"
  9. )
  10. var errTest = errors.New("Test error")
  11. func TestLdapLogin(t *testing.T) {
  12. Convey("Login using ldap", t, func() {
  13. Convey("Given ldap enabled and no server configured", func() {
  14. setting.LdapEnabled = true
  15. ldapLoginScenario("When login", func(sc *ldapLoginScenarioContext) {
  16. sc.withLoginResult(false)
  17. readLDAPConfig = func() *LDAP.Config {
  18. config := &LDAP.Config{
  19. Servers: []*LDAP.ServerConfig{},
  20. }
  21. return config
  22. }
  23. enabled, err := loginUsingLdap(sc.loginUserQuery)
  24. Convey("it should return true", func() {
  25. So(enabled, ShouldBeTrue)
  26. })
  27. Convey("it should return no LDAP servers error", func() {
  28. So(err, ShouldEqual, ErrNoLDAPServers)
  29. })
  30. Convey("it should not call ldap login", func() {
  31. So(sc.ldapAuthenticatorMock.loginCalled, ShouldBeFalse)
  32. })
  33. })
  34. })
  35. Convey("Given ldap disabled", func() {
  36. setting.LdapEnabled = false
  37. ldapLoginScenario("When login", func(sc *ldapLoginScenarioContext) {
  38. sc.withLoginResult(false)
  39. enabled, err := loginUsingLdap(&m.LoginUserQuery{
  40. Username: "user",
  41. Password: "pwd",
  42. })
  43. Convey("it should return false", func() {
  44. So(enabled, ShouldBeFalse)
  45. })
  46. Convey("it should not return error", func() {
  47. So(err, ShouldBeNil)
  48. })
  49. Convey("it should not call ldap login", func() {
  50. So(sc.ldapAuthenticatorMock.loginCalled, ShouldBeFalse)
  51. })
  52. })
  53. })
  54. })
  55. }
  56. func mockLdapAuthenticator(valid bool) *mockAuth {
  57. mock := &mockAuth{
  58. validLogin: valid,
  59. }
  60. newLDAP = func(server *LDAP.ServerConfig) LDAP.IAuth {
  61. return mock
  62. }
  63. return mock
  64. }
  65. type mockAuth struct {
  66. validLogin bool
  67. loginCalled bool
  68. }
  69. func (auth *mockAuth) Login(query *m.LoginUserQuery) error {
  70. auth.loginCalled = true
  71. if !auth.validLogin {
  72. return errTest
  73. }
  74. return nil
  75. }
  76. func (auth *mockAuth) Users() ([]*LDAP.UserInfo, error) {
  77. return nil, nil
  78. }
  79. func (auth *mockAuth) SyncUser(query *m.LoginUserQuery) error {
  80. return nil
  81. }
  82. func (auth *mockAuth) GetGrafanaUserFor(ctx *m.ReqContext, ldapUser *LDAP.UserInfo) (*m.User, error) {
  83. return nil, nil
  84. }
  85. type ldapLoginScenarioContext struct {
  86. loginUserQuery *m.LoginUserQuery
  87. ldapAuthenticatorMock *mockAuth
  88. }
  89. type ldapLoginScenarioFunc func(c *ldapLoginScenarioContext)
  90. func ldapLoginScenario(desc string, fn ldapLoginScenarioFunc) {
  91. Convey(desc, func() {
  92. mock := &mockAuth{}
  93. sc := &ldapLoginScenarioContext{
  94. loginUserQuery: &m.LoginUserQuery{
  95. Username: "user",
  96. Password: "pwd",
  97. IpAddress: "192.168.1.1:56433",
  98. },
  99. ldapAuthenticatorMock: mock,
  100. }
  101. readLDAPConfig = func() *LDAP.Config {
  102. config := &LDAP.Config{
  103. Servers: []*LDAP.ServerConfig{
  104. {
  105. Host: "",
  106. },
  107. },
  108. }
  109. return config
  110. }
  111. newLDAP = func(server *LDAP.ServerConfig) LDAP.IAuth {
  112. return mock
  113. }
  114. defer func() {
  115. newLDAP = LDAP.New
  116. readLDAPConfig = LDAP.ReadConfig
  117. }()
  118. fn(sc)
  119. })
  120. }
  121. func (sc *ldapLoginScenarioContext) withLoginResult(valid bool) {
  122. sc.ldapAuthenticatorMock = mockLdapAuthenticator(valid)
  123. }