ldap_login_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package ldap
  2. import (
  3. "errors"
  4. "testing"
  5. "github.com/grafana/grafana/pkg/infra/log"
  6. "github.com/grafana/grafana/pkg/models"
  7. . "github.com/smartystreets/goconvey/convey"
  8. "gopkg.in/ldap.v3"
  9. )
  10. func TestLDAPLogin(t *testing.T) {
  11. defaultLogin := &models.LoginUserQuery{
  12. Username: "user",
  13. Password: "pwd",
  14. IpAddress: "192.168.1.1:56433",
  15. }
  16. Convey("Login()", t, func() {
  17. Convey("Should get invalid credentials when auth fails", func() {
  18. connection := &MockConnection{}
  19. entry := ldap.Entry{}
  20. result := ldap.SearchResult{Entries: []*ldap.Entry{&entry}}
  21. connection.setSearchResult(&result)
  22. connection.bindProvider = func(username, password string) error {
  23. return &ldap.Error{
  24. ResultCode: 49,
  25. }
  26. }
  27. server := &Server{
  28. Config: &ServerConfig{
  29. SearchBaseDNs: []string{"BaseDNHere"},
  30. },
  31. Connection: connection,
  32. log: log.New("test-logger"),
  33. }
  34. _, err := server.Login(defaultLogin)
  35. So(err, ShouldEqual, ErrInvalidCredentials)
  36. })
  37. Convey("Returns an error when search hasn't find anything", func() {
  38. connection := &MockConnection{}
  39. result := ldap.SearchResult{Entries: []*ldap.Entry{}}
  40. connection.setSearchResult(&result)
  41. connection.bindProvider = func(username, password string) error {
  42. return nil
  43. }
  44. server := &Server{
  45. Config: &ServerConfig{
  46. SearchBaseDNs: []string{"BaseDNHere"},
  47. },
  48. Connection: connection,
  49. log: log.New("test-logger"),
  50. }
  51. _, err := server.Login(defaultLogin)
  52. So(err, ShouldEqual, ErrInvalidCredentials)
  53. })
  54. Convey("When search returns an error", func() {
  55. connection := &MockConnection{}
  56. expected := errors.New("Killa-gorilla")
  57. connection.setSearchError(expected)
  58. connection.bindProvider = func(username, password string) error {
  59. return nil
  60. }
  61. server := &Server{
  62. Config: &ServerConfig{
  63. SearchBaseDNs: []string{"BaseDNHere"},
  64. },
  65. Connection: connection,
  66. log: log.New("test-logger"),
  67. }
  68. _, err := server.Login(defaultLogin)
  69. So(err, ShouldEqual, expected)
  70. })
  71. Convey("When login with valid credentials", func() {
  72. connection := &MockConnection{}
  73. entry := ldap.Entry{
  74. DN: "dn", Attributes: []*ldap.EntryAttribute{
  75. {Name: "username", Values: []string{"markelog"}},
  76. {Name: "surname", Values: []string{"Gaidarenko"}},
  77. {Name: "email", Values: []string{"markelog@gmail.com"}},
  78. {Name: "name", Values: []string{"Oleg"}},
  79. {Name: "memberof", Values: []string{"admins"}},
  80. },
  81. }
  82. result := ldap.SearchResult{Entries: []*ldap.Entry{&entry}}
  83. connection.setSearchResult(&result)
  84. connection.bindProvider = func(username, password string) error {
  85. return nil
  86. }
  87. server := &Server{
  88. Config: &ServerConfig{
  89. Attr: AttributeMap{
  90. Username: "username",
  91. Name: "name",
  92. MemberOf: "memberof",
  93. },
  94. SearchBaseDNs: []string{"BaseDNHere"},
  95. },
  96. Connection: connection,
  97. log: log.New("test-logger"),
  98. }
  99. resp, err := server.Login(defaultLogin)
  100. So(err, ShouldBeNil)
  101. So(resp.Login, ShouldEqual, "markelog")
  102. })
  103. })
  104. }