github.com/cosmos/cosmos-sdk@v0.50.10/client/context_test.go (about)

     1  package client_test
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"os"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/spf13/viper"
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/cosmos/cosmos-sdk/client"
    14  	"github.com/cosmos/cosmos-sdk/client/flags"
    15  	"github.com/cosmos/cosmos-sdk/codec"
    16  	"github.com/cosmos/cosmos-sdk/codec/types"
    17  	"github.com/cosmos/cosmos-sdk/crypto/hd"
    18  	"github.com/cosmos/cosmos-sdk/crypto/keyring"
    19  	"github.com/cosmos/cosmos-sdk/testutil/testdata"
    20  	"github.com/cosmos/cosmos-sdk/types/module/testutil"
    21  )
    22  
    23  func TestMain(m *testing.M) {
    24  	viper.Set(flags.FlagKeyringBackend, keyring.BackendMemory)
    25  	os.Exit(m.Run())
    26  }
    27  
    28  func TestContext_PrintProto(t *testing.T) {
    29  	ctx := client.Context{}
    30  
    31  	animal := &testdata.Dog{
    32  		Size_: "big",
    33  		Name:  "Spot",
    34  	}
    35  	anyAnimal, err := types.NewAnyWithValue(animal)
    36  	require.NoError(t, err)
    37  	hasAnimal := &testdata.HasAnimal{
    38  		Animal: anyAnimal,
    39  		X:      10,
    40  	}
    41  
    42  	// proto
    43  	registry := testdata.NewTestInterfaceRegistry()
    44  	ctx = ctx.WithCodec(codec.NewProtoCodec(registry))
    45  
    46  	// json
    47  	buf := &bytes.Buffer{}
    48  	ctx = ctx.WithOutput(buf)
    49  	ctx.OutputFormat = flags.OutputFormatJSON
    50  	err = ctx.PrintProto(hasAnimal)
    51  	require.NoError(t, err)
    52  	require.Equal(t,
    53  		`{"animal":{"@type":"/testpb.Dog","size":"big","name":"Spot"},"x":"10"}
    54  `, buf.String())
    55  
    56  	// yaml
    57  	buf = &bytes.Buffer{}
    58  	ctx = ctx.WithOutput(buf)
    59  	ctx.OutputFormat = flags.OutputFormatText
    60  	err = ctx.PrintProto(hasAnimal)
    61  	require.NoError(t, err)
    62  	require.Equal(t,
    63  		`animal:
    64    '@type': /testpb.Dog
    65    name: Spot
    66    size: big
    67  x: "10"
    68  `, buf.String())
    69  }
    70  
    71  func TestContext_PrintObjectLegacy(t *testing.T) {
    72  	ctx := client.Context{}
    73  
    74  	animal := &testdata.Dog{
    75  		Size_: "big",
    76  		Name:  "Spot",
    77  	}
    78  	anyAnimal, err := types.NewAnyWithValue(animal)
    79  	require.NoError(t, err)
    80  	hasAnimal := &testdata.HasAnimal{
    81  		Animal: anyAnimal,
    82  		X:      10,
    83  	}
    84  
    85  	// amino
    86  	amino := testdata.NewTestAmino()
    87  	ctx = ctx.WithLegacyAmino(&codec.LegacyAmino{Amino: amino})
    88  
    89  	// json
    90  	buf := &bytes.Buffer{}
    91  	ctx = ctx.WithOutput(buf)
    92  	ctx.OutputFormat = flags.OutputFormatJSON
    93  	err = ctx.PrintObjectLegacy(hasAnimal)
    94  	require.NoError(t, err)
    95  	require.Equal(t,
    96  		`{"type":"testpb/HasAnimal","value":{"animal":{"type":"testpb/Dog","value":{"size":"big","name":"Spot"}},"x":"10"}}
    97  `, buf.String())
    98  
    99  	// yaml
   100  	buf = &bytes.Buffer{}
   101  	ctx = ctx.WithOutput(buf)
   102  	ctx.OutputFormat = flags.OutputFormatText
   103  	err = ctx.PrintObjectLegacy(hasAnimal)
   104  	require.NoError(t, err)
   105  	require.Equal(t,
   106  		`type: testpb/HasAnimal
   107  value:
   108    animal:
   109      type: testpb/Dog
   110      value:
   111        name: Spot
   112        size: big
   113    x: "10"
   114  `, buf.String())
   115  }
   116  
   117  func TestContext_PrintRaw(t *testing.T) {
   118  	ctx := client.Context{}
   119  	hasAnimal := json.RawMessage(`{"animal":{"@type":"/testpb.Dog","size":"big","name":"Spot"},"x":"10"}`)
   120  
   121  	// json
   122  	buf := &bytes.Buffer{}
   123  	ctx = ctx.WithOutput(buf)
   124  	ctx.OutputFormat = flags.OutputFormatJSON
   125  	err := ctx.PrintRaw(hasAnimal)
   126  	require.NoError(t, err)
   127  	require.Equal(t,
   128  		`{"animal":{"@type":"/testpb.Dog","size":"big","name":"Spot"},"x":"10"}
   129  `, buf.String())
   130  
   131  	// yaml
   132  	buf = &bytes.Buffer{}
   133  	ctx = ctx.WithOutput(buf)
   134  	ctx.OutputFormat = flags.OutputFormatText
   135  	err = ctx.PrintRaw(hasAnimal)
   136  	require.NoError(t, err)
   137  	require.Equal(t,
   138  		`animal:
   139    '@type': /testpb.Dog
   140    name: Spot
   141    size: big
   142  x: "10"
   143  `, buf.String())
   144  }
   145  
   146  func TestGetFromFields(t *testing.T) {
   147  	cfg := testutil.MakeTestEncodingConfig()
   148  	path := hd.CreateHDPath(118, 0, 0).String()
   149  
   150  	testCases := []struct {
   151  		clientCtx   client.Context
   152  		keyring     func() keyring.Keyring
   153  		from        string
   154  		expectedErr string
   155  	}{
   156  		{
   157  			keyring: func() keyring.Keyring {
   158  				kb := keyring.NewInMemory(cfg.Codec)
   159  
   160  				_, _, err := kb.NewMnemonic("alice", keyring.English, path, keyring.DefaultBIP39Passphrase, hd.Secp256k1)
   161  				require.NoError(t, err)
   162  
   163  				return kb
   164  			},
   165  			from: "alice",
   166  		},
   167  		{
   168  			keyring: func() keyring.Keyring {
   169  				kb, err := keyring.New(t.Name(), keyring.BackendTest, t.TempDir(), nil, cfg.Codec)
   170  				require.NoError(t, err)
   171  
   172  				_, _, err = kb.NewMnemonic("alice", keyring.English, path, keyring.DefaultBIP39Passphrase, hd.Secp256k1)
   173  				require.NoError(t, err)
   174  
   175  				return kb
   176  			},
   177  			from: "alice",
   178  		},
   179  		{
   180  			keyring: func() keyring.Keyring {
   181  				return keyring.NewInMemory(cfg.Codec)
   182  			},
   183  			from:        "cosmos139f7kncmglres2nf3h4hc4tade85ekfr8sulz5",
   184  			expectedErr: "key with address cosmos139f7kncmglres2nf3h4hc4tade85ekfr8sulz5 not found: key not found",
   185  		},
   186  		{
   187  			keyring: func() keyring.Keyring {
   188  				kb, err := keyring.New(t.Name(), keyring.BackendTest, t.TempDir(), nil, cfg.Codec)
   189  				require.NoError(t, err)
   190  				return kb
   191  			},
   192  			from:        "alice",
   193  			expectedErr: "alice.info: key not found",
   194  		},
   195  		{
   196  			keyring: func() keyring.Keyring {
   197  				return keyring.NewInMemory(cfg.Codec)
   198  			},
   199  			from:      "cosmos139f7kncmglres2nf3h4hc4tade85ekfr8sulz5",
   200  			clientCtx: client.Context{}.WithSimulation(true),
   201  		},
   202  		{
   203  			keyring: func() keyring.Keyring {
   204  				return keyring.NewInMemory(cfg.Codec)
   205  			},
   206  			from:        "alice",
   207  			clientCtx:   client.Context{}.WithSimulation(true),
   208  			expectedErr: "a valid bech32 address must be provided in simulation mode",
   209  		},
   210  		{
   211  			keyring: func() keyring.Keyring {
   212  				return keyring.NewInMemory(cfg.Codec)
   213  			},
   214  			from:      "cosmos139f7kncmglres2nf3h4hc4tade85ekfr8sulz5",
   215  			clientCtx: client.Context{}.WithGenerateOnly(true),
   216  		},
   217  		{
   218  			keyring: func() keyring.Keyring {
   219  				return keyring.NewInMemory(cfg.Codec)
   220  			},
   221  			from:        "alice",
   222  			clientCtx:   client.Context{}.WithGenerateOnly(true),
   223  			expectedErr: "alice.info: key not found",
   224  		},
   225  		{
   226  			keyring: func() keyring.Keyring {
   227  				kb, err := keyring.New(t.Name(), keyring.BackendTest, t.TempDir(), nil, cfg.Codec)
   228  				require.NoError(t, err)
   229  
   230  				_, _, err = kb.NewMnemonic("alice", keyring.English, path, keyring.DefaultBIP39Passphrase, hd.Secp256k1)
   231  				require.NoError(t, err)
   232  
   233  				return kb
   234  			},
   235  			clientCtx: client.Context{}.WithGenerateOnly(true),
   236  			from:      "alice",
   237  		},
   238  	}
   239  
   240  	for _, tc := range testCases {
   241  		_, _, _, err := client.GetFromFields(tc.clientCtx, tc.keyring(), tc.from)
   242  		if tc.expectedErr == "" {
   243  			require.NoError(t, err)
   244  		} else {
   245  			require.True(t, strings.HasPrefix(err.Error(), tc.expectedErr))
   246  		}
   247  	}
   248  }