ldap_login_test.go 4.0 KB

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