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

     1  package testutil
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/gogo/protobuf/proto"
     7  
     8  	"github.com/Finschia/finschia-sdk/testutil"
     9  	"github.com/Finschia/finschia-sdk/testutil/rest"
    10  	sdk "github.com/Finschia/finschia-sdk/types"
    11  	grpctypes "github.com/Finschia/finschia-sdk/types/grpc"
    12  	"github.com/Finschia/finschia-sdk/x/gov/types"
    13  )
    14  
    15  func (s *IntegrationTestSuite) TestGetProposalGRPC() {
    16  	val := s.network.Validators[0]
    17  
    18  	testCases := []struct {
    19  		name   string
    20  		url    string
    21  		expErr bool
    22  	}{
    23  		{
    24  			"empty proposal",
    25  			fmt.Sprintf("%s/cosmos/gov/v1beta1/proposals/%s", val.APIAddress, ""),
    26  			true,
    27  		},
    28  		{
    29  			"get non existing proposal",
    30  			fmt.Sprintf("%s/cosmos/gov/v1beta1/proposals/%s", val.APIAddress, "10"),
    31  			true,
    32  		},
    33  		{
    34  			"get proposal with id",
    35  			fmt.Sprintf("%s/cosmos/gov/v1beta1/proposals/%s", val.APIAddress, "1"),
    36  			false,
    37  		},
    38  	}
    39  
    40  	for _, tc := range testCases {
    41  		s.Run(tc.name, func() {
    42  			resp, err := rest.GetRequest(tc.url)
    43  			s.Require().NoError(err)
    44  
    45  			var proposal types.QueryProposalResponse
    46  			err = val.ClientCtx.Codec.UnmarshalJSON(resp, &proposal)
    47  
    48  			if tc.expErr {
    49  				s.Require().Error(err)
    50  			} else {
    51  				s.Require().NoError(err)
    52  				s.Require().NotNil(proposal.Proposal)
    53  			}
    54  		})
    55  	}
    56  }
    57  
    58  func (s *IntegrationTestSuite) TestGetProposalsGRPC() {
    59  	val := s.network.Validators[0]
    60  
    61  	testCases := []struct {
    62  		name             string
    63  		url              string
    64  		headers          map[string]string
    65  		wantNumProposals int
    66  		expErr           bool
    67  	}{
    68  		{
    69  			"get proposals with height 1",
    70  			fmt.Sprintf("%s/cosmos/gov/v1beta1/proposals", val.APIAddress),
    71  			map[string]string{
    72  				grpctypes.GRPCBlockHeightHeader: "1",
    73  			},
    74  			0,
    75  			true,
    76  		},
    77  		{
    78  			"valid request",
    79  			fmt.Sprintf("%s/cosmos/gov/v1beta1/proposals", val.APIAddress),
    80  			map[string]string{},
    81  			3,
    82  			false,
    83  		},
    84  		{
    85  			"valid request with filter by status",
    86  			fmt.Sprintf("%s/cosmos/gov/v1beta1/proposals?proposal_status=1", val.APIAddress),
    87  			map[string]string{},
    88  			1,
    89  			false,
    90  		},
    91  	}
    92  
    93  	for _, tc := range testCases {
    94  		s.Run(tc.name, func() {
    95  			resp, err := testutil.GetRequestWithHeaders(tc.url, tc.headers)
    96  			s.Require().NoError(err)
    97  
    98  			var proposals types.QueryProposalsResponse
    99  			err = val.ClientCtx.Codec.UnmarshalJSON(resp, &proposals)
   100  
   101  			if tc.expErr {
   102  				s.Require().Empty(proposals.Proposals)
   103  			} else {
   104  				s.Require().NoError(err)
   105  				s.Require().Len(proposals.Proposals, tc.wantNumProposals)
   106  			}
   107  		})
   108  	}
   109  }
   110  
   111  func (s *IntegrationTestSuite) TestGetProposalVoteGRPC() {
   112  	val := s.network.Validators[0]
   113  
   114  	voterAddressBech32 := val.Address.String()
   115  
   116  	testCases := []struct {
   117  		name           string
   118  		url            string
   119  		expErr         bool
   120  		expVoteOptions types.WeightedVoteOptions
   121  	}{
   122  		{
   123  			"empty proposal",
   124  			fmt.Sprintf("%s/cosmos/gov/v1beta1/proposals/%s/votes/%s", val.APIAddress, "", voterAddressBech32),
   125  			true,
   126  			types.NewNonSplitVoteOption(types.OptionYes),
   127  		},
   128  		{
   129  			"get non existing proposal",
   130  			fmt.Sprintf("%s/cosmos/gov/v1beta1/proposals/%s/votes/%s", val.APIAddress, "10", voterAddressBech32),
   131  			true,
   132  			types.NewNonSplitVoteOption(types.OptionYes),
   133  		},
   134  		{
   135  			"get proposal with wrong voter address",
   136  			fmt.Sprintf("%s/cosmos/gov/v1beta1/proposals/%s/votes/%s", val.APIAddress, "1", "wrongVoterAddress"),
   137  			true,
   138  			types.NewNonSplitVoteOption(types.OptionYes),
   139  		},
   140  		{
   141  			"get proposal with id",
   142  			fmt.Sprintf("%s/cosmos/gov/v1beta1/proposals/%s/votes/%s", val.APIAddress, "1", voterAddressBech32),
   143  			false,
   144  			types.NewNonSplitVoteOption(types.OptionYes),
   145  		},
   146  		{
   147  			"get proposal with id for split vote",
   148  			fmt.Sprintf("%s/cosmos/gov/v1beta1/proposals/%s/votes/%s", val.APIAddress, "3", voterAddressBech32),
   149  			false,
   150  			types.WeightedVoteOptions{
   151  				types.WeightedVoteOption{Option: types.OptionYes, Weight: sdk.NewDecWithPrec(60, 2)},
   152  				types.WeightedVoteOption{Option: types.OptionNo, Weight: sdk.NewDecWithPrec(30, 2)},
   153  				types.WeightedVoteOption{Option: types.OptionAbstain, Weight: sdk.NewDecWithPrec(5, 2)},
   154  				types.WeightedVoteOption{Option: types.OptionNoWithVeto, Weight: sdk.NewDecWithPrec(5, 2)},
   155  			},
   156  		},
   157  	}
   158  
   159  	for _, tc := range testCases {
   160  		s.Run(tc.name, func() {
   161  			resp, err := rest.GetRequest(tc.url)
   162  			s.Require().NoError(err)
   163  
   164  			var vote types.QueryVoteResponse
   165  			err = val.ClientCtx.Codec.UnmarshalJSON(resp, &vote)
   166  
   167  			if tc.expErr {
   168  				s.Require().Error(err)
   169  			} else {
   170  				s.Require().NoError(err)
   171  				s.Require().NotEmpty(vote.Vote)
   172  				s.Require().Equal(len(vote.Vote.Options), len(tc.expVoteOptions))
   173  				for i, option := range tc.expVoteOptions {
   174  					s.Require().Equal(option.Option, vote.Vote.Options[i].Option)
   175  					s.Require().True(option.Weight.Equal(vote.Vote.Options[i].Weight))
   176  				}
   177  			}
   178  		})
   179  	}
   180  }
   181  
   182  func (s *IntegrationTestSuite) TestGetProposalVotesGRPC() {
   183  	val := s.network.Validators[0]
   184  
   185  	testCases := []struct {
   186  		name   string
   187  		url    string
   188  		expErr bool
   189  	}{
   190  		{
   191  			"votes with empty proposal id",
   192  			fmt.Sprintf("%s/cosmos/gov/v1beta1/proposals/%s/votes", val.APIAddress, ""),
   193  			true,
   194  		},
   195  		{
   196  			"get votes with valid id",
   197  			fmt.Sprintf("%s/cosmos/gov/v1beta1/proposals/%s/votes", val.APIAddress, "1"),
   198  			false,
   199  		},
   200  	}
   201  
   202  	for _, tc := range testCases {
   203  		s.Run(tc.name, func() {
   204  			resp, err := rest.GetRequest(tc.url)
   205  			s.Require().NoError(err)
   206  
   207  			var votes types.QueryVotesResponse
   208  			err = val.ClientCtx.Codec.UnmarshalJSON(resp, &votes)
   209  
   210  			if tc.expErr {
   211  				s.Require().Error(err)
   212  			} else {
   213  				s.Require().NoError(err)
   214  				s.Require().Len(votes.Votes, 1)
   215  			}
   216  		})
   217  	}
   218  }
   219  
   220  func (s *IntegrationTestSuite) TestGetProposalDepositGRPC() {
   221  	val := s.network.Validators[0]
   222  
   223  	testCases := []struct {
   224  		name   string
   225  		url    string
   226  		expErr bool
   227  	}{
   228  		{
   229  			"get deposit with empty proposal id",
   230  			fmt.Sprintf("%s/cosmos/gov/v1beta1/proposals/%s/deposits/%s", val.APIAddress, "", val.Address.String()),
   231  			true,
   232  		},
   233  		{
   234  			"get deposit of non existing proposal",
   235  			fmt.Sprintf("%s/cosmos/gov/v1beta1/proposals/%s/deposits/%s", val.APIAddress, "10", val.Address.String()),
   236  			true,
   237  		},
   238  		{
   239  			"get deposit with wrong depositer address",
   240  			fmt.Sprintf("%s/cosmos/gov/v1beta1/proposals/%s/deposits/%s", val.APIAddress, "1", "wrongDepositerAddress"),
   241  			true,
   242  		},
   243  		{
   244  			"get deposit valid request",
   245  			fmt.Sprintf("%s/cosmos/gov/v1beta1/proposals/%s/deposits/%s", val.APIAddress, "1", val.Address.String()),
   246  			false,
   247  		},
   248  	}
   249  
   250  	for _, tc := range testCases {
   251  		s.Run(tc.name, func() {
   252  			resp, err := rest.GetRequest(tc.url)
   253  			s.Require().NoError(err)
   254  
   255  			var deposit types.QueryDepositResponse
   256  			err = val.ClientCtx.Codec.UnmarshalJSON(resp, &deposit)
   257  
   258  			if tc.expErr {
   259  				s.Require().Error(err)
   260  			} else {
   261  				s.Require().NoError(err)
   262  				s.Require().NotEmpty(deposit.Deposit)
   263  			}
   264  		})
   265  	}
   266  }
   267  
   268  func (s *IntegrationTestSuite) TestGetProposalDepositsGRPC() {
   269  	val := s.network.Validators[0]
   270  
   271  	testCases := []struct {
   272  		name   string
   273  		url    string
   274  		expErr bool
   275  	}{
   276  		{
   277  			"get deposits with empty proposal id",
   278  			fmt.Sprintf("%s/cosmos/gov/v1beta1/proposals/%s/deposits", val.APIAddress, ""),
   279  			true,
   280  		},
   281  		{
   282  			"valid request",
   283  			fmt.Sprintf("%s/cosmos/gov/v1beta1/proposals/%s/deposits", val.APIAddress, "1"),
   284  			false,
   285  		},
   286  	}
   287  
   288  	for _, tc := range testCases {
   289  		s.Run(tc.name, func() {
   290  			resp, err := rest.GetRequest(tc.url)
   291  			s.Require().NoError(err)
   292  
   293  			var deposits types.QueryDepositsResponse
   294  			err = val.ClientCtx.Codec.UnmarshalJSON(resp, &deposits)
   295  
   296  			if tc.expErr {
   297  				s.Require().Error(err)
   298  			} else {
   299  				s.Require().NoError(err)
   300  				s.Require().Len(deposits.Deposits, 1)
   301  				s.Require().NotEmpty(deposits.Deposits[0])
   302  			}
   303  		})
   304  	}
   305  }
   306  
   307  func (s *IntegrationTestSuite) TestGetTallyGRPC() {
   308  	val := s.network.Validators[0]
   309  
   310  	testCases := []struct {
   311  		name   string
   312  		url    string
   313  		expErr bool
   314  	}{
   315  		{
   316  			"get tally with no proposal id",
   317  			fmt.Sprintf("%s/cosmos/gov/v1beta1/proposals/%s/tally", val.APIAddress, ""),
   318  			true,
   319  		},
   320  		{
   321  			"get tally with non existing proposal",
   322  			fmt.Sprintf("%s/cosmos/gov/v1beta1/proposals/%s/tally", val.APIAddress, "10"),
   323  			true,
   324  		},
   325  		{
   326  			"get tally valid request",
   327  			fmt.Sprintf("%s/cosmos/gov/v1beta1/proposals/%s/tally", val.APIAddress, "1"),
   328  			false,
   329  		},
   330  	}
   331  
   332  	for _, tc := range testCases {
   333  		s.Run(tc.name, func() {
   334  			resp, err := rest.GetRequest(tc.url)
   335  			s.Require().NoError(err)
   336  
   337  			var tally types.QueryTallyResultResponse
   338  			err = val.ClientCtx.Codec.UnmarshalJSON(resp, &tally)
   339  
   340  			if tc.expErr {
   341  				s.Require().Error(err)
   342  			} else {
   343  				s.Require().NoError(err)
   344  				s.Require().NotEmpty(tally.Tally)
   345  			}
   346  		})
   347  	}
   348  }
   349  
   350  func (s *IntegrationTestSuite) TestGetParamsGRPC() {
   351  	val := s.network.Validators[0]
   352  
   353  	testCases := []struct {
   354  		name       string
   355  		url        string
   356  		expErr     bool
   357  		respType   proto.Message
   358  		expectResp proto.Message
   359  	}{
   360  		{
   361  			"request params with empty params type",
   362  			fmt.Sprintf("%s/cosmos/gov/v1beta1/params/%s", val.APIAddress, ""),
   363  			true, nil, nil,
   364  		},
   365  		{
   366  			"get deposit params",
   367  			fmt.Sprintf("%s/cosmos/gov/v1beta1/params/%s", val.APIAddress, types.ParamDeposit),
   368  			false,
   369  			&types.QueryParamsResponse{},
   370  			&types.QueryParamsResponse{
   371  				DepositParams: types.DefaultDepositParams(),
   372  			},
   373  		},
   374  		{
   375  			"get vote params",
   376  			fmt.Sprintf("%s/cosmos/gov/v1beta1/params/%s", val.APIAddress, types.ParamVoting),
   377  			false,
   378  			&types.QueryParamsResponse{},
   379  			&types.QueryParamsResponse{
   380  				VotingParams: types.DefaultVotingParams(),
   381  			},
   382  		},
   383  		{
   384  			"get tally params",
   385  			fmt.Sprintf("%s/cosmos/gov/v1beta1/params/%s", val.APIAddress, types.ParamTallying),
   386  			false,
   387  			&types.QueryParamsResponse{},
   388  			&types.QueryParamsResponse{
   389  				TallyParams: types.DefaultTallyParams(),
   390  			},
   391  		},
   392  	}
   393  
   394  	for _, tc := range testCases {
   395  		s.Run(tc.name, func() {
   396  			resp, err := rest.GetRequest(tc.url)
   397  			s.Require().NoError(err)
   398  
   399  			err = val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType)
   400  
   401  			if tc.expErr {
   402  				s.Require().Error(err)
   403  			} else {
   404  				s.Require().NoError(err)
   405  				s.Require().Equal(tc.expectResp.String(), tc.respType.String())
   406  			}
   407  		})
   408  	}
   409  }