added tests for /keys
This commit is contained in:
parent
370d8df825
commit
7d36d953f2
|
@ -84,7 +84,7 @@ func UpdateKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
|
|||
// TODO check if account exists and if password is correct
|
||||
err = kb.Update(name, m.OldPassword, m.NewPassword)
|
||||
if err != nil {
|
||||
w.WriteHeader(500)
|
||||
w.WriteHeader(401)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
|
|
@ -41,6 +41,11 @@ func GetKeyBase() (keys.Keybase, error) {
|
|||
return keybase, nil
|
||||
}
|
||||
|
||||
// used to set the keybase manually in test
|
||||
func SetKeyBase(kb keys.Keybase) {
|
||||
keybase = kb
|
||||
}
|
||||
|
||||
func printInfo(info keys.Info) {
|
||||
switch viper.Get(cli.OutputFlag) {
|
||||
case "text":
|
||||
|
|
|
@ -0,0 +1,114 @@
|
|||
package lcd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
keys "github.com/cosmos/cosmos-sdk/client/keys"
|
||||
"github.com/cosmos/cosmos-sdk/examples/basecoin/app"
|
||||
cryptoKeys "github.com/tendermint/go-crypto/keys"
|
||||
dbm "github.com/tendermint/tmlibs/db"
|
||||
)
|
||||
|
||||
func TestKeys(t *testing.T) {
|
||||
kb, err := initKeybase()
|
||||
if err != nil {
|
||||
t.Errorf("Couldn't init Keybase. Error $s", err.Error())
|
||||
}
|
||||
cdc := app.MakeCodec()
|
||||
r := initRouter(cdc)
|
||||
|
||||
// empty keys
|
||||
req, _ := http.NewRequest("GET", "/keys", nil)
|
||||
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)
|
||||
}
|
||||
|
||||
info, _, err := kb.Create("test", "1234567890", cryptoKeys.CryptoAlgo("ed25519"))
|
||||
if err != nil {
|
||||
t.Errorf("Couldn't add key. Error $s", err.Error())
|
||||
}
|
||||
|
||||
// existing keys
|
||||
req, _ = http.NewRequest("GET", "/keys", nil)
|
||||
res = httptest.NewRecorder()
|
||||
|
||||
r.ServeHTTP(res, req)
|
||||
checkResponseCode(t, http.StatusOK, res.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())
|
||||
}
|
||||
|
||||
// select key
|
||||
req, _ = http.NewRequest("GET", "/keys/test", nil)
|
||||
res = httptest.NewRecorder()
|
||||
|
||||
r.ServeHTTP(res, req)
|
||||
checkResponseCode(t, http.StatusOK, res.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())
|
||||
}
|
||||
|
||||
// update key
|
||||
var jsonStr = []byte(`{"old_password":"1234567890", "new_password":"12345678901"}`)
|
||||
req, _ = http.NewRequest("PUT", "/keys/test", bytes.NewBuffer(jsonStr))
|
||||
res = httptest.NewRecorder()
|
||||
|
||||
r.ServeHTTP(res, req)
|
||||
checkResponseCode(t, http.StatusOK, res.Code)
|
||||
|
||||
// here it should say unauthorized as we changed the password before
|
||||
req, _ = http.NewRequest("PUT", "/keys/test", bytes.NewBuffer(jsonStr))
|
||||
res = httptest.NewRecorder()
|
||||
|
||||
r.ServeHTTP(res, req)
|
||||
checkResponseCode(t, http.StatusUnauthorized, res.Code)
|
||||
|
||||
// delete key
|
||||
jsonStr = []byte(`{"password":"12345678901"}`)
|
||||
req, _ = http.NewRequest("DELETE", "/keys/test", bytes.NewBuffer(jsonStr))
|
||||
res = httptest.NewRecorder()
|
||||
|
||||
r.ServeHTTP(res, req)
|
||||
checkResponseCode(t, http.StatusOK, res.Code)
|
||||
}
|
||||
|
||||
func initKeybase() (cryptoKeys.Keybase, error) {
|
||||
os.RemoveAll("./testKeybase")
|
||||
db, err := dbm.NewGoLevelDB("keys", "./testKeybase")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
kb := client.GetKeyBase(db)
|
||||
keys.SetKeyBase(kb)
|
||||
return kb, 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