testing.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package ldap
  2. import (
  3. "crypto/tls"
  4. "gopkg.in/ldap.v3"
  5. )
  6. // MockConnection struct for testing
  7. type MockConnection struct {
  8. SearchResult *ldap.SearchResult
  9. SearchError error
  10. SearchCalled bool
  11. SearchAttributes []string
  12. AddParams *ldap.AddRequest
  13. AddCalled bool
  14. DelParams *ldap.DelRequest
  15. DelCalled bool
  16. UnauthenticatedBindCalled bool
  17. BindCalled bool
  18. BindProvider func(username, password string) error
  19. UnauthenticatedBindProvider func() error
  20. }
  21. // Bind mocks Bind connection function
  22. func (c *MockConnection) Bind(username, password string) error {
  23. c.BindCalled = true
  24. if c.BindProvider != nil {
  25. return c.BindProvider(username, password)
  26. }
  27. return nil
  28. }
  29. // UnauthenticatedBind mocks UnauthenticatedBind connection function
  30. func (c *MockConnection) UnauthenticatedBind(username string) error {
  31. c.UnauthenticatedBindCalled = true
  32. if c.UnauthenticatedBindProvider != nil {
  33. return c.UnauthenticatedBindProvider()
  34. }
  35. return nil
  36. }
  37. // Close mocks Close connection function
  38. func (c *MockConnection) Close() {}
  39. func (c *MockConnection) setSearchResult(result *ldap.SearchResult) {
  40. c.SearchResult = result
  41. }
  42. func (c *MockConnection) setSearchError(err error) {
  43. c.SearchError = err
  44. }
  45. // Search mocks Search connection function
  46. func (c *MockConnection) Search(sr *ldap.SearchRequest) (*ldap.SearchResult, error) {
  47. c.SearchCalled = true
  48. c.SearchAttributes = sr.Attributes
  49. if c.SearchError != nil {
  50. return nil, c.SearchError
  51. }
  52. return c.SearchResult, nil
  53. }
  54. // Add mocks Add connection function
  55. func (c *MockConnection) Add(request *ldap.AddRequest) error {
  56. c.AddCalled = true
  57. c.AddParams = request
  58. return nil
  59. }
  60. // Del mocks Del connection function
  61. func (c *MockConnection) Del(request *ldap.DelRequest) error {
  62. c.DelCalled = true
  63. c.DelParams = request
  64. return nil
  65. }
  66. // StartTLS mocks StartTLS connection function
  67. func (c *MockConnection) StartTLS(*tls.Config) error {
  68. return nil
  69. }