mirror of
https://github.com/grafana/grafana.git
synced 2024-11-29 20:24:18 -06:00
9d2f5ef62f
* RBAC: Move metadata to own file * RBAC: Rename test files * RBAC: Add wildcard structure and helper function to generate wildcards from prefix * RBAC: Refactor filter to use WildcardsFromPrefix * RBAC: Refactor GetResourceMetadata to use WildcardsFromPrefix
87 lines
1.6 KiB
Go
87 lines
1.6 KiB
Go
package accesscontrol
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func Test_ScopePrefix(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
scope string
|
|
want string
|
|
}{
|
|
{
|
|
name: "empty",
|
|
scope: "",
|
|
want: "",
|
|
},
|
|
{
|
|
name: "minimal",
|
|
scope: ":",
|
|
want: ":",
|
|
},
|
|
{
|
|
name: "datasources",
|
|
scope: "datasources:",
|
|
want: "datasources:",
|
|
},
|
|
{
|
|
name: "datasources name",
|
|
scope: "datasources:name:testds",
|
|
want: "datasources:name:",
|
|
},
|
|
{
|
|
name: "datasources with colons in name",
|
|
scope: "datasources:name:test:a::ds",
|
|
want: "datasources:name:",
|
|
},
|
|
{
|
|
name: "prefix",
|
|
scope: "datasources:name:",
|
|
want: "datasources:name:",
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
prefix := ScopePrefix(tt.scope)
|
|
|
|
assert.Equal(t, tt.want, prefix)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestWildcardsFromPrefix(t *testing.T) {
|
|
type testCase struct {
|
|
desc string
|
|
prefix string
|
|
expected Wildcards
|
|
}
|
|
|
|
tests := []testCase{
|
|
{
|
|
desc: "should handle empty prefix",
|
|
prefix: "",
|
|
expected: Wildcards{"*"},
|
|
},
|
|
{
|
|
desc: "should generate wildcards for prefix",
|
|
prefix: "dashboards:uid",
|
|
expected: Wildcards{"*", "dashboards:*", "dashboards:uid:*"},
|
|
},
|
|
{
|
|
desc: "should handle trailing :",
|
|
prefix: "dashboards:uid:",
|
|
expected: Wildcards{"*", "dashboards:*", "dashboards:uid:*"},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.desc, func(t *testing.T) {
|
|
wildcards := WildcardsFromPrefix(tt.prefix)
|
|
assert.Equal(t, tt.expected, wildcards)
|
|
})
|
|
}
|
|
}
|