2022-02-22 18:56:50 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-03-03 09:40:07 +00:00
|
|
|
"goauthentik.io/api/v3"
|
2022-02-22 18:56:50 +00:00
|
|
|
)
|
|
|
|
|
2022-12-12 18:30:52 +00:00
|
|
|
func Test_stringify_nil(t *testing.T) {
|
2022-04-11 13:52:55 +00:00
|
|
|
var ex *string
|
2022-12-12 18:30:52 +00:00
|
|
|
assert.Equal(t, ex, stringify(nil))
|
2022-04-11 13:52:55 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 18:56:50 +00:00
|
|
|
func TestAKAttrsToLDAP_String(t *testing.T) {
|
2022-05-27 14:28:26 +00:00
|
|
|
u := api.User{}
|
2022-02-22 18:56:50 +00:00
|
|
|
|
|
|
|
// normal string
|
2022-05-27 14:28:26 +00:00
|
|
|
u.Attributes = map[string]interface{}{
|
2022-02-22 18:56:50 +00:00
|
|
|
"foo": "bar",
|
|
|
|
}
|
2023-02-22 13:18:22 +00:00
|
|
|
assert.Equal(t, 1, len(AttributesToLDAP(u.Attributes, true)))
|
|
|
|
assert.Equal(t, "foo", AttributesToLDAP(u.Attributes, true)[0].Name)
|
|
|
|
assert.Equal(t, []string{"bar"}, AttributesToLDAP(u.Attributes, true)[0].Values)
|
2022-02-22 18:56:50 +00:00
|
|
|
// pointer string
|
2022-05-27 14:28:26 +00:00
|
|
|
u.Attributes = map[string]interface{}{
|
2022-02-22 18:56:50 +00:00
|
|
|
"foo": api.PtrString("bar"),
|
|
|
|
}
|
2023-02-22 13:18:22 +00:00
|
|
|
assert.Equal(t, 1, len(AttributesToLDAP(u.Attributes, true)))
|
|
|
|
assert.Equal(t, "foo", AttributesToLDAP(u.Attributes, true)[0].Name)
|
|
|
|
assert.Equal(t, []string{"bar"}, AttributesToLDAP(u.Attributes, true)[0].Values)
|
2022-02-22 18:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAKAttrsToLDAP_String_List(t *testing.T) {
|
2022-05-27 14:28:26 +00:00
|
|
|
u := api.User{}
|
2022-02-22 18:56:50 +00:00
|
|
|
// string list
|
2022-05-27 14:28:26 +00:00
|
|
|
u.Attributes = map[string]interface{}{
|
2022-02-22 18:56:50 +00:00
|
|
|
"foo": []string{"bar"},
|
|
|
|
}
|
2023-02-22 13:18:22 +00:00
|
|
|
assert.Equal(t, 1, len(AttributesToLDAP(u.Attributes, true)))
|
|
|
|
assert.Equal(t, "foo", AttributesToLDAP(u.Attributes, true)[0].Name)
|
|
|
|
assert.Equal(t, []string{"bar"}, AttributesToLDAP(u.Attributes, true)[0].Values)
|
2022-02-22 18:56:50 +00:00
|
|
|
// pointer string list
|
2022-05-27 14:28:26 +00:00
|
|
|
u.Attributes = map[string]interface{}{
|
2022-02-22 18:56:50 +00:00
|
|
|
"foo": &[]string{"bar"},
|
|
|
|
}
|
2023-02-22 13:18:22 +00:00
|
|
|
assert.Equal(t, 1, len(AttributesToLDAP(u.Attributes, true)))
|
|
|
|
assert.Equal(t, "foo", AttributesToLDAP(u.Attributes, true)[0].Name)
|
|
|
|
assert.Equal(t, []string{"bar"}, AttributesToLDAP(u.Attributes, true)[0].Values)
|
2022-02-22 18:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAKAttrsToLDAP_Dict(t *testing.T) {
|
|
|
|
// dict
|
2022-05-27 14:28:26 +00:00
|
|
|
d := map[string]interface{}{
|
2022-02-22 18:56:50 +00:00
|
|
|
"foo": map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
}
|
2023-02-22 13:18:22 +00:00
|
|
|
assert.Equal(t, 1, len(AttributesToLDAP(d, true)))
|
|
|
|
assert.Equal(t, "foo", AttributesToLDAP(d, true)[0].Name)
|
|
|
|
assert.Equal(t, []string{"map[foo:bar]"}, AttributesToLDAP(d, true)[0].Values)
|
2022-02-22 18:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAKAttrsToLDAP_Mixed(t *testing.T) {
|
|
|
|
// dict
|
2022-05-27 14:28:26 +00:00
|
|
|
d := map[string]interface{}{
|
2022-02-22 18:56:50 +00:00
|
|
|
"foo": []interface{}{
|
|
|
|
"foo",
|
|
|
|
6,
|
|
|
|
},
|
|
|
|
}
|
2023-02-22 13:18:22 +00:00
|
|
|
assert.Equal(t, 1, len(AttributesToLDAP(d, true)))
|
|
|
|
assert.Equal(t, "foo", AttributesToLDAP(d, true)[0].Name)
|
|
|
|
assert.Equal(t, []string{"foo", "6"}, AttributesToLDAP(d, true)[0].Values)
|
2022-02-22 18:56:50 +00:00
|
|
|
}
|