forked from urdt/ussd
Adapt voucher changes to package exports
This commit is contained in:
@@ -66,6 +66,7 @@ type Handlers struct {
|
||||
userdataStore common.DataStore
|
||||
flagManager *asm.FlagParser
|
||||
accountService remote.AccountServiceInterface
|
||||
prefixDb storage.PrefixDb
|
||||
}
|
||||
|
||||
func NewHandlers(appFlags *asm.FlagParser, userdataStore db.Db, accountService remote.AccountServiceInterface) (*Handlers, error) {
|
||||
@@ -75,10 +76,14 @@ func NewHandlers(appFlags *asm.FlagParser, userdataStore db.Db, accountService r
|
||||
userDb := &common.UserDataStore{
|
||||
Db: userdataStore,
|
||||
}
|
||||
// Instantiate the SubPrefixDb with "vouchers" prefix
|
||||
prefixDb := storage.NewSubPrefixDb(userdataStore, []byte("vouchers"))
|
||||
|
||||
h := &Handlers{
|
||||
userdataStore: userDb,
|
||||
flagManager: appFlags,
|
||||
accountService: accountService,
|
||||
prefixDb: prefixDb,
|
||||
}
|
||||
return h, nil
|
||||
}
|
||||
@@ -1051,13 +1056,13 @@ func (h *Handlers) SetDefaultVoucher(ctx context.Context, sym string, input []by
|
||||
if db.IsNotFound(err) {
|
||||
publicKey, err := store.ReadEntry(ctx, sessionId, common.DATA_PUBLIC_KEY)
|
||||
if err != nil {
|
||||
return res, nil
|
||||
return res, err
|
||||
}
|
||||
|
||||
// Fetch vouchers from the API using the public key
|
||||
vouchersResp, err := h.accountService.FetchVouchers(ctx, string(publicKey))
|
||||
if err != nil {
|
||||
return res, nil
|
||||
return res, err
|
||||
}
|
||||
|
||||
// Return if there is no voucher
|
||||
@@ -1114,54 +1119,33 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// process voucher data
|
||||
voucherSymbolList, voucherBalanceList := ProcessVouchers(vouchersResp.Result.Holdings)
|
||||
data := utils.ProcessVouchers(vouchersResp.Result.Holdings)
|
||||
|
||||
prefixdb := storage.NewSubPrefixDb(store, []byte("vouchers"))
|
||||
err = prefixdb.Put(ctx, []byte("sym"), []byte(voucherSymbolList))
|
||||
if err != nil {
|
||||
return res, nil
|
||||
// Store all voucher data
|
||||
dataMap := map[string]string{
|
||||
"sym": data.Symbols,
|
||||
"bal": data.Balances,
|
||||
"deci": data.Decimals,
|
||||
"addr": data.Addresses,
|
||||
}
|
||||
|
||||
err = prefixdb.Put(ctx, []byte("bal"), []byte(voucherBalanceList))
|
||||
if err != nil {
|
||||
return res, nil
|
||||
for key, value := range dataMap {
|
||||
if err := h.prefixDb.Put(ctx, []byte(key), []byte(value)); err != nil {
|
||||
return res, nil
|
||||
}
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// ProcessVouchers formats the holdings into symbol and balance lists.
|
||||
func ProcessVouchers(holdings []struct {
|
||||
ContractAddress string `json:"contractAddress"`
|
||||
TokenSymbol string `json:"tokenSymbol"`
|
||||
TokenDecimals string `json:"tokenDecimals"`
|
||||
Balance string `json:"balance"`
|
||||
}) (string, string) {
|
||||
var numberedSymbols, numberedBalances []string
|
||||
|
||||
for i, voucher := range holdings {
|
||||
numberedSymbols = append(numberedSymbols, fmt.Sprintf("%d:%s", i+1, voucher.TokenSymbol))
|
||||
numberedBalances = append(numberedBalances, fmt.Sprintf("%d:%s", i+1, voucher.Balance))
|
||||
}
|
||||
|
||||
voucherSymbolList := strings.Join(numberedSymbols, "\n")
|
||||
voucherBalanceList := strings.Join(numberedBalances, "\n")
|
||||
|
||||
return voucherSymbolList, voucherBalanceList
|
||||
}
|
||||
|
||||
// GetVoucherList fetches the list of vouchers and formats them
|
||||
func (h *Handlers) GetVoucherList(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
var res resource.Result
|
||||
|
||||
// Read vouchers from the store
|
||||
store := h.userdataStore
|
||||
prefixdb := storage.NewSubPrefixDb(store, []byte("vouchers"))
|
||||
|
||||
voucherData, err := prefixdb.Get(ctx, []byte("sym"))
|
||||
voucherData, err := h.prefixDb.Get(ctx, []byte("sym"))
|
||||
if err != nil {
|
||||
return res, nil
|
||||
return res, err
|
||||
}
|
||||
|
||||
res.Content = string(voucherData)
|
||||
@@ -1172,8 +1156,6 @@ func (h *Handlers) GetVoucherList(ctx context.Context, sym string, input []byte)
|
||||
// ViewVoucher retrieves the token holding and balance from the subprefixDB
|
||||
func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
var res resource.Result
|
||||
store := h.userdataStore
|
||||
|
||||
sessionId, ok := ctx.Value("SessionId").(string)
|
||||
if !ok {
|
||||
return res, fmt.Errorf("missing session")
|
||||
@@ -1182,126 +1164,52 @@ func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (r
|
||||
flag_incorrect_voucher, _ := h.flagManager.GetFlag("flag_incorrect_voucher")
|
||||
|
||||
inputStr := string(input)
|
||||
|
||||
if inputStr == "0" || inputStr == "99" {
|
||||
res.FlagReset = append(res.FlagReset, flag_incorrect_voucher)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
prefixdb := storage.NewSubPrefixDb(store, []byte("vouchers"))
|
||||
|
||||
// Retrieve the voucher symbol list
|
||||
voucherSymbolList, err := prefixdb.Get(ctx, []byte("sym"))
|
||||
metadata, err := utils.GetVoucherData(ctx, h.prefixDb, inputStr)
|
||||
if err != nil {
|
||||
return res, fmt.Errorf("failed to retrieve voucher symbol list: %v", err)
|
||||
return res, fmt.Errorf("failed to retrieve voucher data: %v", err)
|
||||
}
|
||||
|
||||
// Retrieve the voucher balance list
|
||||
voucherBalanceList, err := prefixdb.Get(ctx, []byte("bal"))
|
||||
if err != nil {
|
||||
return res, fmt.Errorf("failed to retrieve voucher balance list: %v", err)
|
||||
}
|
||||
|
||||
// match the voucher symbol and balance with the input
|
||||
matchedSymbol, matchedBalance := MatchVoucher(inputStr, string(voucherSymbolList), string(voucherBalanceList))
|
||||
|
||||
// If a match is found, write the temporary sym and balance
|
||||
if matchedSymbol != "" && matchedBalance != "" {
|
||||
err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_SYM, []byte(matchedSymbol))
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_BAL, []byte(matchedBalance))
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
res.FlagReset = append(res.FlagReset, flag_incorrect_voucher)
|
||||
res.Content = fmt.Sprintf("%s\n%s", matchedSymbol, matchedBalance)
|
||||
} else {
|
||||
if metadata == nil {
|
||||
res.FlagSet = append(res.FlagSet, flag_incorrect_voucher)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
if err := utils.StoreTemporaryVoucher(ctx, h.userdataStore, sessionId, metadata); err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
res.FlagReset = append(res.FlagReset, flag_incorrect_voucher)
|
||||
res.Content = fmt.Sprintf("%s\n%s", metadata.TokenSymbol, metadata.Balance)
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// MatchVoucher finds the matching voucher symbol and balance based on the input.
|
||||
func MatchVoucher(inputStr string, voucherSymbols, voucherBalances string) (string, string) {
|
||||
// Split the lists into slices for processing
|
||||
symbols := strings.Split(voucherSymbols, "\n")
|
||||
balances := strings.Split(voucherBalances, "\n")
|
||||
|
||||
var matchedSymbol, matchedBalance string
|
||||
|
||||
for i, symbol := range symbols {
|
||||
symbolParts := strings.SplitN(symbol, ":", 2)
|
||||
if len(symbolParts) != 2 {
|
||||
continue
|
||||
}
|
||||
voucherNum := symbolParts[0]
|
||||
voucherSymbol := symbolParts[1]
|
||||
|
||||
// Check if input matches either the number or the symbol
|
||||
if inputStr == voucherNum || strings.EqualFold(inputStr, voucherSymbol) {
|
||||
matchedSymbol = voucherSymbol
|
||||
// Ensure there's a corresponding balance
|
||||
if i < len(balances) {
|
||||
matchedBalance = strings.SplitN(balances[i], ":", 2)[1]
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return matchedSymbol, matchedBalance
|
||||
}
|
||||
|
||||
// SetVoucher retrieves the temporary sym and balance,
|
||||
// sets them as the active data and
|
||||
// clears the temporary data
|
||||
// SetVoucher retrieves the temp voucher data and sets it as the active data
|
||||
func (h *Handlers) SetVoucher(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
var res resource.Result
|
||||
var err error
|
||||
store := h.userdataStore
|
||||
|
||||
sessionId, ok := ctx.Value("SessionId").(string)
|
||||
if !ok {
|
||||
return res, fmt.Errorf("missing session")
|
||||
}
|
||||
|
||||
// get the current temporary symbol
|
||||
temporarySym, err := store.ReadEntry(ctx, sessionId, common.DATA_TEMPORARY_SYM)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
// get the current temporary balance
|
||||
temporaryBal, err := store.ReadEntry(ctx, sessionId, common.DATA_TEMPORARY_BAL)
|
||||
|
||||
// Get temporary data
|
||||
tempData, err := utils.GetTemporaryVoucherData(ctx, h.userdataStore, sessionId)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
// set the active symbol
|
||||
err = store.WriteEntry(ctx, sessionId, common.DATA_ACTIVE_SYM, []byte(temporarySym))
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
// set the active balance
|
||||
err = store.WriteEntry(ctx, sessionId, common.DATA_ACTIVE_BAL, []byte(temporaryBal))
|
||||
if err != nil {
|
||||
// Set as active and clear temporary data
|
||||
if err := utils.UpdateVoucherData(ctx, h.userdataStore, sessionId, tempData); err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
// reset the temporary symbol
|
||||
err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_SYM, []byte(""))
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
// reset the temporary balance
|
||||
err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_BAL, []byte(""))
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
res.Content = string(temporarySym)
|
||||
|
||||
res.Content = tempData.TokenSymbol
|
||||
return res, nil
|
||||
}
|
||||
|
||||
@@ -24,6 +24,8 @@ import (
|
||||
"github.com/grassrootseconomics/eth-custodial/pkg/api"
|
||||
testdataloader "github.com/peteole/testdata-loader"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -1488,8 +1490,8 @@ func TestValidateAmount(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Test with invalid amount format",
|
||||
input: []byte("0.02ms"),
|
||||
name: "Test with invalid amount format",
|
||||
input: []byte("0.02ms"),
|
||||
activeBal: []byte("5"),
|
||||
expectedResult: resource.Result{
|
||||
FlagSet: []uint32{flag_invalid_amount},
|
||||
@@ -1818,40 +1820,6 @@ func TestConfirmPin(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetVoucher(t *testing.T) {
|
||||
mockDataStore := new(mocks.MockUserDataStore)
|
||||
|
||||
sessionId := "session123"
|
||||
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
||||
|
||||
temporarySym := []byte("tempSym")
|
||||
temporaryBal := []byte("tempBal")
|
||||
|
||||
// Set expectations for the mock data store
|
||||
mockDataStore.On("ReadEntry", ctx, sessionId, common.DATA_TEMPORARY_SYM).Return(temporarySym, nil)
|
||||
mockDataStore.On("ReadEntry", ctx, sessionId, common.DATA_TEMPORARY_BAL).Return(temporaryBal, nil)
|
||||
mockDataStore.On("WriteEntry", ctx, sessionId, common.DATA_ACTIVE_SYM, temporarySym).Return(nil)
|
||||
mockDataStore.On("WriteEntry", ctx, sessionId, common.DATA_ACTIVE_BAL, temporaryBal).Return(nil)
|
||||
mockDataStore.On("WriteEntry", ctx, sessionId, common.DATA_TEMPORARY_SYM, []byte("")).Return(nil)
|
||||
mockDataStore.On("WriteEntry", ctx, sessionId, common.DATA_TEMPORARY_BAL, []byte("")).Return(nil)
|
||||
|
||||
h := &Handlers{
|
||||
userdataStore: mockDataStore,
|
||||
}
|
||||
|
||||
// Call the method under test
|
||||
res, err := h.SetVoucher(ctx, "someSym", []byte{})
|
||||
|
||||
// Assert that no errors occurred
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Assert that the result content is correct
|
||||
assert.Equal(t, string(temporarySym), res.Content)
|
||||
|
||||
// Assert that expectations were met
|
||||
mockDataStore.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestFetchCustodialBalances(t *testing.T) {
|
||||
fm, err := NewFlagManager(flagsPath)
|
||||
if err != nil {
|
||||
@@ -1933,3 +1901,256 @@ func TestFetchCustodialBalances(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetDefaultVoucher(t *testing.T) {
|
||||
fm, err := NewFlagManager(flagsPath)
|
||||
if err != nil {
|
||||
t.Logf(err.Error())
|
||||
}
|
||||
flag_no_active_voucher, err := fm.GetFlag("flag_no_active_voucher")
|
||||
if err != nil {
|
||||
t.Logf(err.Error())
|
||||
}
|
||||
// Define session ID and mock data
|
||||
sessionId := "session123"
|
||||
publicKey := "0X13242618721"
|
||||
notFoundErr := db.ErrNotFound{}
|
||||
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
vouchersResp *models.VoucherHoldingResponse
|
||||
expectedResult resource.Result
|
||||
}{
|
||||
{
|
||||
name: "Test set default voucher when no active voucher exists",
|
||||
vouchersResp: &models.VoucherHoldingResponse{
|
||||
Ok: true,
|
||||
Description: "Vouchers fetched successfully",
|
||||
Result: models.VoucherResult{
|
||||
Holdings: []dataserviceapi.TokenHoldings{
|
||||
{
|
||||
ContractAddress: "0x123",
|
||||
TokenSymbol: "TOKEN1",
|
||||
TokenDecimals: "18",
|
||||
Balance: "100",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
expectedResult: resource.Result{},
|
||||
},
|
||||
{
|
||||
name: "Test no vouchers available",
|
||||
vouchersResp: &models.VoucherHoldingResponse{
|
||||
Ok: true,
|
||||
Description: "No vouchers available",
|
||||
Result: models.VoucherResult{
|
||||
Holdings: []dataserviceapi.TokenHoldings{},
|
||||
},
|
||||
},
|
||||
expectedResult: resource.Result{
|
||||
FlagSet: []uint32{flag_no_active_voucher},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
mockDataStore := new(mocks.MockUserDataStore)
|
||||
mockAccountService := new(mocks.MockAccountService)
|
||||
|
||||
h := &Handlers{
|
||||
userdataStore: mockDataStore,
|
||||
accountService: mockAccountService,
|
||||
flagManager: fm.parser,
|
||||
}
|
||||
|
||||
mockDataStore.On("ReadEntry", ctx, sessionId, common.DATA_ACTIVE_SYM).Return([]byte(""), notFoundErr)
|
||||
mockDataStore.On("ReadEntry", ctx, sessionId, common.DATA_PUBLIC_KEY).Return([]byte(publicKey), nil)
|
||||
|
||||
mockAccountService.On("FetchVouchers", string(publicKey)).Return(tt.vouchersResp, nil)
|
||||
|
||||
if len(tt.vouchersResp.Result.Holdings) > 0 {
|
||||
firstVoucher := tt.vouchersResp.Result.Holdings[0]
|
||||
mockDataStore.On("WriteEntry", ctx, sessionId, common.DATA_ACTIVE_SYM, []byte(firstVoucher.TokenSymbol)).Return(nil)
|
||||
mockDataStore.On("WriteEntry", ctx, sessionId, common.DATA_ACTIVE_BAL, []byte(firstVoucher.Balance)).Return(nil)
|
||||
}
|
||||
|
||||
res, err := h.SetDefaultVoucher(ctx, "set_default_voucher", []byte("some-input"))
|
||||
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, res, tt.expectedResult, "Expected result should be equal to the actual result")
|
||||
|
||||
mockDataStore.AssertExpectations(t)
|
||||
mockAccountService.AssertExpectations(t)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckVouchers(t *testing.T) {
|
||||
mockDataStore := new(mocks.MockUserDataStore)
|
||||
mockAccountService := new(mocks.MockAccountService)
|
||||
mockSubPrefixDb := new(mocks.MockSubPrefixDb)
|
||||
|
||||
sessionId := "session123"
|
||||
publicKey := "0X13242618721"
|
||||
|
||||
h := &Handlers{
|
||||
userdataStore: mockDataStore,
|
||||
accountService: mockAccountService,
|
||||
prefixDb: mockSubPrefixDb,
|
||||
}
|
||||
|
||||
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
||||
|
||||
mockDataStore.On("ReadEntry", ctx, sessionId, common.DATA_PUBLIC_KEY).Return([]byte(publicKey), nil)
|
||||
|
||||
mockVouchersResponse := &models.VoucherHoldingResponse{}
|
||||
mockVouchersResponse.Result.Holdings = []dataserviceapi.TokenHoldings{
|
||||
{ContractAddress: "0xd4c288865Ce", TokenSymbol: "SRF", TokenDecimals: "6", Balance: "100"},
|
||||
{ContractAddress: "0x41c188d63Qa", TokenSymbol: "MILO", TokenDecimals: "4", Balance: "200"},
|
||||
}
|
||||
|
||||
mockAccountService.On("FetchVouchers", string(publicKey)).Return(mockVouchersResponse, nil)
|
||||
|
||||
mockSubPrefixDb.On("Put", ctx, []byte("sym"), []byte("1:SRF\n2:MILO")).Return(nil)
|
||||
mockSubPrefixDb.On("Put", ctx, []byte("bal"), []byte("1:100\n2:200")).Return(nil)
|
||||
mockSubPrefixDb.On("Put", ctx, []byte("deci"), []byte("1:6\n2:4")).Return(nil)
|
||||
mockSubPrefixDb.On("Put", ctx, []byte("addr"), []byte("1:0xd4c288865Ce\n2:0x41c188d63Qa")).Return(nil)
|
||||
|
||||
_, err := h.CheckVouchers(ctx, "check_vouchers", []byte(""))
|
||||
|
||||
assert.NoError(t, err)
|
||||
|
||||
mockDataStore.AssertExpectations(t)
|
||||
mockAccountService.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestGetVoucherList(t *testing.T) {
|
||||
mockSubPrefixDb := new(mocks.MockSubPrefixDb)
|
||||
|
||||
sessionId := "session123"
|
||||
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
||||
|
||||
h := &Handlers{
|
||||
prefixDb: mockSubPrefixDb,
|
||||
}
|
||||
|
||||
mockSubPrefixDb.On("Get", ctx, []byte("sym")).Return([]byte("1:SRF\n2:MILO"), nil)
|
||||
|
||||
res, err := h.GetVoucherList(ctx, "", []byte(""))
|
||||
assert.NoError(t, err)
|
||||
assert.Contains(t, res.Content, "1:SRF\n2:MILO")
|
||||
|
||||
mockSubPrefixDb.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestViewVoucher(t *testing.T) {
|
||||
fm, err := NewFlagManager(flagsPath)
|
||||
if err != nil {
|
||||
t.Logf(err.Error())
|
||||
}
|
||||
mockDataStore := new(mocks.MockUserDataStore)
|
||||
mockSubPrefixDb := new(mocks.MockSubPrefixDb)
|
||||
|
||||
sessionId := "session123"
|
||||
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
||||
|
||||
h := &Handlers{
|
||||
userdataStore: mockDataStore,
|
||||
flagManager: fm.parser,
|
||||
prefixDb: mockSubPrefixDb,
|
||||
}
|
||||
|
||||
// Define mock voucher data
|
||||
mockVoucherData := map[string]string{
|
||||
"sym": "1:SRF",
|
||||
"bal": "1:100",
|
||||
"deci": "1:6",
|
||||
"addr": "1:0xd4c288865Ce",
|
||||
}
|
||||
|
||||
for key, value := range mockVoucherData {
|
||||
mockSubPrefixDb.On("Get", ctx, []byte(key)).Return([]byte(value), nil)
|
||||
}
|
||||
|
||||
// Set up expectations for mockDataStore
|
||||
expectedData := map[common.DataTyp]string{
|
||||
common.DATA_TEMPORARY_SYM: "SRF",
|
||||
common.DATA_TEMPORARY_BAL: "100",
|
||||
common.DATA_TEMPORARY_DECIMAL: "6",
|
||||
common.DATA_TEMPORARY_ADDRESS: "0xd4c288865Ce",
|
||||
}
|
||||
|
||||
for dataType, dataValue := range expectedData {
|
||||
mockDataStore.On("WriteEntry", ctx, sessionId, dataType, []byte(dataValue)).Return(nil)
|
||||
}
|
||||
|
||||
res, err := h.ViewVoucher(ctx, "view_voucher", []byte("1"))
|
||||
assert.NoError(t, err)
|
||||
assert.Contains(t, res.Content, "SRF\n100")
|
||||
|
||||
mockDataStore.AssertExpectations(t)
|
||||
mockSubPrefixDb.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestSetVoucher(t *testing.T) {
|
||||
mockDataStore := new(mocks.MockUserDataStore)
|
||||
|
||||
sessionId := "session123"
|
||||
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
||||
|
||||
// Define the temporary voucher data
|
||||
tempData := &dataserviceapi.TokenHoldings{
|
||||
TokenSymbol: "SRF",
|
||||
Balance: "200",
|
||||
TokenDecimals: "6",
|
||||
ContractAddress: "0xd4c288865Ce0985a481Eef3be02443dF5E2e4Ea9",
|
||||
}
|
||||
|
||||
// Define the expected active entries
|
||||
activeEntries := map[common.DataTyp][]byte{
|
||||
common.DATA_ACTIVE_SYM: []byte(tempData.TokenSymbol),
|
||||
common.DATA_ACTIVE_BAL: []byte(tempData.Balance),
|
||||
common.DATA_ACTIVE_DECIMAL: []byte(tempData.TokenDecimals),
|
||||
common.DATA_ACTIVE_ADDRESS: []byte(tempData.ContractAddress),
|
||||
}
|
||||
|
||||
// Define the temporary entries to be cleared
|
||||
tempEntries := map[common.DataTyp][]byte{
|
||||
common.DATA_TEMPORARY_SYM: []byte(""),
|
||||
common.DATA_TEMPORARY_BAL: []byte(""),
|
||||
common.DATA_TEMPORARY_DECIMAL: []byte(""),
|
||||
common.DATA_TEMPORARY_ADDRESS: []byte(""),
|
||||
}
|
||||
|
||||
// Mocking ReadEntry calls for temporary data retrieval
|
||||
mockDataStore.On("ReadEntry", ctx, sessionId, common.DATA_TEMPORARY_SYM).Return([]byte(tempData.TokenSymbol), nil)
|
||||
mockDataStore.On("ReadEntry", ctx, sessionId, common.DATA_TEMPORARY_BAL).Return([]byte(tempData.Balance), nil)
|
||||
mockDataStore.On("ReadEntry", ctx, sessionId, common.DATA_TEMPORARY_DECIMAL).Return([]byte(tempData.TokenDecimals), nil)
|
||||
mockDataStore.On("ReadEntry", ctx, sessionId, common.DATA_TEMPORARY_ADDRESS).Return([]byte(tempData.ContractAddress), nil)
|
||||
|
||||
// Mocking WriteEntry calls for setting active data
|
||||
for key, value := range activeEntries {
|
||||
mockDataStore.On("WriteEntry", ctx, sessionId, key, value).Return(nil)
|
||||
}
|
||||
|
||||
// Mocking WriteEntry calls for clearing temporary data
|
||||
for key, value := range tempEntries {
|
||||
mockDataStore.On("WriteEntry", ctx, sessionId, key, value).Return(nil)
|
||||
}
|
||||
|
||||
h := &Handlers{
|
||||
userdataStore: mockDataStore,
|
||||
}
|
||||
|
||||
res, err := h.SetVoucher(ctx, "someSym", []byte{})
|
||||
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, string(tempData.TokenSymbol), res.Content)
|
||||
|
||||
mockDataStore.AssertExpectations(t)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user