laconicd/x/nameservice/client/testutil/grpc.go

648 lines
15 KiB
Go
Raw Normal View History

package testutil
import (
"encoding/json"
"fmt"
2022-04-20 07:37:38 +00:00
"os"
"time"
"github.com/cerc-io/laconicd/x/nameservice/client/cli"
nstypes "github.com/cerc-io/laconicd/x/nameservice/types"
"github.com/cosmos/cosmos-sdk/client/flags"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
2022-04-23 15:53:51 +00:00
"github.com/cosmos/cosmos-sdk/testutil/rest"
sdk "github.com/cosmos/cosmos-sdk/types"
tmcli "github.com/tendermint/tendermint/libs/cli"
)
func (s *IntegrationTestSuite) TestGRPCQueryParams() {
val := s.network.Validators[0]
sr := s.Require()
2022-10-17 11:03:31 +00:00
reqURL := val.APIAddress + "/vulcanize/nameservice/v1beta1/params"
testCases := []struct {
name string
url string
expectErr bool
errorMsg string
}{
{
"invalid url",
2022-10-17 11:03:31 +00:00
reqURL + "/asdasd",
true,
"",
},
{
"Success",
2022-10-17 11:03:31 +00:00
reqURL,
false,
"",
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
resp, _ := rest.GetRequest(tc.url)
require := s.Require()
if tc.expectErr {
require.Contains(string(resp), tc.errorMsg)
} else {
var response nstypes.QueryParamsResponse
err := val.ClientCtx.Codec.UnmarshalJSON(resp, &response)
sr.NoError(err)
params := nstypes.DefaultParams()
params.RecordRent = sdk.NewCoin(s.cfg.BondDenom, nstypes.DefaultRecordRent)
params.RecordRentDuration = 10 * time.Second
params.AuthorityGracePeriod = 10 * time.Second
sr.Equal(response.GetParams().String(), params.String())
}
})
}
}
2022-10-17 06:47:56 +00:00
//nolint: all
func (s *IntegrationTestSuite) TestGRPCQueryWhoIs() {
val := s.network.Validators[0]
sr := s.Require()
reqUrl := val.APIAddress + "/vulcanize/nameservice/v1beta1/whois/%s"
2022-10-17 06:47:56 +00:00
authorityName := "QueryWhoIS"
testCases := []struct {
name string
url string
expectErr bool
errorMsg string
preRun func(authorityName string)
}{
{
"invalid url",
reqUrl + "/asdasd",
true,
"",
func(authorityName string) {
},
},
{
"Success",
reqUrl,
false,
"",
func(authorityName string) {
clientCtx := val.ClientCtx
cmd := cli.GetCmdReserveName()
args := []string{
authorityName,
fmt.Sprintf("--owner=%s", accountAddress),
fmt.Sprintf("--%s=%s", flags.FlagFrom, accountName),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, fmt.Sprintf("3%s", s.cfg.BondDenom)),
}
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
sr.NoError(err)
var d sdk.TxResponse
err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &d)
sr.NoError(err)
sr.Zero(d.Code)
},
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
if !tc.expectErr {
tc.preRun(authorityName)
tc.url = fmt.Sprintf(tc.url, authorityName)
}
resp, _ := rest.GetRequest(tc.url)
require := s.Require()
if tc.expectErr {
require.Contains(string(resp), tc.errorMsg)
} else {
var response nstypes.QueryWhoisResponse
err := val.ClientCtx.Codec.UnmarshalJSON(resp, &response)
sr.NoError(err)
sr.Equal(nstypes.AuthorityActive, response.GetNameAuthority().Status)
}
})
}
}
func (s *IntegrationTestSuite) TestGRPCQueryLookup() {
val := s.network.Validators[0]
sr := s.Require()
2022-10-17 11:03:31 +00:00
reqURL := val.APIAddress + "/vulcanize/nameservice/v1beta1/lookup?crn=%s"
2022-10-17 06:47:56 +00:00
authorityName := "QueryLookUp"
testCases := []struct {
name string
url string
expectErr bool
errorMsg string
preRun func(authorityName string)
}{
{
"invalid url",
2022-10-17 11:03:31 +00:00
reqURL + "/asdasd",
true,
"",
func(authorityName string) {
},
},
{
"Success",
2022-10-17 11:03:31 +00:00
reqURL,
false,
"",
func(authorityName string) {
// create name record
createNameRecord(authorityName, s)
},
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
if !tc.expectErr {
tc.preRun(authorityName)
2022-10-17 11:03:31 +00:00
tc.url = fmt.Sprintf(reqURL, fmt.Sprintf("crn://%s/", authorityName))
}
resp, _ := rest.GetRequest(tc.url)
if tc.expectErr {
sr.Contains(string(resp), tc.errorMsg)
} else {
2022-04-20 07:37:38 +00:00
var response nstypes.QueryLookupCrnResponse
err := val.ClientCtx.Codec.UnmarshalJSON(resp, &response)
sr.NoError(err)
sr.NotZero(len(response.Name.Latest.Id))
}
})
}
}
2022-10-17 06:47:56 +00:00
//nolint: all
func (s *IntegrationTestSuite) TestGRPCQueryRecordExpiryQueue() {
val := s.network.Validators[0]
sr := s.Require()
reqUrl := val.APIAddress + "/vulcanize/nameservice/v1beta1/record-expiry"
testCases := []struct {
name string
url string
expectErr bool
errorMsg string
preRun func(bondId string)
}{
{
"invalid url",
reqUrl + "/asdasd",
true,
"",
func(bondId string) {
},
},
{
"Success",
reqUrl,
false,
"",
func(bondId string) {
dir, err := os.Getwd()
sr.NoError(err)
payloadPath := dir + "/example1.yml"
args := []string{
fmt.Sprintf("--%s=%s", flags.FlagFrom, accountName),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, fmt.Sprintf("3%s", s.cfg.BondDenom)),
}
args = append([]string{payloadPath, bondId}, args...)
clientCtx := val.ClientCtx
cmd := cli.GetCmdSetRecord()
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
sr.NoError(err)
var d sdk.TxResponse
err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &d)
sr.NoError(err)
sr.Zero(d.Code)
},
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
if !tc.expectErr {
2022-10-17 11:03:31 +00:00
tc.preRun(s.bondID)
}
// wait 12 seconds for records expires
time.Sleep(time.Second * 12)
resp, _ := rest.GetRequest(tc.url)
require := s.Require()
if tc.expectErr {
require.Contains(string(resp), tc.errorMsg)
} else {
var response nstypes.QueryGetRecordExpiryQueueResponse
err := val.ClientCtx.Codec.UnmarshalJSON(resp, &response)
sr.NoError(err)
sr.NotZero(len(response.GetRecords()))
}
})
}
}
2022-10-17 06:47:56 +00:00
//nolint: all
func (s *IntegrationTestSuite) TestGRPCQueryAuthorityExpiryQueue() {
val := s.network.Validators[0]
sr := s.Require()
reqUrl := val.APIAddress + "/vulcanize/nameservice/v1beta1/authority-expiry"
testCases := []struct {
name string
url string
expectErr bool
errorMsg string
preRun func(authorityName string)
}{
{
"invalid url",
reqUrl + "/asdasd",
true,
"",
func(authorityName string) {
},
},
{
"Success",
reqUrl,
false,
"",
func(authorityName string) {
// reserving the name
clientCtx := val.ClientCtx
cmd := cli.GetCmdReserveName()
args := []string{
authorityName,
fmt.Sprintf("--owner=%s", accountAddress),
fmt.Sprintf("--%s=%s", flags.FlagFrom, accountName),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, fmt.Sprintf("3%s", s.cfg.BondDenom)),
}
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
sr.NoError(err)
var d sdk.TxResponse
err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &d)
sr.NoError(err)
sr.Zero(d.Code)
},
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
if !tc.expectErr {
tc.preRun("QueryAuthorityExpiryQueue")
}
// wait 12 seconds to name authorites expires
time.Sleep(time.Second * 12)
resp, _ := rest.GetRequest(tc.url)
require := s.Require()
if tc.expectErr {
require.Contains(string(resp), tc.errorMsg)
} else {
var response nstypes.QueryGetAuthorityExpiryQueueResponse
err := val.ClientCtx.Codec.UnmarshalJSON(resp, &response)
sr.NoError(err)
// removed from expiry queue as no bond set
sr.Zero(len(response.GetAuthorities()))
}
})
}
}
2022-10-17 06:47:56 +00:00
//nolint: all
func (s *IntegrationTestSuite) TestGRPCQueryListRecords() {
val := s.network.Validators[0]
sr := s.Require()
reqUrl := val.APIAddress + "/vulcanize/nameservice/v1beta1/records"
testCases := []struct {
name string
url string
expectErr bool
errorMsg string
preRun func(bondId string)
}{
{
"invalid url",
reqUrl + "/asdasd",
true,
"",
func(bondId string) {
},
},
{
"Success",
reqUrl,
false,
"",
func(bondId string) {
dir, err := os.Getwd()
sr.NoError(err)
payloadPath := dir + "/example1.yml"
args := []string{
fmt.Sprintf("--%s=%s", flags.FlagFrom, accountName),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, fmt.Sprintf("3%s", s.cfg.BondDenom)),
}
args = append([]string{payloadPath, bondId}, args...)
clientCtx := val.ClientCtx
cmd := cli.GetCmdSetRecord()
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
sr.NoError(err)
var d sdk.TxResponse
err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &d)
sr.NoError(err)
sr.Zero(d.Code)
},
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
if !tc.expectErr {
2022-10-17 11:03:31 +00:00
tc.preRun(s.bondID)
}
resp, _ := rest.GetRequest(tc.url)
require := s.Require()
if tc.expectErr {
require.Contains(string(resp), tc.errorMsg)
} else {
var response nstypes.QueryListRecordsResponse
err := val.ClientCtx.Codec.UnmarshalJSON(resp, &response)
sr.NoError(err)
sr.NotZero(len(response.GetRecords()))
2022-10-17 11:03:31 +00:00
sr.Equal(s.bondID, response.GetRecords()[0].GetBondId())
}
})
}
}
func (s *IntegrationTestSuite) TestGRPCQueryGetRecordByID() {
val := s.network.Validators[0]
sr := s.Require()
2022-10-17 11:03:31 +00:00
reqURL := val.APIAddress + "/vulcanize/nameservice/v1beta1/records/%s"
testCases := []struct {
name string
url string
expectErr bool
errorMsg string
preRun func(bondId string) string
}{
{
"invalid url",
2022-10-17 11:03:31 +00:00
reqURL + "/asdasd",
true,
"",
func(bondId string) string {
return ""
},
},
{
"Success",
2022-10-17 11:03:31 +00:00
reqURL,
false,
"",
func(bondId string) string {
// creating the record
createRecord(bondId, s)
// list the records
clientCtx := val.ClientCtx
cmd := cli.GetCmdList()
args := []string{
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
}
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
sr.NoError(err)
var records []nstypes.RecordType
err = json.Unmarshal(out.Bytes(), &records)
sr.NoError(err)
2022-10-17 11:03:31 +00:00
return records[0].ID
},
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
2022-10-17 11:03:31 +00:00
var recordID string
if !tc.expectErr {
2022-10-17 11:03:31 +00:00
recordID = tc.preRun(s.bondID)
tc.url = fmt.Sprintf(reqURL, recordID)
}
resp, _ := rest.GetRequest(tc.url)
require := s.Require()
if tc.expectErr {
require.Contains(string(resp), tc.errorMsg)
} else {
2022-10-17 11:03:31 +00:00
var response nstypes.QueryRecordByIDResponse
err := val.ClientCtx.Codec.UnmarshalJSON(resp, &response)
sr.NoError(err)
record := response.GetRecord()
2022-10-17 11:03:31 +00:00
sr.NotZero(len(record.GetID()))
sr.Equal(record.GetID(), recordID)
}
})
}
}
func (s *IntegrationTestSuite) TestGRPCQueryGetRecordByBondID() {
val := s.network.Validators[0]
sr := s.Require()
2022-10-17 11:03:31 +00:00
reqURL := val.APIAddress + "/vulcanize/nameservice/v1beta1/records-by-bond-id/%s"
testCases := []struct {
name string
url string
expectErr bool
errorMsg string
preRun func(bondId string)
}{
{
"invalid url",
2022-10-17 11:03:31 +00:00
reqURL + "/asdasd",
true,
"",
func(bondId string) {
},
},
{
"Success",
2022-10-17 11:03:31 +00:00
reqURL,
false,
"",
func(bondId string) {
// creating the record
createRecord(bondId, s)
},
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
if !tc.expectErr {
2022-10-17 11:03:31 +00:00
tc.preRun(s.bondID)
tc.url = fmt.Sprintf(reqURL, s.bondID)
}
resp, _ := rest.GetRequest(tc.url)
require := s.Require()
if tc.expectErr {
require.Contains(string(resp), tc.errorMsg)
} else {
2022-10-17 11:03:31 +00:00
var response nstypes.QueryRecordByBondIDResponse
err := val.ClientCtx.Codec.UnmarshalJSON(resp, &response)
sr.NoError(err)
records := response.GetRecords()
sr.NotZero(len(records))
2022-10-17 11:03:31 +00:00
sr.Equal(records[0].GetBondId(), s.bondID)
}
})
}
}
func (s *IntegrationTestSuite) TestGRPCQueryGetNameServiceModuleBalance() {
val := s.network.Validators[0]
sr := s.Require()
2022-10-17 11:03:31 +00:00
reqURL := val.APIAddress + "/vulcanize/nameservice/v1beta1/balance"
testCases := []struct {
name string
url string
expectErr bool
errorMsg string
preRun func(bondId string)
}{
{
"invalid url",
2022-10-17 11:03:31 +00:00
reqURL + "/asdasd",
true,
"",
func(bondId string) {
},
},
{
"Success",
2022-10-17 11:03:31 +00:00
reqURL,
false,
"",
func(bondId string) {
// creating the record
createRecord(bondId, s)
},
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
if !tc.expectErr {
2022-10-17 11:03:31 +00:00
tc.preRun(s.bondID)
}
resp, _ := rest.GetRequest(tc.url)
require := s.Require()
if tc.expectErr {
require.Contains(string(resp), tc.errorMsg)
} else {
var response nstypes.GetNameServiceModuleBalanceResponse
err := val.ClientCtx.Codec.UnmarshalJSON(resp, &response)
sr.NoError(err)
sr.NotZero(len(response.GetBalances()))
}
})
}
}
func (s *IntegrationTestSuite) TestGRPCQueryNamesList() {
val := s.network.Validators[0]
sr := s.Require()
2022-10-17 11:03:31 +00:00
reqURL := val.APIAddress + "/vulcanize/nameservice/v1beta1/names"
testCases := []struct {
name string
url string
expectErr bool
errorMsg string
preRun func(authorityName string)
}{
{
"invalid url",
2022-10-17 11:03:31 +00:00
reqURL + "/asdasd",
true,
"",
func(authorityName string) {
},
},
{
"Success",
2022-10-17 11:03:31 +00:00
reqURL,
false,
"",
func(authorityName string) {
// create name record
createNameRecord(authorityName, s)
},
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
if !tc.expectErr {
tc.preRun("ListNameRecords")
}
resp, _ := rest.GetRequest(tc.url)
require := s.Require()
if tc.expectErr {
require.Contains(string(resp), tc.errorMsg)
} else {
var response nstypes.QueryListNameRecordsResponse
err := val.ClientCtx.Codec.UnmarshalJSON(resp, &response)
sr.NoError(err)
sr.NotZero(len(response.GetNames()))
}
})
}
}
2022-10-17 11:03:31 +00:00
func createRecord(bondID string, s *IntegrationTestSuite) {
val := s.network.Validators[0]
sr := s.Require()
dir, err := os.Getwd()
sr.NoError(err)
payloadPath := dir + "/example1.yml"
args := []string{
fmt.Sprintf("--%s=%s", flags.FlagFrom, accountName),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, fmt.Sprintf("3%s", s.cfg.BondDenom)),
}
2022-10-17 11:03:31 +00:00
args = append([]string{payloadPath, bondID}, args...)
clientCtx := val.ClientCtx
cmd := cli.GetCmdSetRecord()
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
sr.NoError(err)
var d sdk.TxResponse
err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &d)
sr.NoError(err)
sr.Zero(d.Code)
}