2021-04-14 09:31:27 -05:00
|
|
|
package accesscontrol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPredefinedRoles(t *testing.T) {
|
2021-07-30 06:58:49 -05:00
|
|
|
for name, r := range FixedRoles {
|
2021-04-14 09:31:27 -05:00
|
|
|
assert.Truef(t,
|
2021-05-25 08:36:01 -05:00
|
|
|
strings.HasPrefix(name, "fixed:"),
|
|
|
|
"expected all fixed roles to be prefixed by 'fixed:', found role '%s'", name,
|
2021-04-14 09:31:27 -05:00
|
|
|
)
|
2021-07-30 06:58:49 -05:00
|
|
|
assert.Equal(t, name, r.Name)
|
|
|
|
assert.NotZero(t, r.Version)
|
|
|
|
// assert.NotEmpty(t, r.Description)
|
2021-04-14 09:31:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPredefinedRoleGrants(t *testing.T) {
|
2021-07-30 06:58:49 -05:00
|
|
|
for _, v := range FixedRoleGrants {
|
2021-04-14 09:31:27 -05:00
|
|
|
assert.True(t,
|
2021-07-30 06:58:49 -05:00
|
|
|
sort.SliceIsSorted(v, func(i, j int) bool {
|
|
|
|
return v[i] < v[j]
|
2021-04-14 09:31:27 -05:00
|
|
|
}),
|
|
|
|
"require role grant lists to be sorted",
|
|
|
|
)
|
2021-07-30 06:58:49 -05:00
|
|
|
for _, r := range v {
|
2021-05-25 08:36:01 -05:00
|
|
|
assert.Contains(t, FixedRoles, r)
|
2021-04-14 09:31:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-23 08:44:42 -05:00
|
|
|
|
2021-04-27 11:22:18 -05:00
|
|
|
func TestConcatPermissions(t *testing.T) {
|
2021-04-23 08:44:42 -05:00
|
|
|
perms1 := []Permission{
|
|
|
|
{
|
|
|
|
Action: "test",
|
|
|
|
Scope: "test:*",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Action: "test1",
|
|
|
|
Scope: "test1:*",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
perms2 := []Permission{
|
|
|
|
{
|
|
|
|
Action: "test1",
|
|
|
|
Scope: "*",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []Permission{
|
|
|
|
{
|
|
|
|
Action: "test",
|
|
|
|
Scope: "test:*",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Action: "test1",
|
|
|
|
Scope: "test1:*",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Action: "test1",
|
|
|
|
Scope: "*",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-04-27 11:22:18 -05:00
|
|
|
perms := ConcatPermissions(perms1, perms2)
|
2021-04-23 08:44:42 -05:00
|
|
|
assert.ElementsMatch(t, perms, expected)
|
|
|
|
}
|