2021-10-19 07:40:21 -05:00
|
|
|
package accesscontrol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-05-02 02:29:30 -05:00
|
|
|
func Test_ScopePrefix(t *testing.T) {
|
2022-01-18 10:34:35 -06:00
|
|
|
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:",
|
|
|
|
},
|
2022-03-23 02:48:32 -05:00
|
|
|
{
|
|
|
|
name: "datasources with colons in name",
|
|
|
|
scope: "datasources:name:test:a::ds",
|
|
|
|
want: "datasources:name:",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prefix",
|
|
|
|
scope: "datasources:name:",
|
|
|
|
want: "datasources:name:",
|
|
|
|
},
|
2022-01-18 10:34:35 -06:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-04-05 07:28:23 -05:00
|
|
|
prefix := ScopePrefix(tt.scope)
|
2022-01-18 10:34:35 -06:00
|
|
|
|
|
|
|
assert.Equal(t, tt.want, prefix)
|
2021-10-19 07:40:21 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-08-26 10:10:35 -05:00
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|