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 d58b210
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
@@ -1,6 +1,7 @@
package keeper_test

import (
types2 "github.com/bnb-chain/greenfield/types"

Check failure on line 4 in x/storage/keeper/cross_app_permission_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.20.x, ubuntu-latest)

File is not `goimports`-ed (goimports)
"math/big"
"math/rand"

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 d58b210

Please sign in to comment.