ldap_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package ldap
  2. import (
  3. "errors"
  4. "testing"
  5. "github.com/grafana/grafana/pkg/infra/log"
  6. . "github.com/smartystreets/goconvey/convey"
  7. "gopkg.in/ldap.v3"
  8. )
  9. func TestPublicAPI(t *testing.T) {
  10. Convey("New()", t, func() {
  11. Convey("Should return ", func() {
  12. result := New(&ServerConfig{
  13. Attr: AttributeMap{},
  14. SearchBaseDNs: []string{"BaseDNHere"},
  15. })
  16. So(result, ShouldImplement, (*IServer)(nil))
  17. })
  18. })
  19. Convey("Close()", t, func() {
  20. Convey("Should close the connection", func() {
  21. connection := &MockConnection{}
  22. server := &Server{
  23. Config: &ServerConfig{
  24. Attr: AttributeMap{},
  25. SearchBaseDNs: []string{"BaseDNHere"},
  26. },
  27. Connection: connection,
  28. }
  29. So(server.Close, ShouldNotPanic)
  30. So(connection.CloseCalled, ShouldBeTrue)
  31. })
  32. Convey("Should panic if no connection is established", func() {
  33. server := &Server{
  34. Config: &ServerConfig{
  35. Attr: AttributeMap{},
  36. SearchBaseDNs: []string{"BaseDNHere"},
  37. },
  38. Connection: nil,
  39. }
  40. So(server.Close, ShouldPanic)
  41. })
  42. })
  43. Convey("Users()", t, func() {
  44. Convey("Finds one user", func() {
  45. MockConnection := &MockConnection{}
  46. entry := ldap.Entry{
  47. DN: "dn", Attributes: []*ldap.EntryAttribute{
  48. {Name: "username", Values: []string{"roelgerrits"}},
  49. {Name: "surname", Values: []string{"Gerrits"}},
  50. {Name: "email", Values: []string{"roel@test.com"}},
  51. {Name: "name", Values: []string{"Roel"}},
  52. {Name: "memberof", Values: []string{"admins"}},
  53. }}
  54. result := ldap.SearchResult{Entries: []*ldap.Entry{&entry}}
  55. MockConnection.setSearchResult(&result)
  56. // Set up attribute map without surname and email
  57. server := &Server{
  58. Config: &ServerConfig{
  59. Attr: AttributeMap{
  60. Username: "username",
  61. Name: "name",
  62. MemberOf: "memberof",
  63. },
  64. SearchBaseDNs: []string{"BaseDNHere"},
  65. },
  66. Connection: MockConnection,
  67. log: log.New("test-logger"),
  68. }
  69. searchResult, err := server.Users([]string{"roelgerrits"})
  70. So(err, ShouldBeNil)
  71. So(searchResult, ShouldNotBeNil)
  72. // User should be searched in ldap
  73. So(MockConnection.SearchCalled, ShouldBeTrue)
  74. // No empty attributes should be added to the search request
  75. So(len(MockConnection.SearchAttributes), ShouldEqual, 3)
  76. })
  77. Convey("Handles a error", func() {
  78. expected := errors.New("Killa-gorilla")
  79. MockConnection := &MockConnection{}
  80. MockConnection.setSearchError(expected)
  81. // Set up attribute map without surname and email
  82. server := &Server{
  83. Config: &ServerConfig{
  84. SearchBaseDNs: []string{"BaseDNHere"},
  85. },
  86. Connection: MockConnection,
  87. log: log.New("test-logger"),
  88. }
  89. _, err := server.Users([]string{"roelgerrits"})
  90. So(err, ShouldEqual, expected)
  91. })
  92. Convey("Should return empty slice if none were found", func() {
  93. MockConnection := &MockConnection{}
  94. result := ldap.SearchResult{Entries: []*ldap.Entry{}}
  95. MockConnection.setSearchResult(&result)
  96. // Set up attribute map without surname and email
  97. server := &Server{
  98. Config: &ServerConfig{
  99. SearchBaseDNs: []string{"BaseDNHere"},
  100. },
  101. Connection: MockConnection,
  102. log: log.New("test-logger"),
  103. }
  104. searchResult, err := server.Users([]string{"roelgerrits"})
  105. So(err, ShouldBeNil)
  106. So(searchResult, ShouldBeEmpty)
  107. })
  108. })
  109. Convey("UserBind()", t, func() {
  110. Convey("Should use provided DN and password", func() {
  111. connection := &MockConnection{}
  112. var actualUsername, actualPassword string
  113. connection.BindProvider = func(username, password string) error {
  114. actualUsername = username
  115. actualPassword = password
  116. return nil
  117. }
  118. server := &Server{
  119. Connection: connection,
  120. Config: &ServerConfig{
  121. BindDN: "cn=admin,dc=grafana,dc=org",
  122. },
  123. }
  124. dn := "cn=user,ou=users,dc=grafana,dc=org"
  125. err := server.UserBind(dn, "pwd")
  126. So(err, ShouldBeNil)
  127. So(actualUsername, ShouldEqual, dn)
  128. So(actualPassword, ShouldEqual, "pwd")
  129. })
  130. Convey("Should handle an error", func() {
  131. connection := &MockConnection{}
  132. expected := &ldap.Error{
  133. ResultCode: uint16(25),
  134. }
  135. connection.BindProvider = func(username, password string) error {
  136. return expected
  137. }
  138. server := &Server{
  139. Connection: connection,
  140. Config: &ServerConfig{
  141. BindDN: "cn=%s,ou=users,dc=grafana,dc=org",
  142. },
  143. log: log.New("test-logger"),
  144. }
  145. err := server.UserBind("user", "pwd")
  146. So(err, ShouldEqual, expected)
  147. })
  148. })
  149. Convey("AdminBind()", t, func() {
  150. Convey("Should use admin DN and password", func() {
  151. connection := &MockConnection{}
  152. var actualUsername, actualPassword string
  153. connection.BindProvider = func(username, password string) error {
  154. actualUsername = username
  155. actualPassword = password
  156. return nil
  157. }
  158. dn := "cn=admin,dc=grafana,dc=org"
  159. server := &Server{
  160. Connection: connection,
  161. Config: &ServerConfig{
  162. BindPassword: "pwd",
  163. BindDN: dn,
  164. },
  165. }
  166. err := server.AdminBind()
  167. So(err, ShouldBeNil)
  168. So(actualUsername, ShouldEqual, dn)
  169. So(actualPassword, ShouldEqual, "pwd")
  170. })
  171. Convey("Should handle an error", func() {
  172. connection := &MockConnection{}
  173. expected := &ldap.Error{
  174. ResultCode: uint16(25),
  175. }
  176. connection.BindProvider = func(username, password string) error {
  177. return expected
  178. }
  179. dn := "cn=admin,dc=grafana,dc=org"
  180. server := &Server{
  181. Connection: connection,
  182. Config: &ServerConfig{
  183. BindPassword: "pwd",
  184. BindDN: dn,
  185. },
  186. log: log.New("test-logger"),
  187. }
  188. err := server.AdminBind()
  189. So(err, ShouldEqual, expected)
  190. })
  191. })
  192. }