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