/
ldap_test.go
63 lines (59 loc) 路 1.58 KB
/
ldap_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
package lib
import (
"encoding/base64"
"fmt"
"net/http"
"strings"
"testing"
)
func TestCheckLdap(t *testing.T) {
// test 1
// disabled anonymous access
opts := LDAPOptions{
ServerAddress: "localhost",
ServerPort: "10389",
BindDN: "uid=admin,ou=system",
BindPassword: "secret",
UserSearchDN: "o=Company",
UserSearchFilter: DefaultUserSearchFilter,
UserAttribute: DefaultUserAttribute,
GroupSearchDN: "o=Company",
GroupSearchFilter: DefaultGroupSearchFilter,
GroupMemberAttribute: DefaultGroupMemberAttribute,
GroupNameAttribute: DefaultGroupNameAttribute,
SkipTLSVerification: true,
StartTLS: true,
}
s := Server{LDAP: opts}
resp, status := s.checkLDAP(base64.StdEncoding.EncodeToString([]byte("nahid:12345")))
if status != http.StatusOK {
t.Error(resp.Status.Error)
}
var (
testFnd, adminFnd bool
)
for _, g := range resp.Status.User.Groups {
if g == "test" {
testFnd = true
}
if g == "guard-test" {
adminFnd = true
}
}
if !testFnd || !adminFnd {
t.Errorf(`expected: group list ["test","guard-test"], got %s`, strings.Join(resp.Status.User.Groups, ","))
}
fmt.Print(resp.Status)
}
func TestParseEncodedToken(t *testing.T) {
user, pass, ok := parseEncodedToken(base64.StdEncoding.EncodeToString([]byte("user1:12345")))
if !ok {
t.Error("Expected: parsing successfull, got parsing unsuccessfull")
}
if user != "user1" {
t.Error("Expected: user: user1, got user:", user)
}
if pass != "12345" {
t.Error("Expected: password: 12345, got password:", pass)
}
}