github.com/Finschia/finschia-sdk@v0.48.1/x/foundation/client/testutil/tx.go (about) 1 package testutil 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/Finschia/finschia-sdk/client/flags" 8 clitestutil "github.com/Finschia/finschia-sdk/testutil/cli" 9 "github.com/Finschia/finschia-sdk/testutil/testdata" 10 sdk "github.com/Finschia/finschia-sdk/types" 11 txtypes "github.com/Finschia/finschia-sdk/types/tx" 12 "github.com/Finschia/finschia-sdk/x/foundation" 13 "github.com/Finschia/finschia-sdk/x/foundation/client/cli" 14 ) 15 16 func (s *IntegrationTestSuite) TestNewTxCmdFundTreasury() { 17 val := s.network.Validators[0] 18 commonArgs := []string{ 19 fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), 20 fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), 21 fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), 22 fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)))), 23 } 24 25 testCases := map[string]struct { 26 args []string 27 valid bool 28 }{ 29 "valid transaction": { 30 []string{ 31 val.Address.String(), 32 sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.OneInt())).String(), 33 }, 34 true, 35 }, 36 "wrong number of args": { 37 []string{ 38 val.Address.String(), 39 sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.OneInt())).String(), 40 "extra", 41 }, 42 false, 43 }, 44 } 45 46 for name, tc := range testCases { 47 tc := tc 48 49 s.Run(name, func() { 50 cmd := cli.NewTxCmdFundTreasury() 51 out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...)) 52 if !tc.valid { 53 s.Require().Error(err) 54 return 55 } 56 s.Require().NoError(err) 57 58 var res sdk.TxResponse 59 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res), out) 60 s.Require().Zero(res.Code, out) 61 }) 62 } 63 } 64 65 func (s *IntegrationTestSuite) TestNewTxCmdWithdrawFromTreasury() { 66 val := s.network.Validators[0] 67 commonArgs := []string{ 68 fmt.Sprintf("--%s", flags.FlagGenerateOnly), 69 } 70 71 testCases := map[string]struct { 72 args []string 73 valid bool 74 }{ 75 "valid transaction": { 76 []string{ 77 s.authority.String(), 78 s.stranger.String(), 79 sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.OneInt())).String(), 80 }, 81 true, 82 }, 83 "wrong number of args": { 84 []string{ 85 s.authority.String(), 86 s.stranger.String(), 87 sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.OneInt())).String(), 88 "extra", 89 }, 90 false, 91 }, 92 } 93 94 for name, tc := range testCases { 95 tc := tc 96 97 s.Run(name, func() { 98 cmd := cli.NewTxCmdWithdrawFromTreasury() 99 out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...)) 100 if !tc.valid { 101 s.Require().Error(err) 102 return 103 } 104 s.Require().NoError(err) 105 106 var res txtypes.Tx 107 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res), out) 108 }) 109 } 110 } 111 112 func (s *IntegrationTestSuite) TestNewTxCmdUpdateMembers() { 113 val := s.network.Validators[0] 114 commonArgs := []string{ 115 fmt.Sprintf("--%s", flags.FlagGenerateOnly), 116 } 117 118 updates := `[{"address":"%s"}]` 119 testCases := map[string]struct { 120 args []string 121 valid bool 122 }{ 123 "valid transaction": { 124 []string{ 125 s.authority.String(), 126 fmt.Sprintf(updates, s.comingMember), 127 }, 128 true, 129 }, 130 "wrong number of args": { 131 []string{ 132 s.authority.String(), 133 fmt.Sprintf(updates, s.comingMember), 134 "extra", 135 }, 136 false, 137 }, 138 } 139 140 for name, tc := range testCases { 141 tc := tc 142 143 s.Run(name, func() { 144 cmd := cli.NewTxCmdUpdateMembers() 145 out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...)) 146 if !tc.valid { 147 s.Require().Error(err) 148 return 149 } 150 s.Require().NoError(err) 151 152 var res txtypes.Tx 153 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res), out) 154 }) 155 } 156 } 157 158 func (s *IntegrationTestSuite) TestNewTxCmdUpdateDecisionPolicy() { 159 val := s.network.Validators[0] 160 commonArgs := []string{ 161 fmt.Sprintf("--%s", flags.FlagGenerateOnly), 162 } 163 164 doMarshal := func(policy foundation.DecisionPolicy) string { 165 bz, err := val.ClientCtx.Codec.MarshalInterfaceJSON(policy) 166 s.Require().NoError(err) 167 return string(bz) 168 } 169 testCases := map[string]struct { 170 args []string 171 valid bool 172 }{ 173 "valid transaction": { 174 []string{ 175 s.authority.String(), 176 doMarshal(&foundation.ThresholdDecisionPolicy{ 177 Threshold: sdk.NewDec(10), 178 Windows: &foundation.DecisionPolicyWindows{ 179 VotingPeriod: time.Hour, 180 }, 181 }), 182 }, 183 true, 184 }, 185 "wrong number of args": { 186 []string{ 187 s.authority.String(), 188 doMarshal(&foundation.ThresholdDecisionPolicy{ 189 Threshold: sdk.NewDec(10), 190 Windows: &foundation.DecisionPolicyWindows{ 191 VotingPeriod: time.Hour, 192 }, 193 }), 194 "extra", 195 }, 196 false, 197 }, 198 } 199 200 for name, tc := range testCases { 201 tc := tc 202 203 s.Run(name, func() { 204 cmd := cli.NewTxCmdUpdateDecisionPolicy() 205 out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...)) 206 if !tc.valid { 207 s.Require().Error(err) 208 return 209 } 210 s.Require().NoError(err) 211 212 var res txtypes.Tx 213 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res), out) 214 }) 215 } 216 } 217 218 func (s *IntegrationTestSuite) TestNewTxCmdSubmitProposal() { 219 val := s.network.Validators[0] 220 commonArgs := []string{ 221 fmt.Sprintf("--%s=%s", flags.FlagFrom, s.permanentMember), 222 fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), 223 fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), 224 fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)))), 225 } 226 227 proposers := `["%s"]` 228 testCases := map[string]struct { 229 args []string 230 valid bool 231 }{ 232 "valid transaction": { 233 []string{ 234 "test proposal", 235 fmt.Sprintf(proposers, s.permanentMember), 236 s.msgToString(testdata.NewTestMsg()), 237 }, 238 true, 239 }, 240 "wrong number of args": { 241 []string{ 242 "test proposal", 243 fmt.Sprintf(proposers, s.permanentMember), 244 s.msgToString(testdata.NewTestMsg()), 245 "extra", 246 }, 247 false, 248 }, 249 } 250 251 for name, tc := range testCases { 252 tc := tc 253 254 s.Run(name, func() { 255 cmd := cli.NewTxCmdSubmitProposal() 256 out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...)) 257 if !tc.valid { 258 s.Require().Error(err) 259 return 260 } 261 s.Require().NoError(err) 262 263 var res sdk.TxResponse 264 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res), out) 265 s.Require().Zero(res.Code, out) 266 }) 267 } 268 } 269 270 func (s *IntegrationTestSuite) TestNewTxCmdWithdrawProposal() { 271 val := s.network.Validators[0] 272 commonArgs := []string{ 273 fmt.Sprintf("--%s=%s", flags.FlagFrom, s.permanentMember), 274 fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), 275 fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), 276 fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)))), 277 } 278 279 id := s.submitProposal(testdata.NewTestMsg(s.authority), false) 280 testCases := map[string]struct { 281 args []string 282 valid bool 283 }{ 284 "valid transaction": { 285 []string{ 286 fmt.Sprint(id), 287 s.permanentMember.String(), 288 }, 289 true, 290 }, 291 "wrong number of args": { 292 []string{ 293 fmt.Sprint(id), 294 s.permanentMember.String(), 295 "extra", 296 }, 297 false, 298 }, 299 } 300 301 for name, tc := range testCases { 302 tc := tc 303 304 s.Run(name, func() { 305 cmd := cli.NewTxCmdWithdrawProposal() 306 out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...)) 307 if !tc.valid { 308 s.Require().Error(err) 309 return 310 } 311 s.Require().NoError(err) 312 313 var res sdk.TxResponse 314 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res), out) 315 s.Require().Zero(res.Code, out) 316 }) 317 } 318 } 319 320 func (s *IntegrationTestSuite) TestNewTxCmdVote() { 321 val := s.network.Validators[0] 322 commonArgs := []string{ 323 fmt.Sprintf("--%s=%s", flags.FlagFrom, s.permanentMember), 324 fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), 325 fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), 326 fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)))), 327 } 328 329 id := s.submitProposal(testdata.NewTestMsg(s.authority), false) 330 testCases := map[string]struct { 331 args []string 332 valid bool 333 }{ 334 "valid transaction": { 335 []string{ 336 fmt.Sprint(id), 337 s.permanentMember.String(), 338 "VOTE_OPTION_YES", 339 "test vote", 340 }, 341 true, 342 }, 343 "wrong number of args": { 344 []string{ 345 fmt.Sprint(id), 346 s.permanentMember.String(), 347 "VOTE_OPTION_YES", 348 "test vote", 349 "extra", 350 }, 351 false, 352 }, 353 } 354 355 for name, tc := range testCases { 356 tc := tc 357 358 s.Run(name, func() { 359 cmd := cli.NewTxCmdVote() 360 out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...)) 361 if !tc.valid { 362 s.Require().Error(err) 363 return 364 } 365 s.Require().NoError(err) 366 367 var res sdk.TxResponse 368 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res), out) 369 s.Require().Zero(res.Code, out) 370 }) 371 } 372 } 373 374 func (s *IntegrationTestSuite) TestNewTxCmdExec() { 375 val := s.network.Validators[0] 376 commonArgs := []string{ 377 fmt.Sprintf("--%s=%s", flags.FlagFrom, s.permanentMember), 378 fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), 379 fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), 380 fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)))), 381 } 382 383 testCases := map[string]struct { 384 args []string 385 valid bool 386 }{ 387 "valid transaction": { 388 []string{ 389 fmt.Sprintf("%d", s.proposalID), 390 s.permanentMember.String(), 391 }, 392 true, 393 }, 394 "wrong number of args": { 395 []string{ 396 fmt.Sprintf("%d", s.proposalID), 397 s.permanentMember.String(), 398 "extra", 399 }, 400 false, 401 }, 402 } 403 404 for name, tc := range testCases { 405 tc := tc 406 407 s.Run(name, func() { 408 cmd := cli.NewTxCmdExec() 409 out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...)) 410 if !tc.valid { 411 s.Require().Error(err) 412 return 413 } 414 s.Require().NoError(err) 415 416 var res sdk.TxResponse 417 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res), out) 418 s.Require().Zero(res.Code, out) 419 }) 420 } 421 } 422 423 func (s *IntegrationTestSuite) TestNewTxCmdLeaveFoundation() { 424 val := s.network.Validators[0] 425 commonArgs := []string{ 426 fmt.Sprintf("--%s=%s", flags.FlagFrom, s.leavingMember), 427 fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), 428 fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), 429 fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)))), 430 } 431 432 testCases := map[string]struct { 433 args []string 434 valid bool 435 }{ 436 "valid transaction": { 437 []string{ 438 s.leavingMember.String(), 439 }, 440 true, 441 }, 442 "wrong number of args": { 443 []string{ 444 s.leavingMember.String(), 445 "extra", 446 }, 447 false, 448 }, 449 } 450 451 for name, tc := range testCases { 452 tc := tc 453 454 s.Run(name, func() { 455 cmd := cli.NewTxCmdLeaveFoundation() 456 out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...)) 457 if !tc.valid { 458 s.Require().Error(err) 459 return 460 } 461 s.Require().NoError(err) 462 463 var res sdk.TxResponse 464 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res), out) 465 s.Require().Zero(res.Code, out) 466 }) 467 } 468 } 469 470 func (s *IntegrationTestSuite) TestNewTxCmdGrant() { 471 val := s.network.Validators[0] 472 commonArgs := []string{ 473 fmt.Sprintf("--%s", flags.FlagGenerateOnly), 474 } 475 476 doMarshal := func(authorization foundation.Authorization) string { 477 bz, err := val.ClientCtx.Codec.MarshalInterfaceJSON(authorization) 478 s.Require().NoError(err) 479 return string(bz) 480 } 481 testCases := map[string]struct { 482 args []string 483 valid bool 484 }{ 485 "valid transaction": { 486 []string{ 487 s.authority.String(), 488 s.comingMember.String(), 489 doMarshal(&foundation.ReceiveFromTreasuryAuthorization{}), 490 }, 491 true, 492 }, 493 "wrong number of args": { 494 []string{ 495 s.authority.String(), 496 s.comingMember.String(), 497 doMarshal(&foundation.ReceiveFromTreasuryAuthorization{}), 498 "extra", 499 }, 500 false, 501 }, 502 } 503 504 for name, tc := range testCases { 505 tc := tc 506 507 s.Run(name, func() { 508 cmd := cli.NewTxCmdGrant() 509 out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...)) 510 if !tc.valid { 511 s.Require().Error(err) 512 return 513 } 514 s.Require().NoError(err) 515 516 var res txtypes.Tx 517 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res), out) 518 }) 519 } 520 } 521 522 func (s *IntegrationTestSuite) TestNewTxCmdRevoke() { 523 val := s.network.Validators[0] 524 commonArgs := []string{ 525 fmt.Sprintf("--%s", flags.FlagGenerateOnly), 526 } 527 528 testCases := map[string]struct { 529 args []string 530 valid bool 531 }{ 532 "valid transaction": { 533 []string{ 534 s.authority.String(), 535 s.leavingMember.String(), 536 foundation.ReceiveFromTreasuryAuthorization{}.MsgTypeURL(), 537 }, 538 true, 539 }, 540 "wrong number of args": { 541 []string{ 542 s.authority.String(), 543 s.leavingMember.String(), 544 foundation.ReceiveFromTreasuryAuthorization{}.MsgTypeURL(), 545 "extra", 546 }, 547 false, 548 }, 549 } 550 551 for name, tc := range testCases { 552 tc := tc 553 554 s.Run(name, func() { 555 cmd := cli.NewTxCmdRevoke() 556 out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...)) 557 if !tc.valid { 558 s.Require().Error(err) 559 return 560 } 561 s.Require().NoError(err) 562 563 var res txtypes.Tx 564 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res), out) 565 }) 566 } 567 }