testing.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. CloseCalled bool
  17. UnauthenticatedBindCalled bool
  18. BindCalled bool
  19. BindProvider func(username, password string) error
  20. UnauthenticatedBindProvider func() error
  21. }
  22. // Bind mocks Bind connection function
  23. func (c *MockConnection) Bind(username, password string) error {
  24. c.BindCalled = true
  25. if c.BindProvider != nil {
  26. return c.BindProvider(username, password)
  27. }
  28. return nil
  29. }
  30. // UnauthenticatedBind mocks UnauthenticatedBind connection function
  31. func (c *MockConnection) UnauthenticatedBind(username string) error {
  32. c.UnauthenticatedBindCalled = true
  33. if c.UnauthenticatedBindProvider != nil {
  34. return c.UnauthenticatedBindProvider()
  35. }
  36. return nil
  37. }
  38. // Close mocks Close connection function
  39. func (c *MockConnection) Close() {
  40. c.CloseCalled = true
  41. }
  42. func (c *MockConnection) setSearchResult(result *ldap.SearchResult) {
  43. c.SearchResult = result
  44. }
  45. func (c *MockConnection) setSearchError(err error) {
  46. c.SearchError = err
  47. }
  48. // Search mocks Search connection function
  49. func (c *MockConnection) Search(sr *ldap.SearchRequest) (*ldap.SearchResult, error) {
  50. c.SearchCalled = true
  51. c.SearchAttributes = sr.Attributes
  52. if c.SearchError != nil {
  53. return nil, c.SearchError
  54. }
  55. return c.SearchResult, nil
  56. }
  57. // Add mocks Add connection function
  58. func (c *MockConnection) Add(request *ldap.AddRequest) error {
  59. c.AddCalled = true
  60. c.AddParams = request
  61. return nil
  62. }
  63. // Del mocks Del connection function
  64. func (c *MockConnection) Del(request *ldap.DelRequest) error {
  65. c.DelCalled = true
  66. c.DelParams = request
  67. return nil
  68. }
  69. // StartTLS mocks StartTLS connection function
  70. func (c *MockConnection) StartTLS(*tls.Config) error {
  71. return nil
  72. }