Lorenz Herzberger 6bff8e835d
Split up e2e test suites to run separately (#256)
* refactor: Split up machine e2e test suites

* refactor: move CreateAccount and FundAccount to testutil/e2e

* refactor: add AttestMachine to testutil/e2e

* refactor: split up asset e2e test suites

* refactor: make use of testutil in e2e dao suite

Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
2024-01-04 07:43:01 +01:00

218 lines
6.7 KiB
Go

package machine
import (
"github.com/planetmint/planetmint-go/config"
"github.com/planetmint/planetmint-go/lib"
clitestutil "github.com/planetmint/planetmint-go/testutil/cli"
"github.com/planetmint/planetmint-go/testutil/network"
"github.com/planetmint/planetmint-go/testutil/sample"
machinecli "github.com/planetmint/planetmint-go/x/machine/client/cli"
machinetypes "github.com/planetmint/planetmint-go/x/machine/types"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/feegrant"
e2etestutil "github.com/planetmint/planetmint-go/testutil/e2e"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
// E2ETestSuite struct definition of machine suite
type E2ETestSuite struct {
suite.Suite
cfg network.Config
network *network.Network
}
// NewE2ETestSuite returns configured machine E2ETestSuite
func NewE2ETestSuite(cfg network.Config) *E2ETestSuite {
return &E2ETestSuite{cfg: cfg}
}
// SetupSuite initializes machine E2ETestSuite
func (s *E2ETestSuite) SetupSuite() {
conf := config.GetConfig()
conf.FeeDenom = "stake"
s.T().Log("setting up e2e test suite")
s.network = network.New(s.T())
// create machine account for attestation
account, err := e2etestutil.CreateAccount(s.network, sample.Name, sample.Mnemonic)
s.Require().NoError(err)
err = e2etestutil.FundAccount(s.network, account)
s.Require().NoError(err)
}
// TearDownSuite clean up after testing
func (s *E2ETestSuite) TearDownSuite() {
s.T().Log("tearing down e2e test suite")
}
// TestAttestMachine attests machine and query attested machine from chain
func (s *E2ETestSuite) TestAttestMachine() {
val := s.network.Validators[0]
// register Ta
prvKey, pubKey := sample.KeyPair()
ta := sample.TrustAnchor(pubKey)
msg1 := machinetypes.NewMsgRegisterTrustAnchor(val.Address.String(), &ta)
out, err := lib.BroadcastTxWithFileLock(val.Address, msg1)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
rawLog, err := clitestutil.GetRawLogFromTxOut(val, out)
s.Require().NoError(err)
assert.Contains(s.T(), rawLog, "planetmintgo.machine.MsgRegisterTrustAnchor")
k, err := val.ClientCtx.Keyring.Key(sample.Name)
s.Require().NoError(err)
addr, _ := k.GetAddress()
// name and address of private key with which to sign
clientCtx := val.ClientCtx.
WithFromAddress(addr).
WithFromName(sample.Name)
libConfig := lib.GetConfig()
libConfig.SetClientCtx(clientCtx)
machine := sample.Machine(sample.Name, pubKey, prvKey, addr.String())
msg2 := machinetypes.NewMsgAttestMachine(addr.String(), &machine)
out, err = lib.BroadcastTxWithFileLock(addr, msg2)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
rawLog, err = clitestutil.GetRawLogFromTxOut(val, out)
s.Require().NoError(err)
assert.Contains(s.T(), rawLog, "planetmintgo.machine.MsgAttestMachine")
args := []string{
pubKey,
}
_, err = clitestutil.ExecTestCLICmd(val.ClientCtx, machinecli.CmdGetMachineByPublicKey(), args)
s.Require().NoError(err)
}
func (s *E2ETestSuite) TestInvalidAttestMachine() {
val := s.network.Validators[0]
// already used in previous test case
prvKey, pubKey := sample.KeyPair()
k, err := val.ClientCtx.Keyring.Key(sample.Name)
s.Require().NoError(err)
addr, _ := k.GetAddress()
machine := sample.Machine(sample.Name, pubKey, prvKey, addr.String())
s.Require().NoError(err)
// name and address of private key with which to sign
clientCtx := val.ClientCtx.
WithFromAddress(addr).
WithFromName(sample.Name)
libConfig := lib.GetConfig()
libConfig.SetClientCtx(clientCtx)
msg := machinetypes.NewMsgAttestMachine(addr.String(), &machine)
out, _ := lib.BroadcastTxWithFileLock(addr, msg)
txResponse, err := lib.GetTxResponseFromOut(out)
s.Require().NoError(err)
s.Require().Equal(int(txResponse.Code), int(4))
unregisteredPubKey, unregisteredPrivKey := sample.KeyPair(2)
machine = sample.Machine(sample.Name, unregisteredPubKey, unregisteredPrivKey, addr.String())
s.Require().NoError(err)
msg = machinetypes.NewMsgAttestMachine(addr.String(), &machine)
out, _ = lib.BroadcastTxWithFileLock(addr, msg)
txResponse, err = lib.GetTxResponseFromOut(out)
s.Require().NoError(err)
s.Require().Equal(int(txResponse.Code), int(3))
}
func (s *E2ETestSuite) TestMachineAllowanceAttestation() {
// create address for machine
val := s.network.Validators[0]
kb := val.ClientCtx.Keyring
account, _, err := kb.NewMnemonic("AllowanceMachine", keyring.English, sample.DefaultDerivationPath, keyring.DefaultBIP39Passphrase, hd.Secp256k1)
s.Require().NoError(err)
addr, err := account.GetAddress()
s.Require().NoError(err)
// register TA
prvKey, pubKey := sample.KeyPair(3)
// name and address of private key with which to sign
libConfig := lib.GetConfig()
libConfig.SetClientCtx(val.ClientCtx)
ta := sample.TrustAnchor(pubKey)
msg1 := machinetypes.NewMsgRegisterTrustAnchor(val.Address.String(), &ta)
out, err := lib.BroadcastTxWithFileLock(val.Address, msg1)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
_, err = clitestutil.GetRawLogFromTxOut(val, out)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
// create allowance for machine
allowedMsgs := []string{"/planetmintgo.machine.MsgAttestMachine"}
limit := sdk.NewCoins(sdk.NewInt64Coin("stake", 2))
basic := feegrant.BasicAllowance{
SpendLimit: limit,
}
var grant feegrant.FeeAllowanceI
grant = &basic
grant, err = feegrant.NewAllowedMsgAllowance(grant, allowedMsgs)
s.Require().NoError(err)
msg2, err := feegrant.NewMsgGrantAllowance(grant, val.Address, addr)
s.Require().NoError(err)
_, err = lib.BroadcastTxWithFileLock(val.Address, msg2)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
_, err = clitestutil.GetRawLogFromTxOut(val, out)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
// attest machine with fee granter without funding the machine account first
machine := sample.Machine(sample.Name, pubKey, prvKey, addr.String())
s.Require().NoError(err)
// name and address of private key with which to sign
clientCtx := val.ClientCtx.
WithFromAddress(addr).
WithFromName("AllowanceMachine").
WithFeeGranterAddress(val.Address)
libConfig.SetClientCtx(clientCtx)
msg3 := machinetypes.NewMsgAttestMachine(addr.String(), &machine)
_, err = lib.BroadcastTxWithFileLock(addr, msg3)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
_, err = clitestutil.GetRawLogFromTxOut(val, out)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
args := []string{
pubKey,
}
_, err = clitestutil.ExecTestCLICmd(val.ClientCtx, machinecli.CmdGetMachineByPublicKey(), args)
s.Require().NoError(err)
}