2022-04-05 07:09:27 +00:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-04-23 15:53:51 +00:00
|
|
|
|
2022-09-07 06:36:11 +00:00
|
|
|
"github.com/cerc-io/laconicd/x/bond/client/cli"
|
|
|
|
bondtypes "github.com/cerc-io/laconicd/x/bond/types"
|
2022-04-05 07:09:27 +00:00
|
|
|
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
|
2022-04-23 15:53:51 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/testutil/rest"
|
2022-04-05 07:09:27 +00:00
|
|
|
tmcli "github.com/tendermint/tendermint/libs/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *IntegrationTestSuite) TestGRPCGetBonds() {
|
|
|
|
val := s.network.Validators[0]
|
|
|
|
sr := s.Require()
|
2022-10-17 11:03:31 +00:00
|
|
|
reqURL := fmt.Sprintf("%s/vulcanize/bond/v1beta1/bonds", val.APIAddress)
|
2022-04-05 07:09:27 +00:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
url string
|
|
|
|
expErr bool
|
|
|
|
errorMsg string
|
|
|
|
preRun func()
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"invalid request with headers",
|
2022-10-17 11:03:31 +00:00
|
|
|
reqURL + "asdasdas",
|
2022-04-05 07:09:27 +00:00
|
|
|
true,
|
|
|
|
"",
|
|
|
|
func() {
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"valid request",
|
2022-10-17 11:03:31 +00:00
|
|
|
reqURL,
|
2022-04-05 07:09:27 +00:00
|
|
|
false,
|
|
|
|
"",
|
|
|
|
func() {
|
|
|
|
s.createBond()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
s.Run(tc.name, func() {
|
|
|
|
resp, _ := rest.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 (s *IntegrationTestSuite) TestGRPCGetParams() {
|
|
|
|
val := s.network.Validators[0]
|
|
|
|
sr := s.Require()
|
2022-10-17 11:03:31 +00:00
|
|
|
reqURL := fmt.Sprintf("%s/vulcanize/bond/v1beta1/params", val.APIAddress)
|
2022-04-05 07:09:27 +00:00
|
|
|
|
2022-10-17 11:03:31 +00:00
|
|
|
resp, err := rest.GetRequest(reqURL)
|
2022-04-05 07:09:27 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
var params bondtypes.QueryParamsResponse
|
|
|
|
err = val.ClientCtx.Codec.UnmarshalJSON(resp, ¶ms)
|
|
|
|
|
|
|
|
sr.NoError(err)
|
|
|
|
sr.Equal(params.GetParams().MaxBondAmount, bondtypes.DefaultParams().MaxBondAmount)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestSuite) TestGRPCGetBondsByOwner() {
|
|
|
|
val := s.network.Validators[0]
|
|
|
|
sr := s.Require()
|
2022-10-17 11:03:31 +00:00
|
|
|
reqURL := val.APIAddress + "/vulcanize/bond/v1beta1/by-owner/%s"
|
2022-04-05 07:09:27 +00:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
url string
|
|
|
|
expErr bool
|
|
|
|
preRun func()
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"empty list",
|
2022-10-17 11:03:31 +00:00
|
|
|
fmt.Sprintf(reqURL, "asdasd"),
|
2022-04-05 07:09:27 +00:00
|
|
|
true,
|
|
|
|
func() {
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"valid request",
|
2022-10-17 11:03:31 +00:00
|
|
|
fmt.Sprintf(reqURL, s.accountAddress),
|
2022-04-05 07:09:27 +00:00
|
|
|
false,
|
|
|
|
func() {
|
|
|
|
s.createBond()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
s.Run(tc.name, func() {
|
|
|
|
if !tc.expErr {
|
|
|
|
tc.preRun()
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := rest.GetRequest(tc.url)
|
|
|
|
s.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(s.accountAddress, bondsList[0].GetOwner())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-17 11:03:31 +00:00
|
|
|
func (s *IntegrationTestSuite) TestGRPCGetBondByID() {
|
2022-04-05 07:09:27 +00:00
|
|
|
val := s.network.Validators[0]
|
|
|
|
sr := s.Require()
|
2022-10-17 11:03:31 +00:00
|
|
|
reqURL := val.APIAddress + "/vulcanize/bond/v1beta1/bonds/%s"
|
2022-04-05 07:09:27 +00:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
url string
|
|
|
|
expErr bool
|
|
|
|
preRun func() string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"invalid request",
|
2022-10-17 11:03:31 +00:00
|
|
|
fmt.Sprintf(reqURL, "asdadad"),
|
2022-04-05 07:09:27 +00:00
|
|
|
true,
|
|
|
|
func() string {
|
|
|
|
return ""
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"valid request",
|
2022-10-17 11:03:31 +00:00
|
|
|
reqURL,
|
2022-04-05 07:09:27 +00:00
|
|
|
false,
|
|
|
|
func() string {
|
|
|
|
// creating the bond
|
|
|
|
s.createBond()
|
|
|
|
|
|
|
|
// getting the bonds list and returning the bond-id
|
|
|
|
clientCtx := val.ClientCtx
|
|
|
|
cmd := cli.GetQueryBondLists()
|
|
|
|
args := []string{
|
|
|
|
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
|
|
|
|
}
|
|
|
|
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]
|
2022-10-17 11:03:31 +00:00
|
|
|
return bond.GetID()
|
2022-04-05 07:09:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
s.Run(tc.name, func() {
|
2022-10-17 11:03:31 +00:00
|
|
|
var bondID string
|
2022-04-05 07:09:27 +00:00
|
|
|
if !tc.expErr {
|
2022-10-17 11:03:31 +00:00
|
|
|
bondID = tc.preRun()
|
|
|
|
tc.url = fmt.Sprintf(reqURL, bondID)
|
2022-04-05 07:09:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := rest.GetRequest(tc.url)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2022-10-17 11:03:31 +00:00
|
|
|
var bonds bondtypes.QueryGetBondByIDResponse
|
2022-04-05 07:09:27 +00:00
|
|
|
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &bonds)
|
|
|
|
|
|
|
|
if tc.expErr {
|
2022-10-17 11:03:31 +00:00
|
|
|
sr.Empty(bonds.GetBond().GetID())
|
2022-04-05 07:09:27 +00:00
|
|
|
} else {
|
|
|
|
sr.NoError(err)
|
2022-10-17 11:03:31 +00:00
|
|
|
sr.NotZero(bonds.GetBond().GetID())
|
|
|
|
sr.Equal(bonds.GetBond().GetID(), bondID)
|
2022-04-05 07:09:27 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestSuite) TestGRPCGetBondModuleBalance() {
|
|
|
|
val := s.network.Validators[0]
|
|
|
|
sr := s.Require()
|
2022-10-17 11:03:31 +00:00
|
|
|
reqURL := fmt.Sprintf("%s/vulcanize/bond/v1beta1/balance", val.APIAddress)
|
2022-04-05 07:09:27 +00:00
|
|
|
|
|
|
|
// creating the bond
|
|
|
|
s.createBond()
|
|
|
|
|
|
|
|
s.Run("valid request", func() {
|
2022-10-17 11:03:31 +00:00
|
|
|
resp, err := rest.GetRequest(reqURL)
|
2022-04-05 07:09:27 +00:00
|
|
|
sr.NoError(err)
|
|
|
|
|
|
|
|
var response bondtypes.QueryGetBondModuleBalanceResponse
|
|
|
|
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &response)
|
|
|
|
|
|
|
|
sr.NoError(err)
|
|
|
|
sr.False(response.GetBalance().IsZero())
|
|
|
|
})
|
|
|
|
}
|