github.com/cosmos/cosmos-sdk@v0.50.10/x/auth/client/cli/encode_test.go (about)

     1  package cli_test
     2  
     3  import (
     4  	"context"
     5  	"encoding/base64"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/cosmos/cosmos-sdk/client"
    11  	"github.com/cosmos/cosmos-sdk/testutil"
    12  	sdk "github.com/cosmos/cosmos-sdk/types"
    13  	moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
    14  	"github.com/cosmos/cosmos-sdk/x/auth"
    15  	"github.com/cosmos/cosmos-sdk/x/auth/client/cli"
    16  )
    17  
    18  func TestGetCommandEncode(t *testing.T) {
    19  	encodingConfig := moduletestutil.MakeTestEncodingConfig(auth.AppModuleBasic{})
    20  	txConfig := encodingConfig.TxConfig
    21  	cdc := encodingConfig.Codec
    22  
    23  	cmd := cli.GetEncodeCommand()
    24  	_ = testutil.ApplyMockIODiscardOutErr(cmd)
    25  
    26  	// Build a test transaction
    27  	builder := txConfig.NewTxBuilder()
    28  	builder.SetGasLimit(50000)
    29  	builder.SetFeeAmount(sdk.Coins{sdk.NewInt64Coin("atom", 150)})
    30  	builder.SetMemo("foomemo")
    31  	jsonEncoded, err := txConfig.TxJSONEncoder()(builder.GetTx())
    32  	require.NoError(t, err)
    33  
    34  	txFile := testutil.WriteToNewTempFile(t, string(jsonEncoded))
    35  	txFileName := txFile.Name()
    36  
    37  	ctx := context.Background()
    38  	clientCtx := client.Context{}.
    39  		WithTxConfig(txConfig).
    40  		WithCodec(cdc)
    41  	ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
    42  
    43  	cmd.SetArgs([]string{txFileName})
    44  	err = cmd.ExecuteContext(ctx)
    45  	require.NoError(t, err)
    46  }
    47  
    48  func TestGetCommandDecode(t *testing.T) {
    49  	encodingConfig := moduletestutil.MakeTestEncodingConfig(auth.AppModuleBasic{})
    50  	txConfig := encodingConfig.TxConfig
    51  	cdc := encodingConfig.Codec
    52  
    53  	clientCtx := client.Context{}.
    54  		WithTxConfig(txConfig).
    55  		WithCodec(cdc)
    56  
    57  	cmd := cli.GetDecodeCommand()
    58  	_ = testutil.ApplyMockIODiscardOutErr(cmd)
    59  
    60  	clientCtx = clientCtx.WithTxConfig(txConfig)
    61  
    62  	// Build a test transaction
    63  	builder := txConfig.NewTxBuilder()
    64  	builder.SetGasLimit(50000)
    65  	builder.SetFeeAmount(sdk.Coins{sdk.NewInt64Coin("atom", 150)})
    66  	builder.SetMemo("foomemo")
    67  
    68  	// Encode transaction
    69  	txBytes, err := clientCtx.TxConfig.TxEncoder()(builder.GetTx())
    70  	require.NoError(t, err)
    71  
    72  	// Convert the transaction into base64 encoded string
    73  	base64Encoded := base64.StdEncoding.EncodeToString(txBytes)
    74  
    75  	ctx := context.Background()
    76  	ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
    77  
    78  	// Execute the command
    79  	cmd.SetArgs([]string{base64Encoded})
    80  	require.NoError(t, cmd.ExecuteContext(ctx))
    81  }