Skip to content

Commit

Permalink
test: campaign message basic validation
Browse files Browse the repository at this point in the history
  • Loading branch information
scorpioborn committed Sep 26, 2023
1 parent c806f34 commit a8cad2a
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 16 deletions.
6 changes: 3 additions & 3 deletions x/reward/keeper/msg_server_campaign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrortypes "github.com/cosmos/cosmos-sdk/types/errors"
sdkerrtypes "github.com/cosmos/cosmos-sdk/types/errors"

"github.com/sge-network/sge/testutil/sample"
"github.com/sge-network/sge/testutil/simapp"
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestCampaignMsgServerUpdate(t *testing.T) {
request: &types.MsgUpdateCampaign{
Uid: uuid.NewString(),
},
err: sdkerrortypes.ErrKeyNotFound,
err: sdkerrtypes.ErrKeyNotFound,
},
} {
t.Run(tc.desc, func(t *testing.T) {
Expand Down Expand Up @@ -182,7 +182,7 @@ func TestCampaignMsgServerDelete(t *testing.T) {
request: &types.MsgDeleteCampaign{
Uid: uuid.NewString(),
},
err: sdkerrortypes.ErrKeyNotFound,
err: sdkerrtypes.ErrKeyNotFound,
},
} {
t.Run(tc.desc, func(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions x/reward/keeper/msg_server_reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrortypes "github.com/cosmos/cosmos-sdk/types/errors"
sdkerrtypes "github.com/cosmos/cosmos-sdk/types/errors"

"github.com/sge-network/sge/x/reward/types"
)
Expand All @@ -16,7 +16,7 @@ func (k msgServer) ApplyReward(goCtx context.Context, msg *types.MsgApplyReward)
// Check if the value already exists
campaign, isFound := k.GetCampaign(ctx, msg.CampaignUid)
if isFound {
return nil, sdkerrors.Wrap(sdkerrortypes.ErrInvalidRequest, "campaign with the same uid is already set")
return nil, sdkerrors.Wrap(sdkerrtypes.ErrInvalidRequest, "campaign with the same uid is already set")
}

if err := campaign.CheckExpiration(uint64(ctx.BlockTime().Unix())); err == nil {
Expand All @@ -25,7 +25,7 @@ func (k msgServer) ApplyReward(goCtx context.Context, msg *types.MsgApplyReward)

rewardFactory, err := campaign.GetRewardsFactory()
if err != nil {
return nil, sdkerrors.Wrap(sdkerrortypes.ErrInvalidRequest, "failed to retrieve reward factory")
return nil, sdkerrors.Wrap(sdkerrtypes.ErrInvalidRequest, "failed to retrieve reward factory")
}

distribution, err := rewardFactory.CalculateDistributions(goCtx, ctx,
Expand All @@ -35,7 +35,7 @@ func (k msgServer) ApplyReward(goCtx context.Context, msg *types.MsgApplyReward)
},
campaign.RewardDefs, msg.Ticket)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrortypes.ErrInvalidRequest, "failed to get destribution from the ticket")
return nil, sdkerrors.Wrap(sdkerrtypes.ErrInvalidRequest, "failed to get destribution from the ticket")
}

if err := campaign.CheckPoolBalance(distribution); err != nil {
Expand Down
32 changes: 28 additions & 4 deletions x/reward/types/messages_campaign.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package types
import (
sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrortypes "github.com/cosmos/cosmos-sdk/types/errors"
sdkerrtypes "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/sge-network/sge/utils"
)

const (
Expand Down Expand Up @@ -50,8 +51,17 @@ func (msg *MsgCreateCampaign) GetSignBytes() []byte {
func (msg *MsgCreateCampaign) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
return sdkerrors.Wrapf(sdkerrortypes.ErrInvalidAddress, "invalid creator address (%s)", err)
return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidAddress, "invalid creator address (%s)", err)
}

if !utils.IsValidUID(msg.Uid) {
return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidRequest, "invalid uid")
}

if msg.Ticket == "" {
return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidRequest, "invalid ticket")
}

return nil
}

Expand Down Expand Up @@ -93,8 +103,17 @@ func (msg *MsgUpdateCampaign) GetSignBytes() []byte {
func (msg *MsgUpdateCampaign) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
return sdkerrors.Wrapf(sdkerrortypes.ErrInvalidAddress, "invalid creator address (%s)", err)
return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidAddress, "invalid creator address (%s)", err)
}

if !utils.IsValidUID(msg.Uid) {
return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidRequest, "invalid uid")
}

if msg.Ticket == "" {
return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidRequest, "invalid ticket")
}

return nil
}

Expand Down Expand Up @@ -134,7 +153,12 @@ func (msg *MsgDeleteCampaign) GetSignBytes() []byte {
func (msg *MsgDeleteCampaign) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
return sdkerrors.Wrapf(sdkerrortypes.ErrInvalidAddress, "invalid creator address (%s)", err)
return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidAddress, "invalid creator address (%s)", err)
}

if !utils.IsValidUID(msg.Uid) {
return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidRequest, "invalid uid")
}

return nil
}
64 changes: 61 additions & 3 deletions x/reward/types/messages_campaign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types_test
import (
"testing"

"github.com/google/uuid"
"github.com/stretchr/testify/require"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand All @@ -21,12 +22,35 @@ func TestMsgCreateCampaign_ValidateBasic(t *testing.T) {
name: "invalid address",
msg: types.MsgCreateCampaign{
Creator: "invalid_address",
Uid: uuid.NewString(),
Ticket: "ticket",
},
err: sdkerrors.ErrInvalidAddress,
}, {
},
{
name: "invalid uid",
msg: types.MsgCreateCampaign{
Creator: sample.AccAddress(),
Uid: "invalid uid",
Ticket: "ticket",
},
err: sdkerrors.ErrInvalidRequest,
},
{
name: "invalid ticket",
msg: types.MsgCreateCampaign{
Creator: sample.AccAddress(),
Uid: uuid.NewString(),
Ticket: "",
},
err: sdkerrors.ErrInvalidRequest,
},
{
name: "valid address",
msg: types.MsgCreateCampaign{
Creator: sample.AccAddress(),
Uid: uuid.NewString(),
Ticket: "ticket",
},
},
}
Expand All @@ -52,12 +76,35 @@ func TestMsgUpdateCampaign_ValidateBasic(t *testing.T) {
name: "invalid address",
msg: types.MsgUpdateCampaign{
Creator: "invalid_address",
Uid: uuid.NewString(),
Ticket: "ticket",
},
err: sdkerrors.ErrInvalidAddress,
}, {
},
{
name: "invalid uid",
msg: types.MsgUpdateCampaign{
Creator: sample.AccAddress(),
Uid: "invalid uid",
Ticket: "ticket",
},
err: sdkerrors.ErrInvalidRequest,
},
{
name: "invalid ticket",
msg: types.MsgUpdateCampaign{
Creator: sample.AccAddress(),
Uid: uuid.NewString(),
Ticket: "",
},
err: sdkerrors.ErrInvalidRequest,
},
{
name: "valid address",
msg: types.MsgUpdateCampaign{
Creator: sample.AccAddress(),
Uid: uuid.NewString(),
Ticket: "ticket",
},
},
}
Expand All @@ -83,12 +130,23 @@ func TestMsgDeleteCampaign_ValidateBasic(t *testing.T) {
name: "invalid address",
msg: types.MsgDeleteCampaign{
Creator: "invalid_address",
Uid: uuid.NewString(),
},
err: sdkerrors.ErrInvalidAddress,
}, {
},
{
name: "invalid uid",
msg: types.MsgDeleteCampaign{
Creator: sample.AccAddress(),
Uid: "invalid uid",
},
err: sdkerrors.ErrInvalidRequest,
},
{
name: "valid address",
msg: types.MsgDeleteCampaign{
Creator: sample.AccAddress(),
Uid: uuid.NewString(),
},
},
}
Expand Down
3 changes: 1 addition & 2 deletions x/reward/types/messages_reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package types
import (
sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrortypes "github.com/cosmos/cosmos-sdk/types/errors"
sdkerrtypes "github.com/cosmos/cosmos-sdk/types/errors"

"github.com/sge-network/sge/utils"
Expand Down Expand Up @@ -51,7 +50,7 @@ func (msg *MsgApplyReward) GetSignBytes() []byte {
func (msg *MsgApplyReward) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
return sdkerrors.Wrapf(sdkerrortypes.ErrInvalidAddress, "invalid creator address (%s)", err)
return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidAddress, "invalid creator address (%s)", err)
}

if !utils.IsValidUID(msg.CampaignUid) {
Expand Down

0 comments on commit a8cad2a

Please sign in to comment.