-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75b9e89
commit b1fd29a
Showing
1 changed file
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |