most tests working
This commit is contained in:
parent
579bd56127
commit
1cd6ec1084
@ -3,6 +3,7 @@ package keys
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
@ -129,7 +130,7 @@ func printCreate(info keys.Info, seed string) {
|
||||
type NewKeyBody struct {
|
||||
Name string `json:"name"`
|
||||
Password string `json:"password"`
|
||||
Seed string `json="seed"`
|
||||
Seed string `json:"seed"`
|
||||
}
|
||||
|
||||
func AddNewKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
|
||||
@ -143,33 +144,44 @@ func AddNewKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
err = decoder.Decode(&m)
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
err = json.Unmarshal(body, &m)
|
||||
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
if m.Name == "" {
|
||||
w.WriteHeader(400)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte("You have to specify a name for the locally stored account."))
|
||||
return
|
||||
}
|
||||
if m.Password == "" {
|
||||
w.WriteHeader(400)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte("You have to specify a password for the locally stored account."))
|
||||
return
|
||||
}
|
||||
if m.Seed == "" {
|
||||
w.WriteHeader(400)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte("You have to specify a seed for the locally stored account."))
|
||||
return
|
||||
}
|
||||
|
||||
// check if already exists
|
||||
infos, err := kb.List()
|
||||
for _, i := range infos {
|
||||
if i.Name == m.Name {
|
||||
w.WriteHeader(http.StatusConflict)
|
||||
w.Write([]byte(fmt.Sprintf("Account with name %s already exists.", m.Name)))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// create account
|
||||
info, err := kb.Recover(m.Name, m.Password, m.Seed)
|
||||
// TODO handle different errors
|
||||
if err != nil {
|
||||
w.WriteHeader(500)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
@ -9,22 +9,17 @@ import (
|
||||
"os"
|
||||
"regexp"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"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/tests"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
abci "github.com/tendermint/abci/types"
|
||||
cryptoKeys "github.com/tendermint/go-crypto/keys"
|
||||
"github.com/tendermint/tendermint/p2p"
|
||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
dbm "github.com/tendermint/tmlibs/db"
|
||||
"github.com/tendermint/tmlibs/log"
|
||||
)
|
||||
|
||||
func TestKeys(t *testing.T) {
|
||||
@ -62,7 +57,6 @@ func TestKeys(t *testing.T) {
|
||||
res, body = request(t, port, "GET", "/keys", nil)
|
||||
require.Equal(t, http.StatusOK, res.StatusCode, body)
|
||||
var m [1]keys.KeyOutput
|
||||
fmt.Println("BUF", body)
|
||||
err = json.Unmarshal([]byte(body), &m)
|
||||
require.Nil(t, err)
|
||||
|
||||
@ -73,7 +67,6 @@ func TestKeys(t *testing.T) {
|
||||
res, body = request(t, port, "GET", "/keys/test", nil)
|
||||
require.Equal(t, http.StatusOK, res.StatusCode, body)
|
||||
var m2 keys.KeyOutput
|
||||
fmt.Println("BUF", body)
|
||||
err = json.Unmarshal([]byte(body), &m2)
|
||||
require.Nil(t, err)
|
||||
|
||||
@ -95,18 +88,6 @@ func TestKeys(t *testing.T) {
|
||||
require.Equal(t, http.StatusOK, res.StatusCode, body)
|
||||
}
|
||||
|
||||
// TODO/XXX: We should be spawning what we need in process, not shelling out
|
||||
func junkInit(t *testing.T) (kill func(), port string, seed string) {
|
||||
seed = tests.TestInitBasecoin(t)
|
||||
cmdStart := tests.StartNodeServerForTest(t)
|
||||
cmdLCD, port := tests.StartLCDServerForTest(t)
|
||||
kill = func() {
|
||||
cmdLCD.Process.Kill()
|
||||
cmdStart.Process.Kill()
|
||||
}
|
||||
return kill, port, seed
|
||||
}
|
||||
|
||||
func TestVersion(t *testing.T) {
|
||||
kill, port, _ := junkInit(t)
|
||||
defer kill()
|
||||
@ -146,6 +127,8 @@ func TestBlock(t *testing.T) {
|
||||
kill, port, _ := junkInit(t)
|
||||
defer kill()
|
||||
|
||||
time.Sleep(time.Second * 2) // TODO: LOL -> wait for blocks
|
||||
|
||||
var resultBlock ctypes.ResultBlock
|
||||
|
||||
res, body := request(t, port, "GET", "/blocks/latest", nil)
|
||||
@ -176,6 +159,8 @@ func TestValidators(t *testing.T) {
|
||||
kill, port, _ := junkInit(t)
|
||||
defer kill()
|
||||
|
||||
time.Sleep(time.Second * 2) // TODO: LOL -> wait for blocks
|
||||
|
||||
var resultVals ctypes.ResultValidators
|
||||
|
||||
res, body := request(t, port, "GET", "/validatorsets/latest", nil)
|
||||
@ -203,19 +188,22 @@ func TestValidators(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCoinSend(t *testing.T) {
|
||||
kill, port, _ := junkInit(t)
|
||||
kill, port, seed := junkInit(t)
|
||||
defer kill()
|
||||
|
||||
// TODO make that account has coins
|
||||
kb := client.MockKeyBase()
|
||||
info, seed, err := kb.Create("account_with_coins", "1234567890", cryptoKeys.CryptoAlgo("ed25519"))
|
||||
require.Nil(t, err)
|
||||
addr := string(info.Address())
|
||||
time.Sleep(time.Second * 2) // TO
|
||||
|
||||
// query empty
|
||||
res, body := request(t, port, "GET", "/accounts/1234567890123456789012345678901234567890", nil)
|
||||
res, body := request(t, port, "GET", "/accounts/8FA6AB57AD6870F6B5B2E57735F38F2F30E73CB6", nil)
|
||||
require.Equal(t, http.StatusNoContent, res.StatusCode, body)
|
||||
|
||||
// create account from seed who has keys
|
||||
var jsonStr = []byte(fmt.Sprintf(`{"name":"account_with_coins", "password":"1234567890", "seed": "%s"}`, seed))
|
||||
res, body = request(t, port, "POST", "/keys", jsonStr)
|
||||
|
||||
assert.Equal(t, http.StatusOK, res.StatusCode, body)
|
||||
addr := body
|
||||
|
||||
// query
|
||||
res, body = request(t, port, "GET", "/accounts/"+addr, nil)
|
||||
require.Equal(t, http.StatusOK, res.StatusCode, body)
|
||||
@ -229,19 +217,15 @@ func TestCoinSend(t *testing.T) {
|
||||
]
|
||||
}`, body)
|
||||
|
||||
// create account to send in keybase
|
||||
var jsonStr = []byte(fmt.Sprintf(`{"name":"test", "password":"1234567890", "seed": "%s"}`, seed))
|
||||
res, body = request(t, port, "POST", "/keys", jsonStr)
|
||||
require.Equal(t, http.StatusOK, res.StatusCode, body)
|
||||
|
||||
// create receive address
|
||||
kb := client.MockKeyBase()
|
||||
receiveInfo, _, err := kb.Create("receive_address", "1234567890", cryptoKeys.CryptoAlgo("ed25519"))
|
||||
require.Nil(t, err)
|
||||
receiveAddr := string(receiveInfo.Address())
|
||||
|
||||
// send
|
||||
jsonStr = []byte(`{
|
||||
"name":"test",
|
||||
"name":"account_with_coins",
|
||||
"password":"1234567890",
|
||||
"amount":[{
|
||||
"denom": "mycoin",
|
||||
@ -268,30 +252,30 @@ func TestCoinSend(t *testing.T) {
|
||||
//__________________________________________________________
|
||||
// helpers
|
||||
|
||||
func defaultLogger() log.Logger {
|
||||
return log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "sdk/app")
|
||||
}
|
||||
// TODO/XXX: We should be spawning what we need in process, not shelling out
|
||||
func junkInit(t *testing.T) (kill func(), port string, seed string) {
|
||||
dir, err := ioutil.TempDir("", "tmp-basecoin-")
|
||||
require.Nil(t, err)
|
||||
|
||||
func prepareClient(t *testing.T) {
|
||||
db := dbm.NewMemDB()
|
||||
app := baseapp.NewBaseApp(t.Name(), defaultLogger(), db)
|
||||
viper.Set(client.FlagNode, "localhost:46657")
|
||||
header := abci.Header{Height: 1}
|
||||
app.BeginBlock(abci.RequestBeginBlock{Header: header})
|
||||
app.Commit()
|
||||
seed = tests.TestInitBasecoin(t, dir)
|
||||
cmdStart := tests.StartNodeServerForTest(t, dir)
|
||||
cmdLCD, port := tests.StartLCDServerForTest(t, dir)
|
||||
kill = func() {
|
||||
cmdLCD.Process.Kill()
|
||||
cmdStart.Process.Kill()
|
||||
os.Remove(dir)
|
||||
}
|
||||
return kill, port, seed
|
||||
}
|
||||
|
||||
func request(t *testing.T, port, method, path string, payload []byte) (*http.Response, string) {
|
||||
var res *http.Response
|
||||
var err error
|
||||
url := fmt.Sprintf("http://localhost:%v%v", port, path)
|
||||
fmt.Println("URL", url)
|
||||
req, err := http.NewRequest(method, url, bytes.NewBuffer(payload))
|
||||
require.Nil(t, err)
|
||||
res, err = http.DefaultClient.Do(req)
|
||||
// res, err = http.Post(url, "application/json", bytes.NewBuffer(payload))
|
||||
fmt.Println("METHOD", method)
|
||||
fmt.Println("RES", res)
|
||||
require.Nil(t, err)
|
||||
|
||||
output, err := ioutil.ReadAll(res.Body)
|
||||
@ -299,12 +283,3 @@ func request(t *testing.T, port, method, path string, payload []byte) (*http.Res
|
||||
|
||||
return res, string(output)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@ -49,14 +49,12 @@ func whereIsBasecli() string {
|
||||
}
|
||||
|
||||
// Init Basecoin Test
|
||||
func TestInitBasecoin(t *testing.T) string {
|
||||
Clean()
|
||||
|
||||
func TestInitBasecoin(t *testing.T, home string) string {
|
||||
var err error
|
||||
|
||||
password := "some-random-password"
|
||||
|
||||
initBasecoind := exec.Command(whereIsBasecoind(), "init", "--home", basecoindDir)
|
||||
initBasecoind := exec.Command(whereIsBasecoind(), "init", "--home", home)
|
||||
cmdWriter, err := initBasecoind.StdinPipe()
|
||||
require.Nil(t, err)
|
||||
|
||||
@ -78,9 +76,9 @@ func TestInitBasecoin(t *testing.T) string {
|
||||
// get seed from initialization
|
||||
theOutput := strings.Split(buf.String(), "\n")
|
||||
var seedLine int
|
||||
for seedLine, o := range theOutput {
|
||||
for _seedLine, o := range theOutput {
|
||||
if strings.HasPrefix(string(o), "Secret phrase") {
|
||||
seedLine++
|
||||
seedLine = _seedLine + 1
|
||||
break
|
||||
}
|
||||
}
|
||||
@ -174,7 +172,6 @@ func StartServer() error {
|
||||
|
||||
// Init Basecoin Test
|
||||
func InitServerForTest(t *testing.T) {
|
||||
// TODO cleanup doesn't work -> keys are still there in each iteration
|
||||
Clean()
|
||||
|
||||
var err error
|
||||
@ -201,9 +198,9 @@ func InitServerForTest(t *testing.T) {
|
||||
}
|
||||
|
||||
// expects TestInitBaseCoin to have been run
|
||||
func StartNodeServerForTest(t *testing.T) *exec.Cmd {
|
||||
func StartNodeServerForTest(t *testing.T, home string) *exec.Cmd {
|
||||
cmdName := whereIsBasecoind()
|
||||
cmdArgs := []string{"start", "--home", basecoindDir}
|
||||
cmdArgs := []string{"start", "--home", home}
|
||||
cmd := exec.Command(cmdName, cmdArgs...)
|
||||
err := cmd.Start()
|
||||
require.Nil(t, err)
|
||||
@ -211,25 +208,21 @@ func StartNodeServerForTest(t *testing.T) *exec.Cmd {
|
||||
}
|
||||
|
||||
// expects TestInitBaseCoin to have been run
|
||||
func StartLCDServerForTest(t *testing.T) (cmd *exec.Cmd, port string) {
|
||||
func StartLCDServerForTest(t *testing.T, home string) (cmd *exec.Cmd, port string) {
|
||||
cmdName := whereIsBasecli()
|
||||
port = strings.Split(server.FreeTCPAddr(t), ":")[2]
|
||||
cmdArgs := []string{
|
||||
"rest-server",
|
||||
"--home",
|
||||
basecoindDir,
|
||||
home,
|
||||
"--bind",
|
||||
fmt.Sprintf("localhost:%s", port),
|
||||
}
|
||||
fmt.Println("----------------------------")
|
||||
cmd = exec.Command(cmdName, cmdArgs...)
|
||||
fmt.Println("CMD", cmd)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
err := cmd.Start()
|
||||
require.Nil(t, err)
|
||||
fmt.Println("PORT", port)
|
||||
fmt.Println("----------------------------")
|
||||
time.Sleep(time.Second * 2) // TODO: LOL
|
||||
return cmd, port
|
||||
}
|
||||
@ -237,8 +230,10 @@ func StartLCDServerForTest(t *testing.T) (cmd *exec.Cmd, port string) {
|
||||
// clean the directories
|
||||
func Clean() {
|
||||
// ignore errors b/c the dirs may not yet exist
|
||||
os.Remove(basecoindDir)
|
||||
os.Remove(basecliDir)
|
||||
err := os.Remove(basecoindDir)
|
||||
panic(err)
|
||||
err = os.Remove(basecliDir)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@ -8,7 +8,7 @@ import (
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/builder"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/wire"
|
||||
)
|
||||
@ -24,6 +24,9 @@ func QueryAccountRequestHandler(storeName string, cdc *wire.Codec, parser sdk.Pa
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
addr := vars["address"]
|
||||
|
||||
fmt.Println("ADDR", addr)
|
||||
|
||||
bz, err := hex.DecodeString(addr)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
@ -32,7 +35,7 @@ func QueryAccountRequestHandler(storeName string, cdc *wire.Codec, parser sdk.Pa
|
||||
}
|
||||
key := sdk.Address(bz)
|
||||
|
||||
res, err := client.Query(key, c.storeName)
|
||||
res, err := builder.Query(key, c.storeName)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(fmt.Sprintf("Could't query account. Error: %s", err.Error())))
|
||||
@ -40,7 +43,7 @@ func QueryAccountRequestHandler(storeName string, cdc *wire.Codec, parser sdk.Pa
|
||||
}
|
||||
|
||||
// the query will return empty if there is no data for this account
|
||||
if res == nil {
|
||||
if len(res) == 0 {
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user