|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "io" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + dataaccess "github.com/gobitfly/beaconchain/pkg/api/data_access" |
| 11 | + "github.com/gobitfly/beaconchain/pkg/api/types" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | +) |
| 14 | + |
| 15 | +// ------------------------------------------------------------ |
| 16 | + |
| 17 | +type dataAccessStub struct { |
| 18 | + dataaccess.DummyService |
| 19 | +} |
| 20 | + |
| 21 | +func (da *dataAccessStub) GetUserInfo(ctx context.Context, id uint64) (*types.UserInfo, error) { |
| 22 | + return &types.UserInfo{ |
| 23 | + PremiumPerks: types.PremiumPerks{ |
| 24 | + ValidatorGroupsPerDashboard: 1, |
| 25 | + }, |
| 26 | + }, nil |
| 27 | +} |
| 28 | + |
| 29 | +func (da *dataAccessStub) GetValidatorDashboardGroupCount(ctx context.Context, dashboardId types.VDBIdPrimary) (uint64, error) { |
| 30 | + var count uint64 |
| 31 | + if dashboardId == 1 { |
| 32 | + count = 1 |
| 33 | + } |
| 34 | + return count, nil |
| 35 | +} |
| 36 | + |
| 37 | +// ------------------------------------------------------------ |
| 38 | + |
| 39 | +func handlerTestSetup() (context.Context, *HandlerService) { |
| 40 | + ctx := context.WithValue(context.Background(), types.CtxUserIdKey, uint64(1)) |
| 41 | + da := &dataAccessStub{} |
| 42 | + return ctx, NewHandlerService(da, da, nil, false) |
| 43 | +} |
| 44 | + |
| 45 | +func TestPostValidatorDashboardGroups(t *testing.T) { |
| 46 | + ctx, h := handlerTestSetup() |
| 47 | + |
| 48 | + t.Run("success", func(t *testing.T) { |
| 49 | + input := inputPostValidatorDashboardGroups{ |
| 50 | + dashboardId: 0, |
| 51 | + name: "test", |
| 52 | + } |
| 53 | + _, err := h.NewPostValidatorDashboardGroups(ctx, input) |
| 54 | + assert.Nil(t, err) |
| 55 | + }) |
| 56 | + t.Run("group count reached", func(t *testing.T) { |
| 57 | + input := inputPostValidatorDashboardGroups{ |
| 58 | + dashboardId: 1, |
| 59 | + name: "test", |
| 60 | + } |
| 61 | + _, err := h.NewPostValidatorDashboardGroups(ctx, input) |
| 62 | + assert.NotNil(t, err) |
| 63 | + assert.True(t, errors.Is(err, errConflict)) |
| 64 | + }) |
| 65 | +} |
| 66 | + |
| 67 | +// ------------------------------------------------------------ |
| 68 | + |
| 69 | +func stringAsBody(s string) io.ReadCloser { |
| 70 | + return io.NopCloser(strings.NewReader(s)) |
| 71 | +} |
| 72 | + |
| 73 | +func TestInputPostValidatorDashboardGroupsCheck(t *testing.T) { |
| 74 | + var i inputPostValidatorDashboardGroups |
| 75 | + params := make(map[string]string) |
| 76 | + t.Run("success", func(t *testing.T) { |
| 77 | + params["dashboard_id"] = "1" |
| 78 | + body := stringAsBody(`{"name":"test"}`) |
| 79 | + i, err := i.Check(params, body) |
| 80 | + assert.Nil(t, err) |
| 81 | + assert.Equal(t, types.VDBIdPrimary(1), i.dashboardId) |
| 82 | + assert.Equal(t, "test", i.name) |
| 83 | + }) |
| 84 | + t.Run("empty name", func(t *testing.T) { |
| 85 | + params["dashboard_id"] = "1" |
| 86 | + body := stringAsBody(`{"name":""}`) |
| 87 | + _, err := i.Check(params, body) |
| 88 | + assert.NotNil(t, err) |
| 89 | + }) |
| 90 | +} |
| 91 | + |
| 92 | +func TestInputGetValidatorDashboardGroupSummaryCheck(t *testing.T) { |
| 93 | + var i inputGetValidatorDashboardGroupSummary |
| 94 | + t.Run("success", func(t *testing.T) { |
| 95 | + params := map[string]string{ |
| 96 | + "dashboard_id": "1", |
| 97 | + "group_id": "1", |
| 98 | + "period": "all_time", |
| 99 | + } |
| 100 | + i, err := i.Check(params, nil) |
| 101 | + assert.Nil(t, err) |
| 102 | + assert.Equal(t, types.VDBIdPrimary(1), i.dashboardIdParam) |
| 103 | + assert.Equal(t, int64(1), i.groupId) |
| 104 | + }) |
| 105 | + t.Run("empty dashboard_id", func(t *testing.T) { |
| 106 | + params := map[string]string{ |
| 107 | + "dashboard_id": "", |
| 108 | + "group_id": "1", |
| 109 | + "period": "all_time", |
| 110 | + } |
| 111 | + _, err := i.Check(params, nil) |
| 112 | + assert.NotNil(t, err) |
| 113 | + }) |
| 114 | + t.Run("empty group_id", func(t *testing.T) { |
| 115 | + params := map[string]string{ |
| 116 | + "dashboard_id": "1", |
| 117 | + "group_id": "", |
| 118 | + "period": "all_time", |
| 119 | + } |
| 120 | + _, err := i.Check(params, nil) |
| 121 | + assert.NotNil(t, err) |
| 122 | + }) |
| 123 | + t.Run("empty period", func(t *testing.T) { |
| 124 | + params := map[string]string{ |
| 125 | + "dashboard_id": "1", |
| 126 | + "group_id": "1", |
| 127 | + "period": "", |
| 128 | + } |
| 129 | + _, err := i.Check(params, nil) |
| 130 | + assert.NotNil(t, err) |
| 131 | + }) |
| 132 | +} |
0 commit comments