2019-05-17 19:57:26 +08:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
"gopkg.in/ldap.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLDAPHelpers(t *testing.T) {
|
2019-06-13 22:47:52 +08:00
|
|
|
Convey("isMemberOf()", t, func() {
|
|
|
|
Convey("Wildcard", func() {
|
|
|
|
result := isMemberOf([]string{}, "*")
|
|
|
|
So(result, ShouldBeTrue)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Should find one", func() {
|
|
|
|
result := isMemberOf([]string{"one", "Two", "three"}, "two")
|
|
|
|
So(result, ShouldBeTrue)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Should not find one", func() {
|
|
|
|
result := isMemberOf([]string{"one", "Two", "three"}, "twos")
|
|
|
|
So(result, ShouldBeFalse)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("getAttribute()", t, func() {
|
|
|
|
Convey("Should get username", func() {
|
|
|
|
value := []string{"roelgerrits"}
|
|
|
|
entry := &ldap.Entry{
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
|
|
|
{
|
|
|
|
Name: "username", Values: value,
|
2019-05-17 19:57:26 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-06-13 22:47:52 +08:00
|
|
|
result := getAttribute("username", entry)
|
|
|
|
|
|
|
|
So(result, ShouldEqual, value[0])
|
2019-05-17 19:57:26 +08:00
|
|
|
})
|
|
|
|
|
2019-06-13 22:47:52 +08:00
|
|
|
Convey("Should not get anything", func() {
|
|
|
|
value := []string{"roelgerrits"}
|
|
|
|
entry := &ldap.Entry{
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
|
|
|
{
|
|
|
|
Name: "killa", Values: value,
|
2019-05-17 19:57:26 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-06-13 22:47:52 +08:00
|
|
|
result := getAttribute("username", entry)
|
2019-05-17 19:57:26 +08:00
|
|
|
|
2019-06-13 22:47:52 +08:00
|
|
|
So(result, ShouldEqual, "")
|
2019-05-17 19:57:26 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-06-13 22:47:52 +08:00
|
|
|
Convey("getArrayAttribute()", t, func() {
|
|
|
|
Convey("Should get username", func() {
|
|
|
|
value := []string{"roelgerrits"}
|
|
|
|
entry := &ldap.Entry{
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
|
|
|
{
|
|
|
|
Name: "username", Values: value,
|
|
|
|
},
|
2019-05-17 19:57:26 +08:00
|
|
|
},
|
|
|
|
}
|
2019-06-13 22:47:52 +08:00
|
|
|
|
|
|
|
result := getArrayAttribute("username", entry)
|
|
|
|
|
|
|
|
So(result, ShouldResemble, value)
|
2019-05-17 19:57:26 +08:00
|
|
|
})
|
|
|
|
|
2019-06-13 22:47:52 +08:00
|
|
|
Convey("Should not get anything", func() {
|
|
|
|
value := []string{"roelgerrits"}
|
|
|
|
entry := &ldap.Entry{
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
|
|
|
{
|
|
|
|
Name: "username", Values: value,
|
|
|
|
},
|
2019-05-17 19:57:26 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-06-13 22:47:52 +08:00
|
|
|
result := getArrayAttribute("something", entry)
|
|
|
|
|
|
|
|
So(result, ShouldResemble, []string{})
|
2019-05-17 19:57:26 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|