-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added basic sample of rewriting CLI integration test in GO
- Loading branch information
1 parent
2ef3fe9
commit abada72
Showing
8 changed files
with
302 additions
and
0 deletions.
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2020 DSR Corporation | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package dclauth_test_cli | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/cli_go/helpers" | ||
testconstants "github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/constants" | ||
"github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/utils" | ||
dclauthtypes "github.com/zigbee-alliance/distributed-compliance-ledger/x/dclauth/types" | ||
"testing" | ||
) | ||
|
||
func TestAuthDemoCLI(t *testing.T) { | ||
suite := utils.SetupTest(t, testconstants.ChainID, false) | ||
|
||
jack := testconstants.JackAccount | ||
alice := testconstants.AliceAccount | ||
|
||
user1 := helpers.CreateAccountInfo(&suite) | ||
|
||
// Propose user1 account by jack | ||
txResult, err := ProposeAccount(user1.Address, user1.Key, dclauthtypes.NodeAdmin, jack) | ||
require.NoError(suite.T, err) | ||
require.Equal(suite.T, txResult.Code, uint32(0)) | ||
|
||
// Approve user1 account by alice | ||
txResult, err = ApproveAccount(user1.Address, alice) | ||
require.NoError(suite.T, err) | ||
require.Equal(suite.T, txResult.Code, uint32(0)) | ||
|
||
// await transaction is written | ||
_, err = helpers.AwaitTxConfirmation(txResult.TxHash) | ||
require.NoError(suite.T, err) | ||
|
||
// Query list of all active accounts | ||
accounts, err := QueryAccounts() | ||
require.NoError(suite.T, err) | ||
require.True(suite.T, AccountIsInList(user1.Address, accounts.Account)) | ||
|
||
// Query user1 account | ||
account, err := QueryAccount(user1.Address) | ||
require.NoError(suite.T, err) | ||
require.Equal(suite.T, account.Address, user1.Address) | ||
} |
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,89 @@ | ||
package dclauth_test_cli | ||
|
||
import ( | ||
"fmt" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/cli_go/helpers" | ||
dclauthtypes "github.com/zigbee-alliance/distributed-compliance-ledger/x/dclauth/types" | ||
) | ||
|
||
func ProposeAccount( | ||
address string, | ||
key string, | ||
role dclauthtypes.AccountRole, | ||
from string) (sdk.TxResponse, error) { | ||
return helpers.Tx( | ||
"auth", | ||
"propose-add-account", | ||
from, | ||
fmt.Sprintf("--address=%s", address), | ||
fmt.Sprintf("--pubkey=%s", key), | ||
fmt.Sprintf("--roles=%s", string(role))) | ||
} | ||
|
||
func ApproveAccount( | ||
address string, | ||
from string) (sdk.TxResponse, error) { | ||
return helpers.Tx( | ||
"auth", | ||
"approve-add-account", | ||
from, | ||
fmt.Sprintf("--address=%s", address)) | ||
} | ||
|
||
func QueryAccounts() (dclauthtypes.QueryAllAccountResponse, error) { | ||
res, err := helpers.Query("auth", "all-accounts") | ||
if err != nil { | ||
return dclauthtypes.QueryAllAccountResponse{}, err | ||
} | ||
|
||
var resp dclauthtypes.QueryAllAccountResponse | ||
err = helpers.Codec.UnmarshalJSON([]byte(res), &resp) | ||
if err != nil { | ||
return dclauthtypes.QueryAllAccountResponse{}, err | ||
} | ||
|
||
return resp, nil | ||
} | ||
|
||
func QueryAccount(address string) (dclauthtypes.Account, error) { | ||
res, err := helpers.Query( | ||
"auth", | ||
"account", | ||
fmt.Sprintf("--address=%s", address)) | ||
if err != nil { | ||
return dclauthtypes.Account{}, err | ||
} | ||
|
||
var resp dclauthtypes.Account | ||
err = helpers.Codec.UnmarshalJSON([]byte(res), &resp) | ||
if err != nil { | ||
return dclauthtypes.Account{}, err | ||
} | ||
|
||
return resp, nil | ||
} | ||
|
||
func QueryPendingAccounts() (dclauthtypes.QueryAllPendingAccountResponse, error) { | ||
res, err := helpers.Query("auth", "all-proposed-accounts") | ||
if err != nil { | ||
return dclauthtypes.QueryAllPendingAccountResponse{}, err | ||
} | ||
|
||
var resp dclauthtypes.QueryAllPendingAccountResponse | ||
err = helpers.Codec.UnmarshalJSON([]byte(res), &resp) | ||
if err != nil { | ||
return dclauthtypes.QueryAllPendingAccountResponse{}, err | ||
} | ||
|
||
return resp, nil | ||
} | ||
|
||
func AccountIsInList(expected string, accounts []dclauthtypes.Account) bool { | ||
for _, account := range accounts { | ||
if expected == account.Address { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
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,40 @@ | ||
package helpers | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/codec" | ||
codectypes "github.com/cosmos/cosmos-sdk/codec/types" | ||
"github.com/cosmos/cosmos-sdk/crypto/hd" | ||
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/go-bip39" | ||
testconstants "github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/constants" | ||
"github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/utils" | ||
) | ||
|
||
type AccountInfo struct { | ||
Name string | ||
Address string | ||
Key string | ||
} | ||
|
||
func CreateAccountInfo(suite *utils.TestSuite) AccountInfo { | ||
name := RandomString() | ||
entropySeed, _ := bip39.NewEntropy(256) | ||
mnemonic, _ := bip39.NewMnemonic(entropySeed) | ||
account, _ := suite.Kr.NewAccount(name, mnemonic, testconstants.Passphrase, sdk.FullFundraiserPath, hd.Secp256k1) | ||
|
||
address, _ := account.GetAddress() | ||
pubKey, _ := account.GetPubKey() | ||
|
||
return AccountInfo{ | ||
Name: name, | ||
Address: address.String(), | ||
Key: FormatKey(pubKey), | ||
} | ||
} | ||
|
||
func FormatKey(pk cryptotypes.PubKey) string { | ||
apk, _ := codectypes.NewAnyWithValue(pk) | ||
bz, _ := codec.ProtoMarshalJSON(apk, nil) | ||
return string(bz) | ||
} |
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,23 @@ | ||
package helpers | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/codec" | ||
govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" | ||
govtypesv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" | ||
"github.com/zigbee-alliance/distributed-compliance-ledger/app" | ||
dclauthtypes "github.com/zigbee-alliance/distributed-compliance-ledger/x/dclauth/types" | ||
pkitypes "github.com/zigbee-alliance/distributed-compliance-ledger/x/pki/types" | ||
) | ||
|
||
var ( | ||
Codec codec.Codec | ||
) | ||
|
||
func init() { | ||
encodingConfig := app.MakeEncodingConfig() | ||
govtypesv1.RegisterInterfaces(encodingConfig.InterfaceRegistry) | ||
govtypesv1beta1.RegisterInterfaces(encodingConfig.InterfaceRegistry) | ||
dclauthtypes.RegisterInterfaces(encodingConfig.InterfaceRegistry) | ||
pkitypes.RegisterInterfaces(encodingConfig.InterfaceRegistry) | ||
Codec = encodingConfig.Codec | ||
} |
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,18 @@ | ||
package helpers | ||
|
||
import ( | ||
"os/exec" | ||
) | ||
|
||
const CliBinaryName = "dcld" | ||
|
||
func Command(args ...string) ([]byte, error) { | ||
cmd := exec.Command(CliBinaryName, args...) | ||
|
||
out, err := cmd.CombinedOutput() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return out, err | ||
} |
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,14 @@ | ||
package helpers | ||
|
||
import ( | ||
tmrand "github.com/cometbft/cometbft/libs/rand" | ||
"github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/utils" | ||
) | ||
|
||
func RandomString() string { | ||
return utils.RandString() | ||
} | ||
|
||
func RandomVid() int32 { | ||
return int32(tmrand.Uint16()) | ||
} |
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,13 @@ | ||
package helpers | ||
|
||
func Query(module, command string, queryArgs ...string) (string, error) { | ||
args := []string{"query", module, command} | ||
args = append(args, queryArgs...) | ||
|
||
output, err := Command(args...) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(output), nil | ||
} |
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,48 @@ | ||
package helpers | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"time" | ||
) | ||
|
||
func Tx(module, command, from string, txArgs ...string) (sdk.TxResponse, error) { | ||
args := []string{"tx", module, command} | ||
|
||
// TXN arguments | ||
args = append(args, txArgs...) | ||
|
||
// Sender account | ||
args = append(args, "--from", from) | ||
|
||
// Broadcast | ||
args = append(args, "--yes") | ||
|
||
output, err := Command(args...) | ||
if err != nil { | ||
return sdk.TxResponse{}, err | ||
} | ||
|
||
var resp sdk.TxResponse | ||
err = Codec.UnmarshalJSON(output, &resp) | ||
if err != nil { | ||
return sdk.TxResponse{}, err | ||
} | ||
|
||
return resp, nil | ||
} | ||
|
||
func AwaitTxConfirmation(hash string) (string, error) { | ||
var ( | ||
result []byte | ||
err error | ||
) | ||
for i := 1; i <= 20; i++ { | ||
result, err = Command("query", "tx", hash) | ||
if err == nil { | ||
return string(result), nil | ||
} else { | ||
time.Sleep(2 * time.Second) | ||
} | ||
} | ||
return "", err | ||
} |