Skip to content

Commit 0ae70a0

Browse files
committed
Added basic sample of rewriting CLI integration test in GO
1 parent 2ef3fe9 commit 0ae70a0

File tree

8 files changed

+309
-0
lines changed

8 files changed

+309
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2020 DSR Corporation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package dclauth_test_cli
16+
17+
import (
18+
"testing"
19+
20+
"github.com/stretchr/testify/require"
21+
"github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/cli_go/helpers"
22+
"github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/utils"
23+
24+
testconstants "github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/constants"
25+
dclauthtypes "github.com/zigbee-alliance/distributed-compliance-ledger/x/dclauth/types"
26+
)
27+
28+
func TestAuthDemoCLI(t *testing.T) {
29+
suite := utils.SetupTest(t, testconstants.ChainID, false)
30+
31+
jack := testconstants.JackAccount
32+
alice := testconstants.AliceAccount
33+
34+
user1 := helpers.CreateAccountInfo(&suite)
35+
36+
// Propose user1 account by jack
37+
txResult, err := ProposeAccount(user1.Address, user1.Key, dclauthtypes.NodeAdmin, jack)
38+
require.NoError(suite.T, err)
39+
require.Equal(suite.T, txResult.Code, uint32(0))
40+
41+
// Approve user1 account by alice
42+
txResult, err = ApproveAccount(user1.Address, alice)
43+
require.NoError(suite.T, err)
44+
require.Equal(suite.T, txResult.Code, uint32(0))
45+
46+
// await transaction is written
47+
_, err = helpers.AwaitTxConfirmation(txResult.TxHash)
48+
require.NoError(suite.T, err)
49+
50+
// Query list of all active accounts
51+
accounts, err := QueryAccounts()
52+
require.NoError(suite.T, err)
53+
require.True(suite.T, AccountIsInList(user1.Address, accounts.Account))
54+
55+
// Query user1 account
56+
account, err := QueryAccount(user1.Address)
57+
require.NoError(suite.T, err)
58+
require.Equal(suite.T, account.Address, user1.Address)
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package dclauth_test_cli
2+
3+
import (
4+
"fmt"
5+
6+
sdk "github.com/cosmos/cosmos-sdk/types"
7+
"github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/cli_go/helpers"
8+
dclauthtypes "github.com/zigbee-alliance/distributed-compliance-ledger/x/dclauth/types"
9+
)
10+
11+
func ProposeAccount(
12+
address string,
13+
key string,
14+
role dclauthtypes.AccountRole,
15+
from string) (sdk.TxResponse, error) {
16+
return helpers.Tx(
17+
"auth",
18+
"propose-add-account",
19+
from,
20+
fmt.Sprintf("--address=%s", address),
21+
fmt.Sprintf("--pubkey=%s", key),
22+
fmt.Sprintf("--roles=%s", string(role)))
23+
}
24+
25+
func ApproveAccount(
26+
address string,
27+
from string) (sdk.TxResponse, error) {
28+
return helpers.Tx(
29+
"auth",
30+
"approve-add-account",
31+
from,
32+
fmt.Sprintf("--address=%s", address))
33+
}
34+
35+
func QueryAccounts() (dclauthtypes.QueryAllAccountResponse, error) {
36+
res, err := helpers.Query("auth", "all-accounts")
37+
if err != nil {
38+
return dclauthtypes.QueryAllAccountResponse{}, err
39+
}
40+
41+
var resp dclauthtypes.QueryAllAccountResponse
42+
err = helpers.Codec.UnmarshalJSON([]byte(res), &resp)
43+
if err != nil {
44+
return dclauthtypes.QueryAllAccountResponse{}, err
45+
}
46+
47+
return resp, nil
48+
}
49+
50+
func QueryAccount(address string) (dclauthtypes.Account, error) {
51+
res, err := helpers.Query(
52+
"auth",
53+
"account",
54+
fmt.Sprintf("--address=%s", address))
55+
if err != nil {
56+
return dclauthtypes.Account{}, err
57+
}
58+
59+
var resp dclauthtypes.Account
60+
err = helpers.Codec.UnmarshalJSON([]byte(res), &resp)
61+
if err != nil {
62+
return dclauthtypes.Account{}, err
63+
}
64+
65+
return resp, nil
66+
}
67+
68+
func QueryPendingAccounts() (dclauthtypes.QueryAllPendingAccountResponse, error) {
69+
res, err := helpers.Query("auth", "all-proposed-accounts")
70+
if err != nil {
71+
return dclauthtypes.QueryAllPendingAccountResponse{}, err
72+
}
73+
74+
var resp dclauthtypes.QueryAllPendingAccountResponse
75+
err = helpers.Codec.UnmarshalJSON([]byte(res), &resp)
76+
if err != nil {
77+
return dclauthtypes.QueryAllPendingAccountResponse{}, err
78+
}
79+
80+
return resp, nil
81+
}
82+
83+
func AccountIsInList(expected string, accounts []dclauthtypes.Account) bool {
84+
for _, account := range accounts {
85+
if expected == account.Address {
86+
return true
87+
}
88+
}
89+
90+
return false
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package helpers
2+
3+
import (
4+
"github.com/cosmos/cosmos-sdk/codec"
5+
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
6+
"github.com/cosmos/cosmos-sdk/crypto/hd"
7+
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
8+
sdk "github.com/cosmos/cosmos-sdk/types"
9+
"github.com/cosmos/go-bip39"
10+
testconstants "github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/constants"
11+
"github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/utils"
12+
)
13+
14+
type AccountInfo struct {
15+
Name string
16+
Address string
17+
Key string
18+
}
19+
20+
func CreateAccountInfo(suite *utils.TestSuite) AccountInfo {
21+
name := RandomString()
22+
entropySeed, _ := bip39.NewEntropy(256)
23+
mnemonic, _ := bip39.NewMnemonic(entropySeed)
24+
account, _ := suite.Kr.NewAccount(name, mnemonic, testconstants.Passphrase, sdk.FullFundraiserPath, hd.Secp256k1)
25+
26+
address, _ := account.GetAddress()
27+
pubKey, _ := account.GetPubKey()
28+
29+
return AccountInfo{
30+
Name: name,
31+
Address: address.String(),
32+
Key: FormatKey(pubKey),
33+
}
34+
}
35+
36+
func FormatKey(pk cryptotypes.PubKey) string {
37+
apk, _ := codectypes.NewAnyWithValue(pk)
38+
bz, _ := codec.ProtoMarshalJSON(apk, nil)
39+
40+
return string(bz)
41+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package helpers
2+
3+
import (
4+
"github.com/cosmos/cosmos-sdk/codec"
5+
govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
6+
govtypesv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
7+
"github.com/zigbee-alliance/distributed-compliance-ledger/app"
8+
dclauthtypes "github.com/zigbee-alliance/distributed-compliance-ledger/x/dclauth/types"
9+
pkitypes "github.com/zigbee-alliance/distributed-compliance-ledger/x/pki/types"
10+
)
11+
12+
var (
13+
Codec codec.Codec
14+
)
15+
16+
func init() {
17+
encodingConfig := app.MakeEncodingConfig()
18+
govtypesv1.RegisterInterfaces(encodingConfig.InterfaceRegistry)
19+
govtypesv1beta1.RegisterInterfaces(encodingConfig.InterfaceRegistry)
20+
dclauthtypes.RegisterInterfaces(encodingConfig.InterfaceRegistry)
21+
pkitypes.RegisterInterfaces(encodingConfig.InterfaceRegistry)
22+
Codec = encodingConfig.Codec
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package helpers
2+
3+
import (
4+
"os/exec"
5+
)
6+
7+
const CliBinaryName = "dcld"
8+
9+
func Command(args ...string) ([]byte, error) {
10+
cmd := exec.Command(CliBinaryName, args...)
11+
12+
out, err := cmd.CombinedOutput()
13+
if err != nil {
14+
return nil, err
15+
}
16+
17+
return out, err
18+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package helpers
2+
3+
import (
4+
tmrand "github.com/cometbft/cometbft/libs/rand"
5+
"github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/utils"
6+
)
7+
8+
func RandomString() string {
9+
return utils.RandString()
10+
}
11+
12+
func RandomVid() int32 {
13+
return int32(tmrand.Uint16())
14+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package helpers
2+
3+
func Query(module, command string, queryArgs ...string) (string, error) {
4+
args := []string{"query", module, command}
5+
args = append(args, queryArgs...)
6+
7+
output, err := Command(args...)
8+
if err != nil {
9+
return "", err
10+
}
11+
12+
return string(output), nil
13+
}
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package helpers
2+
3+
import (
4+
"time"
5+
6+
sdk "github.com/cosmos/cosmos-sdk/types"
7+
)
8+
9+
func Tx(module, command, from string, txArgs ...string) (sdk.TxResponse, error) {
10+
args := []string{"tx", module, command}
11+
12+
// TXN arguments
13+
args = append(args, txArgs...)
14+
15+
// Sender account
16+
args = append(args, "--from", from)
17+
18+
// Broadcast
19+
args = append(args, "--yes")
20+
21+
output, err := Command(args...)
22+
if err != nil {
23+
return sdk.TxResponse{}, err
24+
}
25+
26+
var resp sdk.TxResponse
27+
err = Codec.UnmarshalJSON(output, &resp)
28+
if err != nil {
29+
return sdk.TxResponse{}, err
30+
}
31+
32+
return resp, nil
33+
}
34+
35+
func AwaitTxConfirmation(hash string) (string, error) {
36+
var (
37+
result []byte
38+
err error
39+
)
40+
for i := 1; i <= 20; i++ {
41+
result, err = Command("query", "tx", hash)
42+
if err == nil {
43+
return string(result), nil
44+
} else {
45+
time.Sleep(2 * time.Second)
46+
}
47+
}
48+
49+
return "", err
50+
}

0 commit comments

Comments
 (0)