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 14:26:41 +00:00
|
|
|
mapped := AttributesToLDAP(u.Attributes, func(key string) string {
|
|
|
|
return AttributeKeySanitize(key)
|
|
|
|
}, func(value []string) []string {
|
|
|
|
return value
|
|
|
|
})
|
|
|
|
assert.Equal(t, 1, len(mapped))
|
|
|
|
assert.Equal(t, "foo", mapped[0].Name)
|
|
|
|
assert.Equal(t, []string{"bar"}, mapped[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 14:26:41 +00:00
|
|
|
mapped = AttributesToLDAP(u.Attributes, func(key string) string {
|
|
|
|
return AttributeKeySanitize(key)
|
|
|
|
}, func(value []string) []string {
|
|
|
|
return value
|
|
|
|
})
|
|
|
|
assert.Equal(t, 1, len(mapped))
|
|
|
|
assert.Equal(t, "foo", mapped[0].Name)
|
|
|
|
assert.Equal(t, []string{"bar"}, mapped[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 14:26:41 +00:00
|
|
|
mapped := AttributesToLDAP(u.Attributes, func(key string) string {
|
|
|
|
return AttributeKeySanitize(key)
|
|
|
|
}, func(value []string) []string {
|
|
|
|
return value
|
|
|
|
})
|
|
|
|
assert.Equal(t, 1, len(mapped))
|
|
|
|
assert.Equal(t, "foo", mapped[0].Name)
|
|
|
|
assert.Equal(t, []string{"bar"}, mapped[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 14:26:41 +00:00
|
|
|
mapped = AttributesToLDAP(u.Attributes, func(key string) string {
|
|
|
|
return AttributeKeySanitize(key)
|
|
|
|
}, func(value []string) []string {
|
|
|
|
return value
|
|
|
|
})
|
|
|
|
assert.Equal(t, 1, len(mapped))
|
|
|
|
assert.Equal(t, "foo", mapped[0].Name)
|
|
|
|
assert.Equal(t, []string{"bar"}, mapped[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 14:26:41 +00:00
|
|
|
mapped := AttributesToLDAP(d, func(key string) string {
|
|
|
|
return AttributeKeySanitize(key)
|
|
|
|
}, func(value []string) []string {
|
|
|
|
return value
|
|
|
|
})
|
|
|
|
assert.Equal(t, 1, len(mapped))
|
|
|
|
assert.Equal(t, "foo", mapped[0].Name)
|
|
|
|
assert.Equal(t, []string{"map[foo:bar]"}, mapped[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 14:26:41 +00:00
|
|
|
mapped := AttributesToLDAP(d, func(key string) string {
|
|
|
|
return AttributeKeySanitize(key)
|
|
|
|
}, func(value []string) []string {
|
|
|
|
return value
|
|
|
|
})
|
|
|
|
assert.Equal(t, 1, len(mapped))
|
|
|
|
assert.Equal(t, "foo", mapped[0].Name)
|
|
|
|
assert.Equal(t, []string{"foo", "6"}, mapped[0].Values)
|
2022-02-22 18:56:50 +00:00
|
|
|
}
|