Skip to content

Commit

Permalink
test(voucher): adding unit test for claim and create commands (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy authored Jul 5, 2024
1 parent 96fe317 commit 4e0f796
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 9 deletions.
23 changes: 23 additions & 0 deletions internal/engine/command/role.middleware.go
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
}
8 changes: 4 additions & 4 deletions internal/engine/command/voucher/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ func (v *Voucher) claimHandler(cmd command.Command, _ entity.AppID, callerID str
validatorInfo, err := v.clientManager.GetValidatorInfo(address)
if err != nil {
log.Error("error get validator info", "err", err)
return cmd.ErrorResult(errors.New("bond error"))
return cmd.ErrorResult(err)
}

pubKey := validatorInfo.GetValidator().GetPublicKey()

amountInNanoPAC, err := amt.NewAmount(float64(voucher.Amount))
PACAmount, err := amt.NewAmount(float64(voucher.Amount))
if err != nil {
log.Error("error converting amount to nanoPAC", "err", err)
return cmd.ErrorResult(errors.New("bond error"))
return cmd.ErrorResult(err)
}

txHash, err := v.wallet.BondTransaction(pubKey, address, "Voucher claim for bond in validator", amountInNanoPAC.ToNanoPAC())
txHash, err := v.wallet.BondTransaction(pubKey, address, "Voucher claim from Pagu", PACAmount.ToNanoPAC())
if err != nil {
return cmd.ErrorResult(err)
}
Expand Down
102 changes: 102 additions & 0 deletions internal/engine/command/voucher/claim_test.go
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")
}
4 changes: 0 additions & 4 deletions internal/engine/command/voucher/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import (
)

func (v *Voucher) createHandler(cmd command.Command, _ entity.AppID, _ string, args ...string) command.CommandResult {
if cmd.User.Role != entity.Moderator {
return cmd.ErrorResult(errors.New("only users with Moderator role can create a new voucher"))
}

code := utils.RandomString(8, utils.CapitalAlphanumerical)
for _, err := v.db.GetVoucherByCode(code); err == nil; {
code = utils.RandomString(8, utils.CapitalAlphanumerical)
Expand Down
118 changes: 118 additions & 0 deletions internal/engine/command/voucher/create_test.go
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!")
})
}
2 changes: 1 addition & 1 deletion internal/engine/command/voucher/voucher.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (v *Voucher) GetCommand() command.Command {
},
SubCommands: nil,
AppIDs: entity.AllAppIDs(),
Middlewares: []command.MiddlewareFunc{middlewareHandler.CreateUser},
Middlewares: []command.MiddlewareFunc{middlewareHandler.CreateUser, middlewareHandler.OnlyModerator},
Handler: v.createHandler,
TargetFlag: command.TargetMaskModerator,
}
Expand Down

0 comments on commit 4e0f796

Please sign in to comment.