app-cloudep-permission-server/pkg/domain/rbac/rule_test.go

123 lines
2.5 KiB
Go

package rbac
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestRule_ToString(t *testing.T) {
tests := []struct {
name string
input Rule
expected []string
}{
{
name: "完整六個欄位",
input: Rule{
PolicyType: "p",
Field0: "admin",
Field1: "data1",
Field2: "read",
Field3: "extra1",
Field4: "extra2",
Field5: "extra3",
},
expected: []string{"p", "admin", "data1", "read", "extra1", "extra2", "extra3"},
},
{
name: "部分欄位空白",
input: Rule{
PolicyType: "p",
Field0: "admin",
Field1: "",
Field2: "read",
Field3: "",
Field4: "",
Field5: "extra3",
},
expected: []string{"p", "admin", "read", "extra3"},
},
{
name: "只有 PolicyType",
input: Rule{PolicyType: "p"},
expected: []string{"p"},
},
{
name: "所有欄位皆空",
input: Rule{},
expected: []string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.input.ToString()
assert.Equal(t, tt.expected, result, "ToString output should match expected")
})
}
}
func TestStringToPolicy(t *testing.T) {
tests := []struct {
name string
policyType string
input []string
expected Rule
}{
{
name: "完整六個欄位",
policyType: "p",
input: []string{"admin", "data1", "read", "extra1", "extra2", "extra3"},
expected: Rule{
PolicyType: "p",
Field0: "admin",
Field1: "data1",
Field2: "read",
Field3: "extra1",
Field4: "extra2",
Field5: "extra3",
},
},
{
name: "部分欄位空白",
policyType: "p",
input: []string{"admin", "", "read", "", "", "extra3"},
expected: Rule{
PolicyType: "p",
Field0: "admin",
Field1: "",
Field2: "read",
Field3: "",
Field4: "",
Field5: "extra3",
},
},
{
name: "只有 PolicyType",
policyType: "p",
input: []string{},
expected: Rule{
PolicyType: "p",
},
},
{
name: "只有兩個欄位",
policyType: "g",
input: []string{"user", "admin"},
expected: Rule{
PolicyType: "g",
Field0: "user",
Field1: "admin",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := StringToPolicy(tt.policyType, tt.input)
assert.Equal(t, tt.expected, result, "StringToPolicy output should match expected")
})
}
}