cosmos-sdk/client/lcd/lcd_test.go

394 lines
10 KiB
Go
Raw Normal View History

2018-03-05 08:41:50 -08:00
package lcd
import (
"bytes"
"encoding/json"
2018-03-10 10:54:14 -08:00
"fmt"
2018-03-05 08:41:50 -08:00
"net/http"
"net/http/httptest"
"os"
2018-03-10 09:31:52 -08:00
"regexp"
2018-03-05 08:41:50 -08:00
"testing"
2018-03-09 02:03:15 -08:00
"github.com/spf13/viper"
2018-03-10 09:31:52 -08:00
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
keys "github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/examples/basecoin/app"
"github.com/cosmos/cosmos-sdk/server"
2018-03-08 07:06:40 -08:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2018-03-08 06:50:12 -08:00
abci "github.com/tendermint/abci/types"
2018-03-05 08:41:50 -08:00
cryptoKeys "github.com/tendermint/go-crypto/keys"
2018-03-08 06:50:12 -08:00
"github.com/tendermint/tendermint/p2p"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
2018-03-05 08:41:50 -08:00
dbm "github.com/tendermint/tmlibs/db"
2018-03-08 06:50:12 -08:00
"github.com/tendermint/tmlibs/log"
2018-03-05 08:41:50 -08:00
)
func TestKeys(t *testing.T) {
2018-03-10 08:35:21 -08:00
_, db, err := initKeybase(t)
require.Nil(t, err, "Couldn't init Keybase")
2018-03-08 07:06:40 -08:00
2018-03-05 08:41:50 -08:00
cdc := app.MakeCodec()
r := initRouter(cdc)
// empty keys
2018-03-10 08:35:21 -08:00
res := request(t, r, "GET", "/keys", nil)
require.Equal(t, http.StatusOK, res.Code, res.Body.String())
2018-03-08 07:06:40 -08:00
body := res.Body.String()
2018-03-10 08:35:21 -08:00
assert.Equal(t, body, "[]", "Expected an empty array")
2018-03-05 08:41:50 -08:00
2018-03-10 09:31:52 -08:00
// get seed
res = request(t, r, "GET", "/keys/seed", nil)
require.Equal(t, http.StatusOK, res.Code, res.Body.String())
seed := res.Body.String()
reg, err := regexp.Compile(`([a-z]+ ){12}`)
require.Nil(t, err)
match := reg.MatchString(seed)
assert.True(t, match, "Returned seed has wrong foramt", seed)
2018-03-10 08:15:56 -08:00
// add key
2018-03-10 09:31:52 -08:00
var jsonStr = []byte(`{"name":"test_fail", "password":"1234567890"}`)
res = request(t, r, "POST", "/keys", jsonStr)
assert.Equal(t, http.StatusBadRequest, res.Code, "Account creation should require a seed")
jsonStr = []byte(fmt.Sprintf(`{"name":"test", "password":"1234567890", "seed": "%s"}`, seed))
res = request(t, r, "POST", "/keys", jsonStr)
assert.Equal(t, http.StatusOK, res.Code, res.Body.String())
addr := res.Body.String()
assert.Len(t, addr, 40, "Returned address has wrong format", addr)
2018-03-05 08:41:50 -08:00
// existing keys
2018-03-10 08:35:21 -08:00
res = request(t, r, "GET", "/keys", nil)
require.Equal(t, http.StatusOK, res.Code, res.Body.String())
2018-03-05 08:41:50 -08:00
var m [1]keys.KeyOutput
decoder := json.NewDecoder(res.Body)
err = decoder.Decode(&m)
2018-03-10 08:35:21 -08:00
require.NoError(t, err)
2018-03-05 08:41:50 -08:00
2018-03-08 07:06:40 -08:00
assert.Equal(t, m[0].Name, "test", "Did not serve keys name correctly")
2018-03-10 08:15:56 -08:00
assert.Equal(t, m[0].Address, addr, "Did not serve keys Address correctly")
2018-03-05 08:41:50 -08:00
// select key
2018-03-10 08:35:21 -08:00
res = request(t, r, "GET", "/keys/test", nil)
require.Equal(t, http.StatusOK, res.Code, res.Body.String())
2018-03-05 08:41:50 -08:00
var m2 keys.KeyOutput
decoder = json.NewDecoder(res.Body)
err = decoder.Decode(&m2)
2018-03-08 07:06:40 -08:00
assert.Equal(t, m2.Name, "test", "Did not serve keys name correctly")
2018-03-10 08:15:56 -08:00
assert.Equal(t, m2.Address, addr, "Did not serve keys Address correctly")
2018-03-05 08:41:50 -08:00
// update key
2018-03-10 09:31:52 -08:00
jsonStr = []byte(`{"old_password":"1234567890", "new_password":"12345678901"}`)
2018-03-10 08:35:21 -08:00
res = request(t, r, "PUT", "/keys/test", jsonStr)
require.Equal(t, http.StatusOK, res.Code, res.Body.String())
2018-03-05 08:41:50 -08:00
// here it should say unauthorized as we changed the password before
2018-03-10 08:35:21 -08:00
res = request(t, r, "PUT", "/keys/test", jsonStr)
require.Equal(t, http.StatusUnauthorized, res.Code, res.Body.String())
2018-03-05 08:41:50 -08:00
// delete key
jsonStr = []byte(`{"password":"12345678901"}`)
2018-03-10 08:35:21 -08:00
res = request(t, r, "DELETE", "/keys/test", jsonStr)
require.Equal(t, http.StatusOK, res.Code, res.Body.String())
2018-03-05 08:41:50 -08:00
2018-03-10 08:35:21 -08:00
db.Close()
2018-03-08 06:50:12 -08:00
}
2018-03-09 02:54:19 -08:00
func TestVersion(t *testing.T) {
prepareClient(t)
cdc := app.MakeCodec()
r := initRouter(cdc)
// node info
2018-03-10 08:35:21 -08:00
res := request(t, r, "GET", "/version", nil)
2018-03-09 02:54:19 -08:00
require.Equal(t, http.StatusOK, res.Code, res.Body.String())
2018-03-10 09:36:15 -08:00
reg, err := regexp.Compile(`\d+\.\d+\.\d+(-dev)?`)
require.Nil(t, err)
match := reg.MatchString(res.Body.String())
assert.True(t, match, res.Body.String())
2018-03-09 02:54:19 -08:00
}
2018-03-09 02:03:15 -08:00
func TestNodeStatus(t *testing.T) {
2018-03-12 05:10:30 -07:00
//ch := server.StartServer(t)
//defer close(ch)
//prepareClient(t)
2018-03-12 05:10:30 -07:00
//cdc := app.MakeCodec() // TODO refactor so that cdc is included from a common test init function
//r := initRouter(cdc)
err := tests.InitServer()
require.Nil(t, err)
err := tests.StartServer()
require.Nil(t, err)
2018-03-08 06:50:12 -08:00
2018-03-09 02:03:15 -08:00
// node info
2018-03-10 08:35:21 -08:00
res := request(t, r, "GET", "/node_info", nil)
2018-03-09 01:14:44 -08:00
require.Equal(t, http.StatusOK, res.Code, res.Body.String())
2018-03-08 06:50:12 -08:00
var m p2p.NodeInfo
decoder := json.NewDecoder(res.Body)
2018-03-10 08:35:21 -08:00
err := decoder.Decode(&m)
2018-03-08 07:06:40 -08:00
require.Nil(t, err, "Couldn't parse node info")
2018-03-08 06:50:12 -08:00
assert.NotEqual(t, p2p.NodeInfo{}, m, "res: %v", res)
2018-03-09 02:03:15 -08:00
// syncing
2018-03-10 08:35:21 -08:00
res = request(t, r, "GET", "/syncing", nil)
2018-03-09 02:03:15 -08:00
require.Equal(t, http.StatusOK, res.Code, res.Body.String())
assert.Equal(t, "true", res.Body.String())
}
2018-03-09 02:03:15 -08:00
func TestBlock(t *testing.T) {
2018-03-10 09:33:05 -08:00
_, _ = startServer(t)
// TODO need to kill server after
prepareClient(t)
cdc := app.MakeCodec()
r := initRouter(cdc)
2018-03-10 11:14:13 -08:00
// res := request(t, r, "GET", "/blocks/latest", nil)
// require.Equal(t, http.StatusOK, res.Code, res.Body.String())
2018-03-10 11:14:13 -08:00
// var m ctypes.ResultBlock
// decoder := json.NewDecoder(res.Body)
// err := decoder.Decode(&m)
// require.Nil(t, err, "Couldn't parse block")
2018-03-10 11:14:13 -08:00
// assert.NotEqual(t, ctypes.ResultBlock{}, m)
2018-03-10 08:35:21 -08:00
// --
2018-03-10 11:14:13 -08:00
res := request(t, r, "GET", "/blocks/1", nil)
require.Equal(t, http.StatusOK, res.Code, res.Body.String())
2018-03-10 11:14:13 -08:00
var m ctypes.ResultBlock
decoder := json.NewDecoder(res.Body)
err := decoder.Decode(&m)
require.Nil(t, err, "Couldn't parse block")
assert.NotEqual(t, ctypes.ResultBlock{}, m)
2018-03-10 08:35:21 -08:00
// --
2018-03-10 08:35:21 -08:00
res = request(t, r, "GET", "/blocks/2", nil)
require.Equal(t, http.StatusNotFound, res.Code, res.Body.String())
}
func TestValidators(t *testing.T) {
2018-03-10 09:33:05 -08:00
_, _ = startServer(t)
// TODO need to kill server after
prepareClient(t)
cdc := app.MakeCodec()
r := initRouter(cdc)
2018-03-10 11:14:13 -08:00
// res := request(t, r, "GET", "/validatorsets/latest", nil)
// require.Equal(t, http.StatusOK, res.Code, res.Body.String())
2018-03-10 11:14:13 -08:00
// var m ctypes.ResultValidators
// decoder := json.NewDecoder(res.Body)
// err := decoder.Decode(&m)
// require.Nil(t, err, "Couldn't parse validatorset")
2018-03-10 11:14:13 -08:00
// assert.NotEqual(t, ctypes.ResultValidators{}, m)
2018-03-10 08:35:21 -08:00
// --
2018-03-10 11:14:13 -08:00
res := request(t, r, "GET", "/validatorsets/1", nil)
require.Equal(t, http.StatusOK, res.Code, res.Body.String())
2018-03-10 11:14:13 -08:00
var m ctypes.ResultValidators
decoder := json.NewDecoder(res.Body)
err := decoder.Decode(&m)
require.Nil(t, err, "Couldn't parse validatorset")
assert.NotEqual(t, ctypes.ResultValidators{}, m)
2018-03-10 08:35:21 -08:00
// --
2018-03-10 08:35:21 -08:00
res = request(t, r, "GET", "/validatorsets/2", nil)
require.Equal(t, http.StatusNotFound, res.Code)
2018-03-08 06:50:12 -08:00
}
2018-03-10 09:33:05 -08:00
func TestCoinSend(t *testing.T) {
2018-03-10 10:54:14 -08:00
ch := server.StartServer(t)
defer close(ch)
2018-03-10 09:33:05 -08:00
prepareClient(t)
cdc := app.MakeCodec()
r := initRouter(cdc)
2018-03-10 11:27:02 -08:00
// TODO make that account has coins
kb := client.MockKeyBase()
info, seed, err := kb.Create("account_with_coins", "1234567890", cryptoKeys.CryptoAlgo("ed25519"))
require.NoError(t, err)
addr := string(info.Address())
2018-03-10 10:54:14 -08:00
2018-03-10 09:33:05 -08:00
// query empty
res := request(t, r, "GET", "/accounts/1234567890123456789012345678901234567890", nil)
require.Equal(t, http.StatusNoContent, res.Code, res.Body.String())
// query
2018-03-10 10:54:14 -08:00
res = request(t, r, "GET", "/accounts/"+addr, nil)
2018-03-10 09:33:05 -08:00
require.Equal(t, http.StatusOK, res.Code, res.Body.String())
assert.Equal(t, `{
"coins": [
{
"denom": "mycoin",
"amount": 9007199254740992
}
]
}`, res.Body.String())
2018-03-10 11:27:02 -08:00
// create account to send in keybase
2018-03-10 09:33:05 -08:00
var jsonStr = []byte(fmt.Sprintf(`{"name":"test", "password":"1234567890", "seed": "%s"}`, seed))
res = request(t, r, "POST", "/keys", jsonStr)
require.Equal(t, http.StatusOK, res.Code, res.Body.String())
2018-03-10 11:27:02 -08:00
// create receive address
receiveInfo, _, err := kb.Create("receive_address", "1234567890", cryptoKeys.CryptoAlgo("ed25519"))
require.NoError(t, err)
receiveAddr := string(receiveInfo.Address())
2018-03-10 09:33:05 -08:00
// send
2018-03-10 11:27:02 -08:00
jsonStr = []byte(`{
"name":"test",
"password":"1234567890",
"amount":[{
"denom": "mycoin",
"amount": 1
}]
}`)
2018-03-10 09:33:05 -08:00
res = request(t, r, "POST", "/accounts/"+receiveAddr+"/send", jsonStr)
require.Equal(t, http.StatusOK, res.Code, res.Body.String())
// check if received
res = request(t, r, "GET", "/accounts/"+receiveAddr, nil)
require.Equal(t, http.StatusOK, res.Code, res.Body.String())
assert.Equal(t, `{
"coins": [
{
"denom": "mycoin",
"amount": 1
}
]
}`, res.Body.String())
}
2018-03-08 07:06:40 -08:00
//__________________________________________________________
// helpers
2018-03-08 06:50:12 -08:00
func defaultLogger() log.Logger {
return log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "sdk/app")
}
2018-03-09 02:03:15 -08:00
func prepareClient(t *testing.T) {
2018-03-08 06:50:12 -08:00
db := dbm.NewMemDB()
2018-03-09 02:03:15 -08:00
app := baseapp.NewBaseApp(t.Name(), defaultLogger(), db)
viper.Set(client.FlagNode, "localhost:46657")
2018-03-08 06:50:12 -08:00
header := abci.Header{Height: 1}
app.BeginBlock(abci.RequestBeginBlock{Header: header})
app.Commit()
2018-03-05 08:41:50 -08:00
}
2018-03-09 02:03:15 -08:00
// setupViper creates a homedir to run inside,
// and returns a cleanup function to defer
func setupViper() func() {
rootDir, err := ioutil.TempDir("", "mock-sdk-cmd")
if err != nil {
panic(err) // fuck it!
}
viper.Set("home", rootDir)
return func() {
os.RemoveAll(rootDir)
}
}
2018-03-10 09:33:05 -08:00
// from baseoind.main
func defaultOptions(addr string) func(args []string) (json.RawMessage, error) {
return func(args []string) (json.RawMessage, error) {
opts := fmt.Sprintf(`{
"accounts": [{
"address": "%s",
"coins": [
{
"denom": "mycoin",
"amount": 9007199254740992
}
]
}]
}`, addr)
return json.RawMessage(opts), nil
}
}
func startServer(t *testing.T) (types.Address, string) {
2018-03-10 08:15:56 -08:00
defer setupViper()()
// init server
2018-03-10 09:33:05 -08:00
addr, secret, err := server.GenerateCoinKey()
require.NoError(t, err)
initCmd := server.InitCmd(defaultOptions(addr.String()), log.NewNopLogger())
err = initCmd.RunE(nil, nil)
2018-03-10 08:15:56 -08:00
require.NoError(t, err)
// start server
viper.Set("with-tendermint", true)
startCmd := server.StartCmd(mock.NewApp, log.NewNopLogger())
timeout := time.Duration(3) * time.Second
err = runOrTimeout(startCmd, timeout)
require.NoError(t, err)
2018-03-10 09:33:05 -08:00
return addr, secret
2018-03-10 08:15:56 -08:00
}
// copied from server/start_test.go
func runOrTimeout(cmd *cobra.Command, timeout time.Duration) error {
done := make(chan error)
go func(out chan<- error) {
// this should NOT exit
err := cmd.RunE(nil, nil)
if err != nil {
out <- err
}
out <- fmt.Errorf("start died for unknown reasons")
}(done)
timer := time.NewTimer(timeout)
select {
case err := <-done:
return err
case <-timer.C:
return nil
}
}
2018-03-10 08:35:21 -08:00
func request(t *testing.T, r http.Handler, method string, path string, payload []byte) *httptest.ResponseRecorder {
2018-03-12 05:10:30 -07:00
2018-03-10 08:35:21 -08:00
req, err := http.NewRequest(method, path, bytes.NewBuffer(payload))
require.Nil(t, err)
res := httptest.NewRecorder()
r.ServeHTTP(res, req)
return res
}
func initKeybase(t *testing.T) (cryptoKeys.Keybase, *dbm.GoLevelDB, error) {
os.RemoveAll("./testKeybase")
db, err := dbm.NewGoLevelDB("keys", "./testKeybase")
require.Nil(t, err)
kb := client.GetKeyBase(db)
keys.SetKeyBase(kb)
return kb, db, nil
}