Skip to content

Commit

Permalink
feat: add ut
Browse files Browse the repository at this point in the history
  • Loading branch information
constwz committed Jul 8, 2024
1 parent 77dfae7 commit 86a744a
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions x/storage/keeper/cross_app_permission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/golang/mock/gomock"

"github.com/bnb-chain/greenfield/testutil/sample"
types2 "github.com/bnb-chain/greenfield/types"
"github.com/bnb-chain/greenfield/x/permission/types"
"github.com/bnb-chain/greenfield/x/storage/keeper"
storageTypes "github.com/bnb-chain/greenfield/x/storage/types"
Expand Down Expand Up @@ -71,3 +72,89 @@ func (s *TestSuite) TestSynDeletePolicy() {
s.Require().ErrorIs(res.Err, storageTypes.ErrNoSuchPolicy)
s.Require().NotEmpty(res.Payload)
}

func (s *TestSuite) TestSynCreatePolicyByMsgErr() {
ctrl := gomock.NewController(s.T())
storageKeeper := storageTypes.NewMockStorageKeeper(ctrl)
permissionKeeper := storageTypes.NewMockPermissionKeeper(ctrl)

//resourceIds := []math.Uint{math.NewUint(rand.Uint64()), math.NewUint(rand.Uint64()), math.NewUint(rand.Uint64())}
// policy without expiry
op := sample.RandAccAddress()
resource := types2.NewBucketGRN("test-bucket").String()
policy := storageTypes.MsgPutPolicy{
Principal: &types.Principal{
Type: types.PRINCIPAL_TYPE_GNFD_GROUP,
Value: sample.RandAccAddressHex(),
},
Operator: op.String(),
Resource: resource,
Statements: nil,
ExpirationTime: nil,
}

app := keeper.NewPermissionApp(storageKeeper, permissionKeeper)
data, err := policy.Marshal()
s.NoError(err)

synPackage := storageTypes.CreatePolicySynPackage{
Operator: op,
Data: data,
ExtraData: []byte("extra data"),
}
serializedSynPackage := synPackage.MustSerialize()
serializedSynPackage = append([]byte{storageTypes.OperationCreatePolicy}, serializedSynPackage...)

// case 1: bucket not found
storageKeeper.EXPECT().GetBucketInfo(gomock.Any(), gomock.Any()).Return(nil, false)
res := app.ExecuteSynPackage(s.ctx, &sdk.CrossChainAppContext{}, serializedSynPackage)
s.Require().ErrorIs(res.Err, storageTypes.ErrNoSuchBucket)
}

func (s *TestSuite) TestSynCreatePolicyByMsg() {
ctrl := gomock.NewController(s.T())
storageKeeper := storageTypes.NewMockStorageKeeper(ctrl)
permissionKeeper := storageTypes.NewMockPermissionKeeper(ctrl)

//resourceIds := []math.Uint{math.NewUint(rand.Uint64()), math.NewUint(rand.Uint64()), math.NewUint(rand.Uint64())}
// policy without expiry
op := sample.RandAccAddress()
resource := types2.NewBucketGRN("test-bucket").String()
policy := storageTypes.MsgPutPolicy{
Principal: &types.Principal{
Type: types.PRINCIPAL_TYPE_GNFD_GROUP,
Value: sample.RandAccAddressHex(),
},
Operator: op.String(),
Resource: resource,
Statements: nil,
ExpirationTime: nil,
}

app := keeper.NewPermissionApp(storageKeeper, permissionKeeper)
data, err := policy.Marshal()
s.NoError(err)

synPackage := storageTypes.CreatePolicySynPackage{
Operator: op,
Data: data,
ExtraData: []byte("extra data"),
}
serializedSynPackage := synPackage.MustSerialize()
serializedSynPackage = append([]byte{storageTypes.OperationCreatePolicy}, serializedSynPackage...)

// case 1: bucket not found
storageKeeper.EXPECT().GetBucketInfo(gomock.Any(), gomock.Any()).Return(&storageTypes.BucketInfo{
Owner: op.String(),
BucketName: "test-bucket",
}, true)
storageKeeper.EXPECT().GetBucketInfoById(gomock.Any(), gomock.Any()).Return(&storageTypes.BucketInfo{
Owner: op.String(),
BucketName: "test-bucket",
}, true)
storageKeeper.EXPECT().NormalizePrincipal(gomock.Any(), gomock.Any()).Return().AnyTimes()
storageKeeper.EXPECT().ValidatePrincipal(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
permissionKeeper.EXPECT().PutPolicy(gomock.Any(), gomock.Any()).Return(math.NewUint(1), nil).AnyTimes()
res := app.ExecuteSynPackage(s.ctx, &sdk.CrossChainAppContext{}, serializedSynPackage)
s.Require().ErrorIs(res.Err, nil)
}

0 comments on commit 86a744a

Please sign in to comment.