-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathldapusers_test.go
202 lines (187 loc) · 5.52 KB
/
ldapusers_test.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package doorman
import (
"errors"
"fmt"
"testing"
"github.com/caddyserver/caddy/v2"
"github.com/go-ldap/ldap"
"go.uber.org/zap"
)
type dummyconnection struct {
result *ldap.SearchResult
err error
}
func (dc *dummyconnection) Search(searchRequest *ldap.SearchRequest) (*ldap.SearchResult, error) {
return dc.result, dc.err
}
func (dc *dummyconnection) Bind(username, password string) error {
return nil
}
func (dc *dummyconnection) Close() {}
func createInit(res *ldap.SearchResult, err error) configInitialize {
return func(_ *zap.Logger, cfg *ldapConfiguration) (ldapsearcher, error) {
return &dummyconnection{result: res, err: err}, nil
}
}
func Test_ldapConfiguration_initWithDefaults(t *testing.T) {
ld := (&ldapConfiguration{}).init(caddy.NewReplacer())
if ld.UIDAttribute != defaultUID {
t.Errorf("UIDAttribute is %q but should be %q", ld.UIDAttribute, defaultUID)
}
if ld.MobileAttribute != defaultMobile {
t.Errorf("MobileAttribute is %q but should be %q", ld.MobileAttribute, defaultUID)
}
if ld.EMailAttribute != defaultEMail {
t.Errorf("EMailAttribute is %q but should be %q", ld.EMailAttribute, defaultUID)
}
if ld.NameAttribute != defaultName {
t.Errorf("NameAttribute is %q but should be %q", ld.NameAttribute, defaultUID)
}
}
func createLDAPSearchResult(m map[string]string, num int) *ldap.SearchResult {
if m == nil {
return nil
}
var atts []*ldap.EntryAttribute
for k, v := range m {
atts = append(atts, &ldap.EntryAttribute{
Name: k,
Values: []string{v},
})
}
var entries []*ldap.Entry
for i := 0; i < num; i++ {
entries = append(entries, &ldap.Entry{
Attributes: atts,
})
}
return &ldap.SearchResult{
Entries: entries,
}
}
func Test_search(t *testing.T) {
tests := []struct {
name string
attributes map[string]string
uid string
username string
mobile string
mail string
telephone string
uidattribute string
mobileattribute string
telephoneattribute string
nameattribute string
mailattribute string
numentries int
wantErr bool
wantNoUserErr bool
returnErr bool
}{
{
name: "search for user with all attributes except telephone",
attributes: map[string]string{defaultUID: "test", defaultEMail: "[email protected]", defaultMobile: "123123", defaultName: "test user", defaultTelephone: "0123"},
uid: "test",
mobile: "123123",
mail: "[email protected]",
username: "test user",
telephone: "",
numentries: 1,
},
{
name: "search for user with all attributes including telephone",
attributes: map[string]string{defaultUID: "test", defaultEMail: "[email protected]", defaultMobile: "123123", defaultName: "test user", defaultTelephone: "0123"},
uid: "test",
mobile: "123123",
mail: "[email protected]",
username: "test user",
telephone: "0123",
numentries: 1,
telephoneattribute: defaultTelephone,
},
{
name: "remapped user attributes",
attributes: map[string]string{"myuid": "test", "mymail": "[email protected]", "mymobile": "123123", "myname": "test user", "myphone": "0123"},
uid: "test",
mobile: "123123",
telephone: "0123",
mail: "[email protected]",
username: "test user",
uidattribute: "myuid",
mobileattribute: "mymobile",
nameattribute: "myname",
mailattribute: "mymail",
telephoneattribute: "myphone",
numentries: 1,
},
{
name: "search with empty results",
attributes: make(map[string]string),
wantNoUserErr: true,
numentries: 0,
},
{
name: "search with too many results",
attributes: make(map[string]string),
wantNoUserErr: true,
numentries: 2,
},
{
name: "search returns error",
numentries: 0,
wantErr: true,
returnErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var reserr error
if tt.returnErr {
reserr = fmt.Errorf("simple error")
}
getConnection = createInit(createLDAPSearchResult(tt.attributes, tt.numentries), reserr)
ld := (&ldapConfiguration{}).init(caddy.NewReplacer())
if tt.uidattribute != "" {
ld.UIDAttribute = tt.uidattribute
}
if tt.mobileattribute != "" {
ld.MobileAttribute = tt.mobileattribute
}
if tt.nameattribute != "" {
ld.NameAttribute = tt.nameattribute
}
if tt.mailattribute != "" {
ld.EMailAttribute = tt.mailattribute
}
if tt.telephoneattribute != "" {
ld.TelephoneAttribute = tt.telephoneattribute
}
ue, err := ld.Search(zap.NewNop(), tt.uid)
if err != nil {
if errors.Is(err, ErrNoUser) != tt.wantNoUserErr {
t.Errorf("got error: %v but wanted a ErrNoUser", err)
} else if !(tt.wantErr || tt.wantNoUserErr) {
t.Errorf("got error: %v", err)
}
} else {
if err == nil {
if ue.UID != tt.uid {
t.Errorf("got userid %q, but wanted: %q", ue.UID, tt.uid)
}
if ue.Name != tt.username {
t.Errorf("got username %q, but wanted: %q", ue.Name, tt.username)
}
if ue.Mobile != tt.mobile {
t.Errorf("got mobile %q, but wanted: %q", ue.Mobile, tt.mobile)
}
if ue.Telephone != tt.telephone {
t.Errorf("got telephone %q, but wanted: %q", ue.Telephone, tt.telephone)
}
if ue.EMail != tt.mail {
t.Errorf("got mail %q, but wanted: %q", ue.EMail, tt.mail)
}
}
}
})
}
}