From 19da88254eb9f5e5ca3bca59e3cc0aaee3b01d50 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Sat, 2 Mar 2024 11:56:29 +0530 Subject: [PATCH] Add tests for bond module gRPC requests --- tests/e2e/bond/grpc.go | 159 ++++++++++++++++++++++++++++++++++++ tests/e2e/bond/suite.go | 19 ++++- x/bond/client/cli/query.go | 48 +++++++++++ x/bond/keeper/msg_server.go | 5 +- 4 files changed, 228 insertions(+), 3 deletions(-) create mode 100644 x/bond/client/cli/query.go diff --git a/tests/e2e/bond/grpc.go b/tests/e2e/bond/grpc.go index e1f6d143..48bec960 100644 --- a/tests/e2e/bond/grpc.go +++ b/tests/e2e/bond/grpc.go @@ -22,3 +22,162 @@ func (ets *E2ETestSuite) TestGRPCGetParams() { sr.NoError(err) sr.Equal(params.GetParams().MaxBondAmount, bondtypes.DefaultParams().MaxBondAmount) } + +func (ets *E2ETestSuite) TestGRPCGetBonds() { + val := ets.network.Validators[0] + sr := ets.Require() + reqURL := fmt.Sprintf("%s/cerc/bond/v1/bonds", val.APIAddress) + + testCases := []struct { + name string + url string + expErr bool + errorMsg string + preRun func() string + }{ + { + "invalid request with headers", + reqURL + "asdasdas", + true, + "", + func() string { return "" }, + }, + { + "valid request", + reqURL, + false, + "", + func() string { return ets.createBond() }, + }, + } + for _, tc := range testCases { + ets.Run(tc.name, func() { + tc.preRun() + + resp, _ := testutil.GetRequest(tc.url) + if tc.expErr { + sr.Contains(string(resp), tc.errorMsg) + } else { + var response bondtypes.QueryGetBondsResponse + err := val.ClientCtx.Codec.UnmarshalJSON(resp, &response) + sr.NoError(err) + sr.NotZero(len(response.GetBonds())) + } + }) + } +} + +func (ets *E2ETestSuite) TestGRPCGetBondsByOwner() { + val := ets.network.Validators[0] + sr := ets.Require() + reqURL := val.APIAddress + "/cerc/bond/v1/by-owner/%s" + + testCases := []struct { + name string + url string + expErr bool + preRun func() string + }{ + { + "empty list", + fmt.Sprintf(reqURL, "asdasd"), + true, + func() string { return "" }, + }, + { + "valid request", + fmt.Sprintf(reqURL, ets.accountAddress), + false, + func() string { return ets.createBond() }, + }, + } + + for _, tc := range testCases { + ets.Run(tc.name, func() { + tc.preRun() + + resp, err := testutil.GetRequest(tc.url) + ets.Require().NoError(err) + + var bonds bondtypes.QueryGetBondsByOwnerResponse + err = val.ClientCtx.Codec.UnmarshalJSON(resp, &bonds) + sr.NoError(err) + if tc.expErr { + sr.Empty(bonds.GetBonds()) + } else { + bondsList := bonds.GetBonds() + sr.NotZero(len(bondsList)) + sr.Equal(ets.accountAddress, bondsList[0].GetOwner()) + } + }) + } +} + +func (ets *E2ETestSuite) TestGRPCGetBondByID() { + val := ets.network.Validators[0] + sr := ets.Require() + reqURL := val.APIAddress + "/cerc/bond/v1/bonds/%s" + + testCases := []struct { + name string + url string + expErr bool + preRun func() string + }{ + { + "invalid request", + fmt.Sprintf(reqURL, "asdadad"), + true, + func() string { return "" }, + }, + { + "valid request", + reqURL, + false, + func() string { return ets.createBond() }, + }, + } + for _, tc := range testCases { + ets.Run(tc.name, func() { + var bondID string + if !tc.expErr { + bondID = tc.preRun() + tc.url = fmt.Sprintf(reqURL, bondID) + } + + resp, err := testutil.GetRequest(tc.url) + ets.Require().NoError(err) + + var bonds bondtypes.QueryGetBondByIdResponse + err = val.ClientCtx.Codec.UnmarshalJSON(resp, &bonds) + + if tc.expErr { + sr.Empty(bonds.GetBond().GetId()) + } else { + sr.NoError(err) + sr.NotZero(bonds.GetBond().GetId()) + sr.Equal(bonds.GetBond().GetId(), bondID) + } + }) + } +} + +func (ets *E2ETestSuite) TestGRPCGetBondModuleBalance() { + val := ets.network.Validators[0] + sr := ets.Require() + reqURL := fmt.Sprintf("%s/cerc/bond/v1/balance", val.APIAddress) + + // creating the bond + ets.createBond() + + ets.Run("valid request", func() { + resp, err := testutil.GetRequest(reqURL) + sr.NoError(err) + + var response bondtypes.QueryGetBondModuleBalanceResponse + err = val.ClientCtx.Codec.UnmarshalJSON(resp, &response) + + sr.NoError(err) + sr.False(response.GetBalance().IsZero()) + }) +} diff --git a/tests/e2e/bond/suite.go b/tests/e2e/bond/suite.go index 766940ed..816abc6c 100644 --- a/tests/e2e/bond/suite.go +++ b/tests/e2e/bond/suite.go @@ -14,6 +14,7 @@ import ( "github.com/cosmos/cosmos-sdk/testutil/network" sdk "github.com/cosmos/cosmos-sdk/types" + bondtypes "git.vdb.to/cerc-io/laconic2d/x/bond" "git.vdb.to/cerc-io/laconic2d/x/bond/client/cli" ) @@ -81,7 +82,7 @@ func (ets *E2ETestSuite) createAccountWithBalance(accountName string, accountAdd sr.NoError(err) } -func (ets *E2ETestSuite) createBond() { +func (ets *E2ETestSuite) createBond() string { val := ets.network.Validators[0] sr := ets.Require() createBondCmd := cli.NewCreateBondCmd() @@ -103,4 +104,20 @@ func (ets *E2ETestSuite) createBond() { // wait for tx to take effect err = ets.network.WaitForNextBlock() sr.NoError(err) + + // getting the bonds list and returning the bond-id + clientCtx := val.ClientCtx + cmd := cli.GetQueryBondLists() + args = []string{ + fmt.Sprintf("--%s=json", flags.FlagOutput), + } + out, err = clitestutil.ExecTestCLICmd(clientCtx, cmd, args) + sr.NoError(err) + var queryResponse bondtypes.QueryGetBondsResponse + err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &queryResponse) + sr.NoError(err) + + // extract bond id from bonds list + bond := queryResponse.GetBonds()[0] + return bond.GetId() } diff --git a/x/bond/client/cli/query.go b/x/bond/client/cli/query.go new file mode 100644 index 00000000..9a08e15e --- /dev/null +++ b/x/bond/client/cli/query.go @@ -0,0 +1,48 @@ +package cli + +import ( + "fmt" + "strings" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/version" + "github.com/spf13/cobra" + + bondtypes "git.vdb.to/cerc-io/laconic2d/x/bond" +) + +// GetQueryBondLists implements the bond lists query command. +func GetQueryBondLists() *cobra.Command { + cmd := &cobra.Command{ + Use: "list", + Short: "List bonds.", + Long: strings.TrimSpace( + fmt.Sprintf(`Get bond list . + +Example: +$ %s query %s list +`, + version.AppName, bondtypes.ModuleName, + ), + ), + Args: cobra.ExactArgs(0), + RunE: func(cmd *cobra.Command, args []string) error { + clientCtx, err := client.GetClientQueryContext(cmd) + if err != nil { + return err + } + queryClient := bondtypes.NewQueryClient(clientCtx) + res, err := queryClient.Bonds(cmd.Context(), &bondtypes.QueryGetBondsRequest{}) + if err != nil { + return err + } + + return clientCtx.PrintProto(res) + }, + } + + flags.AddQueryFlagsToCmd(cmd) + + return cmd +} diff --git a/x/bond/keeper/msg_server.go b/x/bond/keeper/msg_server.go index 2a28cb67..719f17b4 100644 --- a/x/bond/keeper/msg_server.go +++ b/x/bond/keeper/msg_server.go @@ -26,7 +26,8 @@ func (ms msgServer) CreateBond(c context.Context, msg *bond.MsgCreateBond) (*bon if err != nil { return nil, err } - _, err = ms.k.CreateBond(ctx, signerAddress, msg.Coins) + + resp, err := ms.k.CreateBond(ctx, signerAddress, msg.Coins) if err != nil { return nil, err } @@ -44,7 +45,7 @@ func (ms msgServer) CreateBond(c context.Context, msg *bond.MsgCreateBond) (*bon ), }) - return &bond.MsgCreateBondResponse{}, nil + return &bond.MsgCreateBondResponse{Id: resp.Id}, nil } // RefillBond implements bond.MsgServer.