Skip to content

Commit

Permalink
feat(x/ica): add tests for icamauth module (#1897)
Browse files Browse the repository at this point in the history
* add unit tests methods

* add tests for the types folder

* remove query test

* add test description
  • Loading branch information
Pantani authored Nov 17, 2022
1 parent 5aa51e2 commit 3387d79
Show file tree
Hide file tree
Showing 4 changed files with 274 additions and 14 deletions.
10 changes: 0 additions & 10 deletions x/icamauth/types/errors.go

This file was deleted.

6 changes: 2 additions & 4 deletions x/icamauth/types/msgs.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package types

import (
fmt "fmt"
"strings"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
proto "github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/proto"
)

var (
Expand Down Expand Up @@ -67,7 +66,7 @@ func NewMsgSubmitTx(sdkMsg sdk.Msg, connectionID, owner string) (*MsgSubmitTx, e
func PackTxMsgAny(sdkMsg sdk.Msg) (*codectypes.Any, error) {
msg, ok := sdkMsg.(proto.Message)
if !ok {
return nil, fmt.Errorf("can't proto marshal %T", sdkMsg)
return nil, sdkerrors.Wrapf(sdkerrors.ErrPackAny, "can't proto marshal %T", sdkMsg)
}

any, err := codectypes.NewAnyWithValue(msg)
Expand All @@ -81,7 +80,6 @@ func PackTxMsgAny(sdkMsg sdk.Msg) (*codectypes.Any, error) {
// UnpackInterfaces implements codectypes.UnpackInterfacesMessage
func (msg MsgSubmitTx) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
var sdkMsg sdk.Msg

return unpacker.UnpackAny(msg.Msg, &sdkMsg)
}

Expand Down
247 changes: 247 additions & 0 deletions x/icamauth/types/msgs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
package types

import (
"errors"
"testing"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/stretchr/testify/require"
)

func TestMsgRegisterAccount_ValidateBasic(t *testing.T) {
tests := []struct {
name string
msg MsgRegisterAccount
err error
}{
{
name: "empty owner",
msg: MsgRegisterAccount{
Owner: "",
},
err: sdkerrors.ErrInvalidAddress,
}, {
name: "invalid bech32 owner address",
msg: MsgRegisterAccount{
Owner: "invalid_address",
},
err: sdkerrors.ErrInvalidAddress,
}, {
name: "valid mesage",
msg: MsgRegisterAccount{
Owner: "cosmos1a6zlyvpnksx8wr6wz8wemur2xe8zyh0yxeh27a",
},
err: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.msg.ValidateBasic()
if tt.err != nil {
require.ErrorIs(t, tt.err, err)
return
}
require.NoError(t, err)
})
}
}

func TestPackTxMsgAny(t *testing.T) {
tests := []struct {
name string
sdkMsg types.Msg
want *codectypes.Any
err error
}{
{
name: "empty bank send message",
sdkMsg: &banktypes.MsgSend{},
want: &codectypes.Any{
TypeUrl: "/cosmos.bank.v1beta1.MsgSend",
Value: []byte{},
},
err: nil,
}, {
name: "bank send message",
sdkMsg: &banktypes.MsgSend{
FromAddress: "cosmos1a6zlyvpnksx8wr6wz8wemur2xe8zyh0yxeh27a",
ToAddress: "cosmos1a6zlyvpnksx8wr6wz8wemur2xe8zyh0yxeh27a",
Amount: sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(10))),
},
want: &codectypes.Any{
TypeUrl: "/cosmos.bank.v1beta1.MsgSend",
Value: []byte{0xa, 0x2d, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x31, 0x61, 0x36, 0x7a, 0x6c, 0x79, 0x76, 0x70, 0x6e, 0x6b, 0x73, 0x78, 0x38, 0x77, 0x72, 0x36, 0x77, 0x7a, 0x38, 0x77, 0x65, 0x6d, 0x75, 0x72, 0x32, 0x78, 0x65, 0x38, 0x7a, 0x79, 0x68, 0x30, 0x79, 0x78, 0x65, 0x68, 0x32, 0x37, 0x61, 0x12, 0x2d, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x31, 0x61, 0x36, 0x7a, 0x6c, 0x79, 0x76, 0x70, 0x6e, 0x6b, 0x73, 0x78, 0x38, 0x77, 0x72, 0x36, 0x77, 0x7a, 0x38, 0x77, 0x65, 0x6d, 0x75, 0x72, 0x32, 0x78, 0x65, 0x38, 0x7a, 0x79, 0x68, 0x30, 0x79, 0x78, 0x65, 0x68, 0x32, 0x37, 0x61, 0x1a, 0xa, 0xa, 0x4, 0x61, 0x74, 0x6f, 0x6d, 0x12, 0x2, 0x31, 0x30},
},
err: nil,
}, {
name: "nil message",
sdkMsg: nil,
err: sdkerrors.ErrPackAny,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := PackTxMsgAny(tt.sdkMsg)
if tt.err != nil {
require.ErrorIs(t, tt.err, err)
return
}
require.NoError(t, err)
require.Equal(t, tt.want.TypeUrl, got.TypeUrl)
require.Equal(t, tt.want.Value, got.Value)
})
}
}

func TestMsgSubmitTx_GetSigners(t *testing.T) {
tests := []struct {
name string
msg MsgSubmitTx
want []sdk.AccAddress
err error
}{
{
name: "valid signers",
msg: MsgSubmitTx{
Owner: "cosmos1a6zlyvpnksx8wr6wz8wemur2xe8zyh0yxeh27a",
},
want: []types.AccAddress{[]byte{238, 133, 242, 48, 51, 180, 12, 119, 15, 78, 17, 221, 157, 240, 106, 54, 78, 34, 93, 228}},
}, {
name: "empty address",
msg: MsgSubmitTx{
Owner: "",
},
err: errors.New("empty address string is not allowed"),
}, {
name: "invalid address",
msg: MsgSubmitTx{
Owner: "invalid_address",
},
err: errors.New("decoding bech32 failed: invalid separator index -1"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.err != nil {
require.PanicsWithError(t, tt.err.Error(), func() {
_ = tt.msg.GetSigners()
})
return
}
require.Equal(t, tt.want, tt.msg.GetSigners())
})
}
}

func TestMsgSubmitTx_GetTxMsg(t *testing.T) {
t.Run("bank message", func(t *testing.T) {
msg := &banktypes.MsgSend{
FromAddress: "cosmos1a6zlyvpnksx8wr6wz8wemur2xe8zyh0yxeh27a",
ToAddress: "cosmos1a6zlyvpnksx8wr6wz8wemur2xe8zyh0yxeh27a",
Amount: sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(10))),
}
anyMsg, err := PackTxMsgAny(msg)
require.NoError(t, err)
submitTx := MsgSubmitTx{Msg: anyMsg}
require.Equal(t, msg, submitTx.GetTxMsg())
})

t.Run("staking message", func(t *testing.T) {
msg := &stakingtypes.MsgDelegate{
DelegatorAddress: "cosmos1a6zlyvpnksx8wr6wz8wemur2xe8zyh0yxeh27a",
ValidatorAddress: "cosmos1a6zlyvpnksx8wr6wz8wemur2xe8zyh0yxeh27a",
Amount: sdk.NewCoin("atom", sdk.NewInt(10)),
}
anyMsg, err := PackTxMsgAny(msg)
require.NoError(t, err)
submitTx := MsgSubmitTx{Msg: anyMsg}
require.Equal(t, msg, submitTx.GetTxMsg())
})
}

func TestMsgSubmitTx_ValidateBasic(t *testing.T) {
tests := []struct {
name string
msg MsgSubmitTx
err error
}{
{
name: "empty owner",
msg: MsgSubmitTx{
Owner: "",
},
err: sdkerrors.ErrInvalidAddress,
}, {
name: "invalid bech32 owner address",
msg: MsgSubmitTx{
Owner: "invalid_address",
},
err: sdkerrors.ErrInvalidAddress,
}, {
name: "valid mesage",
msg: MsgSubmitTx{
Owner: "cosmos1a6zlyvpnksx8wr6wz8wemur2xe8zyh0yxeh27a",
},
err: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.msg.ValidateBasic()
if tt.err != nil {
require.ErrorIs(t, tt.err, err)
return
}
require.NoError(t, err)
})
}
}

func TestNewMsgRegisterAccount(t *testing.T) {
var (
connectionID = "connection-0"
owner = "cosmos1a6zlyvpnksx8wr6wz8wemur2xe8zyh0yxeh27a"
version = "1"
got = NewMsgRegisterAccount(owner, connectionID, version)
)
require.Equal(t, connectionID, got.ConnectionId)
require.Equal(t, owner, got.Owner)
require.Equal(t, version, got.Version)
}

func TestNewMsgSubmitTx(t *testing.T) {
var (
connectionID = "connection-0"
owner = "cosmos1a6zlyvpnksx8wr6wz8wemur2xe8zyh0yxeh27a"
)

t.Run("valid message", func(t *testing.T) {
msg := &banktypes.MsgSend{
FromAddress: "cosmos1a6zlyvpnksx8wr6wz8wemur2xe8zyh0yxeh27a",
ToAddress: "cosmos1a6zlyvpnksx8wr6wz8wemur2xe8zyh0yxeh27a",
Amount: sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(10))),
}
got, err := NewMsgSubmitTx(msg, connectionID, owner)
require.NoError(t, err)

anyMsg, err := PackTxMsgAny(msg)
require.NoError(t, err)

want := &MsgSubmitTx{
Owner: owner,
ConnectionId: connectionID,
Msg: anyMsg,
}
require.EqualValues(t, want, got)
})

t.Run("invalid message field", func(t *testing.T) {
_, err := NewMsgSubmitTx(nil, connectionID, owner)
require.ErrorIs(t, sdkerrors.ErrPackAny, err)
})
}
25 changes: 25 additions & 0 deletions x/icamauth/types/query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package types

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestNewQueryInterchainAccountRequest(t *testing.T) {
var (
connectionID = "connection-0"
owner = "cosmos1a6zlyvpnksx8wr6wz8wemur2xe8zyh0yxeh27a"
got = NewQueryInterchainAccountRequest(connectionID, owner)
)
require.Equal(t, connectionID, got.ConnectionId)
require.Equal(t, owner, got.Owner)
}

func TestNewQueryInterchainAccountResponse(t *testing.T) {
var (
interchainAccAddr = "cosmos1a6zlyvpnksx8wr6wz8wemur2xe8zyh0yxeh27a"
got = NewQueryInterchainAccountResponse(interchainAccAddr)
)
require.Equal(t, interchainAccAddr, got.InterchainAccountAddress)
}

0 comments on commit 3387d79

Please sign in to comment.