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