-
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.
test(voucher): adding unit test for claim and create commands (#145)
- Loading branch information
Showing
6 changed files
with
248 additions
and
9 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,23 @@ | ||
package command | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/pagu-project/Pagu/internal/entity" | ||
) | ||
|
||
func (h *MiddlewareHandler) OnlyAdmin(cmd *Command, _ entity.AppID, _ string, _ ...string) error { | ||
if cmd.User.Role != entity.Admin { | ||
return errors.New("this command is Only Admin") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (h *MiddlewareHandler) OnlyModerator(cmd *Command, _ entity.AppID, _ string, _ ...string) error { | ||
if cmd.User.Role != entity.Moderator { | ||
return errors.New("this command is Only Moderator") | ||
} | ||
|
||
return nil | ||
} |
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
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,102 @@ | ||
package voucher | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
pactus "github.com/pactus-project/pactus/www/grpc/gen/go" | ||
"github.com/pagu-project/Pagu/internal/engine/command" | ||
"github.com/pagu-project/Pagu/internal/entity" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestClaimNormal(t *testing.T) { | ||
voucher, db, client, wallet := setup(t) | ||
|
||
t.Run("normal", func(t *testing.T) { | ||
amount := uint(100) | ||
db.EXPECT().GetVoucherByCode("12345678").Return( | ||
entity.Voucher{ | ||
ValidMonths: 1, | ||
Amount: amount, | ||
ID: 1, | ||
}, nil, | ||
).AnyTimes() | ||
|
||
client.EXPECT().GetValidatorInfo("pc1z").Return( | ||
&pactus.GetValidatorResponse{ | ||
Validator: &pactus.ValidatorInfo{ | ||
PublicKey: "pc1z", | ||
}, | ||
}, nil, | ||
).AnyTimes() | ||
|
||
wallet.EXPECT().BondTransaction("pc1z", "pc1z", "Voucher claim from Pagu", int64(amount*1e9)).Return( | ||
"0x1", nil, | ||
).AnyTimes() | ||
|
||
db.EXPECT().ClaimVoucher(uint(1), "0x1", uint(1)).Return( | ||
nil, | ||
).AnyTimes() | ||
|
||
cmd := command.Command{ | ||
User: &entity.User{ | ||
ID: 1, | ||
}, | ||
} | ||
|
||
result := voucher.claimHandler(cmd, entity.AppIdDiscord, "", "12345678", "pc1z") | ||
assert.True(t, result.Successful) | ||
assert.Equal(t, result.Message, "Voucher claimed successfully: https://pacviewer.com/transaction/0x1") | ||
}) | ||
|
||
t.Run("wrong code", func(t *testing.T) { | ||
cmd := command.Command{ | ||
User: &entity.User{ | ||
ID: 1, | ||
}, | ||
} | ||
|
||
result := voucher.claimHandler(cmd, entity.AppIdDiscord, "", "0", "pc1z") | ||
assert.False(t, result.Successful) | ||
assert.Equal(t, result.Message, "An error occurred: voucher code is not valid, length must be 8") | ||
}) | ||
} | ||
|
||
func TestClaimNotZFound(t *testing.T) { | ||
voucher, db, _, _ := setup(t) | ||
|
||
db.EXPECT().GetVoucherByCode("12345678").Return( | ||
entity.Voucher{}, errors.New(""), | ||
).AnyTimes() | ||
|
||
cmd := command.Command{ | ||
User: &entity.User{ | ||
ID: 1, | ||
}, | ||
} | ||
|
||
result := voucher.claimHandler(cmd, entity.AppIdDiscord, "", "12345678", "pc1z") | ||
assert.False(t, result.Successful) | ||
assert.Equal(t, result.Message, "An error occurred: voucher code is not valid, no voucher found") | ||
} | ||
|
||
func TestClaimAlreadyClaimed(t *testing.T) { | ||
voucher, db, _, _ := setup(t) | ||
|
||
db.EXPECT().GetVoucherByCode("12345678").Return( | ||
entity.Voucher{ | ||
TxHash: "123456789", | ||
}, nil, | ||
).AnyTimes() | ||
|
||
cmd := command.Command{ | ||
User: &entity.User{ | ||
ID: 1, | ||
}, | ||
} | ||
|
||
result := voucher.claimHandler(cmd, entity.AppIdDiscord, "", "12345678", "pc1z") | ||
assert.False(t, result.Successful) | ||
assert.Equal(t, result.Message, "An error occurred: voucher code claimed before") | ||
} |
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
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,118 @@ | ||
package voucher | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/pagu-project/Pagu/internal/engine/command" | ||
"github.com/pagu-project/Pagu/internal/entity" | ||
"github.com/pagu-project/Pagu/internal/repository" | ||
"github.com/pagu-project/Pagu/pkg/client" | ||
"github.com/pagu-project/Pagu/pkg/wallet" | ||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/mock/gomock" | ||
) | ||
|
||
func setup(t *testing.T) (Voucher, repository.MockDatabase, client.MockManager, wallet.MockIWallet) { | ||
ctrl := gomock.NewController(t) | ||
|
||
mockDB := repository.NewMockDatabase(ctrl) | ||
mockClient := client.NewMockManager(ctrl) | ||
mockWallet := wallet.NewMockIWallet(ctrl) | ||
|
||
mockVoucher := NewVoucher(mockDB, mockWallet, mockClient) | ||
|
||
return mockVoucher, *mockDB, *mockClient, *mockWallet | ||
} | ||
|
||
func TestCreate(t *testing.T) { | ||
voucher, db, _, _ := setup(t) | ||
|
||
t.Run("normal", func(t *testing.T) { | ||
db.EXPECT().GetVoucherByCode(gomock.Any()).Return( | ||
entity.Voucher{}, errors.New(""), | ||
).AnyTimes() | ||
|
||
db.EXPECT().AddVoucher(gomock.Any()).Return(nil).AnyTimes() | ||
|
||
cmd := command.Command{ | ||
User: &entity.User{ | ||
ID: 1, | ||
}, | ||
} | ||
|
||
result := voucher.createHandler(cmd, entity.AppIdDiscord, "", "100", "1") | ||
assert.True(t, result.Successful) | ||
assert.Contains(t, result.Message, "Voucher created successfully!") | ||
}) | ||
|
||
t.Run("more than 1000 PAC", func(t *testing.T) { | ||
db.EXPECT().GetVoucherByCode(gomock.Any()).Return( | ||
entity.Voucher{}, errors.New(""), | ||
).AnyTimes() | ||
|
||
db.EXPECT().AddVoucher(gomock.Any()).Return(nil).AnyTimes() | ||
|
||
cmd := command.Command{ | ||
User: &entity.User{ | ||
ID: 1, | ||
}, | ||
} | ||
|
||
result := voucher.createHandler(cmd, entity.AppIdDiscord, "", "1001", "1") | ||
assert.False(t, result.Successful) | ||
assert.Contains(t, result.Message, "stake amount is more than 1000") | ||
}) | ||
|
||
t.Run("wrong amount", func(t *testing.T) { | ||
db.EXPECT().GetVoucherByCode(gomock.Any()).Return( | ||
entity.Voucher{}, errors.New(""), | ||
).AnyTimes() | ||
|
||
db.EXPECT().AddVoucher(gomock.Any()).Return(nil).AnyTimes() | ||
|
||
cmd := command.Command{ | ||
User: &entity.User{ | ||
ID: 1, | ||
}, | ||
} | ||
|
||
result := voucher.createHandler(cmd, entity.AppIdDiscord, "", "1.2", "1") | ||
assert.False(t, result.Successful) | ||
}) | ||
|
||
t.Run("wrong month", func(t *testing.T) { | ||
db.EXPECT().GetVoucherByCode(gomock.Any()).Return( | ||
entity.Voucher{}, errors.New(""), | ||
).AnyTimes() | ||
|
||
db.EXPECT().AddVoucher(gomock.Any()).Return(nil).AnyTimes() | ||
|
||
cmd := command.Command{ | ||
User: &entity.User{ | ||
ID: 1, | ||
}, | ||
} | ||
|
||
result := voucher.createHandler(cmd, entity.AppIdDiscord, "", "100", "1.1") | ||
assert.False(t, result.Successful) | ||
}) | ||
|
||
t.Run("normal with optional arguments", func(t *testing.T) { | ||
db.EXPECT().GetVoucherByCode(gomock.Any()).Return( | ||
entity.Voucher{}, errors.New(""), | ||
).AnyTimes() | ||
|
||
db.EXPECT().AddVoucher(gomock.Any()).Return(nil).AnyTimes() | ||
|
||
cmd := command.Command{ | ||
User: &entity.User{ | ||
ID: 1, | ||
}, | ||
} | ||
|
||
result := voucher.createHandler(cmd, entity.AppIdDiscord, "", "100", "12", "Kayhan", "Testnet node") | ||
assert.True(t, result.Successful) | ||
assert.Contains(t, result.Message, "Voucher created successfully!") | ||
}) | ||
} |
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