prysm-pulse/validator/keymanager/remote-web3signer/client_test.go
james-prysm d28ae62d02
Web3signer keymanager: key manager implementation (#10016)
* initial commit for work on web3signer-keymanager

* WIP key manager client

* adding in more valid unit tests

* refactoring some of my code to work better with current implementations

* adding comments, some error situations and fixing unit tests

* updating go.mod

* gaz

* addressing sandbox errors on test

* fixing more sand box debug items

* missed yet another error case on my test

* reverting go.mod

* addressing comments

* missed removal of unit test

* removing some deepsource issues

* adjusting based on review comments

* addressing comments

* adding the missed types

* changing public keys to only pull once

* fixing commented code format

* gaz

* Update validator/keymanager/remote-web3signer/keymanager.go

updating comments

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update validator/keymanager/remote-web3signer/keymanager.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update validator/keymanager/remote-web3signer/keymanager.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update validator/keymanager/remote-web3signer/keymanager.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update validator/keymanager/remote-web3signer/keymanager.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update validator/keymanager/remote-web3signer/keymanager.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update validator/keymanager/remote-web3signer/client.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* fixing conflicts

* addressing comments

* fixing deepsource issues

* more deep source issue fix

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
2022-01-05 14:32:46 -06:00

105 lines
3.8 KiB
Go

package remote_web3signer
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
// mockTransport is the mock Transport object
type mockTransport struct {
mockResponse *http.Response
}
// RoundTrip is mocking my own implementation of the RoundTripper interface
func (m *mockTransport) RoundTrip(*http.Request) (*http.Response, error) {
return m.mockResponse, nil
}
func getClientMockSignRequest() *SignRequest {
forkData := &Fork{
PreviousVersion: "",
CurrentVersion: "",
Epoch: "",
}
forkInfoData := &ForkInfo{
Fork: forkData,
GenesisValidatorsRoot: "",
}
AggregationSlotData := &AggregationSlot{Slot: ""}
// remember to replace signing root with hex encoding remove 0x
web3SignerRequest := SignRequest{
Type: "foo",
ForkInfo: forkInfoData,
SigningRoot: "0xfasd0fjsa0dfjas0dfjasdf",
AggregationSlot: AggregationSlotData,
}
return &web3SignerRequest
}
func TestClient_Sign_HappyPath(t *testing.T) {
json := `{
"signature": "0xb3baa751d0a9132cfe93e4e3d5ff9075111100e3789dca219ade5a24d27e19d16b3353149da1833e9b691bb38634e8dc04469be7032132906c927d7e1a49b414730612877bc6b2810c8f202daf793d1ab0d6b5cb21d52f9e52e883859887a5d9"
}`
// create a new reader with that JSON
r := ioutil.NopCloser(bytes.NewReader([]byte(json)))
mock := &mockTransport{mockResponse: &http.Response{
StatusCode: 200,
Body: r,
}}
cl := apiClient{BasePath: "example.com", restClient: &http.Client{Transport: mock}}
resp, err := cl.Sign(context.Background(), "a2b5aaad9c6efefe7bb9b1243a043404f3362937cfb6b31833929833173f476630ea2cfeb0d9ddf15f97ca8685948820", getClientMockSignRequest())
assert.NotNil(t, resp)
assert.Nil(t, err)
assert.EqualValues(t, "0xb3baa751d0a9132cfe93e4e3d5ff9075111100e3789dca219ade5a24d27e19d16b3353149da1833e9b691bb38634e8dc04469be7032132906c927d7e1a49b414730612877bc6b2810c8f202daf793d1ab0d6b5cb21d52f9e52e883859887a5d9", fmt.Sprintf("%#x", resp.Marshal()))
}
func TestClient_GetPublicKeys_HappyPath(t *testing.T) {
// public keys are returned hex encoded with 0x
json := `["0xa2b5aaad9c6efefe7bb9b1243a043404f3362937cfb6b31833929833173f476630ea2cfeb0d9ddf15f97ca8685948820"]`
// create a new reader with that JSON
r := ioutil.NopCloser(bytes.NewReader([]byte(json)))
mock := &mockTransport{mockResponse: &http.Response{
StatusCode: 200,
Body: r,
}}
cl := apiClient{BasePath: "example.com", restClient: &http.Client{Transport: mock}}
resp, err := cl.GetPublicKeys(context.Background(), "example.com/api/publickeys")
assert.NotNil(t, resp)
assert.Nil(t, err)
// we would like them as 48byte base64 without 0x
assert.EqualValues(t, "[162 181 170 173 156 110 254 254 123 185 177 36 58 4 52 4 243 54 41 55 207 182 179 24 51 146 152 51 23 63 71 102 48 234 44 254 176 217 221 241 95 151 202 134 133 148 136 32]", fmt.Sprintf("%v", resp[0][:]))
}
// TODO: not really in use, should be revisited
func TestClient_ReloadSignerKeys_HappyPath(t *testing.T) {
mock := &mockTransport{mockResponse: &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewReader(nil)),
}}
cl := apiClient{BasePath: "example.com", restClient: &http.Client{Transport: mock}}
err := cl.ReloadSignerKeys(context.Background())
assert.Nil(t, err)
}
// TODO: not really in use, should be revisited
func TestClient_GetServerStatus_HappyPath(t *testing.T) {
json := `"some server status, not sure what it looks like, need to find some sample data"`
r := ioutil.NopCloser(bytes.NewReader([]byte(json)))
mock := &mockTransport{mockResponse: &http.Response{
StatusCode: 200,
Body: r,
}}
cl := apiClient{BasePath: "example.com", restClient: &http.Client{Transport: mock}}
resp, err := cl.GetServerStatus(context.Background())
assert.NotNil(t, resp)
assert.Nil(t, err)
}