Skip to content

Commit

Permalink
[lockup] fix: MsgLock validation checks and add more tests (#492)
Browse files Browse the repository at this point in the history
* chore: msgs tests

* chore: lint

Co-authored-by: AgentSmithMatrix <98403347+AgentSmithMatrix@users.noreply.github.com>
  • Loading branch information
testinginprod and AgentSmithMatrix authored May 25, 2022
1 parent fd9b661 commit 29c15b4
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 12 deletions.
24 changes: 12 additions & 12 deletions x/lockup/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package types

import (
"fmt"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand All @@ -19,20 +18,21 @@ var (
_ sdk.Msg = (*MsgInitiateUnlock)(nil)
)

// NewMsgLockTokens creates a message to lock tokens.
func NewMsgLockTokens(owner sdk.AccAddress, duration time.Duration, coins sdk.Coins) *MsgLockTokens {
return &MsgLockTokens{
Owner: owner.String(),
Duration: duration,
Coins: coins,
}
}

func (m MsgLockTokens) Route() string { return RouterKey }
func (m MsgLockTokens) Type() string { return TypeMsgLockTokens }
func (m MsgLockTokens) ValidateBasic() error {
if err := m.Coins.Validate(); err != nil {
return fmt.Errorf("invalid coins")
}
if m.Coins.IsZero() {
return fmt.Errorf("zero coins")
}
if m.Duration <= 0 {
return fmt.Errorf("duration should be positive: %d < 0", m.Duration)
return fmt.Errorf("duration should be positive: %d <= 0", m.Duration)
}

if _, err := sdk.AccAddressFromBech32(m.Owner); err != nil {
return fmt.Errorf("invalid address")
}
return nil
}
Expand All @@ -49,7 +49,7 @@ func (m MsgLockTokens) GetSigners() []sdk.AccAddress {
func (m *MsgInitiateUnlock) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(m.Owner)
if err != nil {
return err
return fmt.Errorf("invalid address")
}
return nil
}
Expand Down
116 changes: 116 additions & 0 deletions x/lockup/types/msgs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package types

import (
"testing"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

"github.com/NibiruChain/nibiru/x/testutil/sample"
)

func TestMsgLockTokens_ValidateBasic(t *testing.T) {
type test struct {
msg *MsgLockTokens
wantErr string
}

validAddr := sample.AccAddress().String()
validDuration := 1 * time.Hour
validCoins := sdk.NewCoins(sdk.NewInt64Coin("test", 100))

cases := map[string]test{
"success": {
msg: &MsgLockTokens{
Owner: validAddr,
Duration: validDuration,
Coins: validCoins,
},
},
"invalid address": {
msg: &MsgLockTokens{
Owner: "",
Duration: validDuration,
Coins: validCoins,
},
wantErr: "invalid address",
},
"invalid coins": {
msg: &MsgLockTokens{
Owner: validAddr,
Duration: validDuration,
Coins: sdk.Coins{sdk.Coin{}},
},
wantErr: "invalid coins",
},
"zero coins": {
msg: &MsgLockTokens{
Owner: validAddr,
Duration: validDuration,
Coins: sdk.NewCoins(),
},
wantErr: "zero coins",
},
"invalid duration": {
msg: &MsgLockTokens{
Owner: validAddr,
Duration: 0,
Coins: validCoins,
},
wantErr: "duration should be positive",
},
}

for name, tc := range cases {
tc := tc
t.Run(name, func(t *testing.T) {
err := tc.msg.ValidateBasic()
if tc.wantErr == "" && err != nil {
t.Fatalf("unexpected error: %s", err)
}
if tc.wantErr != "" && err == nil {
t.Fatalf("expected error: %s", err)
}
if tc.wantErr != "" {
require.Contains(t, err.Error(), tc.wantErr)
}
})
}
}

func TestMsgInitiateUnlock_ValidateBasic(t *testing.T) {
type test struct {
msg *MsgInitiateUnlock
wantErr string
}

cases := map[string]test{
"success": {
msg: &MsgInitiateUnlock{
Owner: sample.AccAddress().String(),
LockId: 0,
},
},
"invalid address": {
msg: &MsgInitiateUnlock{Owner: "invalid address", LockId: 0},
wantErr: "invalid address",
},
}

for name, tc := range cases {
tc := tc
t.Run(name, func(t *testing.T) {
err := tc.msg.ValidateBasic()
if tc.wantErr == "" && err != nil {
t.Fatalf("unexpected error: %s", err)
}
if tc.wantErr != "" && err == nil {
t.Fatalf("expected error: %s", err)
}
if tc.wantErr != "" {
require.Contains(t, err.Error(), tc.wantErr)
}
})
}
}

0 comments on commit 29c15b4

Please sign in to comment.