Skip to content

Commit

Permalink
test: PostValidatorDashboardGroups and `GetValidatorDashboardGroupS…
Browse files Browse the repository at this point in the history
…ummary`
  • Loading branch information
LuccaBitfly committed Feb 13, 2025
1 parent 8f448bc commit 885cf5c
Showing 1 changed file with 133 additions and 0 deletions.
133 changes: 133 additions & 0 deletions backend/pkg/api/handlers/validator_dashboard_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package handlers

import (
"context"
"errors"
"io"
"strings"
"testing"

dataaccess "github.com/gobitfly/beaconchain/pkg/api/data_access"
"github.com/gobitfly/beaconchain/pkg/api/types"
"github.com/stretchr/testify/assert"
)

// ------------------------------------------------------------

type dataAccessStub struct {
dataaccess.DummyService
}

func (da *dataAccessStub) GetUserInfo(ctx context.Context, id uint64) (*types.UserInfo, error) {
return &types.UserInfo{
PremiumPerks: types.PremiumPerks{
ValidatorGroupsPerDashboard: 1,
},
}, nil
}

func (da *dataAccessStub) GetValidatorDashboardGroupCount(ctx context.Context, dashboardId types.VDBIdPrimary) (uint64, error) {
var count uint64
if dashboardId == 1 {
count = 1
}
return count, nil
}

// ------------------------------------------------------------

func handlerTestSetup() (context.Context, *HandlerService) {
ctx := context.WithValue(context.Background(), types.CtxUserIdKey, uint64(1))
da := &dataAccessStub{}
return ctx, NewHandlerService(da, da, nil, false)
}

func stringAsBody(s string) io.ReadCloser {
return io.NopCloser(strings.NewReader(s))
}

// ------------------------------------------------------------

func TestInputPostValidatorDashboardGroupsValidate(t *testing.T) {
var i inputPostValidatorDashboardGroups
params := make(map[string]string)
t.Run("success", func(t *testing.T) {
params["dashboard_id"] = "1"
body := stringAsBody(`{"name":"test"}`)
i, err := i.Validate(params, body)
assert.Nil(t, err)
assert.Equal(t, types.VDBIdPrimary(1), i.dashboardId)
assert.Equal(t, "test", i.name)
})
t.Run("empty name", func(t *testing.T) {
params["dashboard_id"] = "1"
body := stringAsBody(`{"name":""}`)
_, err := i.Validate(params, body)
assert.NotNil(t, err)
})
}
func TestPostValidatorDashboardGroups(t *testing.T) {
ctx, h := handlerTestSetup()

t.Run("success", func(t *testing.T) {
input := inputPostValidatorDashboardGroups{
dashboardId: 0,
name: "test",
}
_, err := h.PostValidatorDashboardGroups(ctx, input)
assert.Nil(t, err)
})
t.Run("group count reached", func(t *testing.T) {
input := inputPostValidatorDashboardGroups{
dashboardId: 1,
name: "test",
}
_, err := h.PostValidatorDashboardGroups(ctx, input)
assert.NotNil(t, err)
assert.True(t, errors.Is(err, errConflict))
})
}

// ------------------------------------------------------------

func TestInputGetValidatorDashboardGroupSummaryValidate(t *testing.T) {
var i inputGetValidatorDashboardGroupSummary
t.Run("success", func(t *testing.T) {
params := map[string]string{
"dashboard_id": "1",
"group_id": "1",
"period": "all_time",
}
i, err := i.Validate(params, nil)
assert.Nil(t, err)
assert.Equal(t, types.VDBIdPrimary(1), i.dashboardIdParam)
assert.Equal(t, int64(1), i.groupId)
})
t.Run("empty dashboard_id", func(t *testing.T) {
params := map[string]string{
"dashboard_id": "",
"group_id": "1",
"period": "all_time",
}
_, err := i.Validate(params, nil)
assert.NotNil(t, err)
})
t.Run("empty group_id", func(t *testing.T) {
params := map[string]string{
"dashboard_id": "1",
"group_id": "",
"period": "all_time",
}
_, err := i.Validate(params, nil)
assert.NotNil(t, err)
})
t.Run("empty period", func(t *testing.T) {
params := map[string]string{
"dashboard_id": "1",
"group_id": "1",
"period": "",
}
_, err := i.Validate(params, nil)
assert.NotNil(t, err)
})
}

0 comments on commit 885cf5c

Please sign in to comment.