From 69cf20f690dee5e09c643a0925c5ceb5c5a53952 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Mon, 8 Mar 2021 16:16:40 +0000 Subject: [PATCH] x/gov: fix NormalizeProposalType() return values (bp #8808) (#8816) (cherry picked from commit be23295bdf03b0d2a6801516b33998be66d4bb7e) Closes: #8806 Co-authored-by: Alessio Treglia --- CHANGELOG.md | 3 +- x/gov/client/cli/cli_test.go | 8 ++++ x/gov/client/rest/grpc_query_test.go | 20 +++++++--- x/gov/client/rest/rest_test.go | 56 ++++++++++++++++++++++++++++ x/gov/client/utils/utils.go | 8 ++-- x/gov/client/utils/utils_test.go | 35 +++++++++++++++++ 6 files changed, 120 insertions(+), 10 deletions(-) create mode 100644 x/gov/client/utils/utils_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 909071d6c6f3..629cd69c141d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,7 +45,8 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug fixes -* (x/evidence) [#8461](https://github.com/cosmos/cosmos-sdk/pull/8461) Fix bech32 prefix in evidence validator address conversion +* (x/evidence) [\#8461](https://github.com/cosmos/cosmos-sdk/pull/8461) Fix bech32 prefix in evidence validator address conversion +* (x/gov) [\#8806](https://github.com/cosmos/cosmos-sdk/issues/8806) Fix q gov proposals command's mishandling of the --status parameter's values. ## [v0.41.4](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.41.3) - 2021-03-02 diff --git a/x/gov/client/cli/cli_test.go b/x/gov/client/cli/cli_test.go index 981b13024c47..f0ee9b40e5ac 100644 --- a/x/gov/client/cli/cli_test.go +++ b/x/gov/client/cli/cli_test.go @@ -419,6 +419,14 @@ func (s *IntegrationTestSuite) TestCmdGetProposals() { }, false, }, + { + "get proposals with invalid status", + []string{ + "--status=unknown", + fmt.Sprintf("--%s=json", tmcli.OutputFlag), + }, + true, + }, } for _, tc := range testCases { diff --git a/x/gov/client/rest/grpc_query_test.go b/x/gov/client/rest/grpc_query_test.go index 8e2d4efa9ffa..027ce2962ed0 100644 --- a/x/gov/client/rest/grpc_query_test.go +++ b/x/gov/client/rest/grpc_query_test.go @@ -107,10 +107,11 @@ func (s *IntegrationTestSuite) TestGetProposalsGRPC() { val := s.network.Validators[0] testCases := []struct { - name string - url string - headers map[string]string - expErr bool + name string + url string + headers map[string]string + wantNumProposals int + expErr bool }{ { "get proposals with height 1", @@ -118,12 +119,21 @@ func (s *IntegrationTestSuite) TestGetProposalsGRPC() { map[string]string{ grpctypes.GRPCBlockHeightHeader: "1", }, + 0, true, }, { "valid request", fmt.Sprintf("%s/cosmos/gov/v1beta1/proposals", val.APIAddress), map[string]string{}, + 2, + false, + }, + { + "valid request with filter by status", + fmt.Sprintf("%s/cosmos/gov/v1beta1/proposals?proposal_status=1", val.APIAddress), + map[string]string{}, + 1, false, }, } @@ -141,7 +151,7 @@ func (s *IntegrationTestSuite) TestGetProposalsGRPC() { s.Require().Empty(proposals.Proposals) } else { s.Require().NoError(err) - s.Require().Len(proposals.Proposals, 2) + s.Require().Len(proposals.Proposals, tc.wantNumProposals) } }) } diff --git a/x/gov/client/rest/rest_test.go b/x/gov/client/rest/rest_test.go index 0b1a274022cb..df7f2e4cc6ef 100644 --- a/x/gov/client/rest/rest_test.go +++ b/x/gov/client/rest/rest_test.go @@ -9,6 +9,62 @@ import ( "github.com/cosmos/cosmos-sdk/x/gov/types" ) +func (s *IntegrationTestSuite) TestLegacyGetAllProposals() { + val := s.network.Validators[0] + + testCases := []struct { + name string + url string + numProposals int + expErr bool + expErrMsg string + }{ + { + "get all existing proposals", + fmt.Sprintf("%s/gov/proposals", val.APIAddress), + 2, false, "", + }, + { + "get proposals in deposit period", + fmt.Sprintf("%s/gov/proposals?status=deposit_period", val.APIAddress), + 1, false, "", + }, + { + "get proposals in voting period", + fmt.Sprintf("%s/gov/proposals?status=voting_period", val.APIAddress), + 1, false, "", + }, + { + "wrong status parameter", + fmt.Sprintf("%s/gov/proposals?status=invalidstatus", val.APIAddress), + 0, true, "'invalidstatus' is not a valid proposal status", + }, + } + + for _, tc := range testCases { + tc := tc + s.Run(tc.name, func() { + respJSON, err := rest.GetRequest(tc.url) + s.Require().NoError(err) + + if tc.expErr { + var errResp rest.ErrorResponse + s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(respJSON, &errResp)) + s.Require().Equal(errResp.Error, tc.expErrMsg) + } else { + var resp = rest.ResponseWithHeight{} + err = val.ClientCtx.LegacyAmino.UnmarshalJSON(respJSON, &resp) + s.Require().NoError(err) + + // Check results. + var proposals types.Proposals + s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(resp.Result, &proposals)) + s.Require().Equal(tc.numProposals, len(proposals)) + } + }) + } +} + func (s *IntegrationTestSuite) TestLegacyGetVote() { val := s.network.Validators[0] voterAddressBech32 := val.Address.String() diff --git a/x/gov/client/utils/utils.go b/x/gov/client/utils/utils.go index 84c2884d1a7f..86e4dc5f3e96 100644 --- a/x/gov/client/utils/utils.go +++ b/x/gov/client/utils/utils.go @@ -37,13 +37,13 @@ func NormalizeProposalType(proposalType string) string { func NormalizeProposalStatus(status string) string { switch status { case "DepositPeriod", "deposit_period": - return "DepositPeriod" + return types.StatusDepositPeriod.String() case "VotingPeriod", "voting_period": - return "VotingPeriod" + return types.StatusVotingPeriod.String() case "Passed", "passed": - return "Passed" + return types.StatusPassed.String() case "Rejected", "rejected": - return "Rejected" + return types.StatusRejected.String() default: return status } diff --git a/x/gov/client/utils/utils_test.go b/x/gov/client/utils/utils_test.go new file mode 100644 index 000000000000..11098c380e50 --- /dev/null +++ b/x/gov/client/utils/utils_test.go @@ -0,0 +1,35 @@ +package utils_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/cosmos/cosmos-sdk/x/gov/client/utils" +) + +func TestNormalizeProposalStatus(t *testing.T) { + type args struct { + status string + } + tests := []struct { + name string + args args + want string + }{ + {"invalid", args{"unknown"}, "unknown"}, + {"deposit_period", args{"deposit_period"}, "PROPOSAL_STATUS_DEPOSIT_PERIOD"}, + {"DepositPeriod", args{"DepositPeriod"}, "PROPOSAL_STATUS_DEPOSIT_PERIOD"}, + {"voting_period", args{"deposit_period"}, "PROPOSAL_STATUS_DEPOSIT_PERIOD"}, + {"VotingPeriod", args{"DepositPeriod"}, "PROPOSAL_STATUS_DEPOSIT_PERIOD"}, + {"passed", args{"passed"}, "PROPOSAL_STATUS_PASSED"}, + {"Passed", args{"Passed"}, "PROPOSAL_STATUS_PASSED"}, + {"Rejected", args{"Rejected"}, "PROPOSAL_STATUS_REJECTED"}, + {"rejected", args{"rejected"}, "PROPOSAL_STATUS_REJECTED"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + require.Equal(t, tt.want, utils.NormalizeProposalStatus(tt.args.status)) + }) + } +}