forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge PR cosmos#3517: Increasing test coverage in keys/client package
- Loading branch information
1 parent
cff985f
commit b5fdb83
Showing
49 changed files
with
1,137 additions
and
312 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package keys | ||
|
||
import ( | ||
"bufio" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/spf13/viper" | ||
"github.com/tendermint/tendermint/libs/cli" | ||
|
||
"github.com/cosmos/cosmos-sdk/tests" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_runAddCmdBasic(t *testing.T) { | ||
cmd := addKeyCommand() | ||
assert.NotNil(t, cmd) | ||
|
||
// Missing input (enter password) | ||
err := runAddCmd(cmd, []string{"keyname"}) | ||
assert.EqualError(t, err, "EOF") | ||
|
||
// Prepare a keybase | ||
kbHome, kbCleanUp, err := tests.GetTempDir("Test_runDeleteCmd") | ||
assert.NoError(t, err) | ||
assert.NotNil(t, kbHome) | ||
defer kbCleanUp() | ||
viper.Set(cli.HomeFlag, kbHome) | ||
|
||
/// Test Text | ||
viper.Set(cli.OutputFlag, OutputFormatText) | ||
// Now enter password | ||
cleanUp1 := client.OverrideStdin(bufio.NewReader(strings.NewReader("test1234\ntest1234\n"))) | ||
defer cleanUp1() | ||
err = runAddCmd(cmd, []string{"keyname1"}) | ||
assert.NoError(t, err) | ||
|
||
/// Test Text - Replace? >> FAIL | ||
viper.Set(cli.OutputFlag, OutputFormatText) | ||
// Now enter password | ||
cleanUp2 := client.OverrideStdin(bufio.NewReader(strings.NewReader("test1234\ntest1234\n"))) | ||
defer cleanUp2() | ||
err = runAddCmd(cmd, []string{"keyname1"}) | ||
assert.Error(t, err) | ||
|
||
/// Test Text - Replace? Answer >> PASS | ||
viper.Set(cli.OutputFlag, OutputFormatText) | ||
// Now enter password | ||
cleanUp3 := client.OverrideStdin(bufio.NewReader(strings.NewReader("y\ntest1234\ntest1234\n"))) | ||
defer cleanUp3() | ||
err = runAddCmd(cmd, []string{"keyname1"}) | ||
assert.NoError(t, err) | ||
|
||
// Check JSON | ||
viper.Set(cli.OutputFlag, OutputFormatJSON) | ||
// Now enter password | ||
cleanUp4 := client.OverrideStdin(bufio.NewReader(strings.NewReader("test1234\ntest1234\n"))) | ||
defer cleanUp4() | ||
err = runAddCmd(cmd, []string{"keyname2"}) | ||
assert.NoError(t, err) | ||
} | ||
|
||
type MockResponseWriter struct { | ||
dataHeaderStatus int | ||
dataBody []byte | ||
} | ||
|
||
func (MockResponseWriter) Header() http.Header { | ||
panic("Unexpected call!") | ||
} | ||
|
||
func (w *MockResponseWriter) Write(data []byte) (int, error) { | ||
w.dataBody = append(w.dataBody, data...) | ||
return len(data), nil | ||
} | ||
|
||
func (w *MockResponseWriter) WriteHeader(statusCode int) { | ||
w.dataHeaderStatus = statusCode | ||
} | ||
|
||
func TestCheckAndWriteErrorResponse(t *testing.T) { | ||
mockRW := MockResponseWriter{} | ||
|
||
mockRW.WriteHeader(100) | ||
assert.Equal(t, 100, mockRW.dataHeaderStatus) | ||
|
||
detected := CheckAndWriteErrorResponse(&mockRW, http.StatusBadRequest, errors.New("some ERROR")) | ||
require.True(t, detected) | ||
require.Equal(t, http.StatusBadRequest, mockRW.dataHeaderStatus) | ||
require.Equal(t, "some ERROR", string(mockRW.dataBody[:])) | ||
|
||
mockRW = MockResponseWriter{} | ||
detected = CheckAndWriteErrorResponse(&mockRW, http.StatusBadRequest, nil) | ||
require.False(t, detected) | ||
require.Equal(t, 0, mockRW.dataHeaderStatus) | ||
require.Equal(t, "", string(mockRW.dataBody[:])) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package keys | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type testCases struct { | ||
Keys []KeyOutput | ||
Answers []KeyOutput | ||
JSON [][]byte | ||
} | ||
|
||
func getTestCases() testCases { | ||
return testCases{ | ||
[]KeyOutput{ | ||
{"A", "B", "C", "D", "E"}, | ||
{"A", "B", "C", "D", ""}, | ||
{"", "B", "C", "D", ""}, | ||
{"", "", "", "", ""}, | ||
}, | ||
make([]KeyOutput, 4), | ||
[][]byte{ | ||
[]byte(`{"name":"A","type":"B","address":"C","pub_key":"D","mnemonic":"E"}`), | ||
[]byte(`{"name":"A","type":"B","address":"C","pub_key":"D"}`), | ||
[]byte(`{"name":"","type":"B","address":"C","pub_key":"D"}`), | ||
[]byte(`{"name":"","type":"","address":"","pub_key":""}`), | ||
}, | ||
} | ||
} | ||
|
||
func TestMarshalJSON(t *testing.T) { | ||
type args struct { | ||
o KeyOutput | ||
} | ||
|
||
data := getTestCases() | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
want []byte | ||
wantErr bool | ||
}{ | ||
{"basic", args{data.Keys[0]}, []byte(data.JSON[0]), false}, | ||
{"mnemonic is optional", args{data.Keys[1]}, []byte(data.JSON[1]), false}, | ||
|
||
// REVIEW: Are the next results expected?? | ||
{"empty name", args{data.Keys[2]}, []byte(data.JSON[2]), false}, | ||
{"empty object", args{data.Keys[3]}, []byte(data.JSON[3]), false}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := MarshalJSON(tt.args.o) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("MarshalJSON() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
fmt.Printf("%s\n", got) | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("MarshalJSON() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestUnmarshalJSON(t *testing.T) { | ||
type args struct { | ||
bz []byte | ||
ptr interface{} | ||
} | ||
|
||
data := getTestCases() | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
}{ | ||
{"basic", args{data.JSON[0], &data.Answers[0]}, false}, | ||
{"mnemonic is optional", args{data.JSON[1], &data.Answers[1]}, false}, | ||
|
||
// REVIEW: Are the next results expected?? | ||
{"empty name", args{data.JSON[2], &data.Answers[2]}, false}, | ||
{"empty object", args{data.JSON[3], &data.Answers[3]}, false}, | ||
} | ||
for idx, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := UnmarshalJSON(tt.args.bz, tt.args.ptr); (err != nil) != tt.wantErr { | ||
t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
|
||
// Confirm deserialized objects are the same | ||
require.Equal(t, data.Keys[idx], data.Answers[idx]) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.