Skip to content

Commit

Permalink
restructure crosschain tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Jun 6, 2024
1 parent c7333d7 commit 8c46698
Show file tree
Hide file tree
Showing 12 changed files with 463 additions and 474 deletions.
10 changes: 3 additions & 7 deletions testutil/keeper/authority.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,9 @@ func AuthorityKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
return &k, ctx
}

// MockIsAuthorized mocks the IsAuthorized method of an authority keeper mock
func MockIsAuthorized(m *mock.Mock, address string, policyType types.PolicyType, isAuthorized bool) {
m.On("IsAuthorized", mock.Anything, address, policyType).Return(isAuthorized).Once()
}

func MockCheckAuthorization(m *mock.Mock, msg sdk.Msg, isAuthorized error) {
m.On("CheckAuthorization", mock.Anything, msg).Return(isAuthorized).Once()
// MockCheckAuthorization mocks the CheckAuthorization method of the authority keeper.
func MockCheckAuthorization(m *mock.Mock, msg sdk.Msg, authorizationResult error) {
m.On("CheckAuthorization", mock.Anything, msg).Return(authorizationResult).Once()
}

func SetAdminPolicies(ctx sdk.Context, ak *keeper.Keeper) string {
Expand Down
1 change: 1 addition & 0 deletions x/authority/keeper/msg_server_add_authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func (k msgServer) AddAuthorization(
) (*types.MsgAddAuthorizationResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// check if the caller is authorized to add an authorization
err := k.CheckAuthorization(ctx, msg)
if err != nil {
return nil, errorsmod.Wrap(types.ErrUnauthorized, err.Error())
Expand Down
1 change: 1 addition & 0 deletions x/authority/keeper/msg_server_remove_authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func (k msgServer) RemoveAuthorization(
) (*types.MsgRemoveAuthorizationResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// check if the caller is authorized to remove an authorization
err := k.CheckAuthorization(ctx, msg)
if err != nil {
return nil, errorsmod.Wrap(types.ErrUnauthorized, err.Error())
Expand Down
58 changes: 29 additions & 29 deletions x/crosschain/keeper/msg_server_abort_stuck_cctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ func TestMsgServer_AbortStuckCCTX(t *testing.T) {
msgServer := crosschainkeeper.NewMsgServerImpl(*k)
admin := sample.AccAddress()
authorityMock := keepertest.GetCrosschainAuthorityMock(t, k)
msg := crosschaintypes.MsgAbortStuckCCTX{
Creator: admin,
CctxIndex: sample.GetCctxIndexFromString("cctx_index"),
}
keepertest.MockCheckAuthorization(&authorityMock.Mock, &msg, nil)

// create a cctx
cctx := sample.CrossChainTx(t, "cctx_index")
Expand All @@ -35,6 +30,11 @@ func TestMsgServer_AbortStuckCCTX(t *testing.T) {
k.SetCrossChainTx(ctx, *cctx)

// abort the cctx
msg := crosschaintypes.MsgAbortStuckCCTX{
Creator: admin,
CctxIndex: sample.GetCctxIndexFromString("cctx_index"),
}
keepertest.MockCheckAuthorization(&authorityMock.Mock, &msg, nil)
_, err := msgServer.AbortStuckCCTX(ctx, &msg)

require.NoError(t, err)
Expand All @@ -51,13 +51,7 @@ func TestMsgServer_AbortStuckCCTX(t *testing.T) {

msgServer := crosschainkeeper.NewMsgServerImpl(*k)
admin := sample.AccAddress()
msg := crosschaintypes.MsgAbortStuckCCTX{
Creator: admin,
CctxIndex: sample.GetCctxIndexFromString("cctx_index"),
}
authorityMock := keepertest.GetCrosschainAuthorityMock(t, k)
keepertest.MockCheckAuthorization(&authorityMock.Mock, &msg, nil)

// create a cctx
cctx := sample.CrossChainTx(t, "cctx_index")
cctx.CctxStatus = &crosschaintypes.Status{
Expand All @@ -67,6 +61,11 @@ func TestMsgServer_AbortStuckCCTX(t *testing.T) {
k.SetCrossChainTx(ctx, *cctx)

// abort the cctx
msg := crosschaintypes.MsgAbortStuckCCTX{
Creator: admin,
CctxIndex: sample.GetCctxIndexFromString("cctx_index"),
}
keepertest.MockCheckAuthorization(&authorityMock.Mock, &msg, nil)
_, err := msgServer.AbortStuckCCTX(ctx, &msg)

require.NoError(t, err)
Expand All @@ -83,12 +82,7 @@ func TestMsgServer_AbortStuckCCTX(t *testing.T) {

msgServer := crosschainkeeper.NewMsgServerImpl(*k)
admin := sample.AccAddress()
msg := crosschaintypes.MsgAbortStuckCCTX{
Creator: admin,
CctxIndex: sample.GetCctxIndexFromString("cctx_index"),
}
authorityMock := keepertest.GetCrosschainAuthorityMock(t, k)
keepertest.MockCheckAuthorization(&authorityMock.Mock, &msg, nil)

// create a cctx
cctx := sample.CrossChainTx(t, "cctx_index")
Expand All @@ -99,6 +93,12 @@ func TestMsgServer_AbortStuckCCTX(t *testing.T) {
k.SetCrossChainTx(ctx, *cctx)

// abort the cctx
msg := crosschaintypes.MsgAbortStuckCCTX{
Creator: admin,
CctxIndex: sample.GetCctxIndexFromString("cctx_index"),
}

keepertest.MockCheckAuthorization(&authorityMock.Mock, &msg, nil)
_, err := msgServer.AbortStuckCCTX(ctx, &msg)

require.NoError(t, err)
Expand All @@ -115,12 +115,7 @@ func TestMsgServer_AbortStuckCCTX(t *testing.T) {
msgServer := crosschainkeeper.NewMsgServerImpl(*k)

admin := sample.AccAddress()
msg := crosschaintypes.MsgAbortStuckCCTX{
Creator: admin,
CctxIndex: sample.GetCctxIndexFromString("cctx_index"),
}
authorityMock := keepertest.GetCrosschainAuthorityMock(t, k)
keepertest.MockCheckAuthorization(&authorityMock.Mock, &msg, authoritytypes.ErrUnauthorized)

// create a cctx
cctx := sample.CrossChainTx(t, "cctx_index")
Expand All @@ -131,6 +126,12 @@ func TestMsgServer_AbortStuckCCTX(t *testing.T) {
k.SetCrossChainTx(ctx, *cctx)

// abort the cctx
msg := crosschaintypes.MsgAbortStuckCCTX{
Creator: admin,
CctxIndex: sample.GetCctxIndexFromString("cctx_index"),
}

keepertest.MockCheckAuthorization(&authorityMock.Mock, &msg, authoritytypes.ErrUnauthorized)
_, err := msgServer.AbortStuckCCTX(ctx, &msg)
require.ErrorIs(t, err, authoritytypes.ErrUnauthorized)
})
Expand All @@ -143,14 +144,13 @@ func TestMsgServer_AbortStuckCCTX(t *testing.T) {

admin := sample.AccAddress()
authorityMock := keepertest.GetCrosschainAuthorityMock(t, k)

// abort the cctx
msg := crosschaintypes.MsgAbortStuckCCTX{
Creator: admin,
CctxIndex: sample.GetCctxIndexFromString("cctx_index"),
}

keepertest.MockCheckAuthorization(&authorityMock.Mock, &msg, nil)

// abort the cctx
_, err := msgServer.AbortStuckCCTX(ctx, &msg)
require.ErrorIs(t, err, crosschaintypes.ErrCannotFindCctx)
})
Expand All @@ -163,11 +163,6 @@ func TestMsgServer_AbortStuckCCTX(t *testing.T) {

admin := sample.AccAddress()
authorityMock := keepertest.GetCrosschainAuthorityMock(t, k)
msg := crosschaintypes.MsgAbortStuckCCTX{
Creator: admin,
CctxIndex: sample.GetCctxIndexFromString("cctx_index"),
}
keepertest.MockCheckAuthorization(&authorityMock.Mock, &msg, nil)

// create a cctx
cctx := sample.CrossChainTx(t, "cctx_index")
Expand All @@ -178,6 +173,11 @@ func TestMsgServer_AbortStuckCCTX(t *testing.T) {
k.SetCrossChainTx(ctx, *cctx)

// abort the cctx
msg := crosschaintypes.MsgAbortStuckCCTX{
Creator: admin,
CctxIndex: sample.GetCctxIndexFromString("cctx_index"),
}
keepertest.MockCheckAuthorization(&authorityMock.Mock, &msg, nil)
_, err := msgServer.AbortStuckCCTX(ctx, &msg)
require.ErrorIs(t, err, crosschaintypes.ErrStatusNotPending)
})
Expand Down
Loading

0 comments on commit 8c46698

Please sign in to comment.