Skip to content

Commit

Permalink
test: test proof of concept
Browse files Browse the repository at this point in the history
  • Loading branch information
LuccaBitfly committed Jan 31, 2025
1 parent 75b9e89 commit b1fd29a
Showing 1 changed file with 112 additions and 0 deletions.
112 changes: 112 additions & 0 deletions backend/pkg/api/handlers/validator_dashboard_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package handlers

import (
"context"
"errors"
"io"
"net/url"
"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 TestPostValidatorDashboardGroups(t *testing.T) {
ctx, h := handlerTestSetup()

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

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

func TestInputPostValidatorDashboardGroupsCheck(t *testing.T) {
var i inputPostValidatorDashboardGroups
params := url.Values{}
t.Run("success", func(t *testing.T) {
params.Set("dashboard_id", "1")
readCloser := io.NopCloser(strings.NewReader(`{"name":"test"}`))
i, err := i.Check(params, readCloser)
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.Set("dashboard_id", "1")
readCloser := io.NopCloser(strings.NewReader(`{"name":""}`))
_, err := i.Check(params, readCloser)
assert.NotNil(t, err)
})
}

func TestInputGetValidatorDashboardGroupSummaryCheck(t *testing.T) {
var i inputGetValidatorDashboardGroupSummary
params := url.Values{}
t.Run("success", func(t *testing.T) {
params.Set("dashboard_id", "1")
params.Set("group_id", "1")
i, err := i.Check(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.Set("dashboard_id", "")
params.Set("group_id", "1")
_, err := i.Check(params, nil)
assert.NotNil(t, err)
})
t.Run("empty group_id", func(t *testing.T) {
params.Set("dashboard_id", "1")
params.Set("group_id", "")
_, err := i.Check(params, nil)
assert.NotNil(t, err)
})
}

0 comments on commit b1fd29a

Please sign in to comment.