Skip to content

Commit

Permalink
refactor(tests): refactor group CLI tests to be independent (#11338)
Browse files Browse the repository at this point in the history
## Description

Closes: #11168 



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
aleem1314 authored Mar 9, 2022
1 parent 5356a86 commit 056db12
Showing 1 changed file with 99 additions and 91 deletions.
190 changes: 99 additions & 91 deletions x/group/client/testutil/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type IntegrationTestSuite struct {

const validMetadata = "metadata"

var tooLongMetadata = strings.Repeat("A", 256)

func NewIntegrationTestSuite(cfg network.Config) *IntegrationTestSuite {
return &IntegrationTestSuite{cfg: cfg}
}
Expand Down Expand Up @@ -249,8 +251,8 @@ func (s *IntegrationTestSuite) TestTxCreateGroup() {
invalidMembersMetadata := fmt.Sprintf(`{"members": [{
"address": "%s",
"weight": "1",
"metadata": "AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQ=="
}]}`, val.Address.String())
"metadata": "%s"
}]}`, val.Address.String(), tooLongMetadata)
invalidMembersMetadataFile := testutil.WriteToNewTempFile(s.T(), invalidMembersMetadata)

testCases := []struct {
Expand Down Expand Up @@ -377,31 +379,38 @@ func (s *IntegrationTestSuite) TestTxCreateGroup() {
func (s *IntegrationTestSuite) TestTxUpdateGroupAdmin() {
val := s.network.Validators[0]
clientCtx := val.ClientCtx
require := s.Require()

var commonFlags = []string{
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
}

validMembers := fmt.Sprintf(`{"members": [{
groupIDs := make([]string, 2)
for i := 0; i < 2; i++ {
validMembers := fmt.Sprintf(`{"members": [{
"address": "%s",
"weight": "1",
"metadata": "%s"
}]}`, val.Address.String(), validMetadata)
validMembersFile := testutil.WriteToNewTempFile(s.T(), validMembers)
out, err := cli.ExecTestCLICmd(val.ClientCtx, client.MsgCreateGroupCmd(),
append(
[]string{
val.Address.String(),
validMetadata,
validMembersFile.Name(),
},
commonFlags...,
),
)

s.Require().NoError(err, out.String())
validMembersFile := testutil.WriteToNewTempFile(s.T(), validMembers)
out, err := cli.ExecTestCLICmd(val.ClientCtx, client.MsgCreateGroupCmd(),
append(
[]string{
val.Address.String(),
validMetadata,
validMembersFile.Name(),
},
commonFlags...,
),
)
require.NoError(err, out.String())
var txResp sdk.TxResponse
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &txResp), out.String())
s.Require().Equal(uint32(0), txResp.Code, out.String())
groupIDs[i] = s.getGroupIdFromTxResponse(txResp)
}

testCases := []struct {
name string
Expand All @@ -416,7 +425,7 @@ func (s *IntegrationTestSuite) TestTxUpdateGroupAdmin() {
append(
[]string{
val.Address.String(),
"3",
groupIDs[0],
s.network.Validators[1].Address.String(),
},
commonFlags...,
Expand All @@ -431,7 +440,7 @@ func (s *IntegrationTestSuite) TestTxUpdateGroupAdmin() {
append(
[]string{
val.Address.String(),
"4",
groupIDs[1],
s.network.Validators[1].Address.String(),
fmt.Sprintf("--%s=%s", flags.FlagSignMode, flags.SignModeLegacyAminoJSON),
},
Expand Down Expand Up @@ -482,13 +491,13 @@ func (s *IntegrationTestSuite) TestTxUpdateGroupAdmin() {

out, err := cli.ExecTestCLICmd(clientCtx, cmd, tc.args)
if tc.expectErr {
s.Require().Contains(out.String(), tc.expectErrMsg)
require.Contains(out.String(), tc.expectErrMsg)
} else {
s.Require().NoError(err, out.String())
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), tc.respType), out.String())
require.NoError(err, out.String())
require.NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), tc.respType), out.String())

txResp := tc.respType.(*sdk.TxResponse)
s.Require().Equal(tc.expectedCode, txResp.Code, out.String())
require.Equal(tc.expectedCode, txResp.Code, out.String())
}
})
}
Expand Down Expand Up @@ -603,8 +612,8 @@ func (s *IntegrationTestSuite) TestTxUpdateGroupMembers() {
invalidMembersMetadata := fmt.Sprintf(`{"members": [{
"address": "%s",
"weight": "1",
"metadata": "AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQ=="
}]}`, val.Address.String())
"metadata": "%s"
}]}`, val.Address.String(), tooLongMetadata)
invalidMembersMetadataFileName := testutil.WriteToNewTempFile(s.T(), invalidMembersMetadata).Name()

testCases := []struct {
Expand Down Expand Up @@ -734,8 +743,8 @@ func (s *IntegrationTestSuite) TestTxCreateGroupWithPolicy() {
invalidMembersMetadata := fmt.Sprintf(`{"members": [{
"address": "%s",
"weight": "1",
"metadata": "AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQ=="
}]}`, val.Address.String())
"metadata": "%s"
}]}`, val.Address.String(), tooLongMetadata)
invalidMembersMetadataFile := testutil.WriteToNewTempFile(s.T(), invalidMembersMetadata)

testCases := []struct {
Expand Down Expand Up @@ -1453,15 +1462,7 @@ func (s *IntegrationTestSuite) TestTxUpdateGroupPolicyMetadata() {
}
}

// TestTxCreateProposal tests submitting proposal.
//
// Please don't rename this to TestTxSubmitProposal. It will redefine the order
// of the tests being run in this file with `go test`, and will mess up the
// proposal ids in other tests (e.g. voting or Exec tests).
// This is a headache, but requires a bigger refactor of all tests in this file
// so that each one is independent.
// https://github.com/cosmos/cosmos-sdk/issues/11168
func (s *IntegrationTestSuite) TestTxCreateProposal() {
func (s *IntegrationTestSuite) TestTxSubmitProposal() {
val := s.network.Validators[0]
clientCtx := val.ClientCtx

Expand Down Expand Up @@ -1556,7 +1557,7 @@ func (s *IntegrationTestSuite) TestTxCreateProposal() {
s.createCLIProposal(
s.groupPolicies[0].Address, val.Address.String(),
s.groupPolicies[0].Address, val.Address.String(),
"AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQ==",
tooLongMetadata,
),
},
commonFlags...,
Expand Down Expand Up @@ -1801,7 +1802,7 @@ func (s *IntegrationTestSuite) TestTxVote() {
"2",
val.Address.String(),
"VOTE_OPTION_YES",
"AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQ==",
tooLongMetadata,
},
commonFlags...,
),
Expand Down Expand Up @@ -1998,15 +1999,17 @@ func (s *IntegrationTestSuite) getProposalIdFromTxResponse(txResp sdk.TxResponse
func (s *IntegrationTestSuite) TestTxExec() {
val := s.network.Validators[0]
clientCtx := val.ClientCtx
require := s.Require()

var commonFlags = []string{
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
}

var proposalIDs []string
// create proposals and vote
for i := 3; i <= 4; i++ {
for i := 0; i < 2; i++ {
out, err := cli.ExecTestCLICmd(val.ClientCtx, client.MsgSubmitProposalCmd(),
append(
[]string{
Expand All @@ -2019,21 +2022,26 @@ func (s *IntegrationTestSuite) TestTxExec() {
commonFlags...,
),
)
s.Require().NoError(err, out.String())
fmt.Println(out.String())
require.NoError(err, out.String())

var txResp sdk.TxResponse
require.NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &txResp), out.String())
require.Equal(uint32(0), txResp.Code, out.String())
proposalID := s.getProposalIdFromTxResponse(txResp)
proposalIDs = append(proposalIDs, proposalID)

out, err = cli.ExecTestCLICmd(val.ClientCtx, client.MsgVoteCmd(),
append(
[]string{
fmt.Sprintf("%d", i),
proposalID,
val.Address.String(),
"VOTE_OPTION_YES",
"",
},
commonFlags...,
),
)
s.Require().NoError(err, out.String())
require.NoError(err, out.String())
}

testCases := []struct {
Expand All @@ -2044,25 +2052,25 @@ func (s *IntegrationTestSuite) TestTxExec() {
respType proto.Message
expectedCode uint32
}{
// {
// "correct data",
// append(
// []string{
// "3",
// fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
// },
// commonFlags...,
// ),
// false,
// "",
// &sdk.TxResponse{},
// 0,
// },
{
"correct data",
append(
[]string{
proposalIDs[0],
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
},
commonFlags...,
),
false,
"",
&sdk.TxResponse{},
0,
},
{
"with amino-json",
append(
[]string{
"4",
proposalIDs[1],
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
fmt.Sprintf("--%s=%s", flags.FlagSignMode, flags.SignModeLegacyAminoJSON),
},
Expand All @@ -2073,34 +2081,34 @@ func (s *IntegrationTestSuite) TestTxExec() {
&sdk.TxResponse{},
0,
},
// {
// "invalid proposal id",
// append(
// []string{
// "abcd",
// fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
// },
// commonFlags...,
// ),
// true,
// "invalid syntax",
// nil,
// 0,
// },
// {
// "proposal not found",
// append(
// []string{
// "1234",
// fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
// },
// commonFlags...,
// ),
// true,
// "proposal: not found",
// nil,
// 0,
// },
{
"invalid proposal id",
append(
[]string{
"abcd",
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
},
commonFlags...,
),
true,
"invalid syntax",
nil,
0,
},
{
"proposal not found",
append(
[]string{
"1234",
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
},
commonFlags...,
),
true,
"proposal: not found",
nil,
0,
},
}

for _, tc := range testCases {
Expand All @@ -2111,13 +2119,13 @@ func (s *IntegrationTestSuite) TestTxExec() {

out, err := cli.ExecTestCLICmd(clientCtx, cmd, tc.args)
if tc.expectErr {
s.Require().Contains(out.String(), tc.expectErrMsg)
require.Contains(out.String(), tc.expectErrMsg)
} else {
s.Require().NoError(err, out.String())
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), tc.respType), out.String())
require.NoError(err, out.String())
require.NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), tc.respType), out.String())

txResp := tc.respType.(*sdk.TxResponse)
s.Require().Equal(tc.expectedCode, txResp.Code, out.String())
require.Equal(tc.expectedCode, txResp.Code, out.String())
}
})
}
Expand Down Expand Up @@ -2313,7 +2321,7 @@ func (s *IntegrationTestSuite) getGroupIdFromTxResponse(txResp sdk.TxResponse) s
// createCLIProposal writes a CLI proposal with a MsgSend to a file. Returns
// the path to the JSON file.
func (s *IntegrationTestSuite) createCLIProposal(groupPolicyAddress, proposer, sendFrom, sendTo, metadata string) string {
bz, err := base64.StdEncoding.DecodeString(metadata)
_, err := base64.StdEncoding.DecodeString(metadata)
s.Require().NoError(err)

msg := banktypes.MsgSend{
Expand All @@ -2331,7 +2339,7 @@ func (s *IntegrationTestSuite) createCLIProposal(groupPolicyAddress, proposer, s
Proposers: []string{proposer},
}

bz, err = json.Marshal(&p)
bz, err := json.Marshal(&p)
s.Require().NoError(err)

return testutil.WriteToNewTempFile(s.T(), string(bz)).Name()
Expand Down

0 comments on commit 056db12

Please sign in to comment.