prysm-pulse/validator/rpc/apimiddleware/custom_hooks_test.go
Sammy Rosso 20f4d21b83
Keymanager API: Add validator voluntary exit endpoint (#12299)
* Initial setup

* Fix + Cleanup

* Add query

* Fix

* Add epoch

* James' review part 1

* James' review part 2

* James' review part 3

* Radek' review

* Gazelle

* Fix cycle

* Start unit test

* fixing part of the test

* Mostly fix test

* Fix tests

* Cleanup

* Handle error

* Remove times

* Fix all tests

* Fix accidental deletion

* Unmarshal epoch

* Add custom_type

* Small fix

* Fix epoch

* Lint fix

* Add test + fix empty query panic

* Add comment

* Fix regex

* Add correct error message

* Change current epoch to use slot

* Return error if incorrect epoch passed

* Remove redundant type conversion

* Fix tests

* gaz

* Remove nodeClient + pass slot

* Remove slot from parameters

* Fix tests

* Fix test attempt 2

* Fix test attempt 2

* Remove nodeClient from ProposeExit

* Fix

* Fix tests

---------

Co-authored-by: james-prysm <james@prysmaticlabs.com>
Co-authored-by: james-prysm <90280386+james-prysm@users.noreply.github.com>
2023-06-21 14:06:16 -05:00

50 lines
1.4 KiB
Go

package apimiddleware
import (
"bytes"
"encoding/json"
"fmt"
"net/http/httptest"
"testing"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v4/api/gateway/apimiddleware"
"github.com/prysmaticlabs/prysm/v4/testing/assert"
"github.com/prysmaticlabs/prysm/v4/testing/require"
)
func TestSetVoluntaryExitEpoch(t *testing.T) {
t.Run("ok", func(t *testing.T) {
endpoint := &apimiddleware.Endpoint{
PostRequest: &SetVoluntaryExitRequestJson{},
}
epoch := "300"
var body bytes.Buffer
request := httptest.NewRequest("POST", fmt.Sprintf("http://foo.example?epoch=%s", epoch), &body)
runDefault, errJson := setVoluntaryExitEpoch(endpoint, nil, request)
require.Equal(t, true, errJson == nil)
assert.Equal(t, apimiddleware.RunDefault(true), runDefault)
var b SetVoluntaryExitRequestJson
err := json.NewDecoder(request.Body).Decode(&b)
require.NoError(t, err)
require.Equal(t, epoch, b.Epoch)
})
t.Run("invalid query returns error", func(t *testing.T) {
endpoint := &apimiddleware.Endpoint{
PostRequest: &SetVoluntaryExitRequestJson{},
}
epoch := "/12"
var body bytes.Buffer
request := httptest.NewRequest("POST", fmt.Sprintf("http://foo.example?epoch=%s", epoch), &body)
runDefault, errJson := setVoluntaryExitEpoch(endpoint, nil, request)
assert.NotNil(t, errJson)
assert.Equal(t, apimiddleware.RunDefault(false), runDefault)
err := errors.New(errJson.Msg())
assert.ErrorContains(t, "invalid epoch", err)
})
}