github.com/Finschia/finschia-sdk@v0.49.1/x/distribution/client/testutil/grpc.go (about)

     1  package testutil
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/gogo/protobuf/proto"
     7  	"github.com/stretchr/testify/suite"
     8  
     9  	"github.com/Finschia/finschia-sdk/testutil"
    10  	"github.com/Finschia/finschia-sdk/testutil/network"
    11  	"github.com/Finschia/finschia-sdk/testutil/rest"
    12  	sdk "github.com/Finschia/finschia-sdk/types"
    13  	grpctypes "github.com/Finschia/finschia-sdk/types/grpc"
    14  	"github.com/Finschia/finschia-sdk/types/query"
    15  	"github.com/Finschia/finschia-sdk/x/distribution/types"
    16  )
    17  
    18  type GRPCQueryTestSuite struct {
    19  	suite.Suite
    20  
    21  	cfg     network.Config
    22  	network *network.Network
    23  }
    24  
    25  func (s *GRPCQueryTestSuite) SetupSuite() {
    26  	s.T().Log("setting up integration test suite")
    27  
    28  	cfg := network.DefaultConfig()
    29  	cfg.NumValidators = 1
    30  	s.cfg = cfg
    31  
    32  	s.network = network.New(s.T(), s.cfg)
    33  
    34  	_, err := s.network.WaitForHeight(1)
    35  	s.Require().NoError(err)
    36  }
    37  
    38  func (s *GRPCQueryTestSuite) TestQueryParamsGRPC() {
    39  	val := s.network.Validators[0]
    40  	baseURL := val.APIAddress
    41  
    42  	testCases := []struct {
    43  		name     string
    44  		url      string
    45  		respType proto.Message
    46  		expected proto.Message
    47  	}{
    48  		{
    49  			"gRPC request params",
    50  			fmt.Sprintf("%s/cosmos/distribution/v1beta1/params", baseURL),
    51  			&types.QueryParamsResponse{},
    52  			&types.QueryParamsResponse{
    53  				Params: types.DefaultParams(),
    54  			},
    55  		},
    56  	}
    57  
    58  	for _, tc := range testCases {
    59  		resp, err := rest.GetRequest(tc.url)
    60  		s.Run(tc.name, func() {
    61  			s.Require().NoError(err)
    62  			s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType))
    63  			s.Require().Equal(tc.expected, tc.respType)
    64  		})
    65  	}
    66  }
    67  
    68  func (s *GRPCQueryTestSuite) TestQueryOutstandingRewardsGRPC() {
    69  	val := s.network.Validators[0]
    70  	baseURL := val.APIAddress
    71  
    72  	rewards, err := sdk.ParseDecCoins("19.6stake")
    73  	s.Require().NoError(err)
    74  
    75  	testCases := []struct {
    76  		name     string
    77  		url      string
    78  		headers  map[string]string
    79  		expErr   bool
    80  		respType proto.Message
    81  		expected proto.Message
    82  	}{
    83  		{
    84  			"gRPC request params with wrong validator address",
    85  			fmt.Sprintf("%s/cosmos/distribution/v1beta1/validators/%s/outstanding_rewards", baseURL, "wrongAddress"),
    86  			map[string]string{},
    87  			true,
    88  			&types.QueryValidatorOutstandingRewardsResponse{},
    89  			&types.QueryValidatorOutstandingRewardsResponse{},
    90  		},
    91  		{
    92  			"gRPC request params valid address",
    93  			fmt.Sprintf("%s/cosmos/distribution/v1beta1/validators/%s/outstanding_rewards", baseURL, val.ValAddress.String()),
    94  			map[string]string{
    95  				grpctypes.GRPCBlockHeightHeader: "2",
    96  			},
    97  			false,
    98  			&types.QueryValidatorOutstandingRewardsResponse{},
    99  			&types.QueryValidatorOutstandingRewardsResponse{
   100  				Rewards: types.ValidatorOutstandingRewards{
   101  					Rewards: rewards,
   102  				},
   103  			},
   104  		},
   105  	}
   106  
   107  	for _, tc := range testCases {
   108  		resp, err := testutil.GetRequestWithHeaders(tc.url, tc.headers)
   109  		s.Run(tc.name, func() {
   110  			if tc.expErr {
   111  				s.Require().Error(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType))
   112  			} else {
   113  				s.Require().NoError(err)
   114  				s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType))
   115  				s.Require().Equal(tc.expected.String(), tc.respType.String())
   116  			}
   117  		})
   118  	}
   119  }
   120  
   121  func (s *GRPCQueryTestSuite) TestQueryValidatorCommissionGRPC() {
   122  	val := s.network.Validators[0]
   123  	baseURL := val.APIAddress
   124  
   125  	commission, err := sdk.ParseDecCoins("9.8stake")
   126  	s.Require().NoError(err)
   127  
   128  	testCases := []struct {
   129  		name     string
   130  		url      string
   131  		headers  map[string]string
   132  		expErr   bool
   133  		respType proto.Message
   134  		expected proto.Message
   135  	}{
   136  		{
   137  			"gRPC request params with wrong validator address",
   138  			fmt.Sprintf("%s/cosmos/distribution/v1beta1/validators/%s/commission", baseURL, "wrongAddress"),
   139  			map[string]string{},
   140  			true,
   141  			&types.QueryValidatorCommissionResponse{},
   142  			&types.QueryValidatorCommissionResponse{},
   143  		},
   144  		{
   145  			"gRPC request params valid address",
   146  			fmt.Sprintf("%s/cosmos/distribution/v1beta1/validators/%s/commission", baseURL, val.ValAddress.String()),
   147  			map[string]string{
   148  				grpctypes.GRPCBlockHeightHeader: "2",
   149  			},
   150  			false,
   151  			&types.QueryValidatorCommissionResponse{},
   152  			&types.QueryValidatorCommissionResponse{
   153  				Commission: types.ValidatorAccumulatedCommission{
   154  					Commission: commission,
   155  				},
   156  			},
   157  		},
   158  	}
   159  
   160  	for _, tc := range testCases {
   161  		resp, err := testutil.GetRequestWithHeaders(tc.url, tc.headers)
   162  		s.Run(tc.name, func() {
   163  			if tc.expErr {
   164  				s.Require().Error(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType))
   165  			} else {
   166  				s.Require().NoError(err)
   167  				s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType))
   168  				s.Require().Equal(tc.expected.String(), tc.respType.String())
   169  			}
   170  		})
   171  	}
   172  }
   173  
   174  func (s *GRPCQueryTestSuite) TestQuerySlashesGRPC() {
   175  	val := s.network.Validators[0]
   176  	baseURL := val.APIAddress
   177  
   178  	testCases := []struct {
   179  		name     string
   180  		url      string
   181  		expErr   bool
   182  		respType proto.Message
   183  		expected proto.Message
   184  	}{
   185  		{
   186  			"invalid validator address",
   187  			fmt.Sprintf("%s/cosmos/distribution/v1beta1/validators/%s/slashes", baseURL, ""),
   188  			true,
   189  			&types.QueryValidatorSlashesResponse{},
   190  			nil,
   191  		},
   192  		{
   193  			"invalid start height",
   194  			fmt.Sprintf("%s/cosmos/distribution/v1beta1/validators/%s/slashes?starting_height=%s&ending_height=%s", baseURL, val.ValAddress.String(), "-1", "3"),
   195  			true,
   196  			&types.QueryValidatorSlashesResponse{},
   197  			nil,
   198  		},
   199  		{
   200  			"invalid start height",
   201  			fmt.Sprintf("%s/cosmos/distribution/v1beta1/validators/%s/slashes?starting_height=%s&ending_height=%s", baseURL, val.ValAddress.String(), "1", "-3"),
   202  			true,
   203  			&types.QueryValidatorSlashesResponse{},
   204  			nil,
   205  		},
   206  		{
   207  			"valid request get slashes",
   208  			fmt.Sprintf("%s/cosmos/distribution/v1beta1/validators/%s/slashes?starting_height=%s&ending_height=%s", baseURL, val.ValAddress.String(), "1", "3"),
   209  			false,
   210  			&types.QueryValidatorSlashesResponse{},
   211  			&types.QueryValidatorSlashesResponse{
   212  				Pagination: &query.PageResponse{},
   213  			},
   214  		},
   215  	}
   216  
   217  	for _, tc := range testCases {
   218  		resp, err := rest.GetRequest(tc.url)
   219  
   220  		s.Run(tc.name, func() {
   221  			if tc.expErr {
   222  				s.Require().Error(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType))
   223  			} else {
   224  				s.Require().NoError(err)
   225  				s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType))
   226  				s.Require().Equal(tc.expected.String(), tc.respType.String())
   227  			}
   228  		})
   229  	}
   230  }
   231  
   232  func (s *GRPCQueryTestSuite) TestQueryDelegatorRewardsGRPC() {
   233  	val := s.network.Validators[0]
   234  	baseURL := val.APIAddress
   235  
   236  	rewards, err := sdk.ParseDecCoins("9.8stake")
   237  	s.Require().NoError(err)
   238  
   239  	testCases := []struct {
   240  		name     string
   241  		url      string
   242  		headers  map[string]string
   243  		expErr   bool
   244  		respType proto.Message
   245  		expected proto.Message
   246  	}{
   247  		{
   248  			"wrong delegator address",
   249  			fmt.Sprintf("%s/cosmos/distribution/v1beta1/delegators/%s/rewards", baseURL, "wrongDelegatorAddress"),
   250  			map[string]string{},
   251  			true,
   252  			&types.QueryDelegationTotalRewardsResponse{},
   253  			nil,
   254  		},
   255  		{
   256  			"valid request",
   257  			fmt.Sprintf("%s/cosmos/distribution/v1beta1/delegators/%s/rewards", baseURL, val.Address.String()),
   258  			map[string]string{
   259  				grpctypes.GRPCBlockHeightHeader: "2",
   260  			},
   261  			false,
   262  			&types.QueryDelegationTotalRewardsResponse{},
   263  			&types.QueryDelegationTotalRewardsResponse{
   264  				Rewards: []types.DelegationDelegatorReward{
   265  					types.NewDelegationDelegatorReward(val.ValAddress, rewards),
   266  				},
   267  				Total: rewards,
   268  			},
   269  		},
   270  		{
   271  			"wrong validator address(specific validator rewards)",
   272  			fmt.Sprintf("%s/cosmos/distribution/v1beta1/delegators/%s/rewards/%s", baseURL, val.Address.String(), "wrongValAddress"),
   273  			map[string]string{},
   274  			true,
   275  			&types.QueryDelegationTotalRewardsResponse{},
   276  			nil,
   277  		},
   278  		{
   279  			"valid request(specific validator rewards)",
   280  			fmt.Sprintf("%s/cosmos/distribution/v1beta1/delegators/%s/rewards/%s", baseURL, val.Address.String(), val.ValAddress.String()),
   281  			map[string]string{
   282  				grpctypes.GRPCBlockHeightHeader: "2",
   283  			},
   284  			false,
   285  			&types.QueryDelegationRewardsResponse{},
   286  			&types.QueryDelegationRewardsResponse{
   287  				Rewards: rewards,
   288  			},
   289  		},
   290  	}
   291  
   292  	for _, tc := range testCases {
   293  		resp, err := testutil.GetRequestWithHeaders(tc.url, tc.headers)
   294  
   295  		s.Run(tc.name, func() {
   296  			if tc.expErr {
   297  				s.Require().Error(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType))
   298  			} else {
   299  				s.Require().NoError(err)
   300  				s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType))
   301  				s.Require().Equal(tc.expected.String(), tc.respType.String())
   302  			}
   303  		})
   304  	}
   305  }
   306  
   307  func (s *GRPCQueryTestSuite) TestQueryDelegatorValidatorsGRPC() {
   308  	val := s.network.Validators[0]
   309  	baseURL := val.APIAddress
   310  
   311  	testCases := []struct {
   312  		name     string
   313  		url      string
   314  		expErr   bool
   315  		respType proto.Message
   316  		expected proto.Message
   317  	}{
   318  		{
   319  			"empty delegator address",
   320  			fmt.Sprintf("%s/cosmos/distribution/v1beta1/delegators/%s/validators", baseURL, ""),
   321  			true,
   322  			&types.QueryDelegatorValidatorsResponse{},
   323  			nil,
   324  		},
   325  		{
   326  			"wrong delegator address",
   327  			fmt.Sprintf("%s/cosmos/distribution/v1beta1/delegators/%s/validators", baseURL, "wrongDelegatorAddress"),
   328  			true,
   329  			&types.QueryDelegatorValidatorsResponse{},
   330  			nil,
   331  		},
   332  		{
   333  			"valid request",
   334  			fmt.Sprintf("%s/cosmos/distribution/v1beta1/delegators/%s/validators", baseURL, val.Address.String()),
   335  			false,
   336  			&types.QueryDelegatorValidatorsResponse{},
   337  			&types.QueryDelegatorValidatorsResponse{
   338  				Validators: []string{val.ValAddress.String()},
   339  			},
   340  		},
   341  	}
   342  
   343  	for _, tc := range testCases {
   344  		resp, err := rest.GetRequest(tc.url)
   345  
   346  		s.Run(tc.name, func() {
   347  			if tc.expErr {
   348  				s.Require().Error(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType))
   349  			} else {
   350  				s.Require().NoError(err)
   351  				s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType))
   352  				s.Require().Equal(tc.expected.String(), tc.respType.String())
   353  			}
   354  		})
   355  	}
   356  }
   357  
   358  func (s *GRPCQueryTestSuite) TestQueryWithdrawAddressGRPC() {
   359  	val := s.network.Validators[0]
   360  	baseURL := val.APIAddress
   361  
   362  	testCases := []struct {
   363  		name     string
   364  		url      string
   365  		expErr   bool
   366  		respType proto.Message
   367  		expected proto.Message
   368  	}{
   369  		{
   370  			"empty delegator address",
   371  			fmt.Sprintf("%s/cosmos/distribution/v1beta1/delegators/%s/withdraw_address", baseURL, ""),
   372  			true,
   373  			&types.QueryDelegatorWithdrawAddressResponse{},
   374  			nil,
   375  		},
   376  		{
   377  			"wrong delegator address",
   378  			fmt.Sprintf("%s/cosmos/distribution/v1beta1/delegators/%s/withdraw_address", baseURL, "wrongDelegatorAddress"),
   379  			true,
   380  			&types.QueryDelegatorWithdrawAddressResponse{},
   381  			nil,
   382  		},
   383  		{
   384  			"valid request",
   385  			fmt.Sprintf("%s/cosmos/distribution/v1beta1/delegators/%s/withdraw_address", baseURL, val.Address.String()),
   386  			false,
   387  			&types.QueryDelegatorWithdrawAddressResponse{},
   388  			&types.QueryDelegatorWithdrawAddressResponse{
   389  				WithdrawAddress: val.Address.String(),
   390  			},
   391  		},
   392  	}
   393  
   394  	for _, tc := range testCases {
   395  		resp, err := rest.GetRequest(tc.url)
   396  
   397  		s.Run(tc.name, func() {
   398  			if tc.expErr {
   399  				s.Require().Error(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType))
   400  			} else {
   401  				s.Require().NoError(err)
   402  				s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType))
   403  				s.Require().Equal(tc.expected.String(), tc.respType.String())
   404  			}
   405  		})
   406  	}
   407  }
   408  
   409  func (s *GRPCQueryTestSuite) TestQueryValidatorCommunityPoolGRPC() {
   410  	val := s.network.Validators[0]
   411  	baseURL := val.APIAddress
   412  
   413  	communityPool, err := sdk.ParseDecCoins("0.4stake")
   414  	s.Require().NoError(err)
   415  
   416  	testCases := []struct {
   417  		name     string
   418  		url      string
   419  		headers  map[string]string
   420  		expErr   bool
   421  		respType proto.Message
   422  		expected proto.Message
   423  	}{
   424  		{
   425  			"gRPC request params with wrong validator address",
   426  			fmt.Sprintf("%s/cosmos/distribution/v1beta1/community_pool", baseURL),
   427  			map[string]string{
   428  				grpctypes.GRPCBlockHeightHeader: "2",
   429  			},
   430  			false,
   431  			&types.QueryCommunityPoolResponse{},
   432  			&types.QueryCommunityPoolResponse{
   433  				Pool: communityPool,
   434  			},
   435  		},
   436  	}
   437  
   438  	for _, tc := range testCases {
   439  		resp, err := testutil.GetRequestWithHeaders(tc.url, tc.headers)
   440  
   441  		s.Run(tc.name, func() {
   442  			if tc.expErr {
   443  				s.Require().Error(err)
   444  			} else {
   445  				s.Require().NoError(err)
   446  				s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType))
   447  				s.Require().Equal(tc.expected.String(), tc.respType.String())
   448  			}
   449  		})
   450  	}
   451  }