milvus/pkg/util/funcutil/policy_test.go
jaime c9d0c157ec
Move some modules from internal to public package (#22572)
Signed-off-by: jaime <yun.zhang@zilliz.com>
2023-04-06 19:14:32 +08:00

71 lines
1.8 KiB
Go

package funcutil
import (
"testing"
"github.com/milvus-io/milvus-proto/go-api/commonpb"
"github.com/milvus-io/milvus-proto/go-api/milvuspb"
"github.com/stretchr/testify/assert"
)
func Test_GetPrivilegeExtObj(t *testing.T) {
request := &milvuspb.LoadCollectionRequest{
DbName: "test",
CollectionName: "col1",
}
privilegeExt, err := GetPrivilegeExtObj(request)
assert.Nil(t, err)
assert.Equal(t, commonpb.ObjectType_Collection, privilegeExt.ObjectType)
assert.Equal(t, commonpb.ObjectPrivilege_PrivilegeLoad, privilegeExt.ObjectPrivilege)
assert.Equal(t, int32(3), privilegeExt.ObjectNameIndex)
request2 := &milvuspb.CreatePartitionRequest{}
_, err = GetPrivilegeExtObj(request2)
assert.NotNil(t, err)
}
func Test_GetResourceName(t *testing.T) {
{
request := &milvuspb.HasCollectionRequest{
DbName: "test",
CollectionName: "col1",
}
assert.Equal(t, "*", GetObjectName(request, 0))
assert.Equal(t, "col1", GetObjectName(request, 3))
}
{
request := &milvuspb.SelectUserRequest{
User: &milvuspb.UserEntity{Name: "test"},
}
assert.Equal(t, "test", GetObjectName(request, 2))
request = &milvuspb.SelectUserRequest{}
assert.Equal(t, "*", GetObjectName(request, 2))
}
}
func Test_GetResourceNames(t *testing.T) {
request := &milvuspb.FlushRequest{
DbName: "test",
CollectionNames: []string{"col1", "col2"},
}
assert.Equal(t, 0, len(GetObjectNames(request, 0)))
assert.Equal(t, 0, len(GetObjectNames(request, 2)))
names := GetObjectNames(request, 3)
assert.Equal(t, 2, len(names))
}
func Test_PolicyForPrivilege(t *testing.T) {
assert.Equal(t,
`{"PType":"p","V0":"admin","V1":"COLLECTION-col1","V2":"ALL"}`,
PolicyForPrivilege("admin", "COLLECTION", "col1", "ALL"))
}
func Test_PolicyForResource(t *testing.T) {
assert.Equal(t,
`COLLECTION-col1`,
PolicyForResource("COLLECTION", "col1"))
}