44 lines
1.7 KiB
Go
44 lines
1.7 KiB
Go
package validator
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"github.com/stretchr/testify/assert"
|
||
|
|
|
||
|
|
"apskel-pos-be/internal/constants"
|
||
|
|
"apskel-pos-be/internal/contract"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestWalletValidator_AdjustRequest(t *testing.T) {
|
||
|
|
v := NewWalletValidator()
|
||
|
|
|
||
|
|
ok := &contract.AdjustCustomerWalletRequest{Currency: " point ", Amount: -25, Reason: " komplain #45 "}
|
||
|
|
err, _ := v.ValidateAdjustRequest(ok)
|
||
|
|
assert.NoError(t, err)
|
||
|
|
assert.Equal(t, constants.WalletCurrencyPoint, ok.Currency, "currency is normalised")
|
||
|
|
assert.Equal(t, "komplain #45", ok.Reason, "reason is trimmed")
|
||
|
|
|
||
|
|
legit := &contract.AdjustCustomerWalletRequest{Currency: "COIN", Amount: 5, Reason: "Bayar tunai tapi poin tidak masuk"}
|
||
|
|
err, _ = v.ValidateAdjustRequest(legit)
|
||
|
|
assert.NoError(t, err, "mentioning a cash payment is not a cash-out")
|
||
|
|
|
||
|
|
for name, req := range map[string]*contract.AdjustCustomerWalletRequest{
|
||
|
|
"nil": nil,
|
||
|
|
"no currency": {Amount: 5, Reason: "x"},
|
||
|
|
"unknown currency": {Currency: "GOLD", Amount: 5, Reason: "x"},
|
||
|
|
"zero amount": {Currency: "POINT", Amount: 0, Reason: "x"},
|
||
|
|
"no reason": {Currency: "POINT", Amount: 5, Reason: " "},
|
||
|
|
"reason too long": {Currency: "POINT", Amount: 5, Reason: strings.Repeat("a", 256)},
|
||
|
|
"cash-out reason": {Currency: "POINT", Amount: -500, Reason: "Pencairan saldo ke rekening"},
|
||
|
|
"cash-out, English": {Currency: "POINT", Amount: -500, Reason: "customer asked to cash out"},
|
||
|
|
"long key": {Currency: "POINT", Amount: 5, Reason: "x", IdempotencyKey: strings.Repeat("k", 51)},
|
||
|
|
} {
|
||
|
|
t.Run(name, func(t *testing.T) {
|
||
|
|
err, code := v.ValidateAdjustRequest(req)
|
||
|
|
assert.Error(t, err)
|
||
|
|
assert.NotEmpty(t, code)
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|