2022-11-22 12:12:55 +00:00
|
|
|
//go:build use_beacon_api
|
|
|
|
// +build use_beacon_api
|
|
|
|
|
|
|
|
package beacon_api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-12-06 12:27:26 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/pkg/errors"
|
2022-11-28 10:58:04 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v3/api/gateway/apimiddleware"
|
2022-11-22 12:12:55 +00:00
|
|
|
rpcmiddleware "github.com/prysmaticlabs/prysm/v3/beacon-chain/rpc/apimiddleware"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/testing/require"
|
2022-12-06 12:27:26 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v3/validator/client/beacon-api/mock"
|
2022-11-22 12:12:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetGenesis_ValidGenesis(t *testing.T) {
|
2022-12-06 12:27:26 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
genesisResponseJson := rpcmiddleware.GenesisResponseJson{}
|
|
|
|
jsonRestHandler := mock.NewMockjsonRestHandler(ctrl)
|
|
|
|
jsonRestHandler.EXPECT().GetRestJsonResponse(
|
|
|
|
"/eth/v1/beacon/genesis",
|
|
|
|
&genesisResponseJson,
|
|
|
|
).Return(
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
).SetArg(
|
|
|
|
1,
|
|
|
|
rpcmiddleware.GenesisResponseJson{
|
|
|
|
Data: &rpcmiddleware.GenesisResponse_GenesisJson{
|
|
|
|
GenesisTime: "1234",
|
|
|
|
GenesisValidatorsRoot: "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
).Times(1)
|
|
|
|
|
|
|
|
genesisProvider := &beaconApiGenesisProvider{jsonRestHandler: jsonRestHandler}
|
2022-12-05 10:27:41 +00:00
|
|
|
resp, httpError, err := genesisProvider.GetGenesis()
|
2022-11-22 12:12:55 +00:00
|
|
|
assert.NoError(t, err)
|
2022-11-28 10:58:04 +00:00
|
|
|
assert.Equal(t, (*apimiddleware.DefaultErrorJson)(nil), httpError)
|
2022-11-22 12:12:55 +00:00
|
|
|
require.NotNil(t, resp)
|
2022-12-05 10:27:41 +00:00
|
|
|
assert.Equal(t, "1234", resp.GenesisTime)
|
|
|
|
assert.Equal(t, "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2", resp.GenesisValidatorsRoot)
|
2022-11-22 12:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetGenesis_NilData(t *testing.T) {
|
2022-12-06 12:27:26 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
genesisResponseJson := rpcmiddleware.GenesisResponseJson{}
|
|
|
|
jsonRestHandler := mock.NewMockjsonRestHandler(ctrl)
|
|
|
|
jsonRestHandler.EXPECT().GetRestJsonResponse(
|
|
|
|
"/eth/v1/beacon/genesis",
|
|
|
|
&genesisResponseJson,
|
|
|
|
).Return(
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
).SetArg(
|
|
|
|
1,
|
|
|
|
rpcmiddleware.GenesisResponseJson{Data: nil},
|
|
|
|
).Times(1)
|
|
|
|
|
|
|
|
genesisProvider := &beaconApiGenesisProvider{jsonRestHandler: jsonRestHandler}
|
2022-12-05 10:27:41 +00:00
|
|
|
_, httpError, err := genesisProvider.GetGenesis()
|
2022-11-28 10:58:04 +00:00
|
|
|
assert.Equal(t, (*apimiddleware.DefaultErrorJson)(nil), httpError)
|
2022-12-05 10:27:41 +00:00
|
|
|
assert.ErrorContains(t, "genesis data is nil", err)
|
2022-11-22 12:12:55 +00:00
|
|
|
}
|
|
|
|
|
2022-12-06 12:27:26 +00:00
|
|
|
func TestGetGenesis_JsonResponseError(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
expectedHttpErrorJson := &apimiddleware.DefaultErrorJson{
|
|
|
|
Message: "http error message",
|
|
|
|
Code: 999,
|
|
|
|
}
|
|
|
|
|
|
|
|
genesisResponseJson := rpcmiddleware.GenesisResponseJson{}
|
|
|
|
jsonRestHandler := mock.NewMockjsonRestHandler(ctrl)
|
|
|
|
jsonRestHandler.EXPECT().GetRestJsonResponse(
|
|
|
|
"/eth/v1/beacon/genesis",
|
|
|
|
&genesisResponseJson,
|
|
|
|
).Return(
|
|
|
|
expectedHttpErrorJson,
|
|
|
|
errors.New("some specific json response error"),
|
|
|
|
).Times(1)
|
|
|
|
|
|
|
|
genesisProvider := &beaconApiGenesisProvider{jsonRestHandler: jsonRestHandler}
|
2022-12-05 10:27:41 +00:00
|
|
|
_, httpError, err := genesisProvider.GetGenesis()
|
2022-12-06 12:27:26 +00:00
|
|
|
assert.ErrorContains(t, "failed to get json response", err)
|
|
|
|
assert.ErrorContains(t, "some specific json response error", err)
|
|
|
|
assert.DeepEqual(t, expectedHttpErrorJson, httpError)
|
2022-11-22 12:12:55 +00:00
|
|
|
}
|