most tests working

This commit is contained in:
Fabian Weber
2018-03-17 22:14:19 +01:00
committed by Ethan Buchman
parent 579bd56127
commit 1cd6ec1084
4 changed files with 69 additions and 84 deletions
+21 -9
View File
@@ -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
}
+30 -55
View File
@@ -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
}