Files
grafana/pkg/services/ldap/ldap_helpers_test.go
T

142 lines
2.9 KiB
Go
Raw Normal View History

2019-05-17 14:57:26 +03:00
package ldap
import (
2021-09-14 10:49:37 +02:00
"fmt"
2019-05-17 14:57:26 +03:00
"testing"
2021-09-14 10:49:37 +02:00
"github.com/stretchr/testify/assert"
2019-05-17 14:57:26 +03:00
"gopkg.in/ldap.v3"
)
2021-09-14 10:49:37 +02:00
func TestIsMemberOf(t *testing.T) {
tests := []struct {
memberOf []string
group string
expected bool
}{
{memberOf: []string{}, group: "*", expected: true},
{memberOf: []string{"one", "Two", "three"}, group: "two", expected: true},
{memberOf: []string{"one", "Two", "three"}, group: "twos", expected: false},
}
for _, tc := range tests {
t.Run(fmt.Sprintf("isMemberOf(%v, \"%s\") = %v", tc.memberOf, tc.group, tc.expected), func(t *testing.T) {
assert.Equal(t, tc.expected, IsMemberOf(tc.memberOf, tc.group))
2019-06-13 17:47:52 +03:00
})
2021-09-14 10:49:37 +02:00
}
}
2019-06-13 17:47:52 +03:00
2021-09-14 10:49:37 +02:00
func TestGetUsersIteration(t *testing.T) {
const pageSize = UsersMaxRequest
iterations := map[int]int{
0: 0,
400: 1,
600: 2,
1500: 3,
}
2019-06-13 17:47:52 +03:00
2021-09-14 10:49:37 +02:00
for userCount, expectedIterations := range iterations {
t.Run(fmt.Sprintf("getUserIteration iterates %d times for %d users", expectedIterations, userCount), func(t *testing.T) {
logins := make([]string, userCount)
2019-06-13 17:47:52 +03:00
2019-07-03 17:39:54 +03:00
i := 0
2021-09-14 10:49:37 +02:00
_ = getUsersIteration(logins, func(first int, last int) error {
assert.Equal(t, pageSize*i, first)
2019-07-03 17:39:54 +03:00
2021-09-14 10:49:37 +02:00
expectedLast := pageSize*i + pageSize
if expectedLast > userCount {
expectedLast = userCount
2019-07-03 17:39:54 +03:00
}
2021-09-14 10:49:37 +02:00
assert.Equal(t, expectedLast, last)
2019-07-03 17:39:54 +03:00
i++
return nil
})
2021-09-14 10:49:37 +02:00
assert.Equal(t, expectedIterations, i)
2019-07-03 17:39:54 +03:00
})
2021-09-14 10:49:37 +02:00
}
}
2019-07-03 17:39:54 +03:00
2021-09-14 10:49:37 +02:00
func TestGetAttribute(t *testing.T) {
t.Run("DN", func(t *testing.T) {
entry := &ldap.Entry{
DN: "test",
}
2019-07-03 17:39:54 +03:00
2021-09-14 10:49:37 +02:00
result := getAttribute("dn", entry)
assert.Equal(t, "test", result)
2019-07-03 17:39:54 +03:00
})
2021-09-14 10:49:37 +02:00
t.Run("username", func(t *testing.T) {
value := "roelgerrits"
entry := &ldap.Entry{
Attributes: []*ldap.EntryAttribute{
{
Name: "username", Values: []string{value},
2019-05-17 14:57:26 +03:00
},
2021-09-14 10:49:37 +02:00
},
}
2019-05-17 14:57:26 +03:00
2021-09-14 10:49:37 +02:00
result := getAttribute("username", entry)
assert.Equal(t, value, result)
})
2019-05-17 14:57:26 +03:00
2021-09-14 10:49:37 +02:00
t.Run("no result", func(t *testing.T) {
value := []string{"roelgerrits"}
entry := &ldap.Entry{
Attributes: []*ldap.EntryAttribute{
{
Name: "killa", Values: value,
2019-05-17 14:57:26 +03:00
},
2021-09-14 10:49:37 +02:00
},
}
2019-05-17 14:57:26 +03:00
2021-09-14 10:49:37 +02:00
result := getAttribute("username", entry)
assert.Empty(t, result)
2019-05-17 14:57:26 +03:00
})
2021-09-14 10:49:37 +02:00
}
2019-05-17 14:57:26 +03:00
2021-09-14 10:49:37 +02:00
func TestGetArrayAttribute(t *testing.T) {
t.Run("DN", func(t *testing.T) {
entry := &ldap.Entry{
DN: "test",
}
2019-07-24 12:49:18 +03:00
2021-09-14 10:49:37 +02:00
result := getArrayAttribute("dn", entry)
2019-07-24 12:49:18 +03:00
2021-09-14 10:49:37 +02:00
assert.EqualValues(t, []string{"test"}, result)
})
2019-07-24 12:49:18 +03:00
2021-09-14 10:49:37 +02:00
t.Run("username", func(t *testing.T) {
value := []string{"roelgerrits"}
entry := &ldap.Entry{
Attributes: []*ldap.EntryAttribute{
{
Name: "username", Values: value,
2019-05-17 14:57:26 +03:00
},
2021-09-14 10:49:37 +02:00
},
}
2019-06-13 17:47:52 +03:00
2021-09-14 10:49:37 +02:00
result := getArrayAttribute("username", entry)
2019-06-13 17:47:52 +03:00
2021-09-14 10:49:37 +02:00
assert.EqualValues(t, value, result)
})
2019-05-17 14:57:26 +03:00
2021-09-14 10:49:37 +02:00
t.Run("no result", func(t *testing.T) {
value := []string{"roelgerrits"}
entry := &ldap.Entry{
Attributes: []*ldap.EntryAttribute{
{
Name: "username", Values: value,
2019-05-17 14:57:26 +03:00
},
2021-09-14 10:49:37 +02:00
},
}
2019-05-17 14:57:26 +03:00
2021-09-14 10:49:37 +02:00
result := getArrayAttribute("something", entry)
2019-06-13 17:47:52 +03:00
2021-09-14 10:49:37 +02:00
assert.Empty(t, result)
2019-05-17 14:57:26 +03:00
})
}