github.com/Finschia/finschia-sdk@v0.48.1/x/slashing/client/testutil/suite.go (about)

     1  package testutil
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	ostcli "github.com/Finschia/ostracon/libs/cli"
     8  	"github.com/gogo/protobuf/proto"
     9  	"github.com/stretchr/testify/suite"
    10  
    11  	"github.com/Finschia/finschia-sdk/client/flags"
    12  	clitestutil "github.com/Finschia/finschia-sdk/testutil/cli"
    13  	"github.com/Finschia/finschia-sdk/testutil/network"
    14  	sdk "github.com/Finschia/finschia-sdk/types"
    15  	"github.com/Finschia/finschia-sdk/x/slashing/client/cli"
    16  )
    17  
    18  type IntegrationTestSuite struct {
    19  	suite.Suite
    20  
    21  	cfg     network.Config
    22  	network *network.Network
    23  }
    24  
    25  func NewIntegrationTestSuite(cfg network.Config) *IntegrationTestSuite {
    26  	return &IntegrationTestSuite{cfg: cfg}
    27  }
    28  
    29  // SetupSuite executes bootstrapping logic before all the tests, i.e. once before
    30  // the entire suite, start executing.
    31  func (s *IntegrationTestSuite) SetupSuite() {
    32  	s.T().Log("setting up integration test suite")
    33  
    34  	s.network = network.New(s.T(), s.cfg)
    35  
    36  	_, err := s.network.WaitForHeight(1)
    37  	s.Require().NoError(err)
    38  }
    39  
    40  // TearDownSuite performs cleanup logic after all the tests, i.e. once after the
    41  // entire suite, has finished executing.
    42  func (s *IntegrationTestSuite) TearDownSuite() {
    43  	s.T().Log("tearing down integration test suite")
    44  	s.network.Cleanup()
    45  }
    46  
    47  func (s *IntegrationTestSuite) TestGetCmdQuerySigningInfo() {
    48  	val := s.network.Validators[0]
    49  	pubKeyBz, err := s.cfg.Codec.MarshalInterfaceJSON(val.PubKey)
    50  	s.Require().NoError(err)
    51  	pubKeyStr := string(pubKeyBz)
    52  
    53  	testCases := []struct {
    54  		name           string
    55  		args           []string
    56  		expectErr      bool
    57  		expectedOutput string
    58  	}{
    59  		{"invalid address", []string{"foo"}, true, ``},
    60  		{
    61  			"valid address (json output)",
    62  			[]string{
    63  				pubKeyStr,
    64  				fmt.Sprintf("--%s=json", ostcli.OutputFlag),
    65  				fmt.Sprintf("--%s=1", flags.FlagHeight),
    66  			},
    67  			false,
    68  			fmt.Sprintf("{\"address\":\"%s\",\"start_height\":\"0\",\"index_offset\":\"0\",\"jailed_until\":\"1970-01-01T00:00:00Z\",\"tombstoned\":false,\"missed_blocks_counter\":\"0\"}", sdk.ConsAddress(val.PubKey.Address())),
    69  		},
    70  		{
    71  			"valid address (text output)",
    72  			[]string{
    73  				pubKeyStr,
    74  				fmt.Sprintf("--%s=text", ostcli.OutputFlag),
    75  				fmt.Sprintf("--%s=1", flags.FlagHeight),
    76  			},
    77  			false,
    78  			fmt.Sprintf(`address: %s
    79  index_offset: "0"
    80  jailed_until: "1970-01-01T00:00:00Z"
    81  missed_blocks_counter: "0"
    82  start_height: "0"
    83  tombstoned: false`, sdk.ConsAddress(val.PubKey.Address())),
    84  		},
    85  	}
    86  
    87  	for _, tc := range testCases {
    88  		tc := tc
    89  
    90  		s.Run(tc.name, func() {
    91  			cmd := cli.GetCmdQuerySigningInfo()
    92  			clientCtx := val.ClientCtx
    93  
    94  			out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
    95  			if tc.expectErr {
    96  				s.Require().Error(err)
    97  			} else {
    98  				s.Require().NoError(err)
    99  				s.Require().Equal(tc.expectedOutput, strings.TrimSpace(out.String()))
   100  			}
   101  		})
   102  	}
   103  }
   104  
   105  func (s *IntegrationTestSuite) TestGetCmdQueryParams() {
   106  	val := s.network.Validators[0]
   107  
   108  	testCases := []struct {
   109  		name           string
   110  		args           []string
   111  		expectedOutput string
   112  	}{
   113  		{
   114  			"json output",
   115  			[]string{fmt.Sprintf("--%s=json", ostcli.OutputFlag)},
   116  			`{"signed_blocks_window":"100","min_signed_per_window":"0.500000000000000000","downtime_jail_duration":"600s","slash_fraction_double_sign":"0.050000000000000000","slash_fraction_downtime":"0.010000000000000000"}`,
   117  		},
   118  		{
   119  			"text output",
   120  			[]string{fmt.Sprintf("--%s=text", ostcli.OutputFlag)},
   121  			`downtime_jail_duration: 600s
   122  min_signed_per_window: "0.500000000000000000"
   123  signed_blocks_window: "100"
   124  slash_fraction_double_sign: "0.050000000000000000"
   125  slash_fraction_downtime: "0.010000000000000000"`,
   126  		},
   127  	}
   128  
   129  	for _, tc := range testCases {
   130  		tc := tc
   131  
   132  		s.Run(tc.name, func() {
   133  			cmd := cli.GetCmdQueryParams()
   134  			clientCtx := val.ClientCtx
   135  
   136  			out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
   137  			s.Require().NoError(err)
   138  			s.Require().Equal(tc.expectedOutput, strings.TrimSpace(out.String()))
   139  		})
   140  	}
   141  }
   142  
   143  func (s *IntegrationTestSuite) TestNewUnjailTxCmd() {
   144  	val := s.network.Validators[0]
   145  	testCases := []struct {
   146  		name         string
   147  		args         []string
   148  		expectErr    bool
   149  		expectedCode uint32
   150  		respType     proto.Message
   151  	}{
   152  		{
   153  			"valid transaction",
   154  			[]string{
   155  				fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
   156  				fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
   157  				fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), // sync mode as there are no funds yet
   158  				fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
   159  			},
   160  			false, 0, &sdk.TxResponse{},
   161  		},
   162  	}
   163  
   164  	for _, tc := range testCases {
   165  		tc := tc
   166  
   167  		s.Run(tc.name, func() {
   168  			cmd := cli.NewUnjailTxCmd()
   169  			clientCtx := val.ClientCtx
   170  
   171  			out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
   172  			if tc.expectErr {
   173  				s.Require().Error(err)
   174  			} else {
   175  				s.Require().NoError(err)
   176  				s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), tc.respType), out.String())
   177  
   178  				txResp := tc.respType.(*sdk.TxResponse)
   179  				s.Require().Equal(tc.expectedCode, txResp.Code, out.String())
   180  			}
   181  		})
   182  	}
   183  }