Files
upstream-corteza/server/pkg/rbac/resource_test.go
upstream-mirror 7e385fc126
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
2026-09-18 01:22:23 +05:00

142 lines
2.7 KiB
Go

package rbac
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestResourceType(t *testing.T) {
var (
tcc = []struct {
in string
exp string
}{
{"a:b/c/d", "a:b"},
{"a:b/c", "a:b"},
{"a:b/", "a:b"},
{"a:b", "a:b"},
{"a/", "a"},
{"a", "a"},
{"", ""},
}
)
for _, tc := range tcc {
t.Run(tc.in, func(t *testing.T) {
require.Equal(t, tc.exp, ResourceType(tc.in))
})
}
}
func TestResourceComponent(t *testing.T) {
var (
tcc = []struct {
in string
exp string
}{
{"ns::cmp:r/1/2/3", "ns::cmp"},
{"ns::cmp:r", "ns::cmp"},
{"ns::cmp/", "ns::cmp"},
{"ns::cmp", "ns::cmp"},
{"cmp", "cmp"},
}
)
for _, tc := range tcc {
t.Run(tc.in, func(t *testing.T) {
require.Equal(t, tc.exp, ResourceComponent(tc.in))
})
}
}
func TestResourceMatch(t *testing.T) {
var (
tcc = []struct {
m string
r string
e bool
}{
{"::corteza:test/a/b/c", "::corteza:test/a/b/c", true},
{"::corteza:test/a/b/*", "::corteza:test/a/b/c", true},
{"::corteza:test/a/*/*", "::corteza:test/a/b/c", true},
{"::corteza:test/*/*/*", "::corteza:test/a/b/c", true},
{"::corteza:test/a/*/*", "::corteza:test/1/2/3", false},
}
)
for _, tc := range tcc {
t.Run(tc.m, func(t *testing.T) {
require.Equal(t, tc.e, matchResource(tc.m, tc.r))
})
}
}
func TestLevel(t *testing.T) {
var (
tcc = []struct {
r string
l int
}{
{"corteza::test/a/b/c", 111},
{"corteza::test/a/b/*", 11},
{"corteza::test/a/*/*", 1},
{"corteza::test/*/*/*", 0},
{"corteza::test/a/*/123", 101},
}
)
for _, tc := range tcc {
t.Run(tc.r, func(t *testing.T) {
require.Equal(t, tc.l, level(tc.r))
})
}
}
func TestIsSpecific(t *testing.T) {
var (
tcc = []struct {
r string
e bool
}{
{"corteza::test/a/b/c", true},
{"corteza::test/a/b/*", true},
{"corteza::test/a/*/*", true},
{"corteza::test/a/*/123", true},
{"corteza::test/*/*/*", false},
{"corteza::test/*/*", false},
{"corteza::test/*", false},
{"corteza::test/", false},
}
)
for _, tc := range tcc {
t.Run(tc.r, func(t *testing.T) {
require.Equal(t, tc.e, isSpecific(tc.r))
})
}
}
func TestParseResourceID(t *testing.T) {
var (
tcc = []struct {
in string
outType string
outIDs []uint64
}{
{"corteza::test/*/*/*", "corteza::test", []uint64{0, 0, 0}},
{"corteza::test/234/*/123", "corteza::test", []uint64{234, 0, 123}},
{"corteza::test/3/2/1", "corteza::test", []uint64{3, 2, 1}},
}
)
for _, tc := range tcc {
t.Run(tc.in, func(t *testing.T) {
outType, outIDs := ParseResourceID(tc.in)
require.Equal(t, tc.outType, outType)
require.Equal(t, tc.outIDs, outIDs)
})
}
}