lcd tests to assert
This commit is contained in:
parent
cdba13c531
commit
c7589cc67c
|
@ -12,6 +12,8 @@ import (
|
|||
"github.com/cosmos/cosmos-sdk/client"
|
||||
keys "github.com/cosmos/cosmos-sdk/client/keys"
|
||||
"github.com/cosmos/cosmos-sdk/examples/basecoin/app"
|
||||
"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"
|
||||
|
@ -20,113 +22,106 @@ import (
|
|||
)
|
||||
|
||||
func TestKeys(t *testing.T) {
|
||||
kb, db, err := initKeybase()
|
||||
if err != nil {
|
||||
t.Errorf("Couldn't init Keybase. Error $s", err.Error())
|
||||
}
|
||||
kb, db, err := initKeybase(t)
|
||||
require.Nil(t, err, "Couldn't init Keybase")
|
||||
|
||||
cdc := app.MakeCodec()
|
||||
r := initRouter(cdc)
|
||||
|
||||
// empty keys
|
||||
req, _ := http.NewRequest("GET", "/keys", nil)
|
||||
req, err := http.NewRequest("GET", "/keys", nil)
|
||||
require.Nil(t, err)
|
||||
res := httptest.NewRecorder()
|
||||
|
||||
r.ServeHTTP(res, req)
|
||||
checkResponseCode(t, http.StatusOK, res.Code)
|
||||
if body := res.Body.String(); body != "[]" {
|
||||
t.Errorf("Expected an empty array. Got %s", body)
|
||||
}
|
||||
assert.Equal(t, http.StatusOK, res.Code, "Expected response code")
|
||||
body := res.Body.String()
|
||||
require.Equal(t, body, "[]", "Expected an empty array")
|
||||
|
||||
info, _, err := kb.Create("test", "1234567890", cryptoKeys.CryptoAlgo("ed25519"))
|
||||
if err != nil {
|
||||
t.Errorf("Couldn't add key. Error $s", err.Error())
|
||||
}
|
||||
require.Nil(t, err, "Couldn't add key")
|
||||
|
||||
// existing keys
|
||||
req, _ = http.NewRequest("GET", "/keys", nil)
|
||||
req, err = http.NewRequest("GET", "/keys", nil)
|
||||
require.Nil(t, err)
|
||||
res = httptest.NewRecorder()
|
||||
|
||||
r.ServeHTTP(res, req)
|
||||
checkResponseCode(t, http.StatusOK, res.Code)
|
||||
assert.Equal(t, http.StatusOK, res.Code, "Expected response code")
|
||||
var m [1]keys.KeyOutput
|
||||
decoder := json.NewDecoder(res.Body)
|
||||
err = decoder.Decode(&m)
|
||||
|
||||
if m[0].Name != "test" {
|
||||
t.Errorf("Did not serve keys name correctly. Got %s", m[0].Name)
|
||||
}
|
||||
if m[0].Address != info.PubKey.Address().String() {
|
||||
t.Errorf("Did not serve keys Address correctly. Got %s, Expected %s", m[0].Address, info.PubKey.Address().String())
|
||||
}
|
||||
assert.Equal(t, m[0].Name, "test", "Did not serve keys name correctly")
|
||||
assert.Equal(t, m[0].Address, info.PubKey.Address().String(), "Did not serve keys Address correctly")
|
||||
|
||||
// select key
|
||||
req, _ = http.NewRequest("GET", "/keys/test", nil)
|
||||
res = httptest.NewRecorder()
|
||||
|
||||
r.ServeHTTP(res, req)
|
||||
checkResponseCode(t, http.StatusOK, res.Code)
|
||||
assert.Equal(t, http.StatusOK, res.Code, "Expected response code")
|
||||
var m2 keys.KeyOutput
|
||||
decoder = json.NewDecoder(res.Body)
|
||||
err = decoder.Decode(&m2)
|
||||
|
||||
if m2.Name != "test" {
|
||||
t.Errorf("Did not serve keys name correctly. Got %s", m2.Name)
|
||||
}
|
||||
if m2.Address != info.PubKey.Address().String() {
|
||||
t.Errorf("Did not serve keys Address correctly. Got %s, Expected %s", m2.Address, info.PubKey.Address().String())
|
||||
}
|
||||
assert.Equal(t, m2.Name, "test", "Did not serve keys name correctly")
|
||||
assert.Equal(t, m2.Address, info.PubKey.Address().String(), "Did not serve keys Address correctly")
|
||||
|
||||
// update key
|
||||
var jsonStr = []byte(`{"old_password":"1234567890", "new_password":"12345678901"}`)
|
||||
req, _ = http.NewRequest("PUT", "/keys/test", bytes.NewBuffer(jsonStr))
|
||||
req, err = http.NewRequest("PUT", "/keys/test", bytes.NewBuffer(jsonStr))
|
||||
require.Nil(t, err)
|
||||
res = httptest.NewRecorder()
|
||||
|
||||
r.ServeHTTP(res, req)
|
||||
checkResponseCode(t, http.StatusOK, res.Code)
|
||||
assert.Equal(t, http.StatusOK, res.Code, "Expected response code")
|
||||
|
||||
// here it should say unauthorized as we changed the password before
|
||||
req, _ = http.NewRequest("PUT", "/keys/test", bytes.NewBuffer(jsonStr))
|
||||
req, err = http.NewRequest("PUT", "/keys/test", bytes.NewBuffer(jsonStr))
|
||||
require.Nil(t, err)
|
||||
res = httptest.NewRecorder()
|
||||
|
||||
r.ServeHTTP(res, req)
|
||||
checkResponseCode(t, http.StatusUnauthorized, res.Code)
|
||||
assert.Equal(t, http.StatusUnauthorized, res.Code, "Expected response code")
|
||||
|
||||
// delete key
|
||||
jsonStr = []byte(`{"password":"12345678901"}`)
|
||||
req, _ = http.NewRequest("DELETE", "/keys/test", bytes.NewBuffer(jsonStr))
|
||||
req, err = http.NewRequest("DELETE", "/keys/test", bytes.NewBuffer(jsonStr))
|
||||
require.Nil(t, err)
|
||||
res = httptest.NewRecorder()
|
||||
|
||||
r.ServeHTTP(res, req)
|
||||
checkResponseCode(t, http.StatusOK, res.Code)
|
||||
assert.Equal(t, http.StatusOK, res.Code, "Expected response code")
|
||||
|
||||
db.Close()
|
||||
}
|
||||
|
||||
func TestNodeInfo(t *testing.T) {
|
||||
prepareApp(t)
|
||||
_, db, err := initKeybase()
|
||||
if err != nil {
|
||||
t.Errorf("Couldn't init Keybase. Error $s", err.Error())
|
||||
}
|
||||
_, db, err := initKeybase(t)
|
||||
require.Nil(t, err, "Couldn't init Keybase")
|
||||
cdc := app.MakeCodec()
|
||||
r := initRouter(cdc)
|
||||
|
||||
req, _ := http.NewRequest("GET", "/node_info", nil)
|
||||
req, err := http.NewRequest("GET", "/node_info", nil)
|
||||
require.Nil(t, err)
|
||||
res := httptest.NewRecorder()
|
||||
|
||||
r.ServeHTTP(res, req)
|
||||
checkResponseCode(t, http.StatusOK, res.Code)
|
||||
require.Equal(t, http.StatusOK, res.Code, "Expected response code")
|
||||
|
||||
var m p2p.NodeInfo
|
||||
decoder := json.NewDecoder(res.Body)
|
||||
err = decoder.Decode(&m)
|
||||
if err != nil {
|
||||
t.Errorf("Couldn't parse node info, Got %s", res.Body.String())
|
||||
}
|
||||
require.Nil(t, err, "Couldn't parse node info")
|
||||
|
||||
db.Close()
|
||||
}
|
||||
|
||||
//__________________________________________________________
|
||||
// helpers
|
||||
|
||||
func defaultLogger() log.Logger {
|
||||
return log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "sdk/app")
|
||||
}
|
||||
|
@ -142,19 +137,11 @@ func prepareApp(t *testing.T) {
|
|||
app.Commit()
|
||||
}
|
||||
|
||||
func initKeybase() (cryptoKeys.Keybase, *dbm.GoLevelDB, error) {
|
||||
func initKeybase(t *testing.T) (cryptoKeys.Keybase, *dbm.GoLevelDB, error) {
|
||||
os.RemoveAll("./testKeybase")
|
||||
db, err := dbm.NewGoLevelDB("keys", "./testKeybase")
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
require.Nil(t, err)
|
||||
kb := client.GetKeyBase(db)
|
||||
keys.SetKeyBase(kb)
|
||||
return kb, db, nil
|
||||
}
|
||||
|
||||
func checkResponseCode(t *testing.T, expected, actual int) {
|
||||
if expected != actual {
|
||||
t.Errorf("Expected response code %d. Got %d\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue