Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit 6d45928

Browse files
committed
[#1839] Percentage can be zero
1 parent 1a34dc7 commit 6d45928

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

api/jsonapi_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func TestPatchProfileCurrencyUpdate(t *testing.T) {
193193
}, nil, validateProfile)
194194
}
195195

196-
func TestPatchProfileValidatesAsInvalid(t *testing.T) {
196+
func TestPatchProfileCanBeInvalid(t *testing.T) {
197197
var (
198198
// init profile for patch
199199
postProfile = `{
@@ -245,13 +245,13 @@ func TestPatchProfileValidatesAsInvalid(t *testing.T) {
245245
invalidPatchProfile = `{
246246
"moderatorInfo": {
247247
"fee": {
248-
"percentage": 0
248+
"percentage": -1
249249
}
250250
}
251251
}`
252252
)
253253

254-
expectedErr := fmt.Errorf("invalid profile: %s", repo.ErrModeratorFeeHasNonPositivePercent)
254+
expectedErr := fmt.Errorf("invalid profile: %s", repo.ErrModeratorFeeHasNegativePercentage)
255255
runAPITests(t, apiTests{
256256
{"POST", "/ob/profile", postProfile, 200, anyResponseJSON},
257257
{"PATCH", "/ob/profile", patchProfile, 200, anyResponseJSON},

repo/profile.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ var (
1919
ErrMissingModeratorFee = errors.New("moderator info is missing fee schedule")
2020
// ErrUnknownModeratorFeeType indicates the feeType is unknown
2121
ErrUnknownModeratorFeeType = errors.New("moderator fee type is unknown")
22-
// ErrModeratorFeeHasNonPositivePercent indicates when the percentage is non-positive but should be
23-
ErrModeratorFeeHasNonPositivePercent = errors.New("percentage moderator fee should be greater than zero")
22+
// ErrModeratorFeeHasNegativePercentage indicates when the percentage is non-positive but should be
23+
ErrModeratorFeeHasNegativePercentage = errors.New("percentage moderator fee should not be negative")
2424
// ErrFixedFeeHasNonZeroPercentage indicates when the percentage is not zero but should be
2525
ErrFixedFeeHasNonZeroPercentage = errors.New("fixed moderator fee should have a zero percentage amount")
2626
// ErrPercentageFeeHasFixedFee indicates that a fixed fee is included when there should not be
@@ -132,15 +132,15 @@ func (p *Profile) validateModeratorFees() error {
132132
return err
133133
}
134134
case pb.Moderator_Fee_PERCENTAGE.String():
135-
if p.ModeratorInfo.Fee.Percentage <= 0 {
136-
return ErrModeratorFeeHasNonPositivePercent
135+
if p.ModeratorInfo.Fee.Percentage < 0 {
136+
return ErrModeratorFeeHasNegativePercentage
137137
}
138138
if p.ModeratorInfo.Fee.FixedFee != nil {
139139
return ErrPercentageFeeHasFixedFee
140140
}
141141
case pb.Moderator_Fee_FIXED_PLUS_PERCENTAGE.String():
142-
if p.ModeratorInfo.Fee.Percentage <= 0 {
143-
return ErrModeratorFeeHasNonPositivePercent
142+
if p.ModeratorInfo.Fee.Percentage < 0 {
143+
return ErrModeratorFeeHasNegativePercentage
144144
}
145145
if err := validateFixedFee(); err != nil {
146146
return err

repo/profile_test.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func TestProfileValidPercentageFee(t *testing.T) {
239239
}
240240
}
241241

242-
func TestProfileInvalidPercentageFeeZero(t *testing.T) {
242+
func TestProfileValidPercentageFeeZero(t *testing.T) {
243243
p := factory.NewProfile()
244244
p.ModeratorInfo = &repo.ModeratorInfo{
245245
Fee: &repo.ModeratorFee{
@@ -248,10 +248,8 @@ func TestProfileInvalidPercentageFeeZero(t *testing.T) {
248248
},
249249
}
250250

251-
if err := p.Valid(); err == nil {
252-
t.Errorf("expected zero percentage fee to be invalid")
253-
} else if err != repo.ErrModeratorFeeHasNonPositivePercent {
254-
t.Errorf("expected ErrModeratorFeeHasNonPositivePercent error, but was (%s)", err.Error())
251+
if err := p.Valid(); err != nil {
252+
t.Errorf("expected profile example to be valid")
255253
}
256254
}
257255

@@ -293,7 +291,7 @@ func TestProfileValidPercentageWithFixedFee(t *testing.T) {
293291
}
294292
}
295293

296-
func TestProfileInvalidPercentWithFixedHavingZeroPercent(t *testing.T) {
294+
func TestProfileValidPercentWithFixedHavingZeroPercent(t *testing.T) {
297295
p := factory.NewProfile()
298296
p.ModeratorInfo = &repo.ModeratorInfo{
299297
Fee: &repo.ModeratorFee{
@@ -306,10 +304,28 @@ func TestProfileInvalidPercentWithFixedHavingZeroPercent(t *testing.T) {
306304
},
307305
}
308306

307+
if err := p.Valid(); err != nil {
308+
t.Errorf("expected profile exmaple to be valid")
309+
}
310+
}
311+
312+
func TestProfileInvalidPercentWithFixedHavingNegativePercent(t *testing.T) {
313+
p := factory.NewProfile()
314+
p.ModeratorInfo = &repo.ModeratorInfo{
315+
Fee: &repo.ModeratorFee{
316+
FeeType: pb.Moderator_Fee_FIXED_PLUS_PERCENTAGE.String(),
317+
Percentage: -1,
318+
FixedFee: &repo.ModeratorFixedFee{
319+
Amount: "1234",
320+
AmountCurrency: factory.NewCurrencyDefinition("BTC"),
321+
},
322+
},
323+
}
324+
309325
if err := p.Valid(); err == nil {
310-
t.Errorf("expected percentage with fixed fee but percent is zero to be invalid")
311-
} else if err != repo.ErrModeratorFeeHasNonPositivePercent {
312-
t.Errorf("expected ErrModeratorFeeHasNonPositivePercent error, but got (%s)", err.Error())
326+
t.Errorf("expected percentage with fixed fee but negative percentage to be invalid")
327+
} else if err != repo.ErrModeratorFeeHasNegativePercentage {
328+
t.Errorf("expected ErrModeratorFeeHasNegativePercentage error, but got (%s)", err.Error())
313329
}
314330
}
315331

0 commit comments

Comments
 (0)