-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
accbc76
commit 59f0c90
Showing
4 changed files
with
186 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,61 @@ | ||
package testclient | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
"github.com/spf13/pflag" | ||
"github.com/stretchr/testify/require" | ||
|
||
"pocket/app" | ||
"pocket/cmd/pocketd/cmd" | ||
) | ||
|
||
const CometLocalWebsocketURL = "ws://localhost:36657/websocket" | ||
|
||
var ( | ||
EncodingConfig = app.MakeEncodingConfig() | ||
) | ||
|
||
func init() { | ||
// NB: ensure to run tests with --build test flag | ||
cmd.InitSDKConfig() | ||
} | ||
|
||
func NewLocalnetClientCtx(t *testing.T, flagSet *pflag.FlagSet) *client.Context { | ||
homedir := DefaultHomeDir() | ||
clientCtx := client.Context{}. | ||
WithCodec(EncodingConfig.Marshaler). | ||
WithTxConfig(EncodingConfig.TxConfig). | ||
WithHomeDir(homedir). | ||
WithAccountRetriever(authtypes.AccountRetriever{}). | ||
WithInterfaceRegistry(EncodingConfig.InterfaceRegistry) | ||
|
||
clientCtx, err := client.ReadPersistentCommandFlags(clientCtx, flagSet) | ||
require.NoError(t, err) | ||
return &clientCtx | ||
} | ||
|
||
func NewLocalnetFlagSet(t *testing.T) *pflag.FlagSet { | ||
mockFlagSet := pflag.NewFlagSet("test", pflag.ContinueOnError) | ||
mockFlagSet.String(flags.FlagNode, "tcp://127.0.0.1:36657", "use localnet poktrolld node") | ||
mockFlagSet.String(flags.FlagHome, "", "use localnet poktrolld node") | ||
mockFlagSet.String(flags.FlagKeyringBackend, "test", "use test keyring") | ||
err := mockFlagSet.Parse([]string{}) | ||
require.NoError(t, err) | ||
|
||
return mockFlagSet | ||
} | ||
|
||
func DefaultHomeDir() string { | ||
userHomeDir, err := os.UserHomeDir() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return filepath.Join(userHomeDir, "."+app.Name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package testclient | ||
|
||
import ( | ||
cosmoshd "github.com/cosmos/cosmos-sdk/crypto/hd" | ||
cosmoskeyring "github.com/cosmos/cosmos-sdk/crypto/keyring" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func NewKey( | ||
t *testing.T, name string, | ||
keyring cosmoskeyring.Keyring, | ||
) (key *cosmoskeyring.Record, mnemonic string) { | ||
t.Helper() | ||
|
||
key, mnemonic, err := keyring.NewMnemonic( | ||
name, | ||
cosmoskeyring.English, | ||
"m/44'/118'/0'/0/0", | ||
cosmoskeyring.DefaultBIP39Passphrase, | ||
cosmoshd.Secp256k1, | ||
) | ||
require.NoError(t, err) | ||
require.NotNil(t, key) | ||
|
||
return key, mnemonic | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package testtx | ||
|
||
import ( | ||
"testing" | ||
|
||
"cosmossdk.io/depinject" | ||
cometbytes "github.com/cometbft/cometbft/libs/bytes" | ||
comettypes "github.com/cometbft/cometbft/types" | ||
cosmosclient "github.com/cosmos/cosmos-sdk/client" | ||
cosmostx "github.com/cosmos/cosmos-sdk/client/tx" | ||
cosmoskeyring "github.com/cosmos/cosmos-sdk/crypto/keyring" | ||
cosmostypes "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/require" | ||
|
||
"pocket/internal/mocks/mockclient" | ||
"pocket/internal/testclient" | ||
"pocket/pkg/client" | ||
"pocket/pkg/client/tx" | ||
) | ||
|
||
func NewOneTimeTxTxContext( | ||
t *testing.T, | ||
keyring cosmoskeyring.Keyring, | ||
signingKeyName string, | ||
expectedTx *cometbytes.HexBytes, | ||
expectedTxHash *cometbytes.HexBytes, | ||
) *mockclient.MockTxContext { | ||
t.Helper() | ||
|
||
txCtxMock, txCtx := NewAnyTimesTxTxContext(t, keyring) | ||
txCtxMock.EXPECT().NewTxBuilder(). | ||
DoAndReturn(txCtx.NewTxBuilder). | ||
Times(1) | ||
txCtxMock.EXPECT().SignTx( | ||
gomock.Eq(signingKeyName), | ||
gomock.AssignableToTypeOf(txCtx.NewTxBuilder()), | ||
gomock.Eq(false), gomock.Eq(false), | ||
).DoAndReturn(txCtx.SignTx).Times(1) | ||
txCtxMock.EXPECT().EncodeTx(gomock.Any()). | ||
DoAndReturn(func(txBuilder cosmosclient.TxBuilder) (_ []byte, err error) { | ||
// intercept cosmosTxContext#EncodeTx to get the encoded tx cometbytes | ||
*expectedTx, err = txCtx.EncodeTx(txBuilder) | ||
*expectedTxHash = comettypes.Tx(expectedTx.Bytes()).Hash() | ||
require.NoError(t, err) | ||
return expectedTx.Bytes(), nil | ||
}).Times(1) | ||
// intercept #BroadcastTxSync() call to mock response and prevent actual broadcast | ||
txCtxMock.EXPECT().BroadcastTxSync(gomock.Any()). | ||
DoAndReturn(func(txBytes []byte) (*cosmostypes.TxResponse, error) { | ||
return &cosmostypes.TxResponse{ | ||
Height: 1, | ||
TxHash: expectedTxHash.String(), | ||
RawLog: "", | ||
Logs: nil, | ||
Tx: nil, | ||
Timestamp: "", | ||
Events: nil, | ||
}, nil | ||
}).Times(1) | ||
|
||
return txCtxMock | ||
} | ||
|
||
func NewAnyTimesTxTxContext( | ||
t *testing.T, | ||
keyring cosmoskeyring.Keyring, | ||
) (*mockclient.MockTxContext, client.TxContext) { | ||
t.Helper() | ||
var ( | ||
ctrl = gomock.NewController(t) | ||
flagSet = testclient.NewLocalnetFlagSet(t) | ||
) | ||
|
||
// intercept #GetAccountNumberSequence() call to mock response and prevent actual query | ||
accountRetrieverMock := mockclient.NewMockAccountRetriever(ctrl) | ||
accountRetrieverMock.EXPECT().GetAccountNumberSequence(gomock.Any(), gomock.Any()). | ||
Return(uint64(1), uint64(1), nil). | ||
AnyTimes() | ||
|
||
clientCtx := testclient.NewLocalnetClientCtx(t, flagSet). | ||
WithKeyring(keyring). | ||
WithAccountRetriever(accountRetrieverMock) | ||
|
||
txFactory, err := cosmostx.NewFactoryCLI(clientCtx, flagSet) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, txFactory) | ||
|
||
txCtxDeps := depinject.Supply(txFactory, clientCtx) | ||
txCtx, err := tx.NewTxContext(txCtxDeps) | ||
require.NoError(t, err) | ||
txCtxMock := mockclient.NewMockTxContext(ctrl) | ||
txCtxMock.EXPECT().GetKeyring().Return(keyring).AnyTimes() | ||
|
||
return txCtxMock, txCtx | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters