-
Notifications
You must be signed in to change notification settings - Fork 16
/
ldap_test.go
85 lines (69 loc) · 2.91 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"github.com/jtblin/go-ldap-client"
"github.com/stretchr/testify/assert"
"testing"
)
func checkPackLDAPAttributes(t *testing.T, headers map[string]string, expected []string) {
actual := packLDAPAttributes(headers)
assert.Equal(t, expected, actual, "Arguments to headers packing failed")
}
func TestPackLDAPAttributes(t *testing.T) {
checkPackLDAPAttributes(t, map[string]string{"header": "attribute"}, []string{"attribute"})
checkPackLDAPAttributes(t, map[string]string{"header": "attribute", "another": "attribute"}, []string{"attribute", "attribute"})
checkPackLDAPAttributes(t, map[string]string{"header": ""}, []string{""})
}
func TestConfigureLDAPServer(t *testing.T) {
client := &ldap.LDAPClient{}
var err error
err = configureLDAPServer("|||", client)
assert.NotNil(t, err)
err = configureLDAPServer("foo://example.com", client)
assert.NotNil(t, err)
err = configureLDAPServer("example.com:aa", client)
assert.NotNil(t, err)
err = configureLDAPServer("example.com", client)
assert.Nil(t, err)
assert.Equal(t, "example.com", client.Host)
assert.Equal(t, 389, client.Port)
assert.Equal(t, false, client.UseSSL)
assert.Equal(t, "", client.ServerName)
assert.Equal(t, false, client.InsecureSkipVerify)
err = configureLDAPServer("example.com:123", client)
assert.Nil(t, err)
assert.Equal(t, "example.com", client.Host)
assert.Equal(t, 123, client.Port)
assert.Equal(t, false, client.UseSSL)
assert.Equal(t, "", client.ServerName)
assert.Equal(t, false, client.InsecureSkipVerify)
err = configureLDAPServer("ldap://example.com", client)
assert.Nil(t, err)
assert.Equal(t, "example.com", client.Host)
assert.Equal(t, 389, client.Port)
assert.Equal(t, false, client.UseSSL)
assert.Equal(t, "", client.ServerName)
assert.Equal(t, false, client.InsecureSkipVerify)
err = configureLDAPServer("ldaps://example.com", client)
assert.Nil(t, err)
assert.Equal(t, "example.com", client.Host)
assert.Equal(t, 636, client.Port)
assert.Equal(t, true, client.UseSSL)
assert.Equal(t, "example.com", client.ServerName)
assert.Equal(t, false, client.InsecureSkipVerify)
err = configureLDAPServer("ldaps://example.com?ServerName=alt-example.com&InsecureSkipVerify=garbage", client)
assert.NotNil(t, err)
err = configureLDAPServer("ldaps://example.com?ServerName=alt-example.com&InsecureSkipVerify=true", client)
assert.Nil(t, err)
assert.Equal(t, "example.com", client.Host)
assert.Equal(t, 636, client.Port)
assert.Equal(t, true, client.UseSSL)
assert.Equal(t, "alt-example.com", client.ServerName)
assert.Equal(t, true, client.InsecureSkipVerify)
err = configureLDAPServer("ldaps://example.com?ServerName=alt-example.com&InsecureSkipVerify=false", client)
assert.Nil(t, err)
assert.Equal(t, "example.com", client.Host)
assert.Equal(t, 636, client.Port)
assert.Equal(t, true, client.UseSSL)
assert.Equal(t, "alt-example.com", client.ServerName)
assert.Equal(t, false, client.InsecureSkipVerify)
}