start implement rest e2e test

Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
Lorenz Herzberger 2023-07-10 11:09:43 +02:00
parent eda98bd463
commit e904b04afa
No known key found for this signature in database
GPG Key ID: FA5EE906EB55316A
2 changed files with 145 additions and 0 deletions

99
tests/e2e/machine/rest.go Normal file
View File

@ -0,0 +1,99 @@
package machine
import (
"fmt"
"planetmint-go/testutil"
machinetypes "planetmint-go/x/machine/types"
"github.com/cosmos/cosmos-sdk/codec/types"
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)
func (s *E2ETestSuite) TestAttestMachineREST() {
val := s.network.Validators[0]
baseURL := val.APIAddress
// Query Sequence Number
k, err := val.ClientCtx.Keyring.Key("machine")
s.Require().NoError(err)
addr, err := k.GetAddress()
s.Require().NoError(err)
reqAccountInfo := fmt.Sprintf("%s/cosmos/auth/v1beta1/account_info/%s", baseURL, addr.String())
respAccountInfo, err := testutil.GetRequest(reqAccountInfo)
s.Require().NoError(err)
s.T().Log(string(respAccountInfo))
var resAccountInfo authtypes.QueryAccountInfoResponse
err = val.ClientCtx.Codec.UnmarshalJSON(respAccountInfo, &resAccountInfo)
s.Require().NoError(err)
s.T().Log(resAccountInfo.Info.AccountNumber)
// Create Attest Machine TX
machine := machinetypes.Machine{
Name: "machine",
Ticker: "machine_ticker",
Issued: 1,
Amount: 1000,
Precision: 8,
IssuerPlanetmint: pubKey,
IssuerLiquid: pubKey,
MachineId: pubKey,
Metadata: &machinetypes.Metadata{
AdditionalDataCID: "CID",
Gps: "{\"Latitude\":\"-48.876667\",\"Longitude\":\"-123.393333\"}",
},
}
// machineJSON, err := json.Marshal(&machine)
// s.Require().NoError(err)
msg := machinetypes.MsgAttestMachine{
Creator: string(addr),
Machine: &machine,
}
msg.Type()
// Encode TX
reqEncodeTx := fmt.Sprintf("%s/cosmos/tx/encode", baseURL)
signerInfos := make([]txtypes.SignerInfo, 1)
signerInfos[1].PublicKey = &types.Any{
TypeUrl: "/cosmos.crypto.secp256k1.PubKey",
Value: k.PubKey.Value,
}
signerInfos[1].ModeInfo = &txtypes.ModeInfo{
Sum: &txtypes.ModeInfo_Single_{
Single: &txtypes.ModeInfo_Single{
Mode: signing.SignMode_SIGN_MODE_DIRECT,
},
},
}
signerInfos[1].Sequence = resAccountInfo.Info.Sequence
reqEncodeTxBody := txtypes.TxEncodeRequest{
Tx: &txtypes.Tx{
Body: &txtypes.TxBody{
// Messages: make([]*types.Any, 0),
},
AuthInfo: &txtypes.AuthInfo{
SignerInfos: signerInfos,
Tip: nil,
},
Signatures: [][]byte{},
},
}
s.Require().NoError(err)
// var resEncodeTX TXEncodeRequest
respEncodeTx, err := testutil.PostRequest(reqEncodeTx, "application/json", []byte(reqEncodeTxBody))
s.Require().NoError(err)
s.T().Log(respEncodeTx)
// Send encoded TX to /cosmos/tx/v1beta1/txs
// Query Machine by Pubkey
}

46
testutil/rest.go Normal file
View File

@ -0,0 +1,46 @@
package testutil
import (
"bytes"
"fmt"
"io"
"net/http"
)
// GetRequest defines a wrapper around an HTTP GET request with a provided URL.
// An error is returned if the request or reading the body fails.
func GetRequest(url string) ([]byte, error) {
res, err := http.Get(url) //nolint:gosec // only used for testing
if err != nil {
return nil, err
}
defer func() {
_ = res.Body.Close()
}()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
return body, nil
}
// PostRequest defines a wrapper around an HTTP POST request with a provided URL and data.
// An error is returned if the request or reading the body fails.
func PostRequest(url, contentType string, data []byte) ([]byte, error) {
res, err := http.Post(url, contentType, bytes.NewBuffer(data)) //nolint:gosec // only used for testing
if err != nil {
return nil, fmt.Errorf("error while sending post request: %w", err)
}
defer func() {
_ = res.Body.Close()
}()
bz, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("error reading response body: %w", err)
}
return bz, nil
}