github.com/Finschia/finschia-sdk@v0.49.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  		s.Run(tc.name, func() {
    89  			cmd := cli.GetCmdQuerySigningInfo()
    90  			clientCtx := val.ClientCtx
    91  
    92  			out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
    93  			if tc.expectErr {
    94  				s.Require().Error(err)
    95  			} else {
    96  				s.Require().NoError(err)
    97  				s.Require().Equal(tc.expectedOutput, strings.TrimSpace(out.String()))
    98  			}
    99  		})
   100  	}
   101  }
   102  
   103  func (s *IntegrationTestSuite) TestGetCmdQueryParams() {
   104  	val := s.network.Validators[0]
   105  
   106  	testCases := []struct {
   107  		name           string
   108  		args           []string
   109  		expectedOutput string
   110  	}{
   111  		{
   112  			"json output",
   113  			[]string{fmt.Sprintf("--%s=json", ostcli.OutputFlag)},
   114  			`{"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"}`,
   115  		},
   116  		{
   117  			"text output",
   118  			[]string{fmt.Sprintf("--%s=text", ostcli.OutputFlag)},
   119  			`downtime_jail_duration: 600s
   120  min_signed_per_window: "0.500000000000000000"
   121  signed_blocks_window: "100"
   122  slash_fraction_double_sign: "0.050000000000000000"
   123  slash_fraction_downtime: "0.010000000000000000"`,
   124  		},
   125  	}
   126  
   127  	for _, tc := range testCases {
   128  		s.Run(tc.name, func() {
   129  			cmd := cli.GetCmdQueryParams()
   130  			clientCtx := val.ClientCtx
   131  
   132  			out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
   133  			s.Require().NoError(err)
   134  			s.Require().Equal(tc.expectedOutput, strings.TrimSpace(out.String()))
   135  		})
   136  	}
   137  }
   138  
   139  func (s *IntegrationTestSuite) TestNewUnjailTxCmd() {
   140  	val := s.network.Validators[0]
   141  	testCases := []struct {
   142  		name         string
   143  		args         []string
   144  		expectErr    bool
   145  		expectedCode uint32
   146  		respType     proto.Message
   147  	}{
   148  		{
   149  			"valid transaction",
   150  			[]string{
   151  				fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
   152  				fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
   153  				fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), // sync mode as there are no funds yet
   154  				fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
   155  			},
   156  			false, 0, &sdk.TxResponse{},
   157  		},
   158  	}
   159  
   160  	for _, tc := range testCases {
   161  		s.Run(tc.name, func() {
   162  			cmd := cli.NewUnjailTxCmd()
   163  			clientCtx := val.ClientCtx
   164  
   165  			out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
   166  			if tc.expectErr {
   167  				s.Require().Error(err)
   168  			} else {
   169  				s.Require().NoError(err)
   170  				s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), tc.respType), out.String())
   171  
   172  				txResp := tc.respType.(*sdk.TxResponse)
   173  				s.Require().Equal(tc.expectedCode, txResp.Code, out.String())
   174  			}
   175  		})
   176  	}
   177  }