Refactor keys handler according to code reviewers

This commit is contained in:
HaoyangLiu 2018-10-05 07:19:52 +08:00
parent 1acab69326
commit 6d645f20fa
2 changed files with 24 additions and 21 deletions

View File

@ -177,7 +177,7 @@ func AddNewKeyRequestHandler(indent bool) http.HandlerFunc {
kb, err := GetKeyBase()
if err != nil {
w.WriteHeader(500)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
@ -207,8 +207,8 @@ func AddNewKeyRequestHandler(indent bool) http.HandlerFunc {
// check if already exists
infos, err := kb.List()
for _, i := range infos {
if i.GetName() == m.Name {
for _, info := range infos {
if info.GetName() == m.Name {
w.WriteHeader(http.StatusConflict)
w.Write([]byte(fmt.Sprintf("Account with name %s already exists.", m.Name)))
return
@ -311,8 +311,8 @@ func RecoverRequestHandler(indent bool) http.HandlerFunc {
}
// check if already exists
infos, err := kb.List()
for _, i := range infos {
if i.GetName() == name {
for _, info := range infos {
if info.GetName() == name {
w.WriteHeader(http.StatusConflict)
w.Write([]byte(fmt.Sprintf("Account with name %s already exists.", name)))
return

View File

@ -54,28 +54,19 @@ func TestKeys(t *testing.T) {
match := reg.MatchString(seed)
require.True(t, match, "Returned seed has wrong format", seed)
recoverName := "test_recovername"
recoverPassword := "0987654321"
// recover key
jsonStr := []byte(fmt.Sprintf(`{"password":"%s", "seed":"%s"}`, recoverPassword, seed))
res, body = Request(t, port, "POST", fmt.Sprintf("/keys/%s/recover", recoverName), jsonStr)
require.Equal(t, http.StatusOK, res.StatusCode, body)
var resp keys.KeyOutput
err = codec.Cdc.UnmarshalJSON([]byte(body), &resp)
require.Nil(t, err, body)
addr1Bech32 := resp.Address
_, err = sdk.AccAddressFromBech32(addr1Bech32)
require.NoError(t, err, "Failed to return a correct bech32 address")
recoverName := "test_recovername"
recoverPassword := "1234567890"
doRecoverKey(t, port, recoverName, recoverPassword, seed)
newName := "test_newname"
newPassword := "0987654321"
// add key
jsonStr = []byte(fmt.Sprintf(`{"name":"%s", "password":"%s", "seed":"%s"}`, newName, newPassword, seed))
jsonStr := []byte(fmt.Sprintf(`{"name":"%s", "password":"%s", "seed":"%s"}`, newName, newPassword, seed))
res, body = Request(t, port, "POST", "/keys", jsonStr)
require.Equal(t, http.StatusOK, res.StatusCode, body)
var resp keys.KeyOutput
err = codec.Cdc.UnmarshalJSON([]byte(body), &resp)
require.Nil(t, err, body)
@ -101,8 +92,6 @@ func TestKeys(t *testing.T) {
require.Equal(t, addrBech32, m[0].Address, "Did not serve keys Address correctly")
require.Equal(t, newName, m[1].Name, "Did not serve keys name correctly")
require.Equal(t, addr2Bech32, m[1].Address, "Did not serve keys Address correctly")
require.Equal(t, recoverName, m[2].Name, "Did not serve keys name correctly")
require.Equal(t, addr1Bech32, m[2].Address, "Did not serve keys Address correctly")
// select key
keyEndpoint := fmt.Sprintf("/keys/%s", newName)
@ -871,6 +860,20 @@ func doSendWithGas(t *testing.T, port, seed, name, password string, addr sdk.Acc
return
}
func doRecoverKey(t *testing.T, port, recoverName, recoverPassword, seed string) {
jsonStr := []byte(fmt.Sprintf(`{"password":"%s", "seed":"%s"}`, recoverPassword, seed))
res, body := Request(t, port, "POST", fmt.Sprintf("/keys/%s/recover", recoverName), jsonStr)
require.Equal(t, http.StatusOK, res.StatusCode, body)
var resp keys.KeyOutput
err := codec.Cdc.UnmarshalJSON([]byte(body), &resp)
require.Nil(t, err, body)
addr1Bech32 := resp.Address
_, err = sdk.AccAddressFromBech32(addr1Bech32)
require.NoError(t, err, "Failed to return a correct bech32 address")
}
func doSend(t *testing.T, port, seed, name, password string, addr sdk.AccAddress) (receiveAddr sdk.AccAddress, resultTx ctypes.ResultBroadcastTxCommit) {
res, body, receiveAddr := doSendWithGas(t, port, seed, name, password, addr, "", 0, "")
require.Equal(t, http.StatusOK, res.StatusCode, body)