2021-04-14 09:31:27 -05:00
|
|
|
package accesscontrol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-11-17 08:40:39 -06:00
|
|
|
func TestFixedRoles(t *testing.T) {
|
2021-08-04 07:44:37 -05:00
|
|
|
for name, role := 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-08-04 07:44:37 -05:00
|
|
|
assert.Equal(t, name, role.Name)
|
|
|
|
assert.NotZero(t, role.Version)
|
2021-04-14 09:31:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-17 08:40:39 -06:00
|
|
|
func TestFixedRoleGrants(t *testing.T) {
|
2021-08-04 07:44:37 -05:00
|
|
|
for _, grants := range FixedRoleGrants {
|
|
|
|
// Check grants list is sorted
|
2021-04-14 09:31:27 -05:00
|
|
|
assert.True(t,
|
2021-08-04 07:44:37 -05:00
|
|
|
sort.SliceIsSorted(grants, func(i, j int) bool {
|
|
|
|
return grants[i] < grants[j]
|
2021-04-14 09:31:27 -05:00
|
|
|
}),
|
|
|
|
"require role grant lists to be sorted",
|
|
|
|
)
|
2021-08-04 07:44:37 -05:00
|
|
|
|
|
|
|
// Check all granted roles have been registered
|
|
|
|
for _, r := range grants {
|
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)
|
|
|
|
}
|