Compare commits
2 Commits
92af428af4
...
c8a705ff44
| Author | SHA1 | Date | |
|---|---|---|---|
| c8a705ff44 | |||
| a55d254c4f |
@ -2,20 +2,17 @@ package application
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.defalsify.org/vise.git/cache"
|
"git.defalsify.org/vise.git/cache"
|
||||||
"git.defalsify.org/vise.git/persist"
|
"git.defalsify.org/vise.git/persist"
|
||||||
"git.defalsify.org/vise.git/resource"
|
"git.defalsify.org/vise.git/resource"
|
||||||
"git.defalsify.org/vise.git/state"
|
"git.defalsify.org/vise.git/state"
|
||||||
"git.grassecon.net/grassrootseconomics/common/pin"
|
"git.grassecon.net/grassrootseconomics/common/pin"
|
||||||
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
|
|
||||||
"git.grassecon.net/grassrootseconomics/sarafu-api/testutil/mocks"
|
"git.grassecon.net/grassrootseconomics/sarafu-api/testutil/mocks"
|
||||||
"git.grassecon.net/grassrootseconomics/sarafu-api/testutil/testservice"
|
"git.grassecon.net/grassrootseconomics/sarafu-api/testutil/testservice"
|
||||||
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
|
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
|
||||||
@ -28,7 +25,6 @@ import (
|
|||||||
|
|
||||||
visedb "git.defalsify.org/vise.git/db"
|
visedb "git.defalsify.org/vise.git/db"
|
||||||
memdb "git.defalsify.org/vise.git/db/mem"
|
memdb "git.defalsify.org/vise.git/db/mem"
|
||||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -277,7 +273,6 @@ func TestCheckIdentifier(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func TestGetFlag(t *testing.T) {
|
func TestGetFlag(t *testing.T) {
|
||||||
fm, err := NewFlagManager(flagsPath)
|
fm, err := NewFlagManager(flagsPath)
|
||||||
expectedFlag := uint32(9)
|
expectedFlag := uint32(9)
|
||||||
@ -496,523 +491,6 @@ func TestQuit(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestManageVouchers(t *testing.T) {
|
|
||||||
sessionId := "session123"
|
|
||||||
publicKey := "0X13242618721"
|
|
||||||
|
|
||||||
ctx, userStore := InitializeTestStore(t)
|
|
||||||
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
|
||||||
_, logdb := InitializeTestLogdbStore(t)
|
|
||||||
|
|
||||||
logDb := store.LogDb{
|
|
||||||
Db: logdb,
|
|
||||||
}
|
|
||||||
|
|
||||||
fm, err := NewFlagManager(flagsPath)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
flag_no_active_voucher, err := fm.GetFlag("flag_no_active_voucher")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
flag_api_error, err := fm.GetFlag("flag_api_call_error")
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY, []byte(publicKey))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
vouchersResp []dataserviceapi.TokenHoldings
|
|
||||||
storedActiveVoucher string
|
|
||||||
expectedVoucherSymbols []byte
|
|
||||||
expectedUpdatedAddress []byte
|
|
||||||
expectedResult resource.Result
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "No vouchers available",
|
|
||||||
vouchersResp: []dataserviceapi.TokenHoldings{},
|
|
||||||
expectedVoucherSymbols: []byte(""),
|
|
||||||
expectedUpdatedAddress: []byte(""),
|
|
||||||
expectedResult: resource.Result{
|
|
||||||
FlagSet: []uint32{flag_no_active_voucher},
|
|
||||||
FlagReset: []uint32{flag_api_error},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Set default voucher when no active voucher is set",
|
|
||||||
vouchersResp: []dataserviceapi.TokenHoldings{
|
|
||||||
{
|
|
||||||
TokenAddress: "0x123",
|
|
||||||
TokenSymbol: "TOKEN1",
|
|
||||||
TokenDecimals: "18",
|
|
||||||
Balance: "100",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
expectedVoucherSymbols: []byte("1:TOKEN1"),
|
|
||||||
expectedUpdatedAddress: []byte("0x123"),
|
|
||||||
expectedResult: resource.Result{
|
|
||||||
FlagReset: []uint32{flag_api_error, flag_no_active_voucher},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Check and update active voucher balance",
|
|
||||||
vouchersResp: []dataserviceapi.TokenHoldings{
|
|
||||||
{TokenAddress: "0xd4c288865Ce", TokenSymbol: "SRF", TokenDecimals: "6", Balance: "100"},
|
|
||||||
{TokenAddress: "0x41c188d63Qa", TokenSymbol: "MILO", TokenDecimals: "4", Balance: "200"},
|
|
||||||
},
|
|
||||||
storedActiveVoucher: "SRF",
|
|
||||||
expectedVoucherSymbols: []byte("1:SRF\n2:MILO"),
|
|
||||||
expectedUpdatedAddress: []byte("0xd4c288865Ce"),
|
|
||||||
expectedResult: resource.Result{
|
|
||||||
FlagReset: []uint32{flag_api_error, flag_no_active_voucher},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
mockAccountService := new(mocks.MockAccountService)
|
|
||||||
|
|
||||||
h := &MenuHandlers{
|
|
||||||
userdataStore: userStore,
|
|
||||||
accountService: mockAccountService,
|
|
||||||
flagManager: fm,
|
|
||||||
logDb: logDb,
|
|
||||||
}
|
|
||||||
|
|
||||||
mockAccountService.On("FetchVouchers", string(publicKey)).Return(tt.vouchersResp, nil)
|
|
||||||
|
|
||||||
// Store active voucher if needed
|
|
||||||
if tt.storedActiveVoucher != "" {
|
|
||||||
err := userStore.WriteEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM, []byte(tt.storedActiveVoucher))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_ACTIVE_ADDRESS, []byte("0x41c188D45rfg6ds"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
res, err := h.ManageVouchers(ctx, "manage_vouchers", []byte(""))
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, tt.expectedResult, res)
|
|
||||||
|
|
||||||
if tt.storedActiveVoucher != "" {
|
|
||||||
// Validate stored voucher symbols
|
|
||||||
voucherData, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_VOUCHER_SYMBOLS)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, tt.expectedVoucherSymbols, voucherData)
|
|
||||||
|
|
||||||
// Validate stored active contract address
|
|
||||||
updatedAddress, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_ADDRESS)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, tt.expectedUpdatedAddress, updatedAddress)
|
|
||||||
|
|
||||||
mockAccountService.AssertExpectations(t)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetVoucherList(t *testing.T) {
|
|
||||||
sessionId := "session123"
|
|
||||||
|
|
||||||
ctx, store := InitializeTestStore(t)
|
|
||||||
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
|
||||||
|
|
||||||
// Initialize MenuHandlers
|
|
||||||
h := &MenuHandlers{
|
|
||||||
userdataStore: store,
|
|
||||||
ReplaceSeparatorFunc: mockReplaceSeparator,
|
|
||||||
}
|
|
||||||
|
|
||||||
mockSymbols := []byte("1:SRF\n2:MILO")
|
|
||||||
mockBalances := []byte("1:10.099999\n2:40.7")
|
|
||||||
|
|
||||||
// Put voucher symnols and balances data to the store
|
|
||||||
err := store.WriteEntry(ctx, sessionId, storedb.DATA_VOUCHER_SYMBOLS, mockSymbols)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
err = store.WriteEntry(ctx, sessionId, storedb.DATA_VOUCHER_BALANCES, mockBalances)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
expectedList := []byte("1: SRF 10.09\n2: MILO 40.70")
|
|
||||||
|
|
||||||
res, err := h.GetVoucherList(ctx, "", []byte(""))
|
|
||||||
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, res.Content, string(expectedList))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestViewVoucher(t *testing.T) {
|
|
||||||
fm, err := NewFlagManager(flagsPath)
|
|
||||||
if err != nil {
|
|
||||||
t.Logf(err.Error())
|
|
||||||
}
|
|
||||||
ctx, store := InitializeTestStore(t)
|
|
||||||
sessionId := "session123"
|
|
||||||
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
|
||||||
|
|
||||||
h := &MenuHandlers{
|
|
||||||
userdataStore: store,
|
|
||||||
flagManager: fm,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Define mock voucher data
|
|
||||||
mockData := map[storedb.DataTyp][]byte{
|
|
||||||
storedb.DATA_VOUCHER_SYMBOLS: []byte("1:SRF\n2:MILO"),
|
|
||||||
storedb.DATA_VOUCHER_BALANCES: []byte("1:100\n2:200"),
|
|
||||||
storedb.DATA_VOUCHER_DECIMALS: []byte("1:6\n2:4"),
|
|
||||||
storedb.DATA_VOUCHER_ADDRESSES: []byte("1:0xd4c288865Ce\n2:0x41c188d63Qa"),
|
|
||||||
}
|
|
||||||
|
|
||||||
// Put the data
|
|
||||||
for key, value := range mockData {
|
|
||||||
err := store.WriteEntry(ctx, sessionId, key, []byte(value))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
res, err := h.ViewVoucher(ctx, "view_voucher", []byte("1"))
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, res.Content, "Symbol: SRF\nBalance: 100")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSetVoucher(t *testing.T) {
|
|
||||||
ctx, store := InitializeTestStore(t)
|
|
||||||
sessionId := "session123"
|
|
||||||
|
|
||||||
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
|
||||||
|
|
||||||
h := &MenuHandlers{
|
|
||||||
userdataStore: store,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Define the temporary voucher data
|
|
||||||
tempData := &dataserviceapi.TokenHoldings{
|
|
||||||
TokenSymbol: "SRF",
|
|
||||||
Balance: "200",
|
|
||||||
TokenDecimals: "6",
|
|
||||||
TokenAddress: "0xd4c288865Ce0985a481Eef3be02443dF5E2e4Ea9",
|
|
||||||
}
|
|
||||||
|
|
||||||
expectedData := fmt.Sprintf("%s,%s,%s,%s", tempData.TokenSymbol, tempData.Balance, tempData.TokenDecimals, tempData.TokenAddress)
|
|
||||||
|
|
||||||
// store the expectedData
|
|
||||||
if err := store.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte(expectedData)); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
res, err := h.SetVoucher(ctx, "set_voucher", []byte(""))
|
|
||||||
|
|
||||||
assert.NoError(t, err)
|
|
||||||
|
|
||||||
assert.Equal(t, string(tempData.TokenSymbol), res.Content)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetVoucherDetails(t *testing.T) {
|
|
||||||
ctx, store := InitializeTestStore(t)
|
|
||||||
fm, err := NewFlagManager(flagsPath)
|
|
||||||
if err != nil {
|
|
||||||
t.Logf(err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
flag_api_error, _ := fm.GetFlag("flag_api_call_error")
|
|
||||||
mockAccountService := new(mocks.MockAccountService)
|
|
||||||
|
|
||||||
sessionId := "session123"
|
|
||||||
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
|
||||||
expectedResult := resource.Result{}
|
|
||||||
|
|
||||||
tokA_AAddress := "0x0000000000000000000000000000000000000000"
|
|
||||||
|
|
||||||
h := &MenuHandlers{
|
|
||||||
userdataStore: store,
|
|
||||||
flagManager: fm,
|
|
||||||
accountService: mockAccountService,
|
|
||||||
}
|
|
||||||
err = store.WriteEntry(ctx, sessionId, storedb.DATA_ACTIVE_ADDRESS, []byte(tokA_AAddress))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
tokenDetails := &models.VoucherDataResult{
|
|
||||||
TokenName: "Token A",
|
|
||||||
TokenSymbol: "TOKA",
|
|
||||||
TokenLocation: "Kilifi,Kenya",
|
|
||||||
TokenCommodity: "Farming",
|
|
||||||
}
|
|
||||||
expectedResult.Content = fmt.Sprintf(
|
|
||||||
"Name: %s\nSymbol: %s\nCommodity: %s\nLocation: %s", tokenDetails.TokenName, tokenDetails.TokenSymbol, tokenDetails.TokenCommodity, tokenDetails.TokenLocation,
|
|
||||||
)
|
|
||||||
mockAccountService.On("VoucherData", string(tokA_AAddress)).Return(tokenDetails, nil)
|
|
||||||
res, err := h.GetVoucherDetails(ctx, "SessionId", []byte(""))
|
|
||||||
expectedResult.FlagReset = append(expectedResult.FlagReset, flag_api_error)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, expectedResult, res)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCheckTransactions(t *testing.T) {
|
|
||||||
mockAccountService := new(mocks.MockAccountService)
|
|
||||||
sessionId := "session123"
|
|
||||||
publicKey := "0X13242618721"
|
|
||||||
|
|
||||||
ctx, userStore := InitializeTestStore(t)
|
|
||||||
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
|
||||||
_, logdb := InitializeTestLogdbStore(t)
|
|
||||||
|
|
||||||
logDb := store.LogDb{
|
|
||||||
Db: logdb,
|
|
||||||
}
|
|
||||||
|
|
||||||
spdb := InitializeTestSubPrefixDb(t, ctx)
|
|
||||||
|
|
||||||
fm, err := NewFlagManager(flagsPath)
|
|
||||||
if err != nil {
|
|
||||||
t.Logf(err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
h := &MenuHandlers{
|
|
||||||
userdataStore: userStore,
|
|
||||||
accountService: mockAccountService,
|
|
||||||
prefixDb: spdb,
|
|
||||||
logDb: logDb,
|
|
||||||
flagManager: fm,
|
|
||||||
}
|
|
||||||
|
|
||||||
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY, []byte(publicKey))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
mockTXResponse := []dataserviceapi.Last10TxResponse{
|
|
||||||
{
|
|
||||||
Sender: "0X13242618721", Recipient: "0x41c188d63Qa", TransferValue: "100", ContractAddress: "0X1324262343rfdGW23",
|
|
||||||
TxHash: "0x123wefsf34rf", DateBlock: time.Now(), TokenSymbol: "SRF", TokenDecimals: "6",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Sender: "0x41c188d63Qa", Recipient: "0X13242618721", TransferValue: "200", ContractAddress: "0X1324262343rfdGW23",
|
|
||||||
TxHash: "0xq34wresfdb44", DateBlock: time.Now(), TokenSymbol: "SRF", TokenDecimals: "6",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
expectedSenders := []byte("0X13242618721\n0x41c188d63Qa")
|
|
||||||
|
|
||||||
mockAccountService.On("FetchTransactions", string(publicKey)).Return(mockTXResponse, nil)
|
|
||||||
|
|
||||||
_, err = h.CheckTransactions(ctx, "check_transactions", []byte(""))
|
|
||||||
assert.NoError(t, err)
|
|
||||||
|
|
||||||
// Read tranfers senders data from the store
|
|
||||||
senderData, err := spdb.Get(ctx, storedb.ToBytes(storedb.DATA_TX_SENDERS))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// assert that the data is stored correctly
|
|
||||||
assert.Equal(t, expectedSenders, senderData)
|
|
||||||
|
|
||||||
mockAccountService.AssertExpectations(t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetTransactionsList(t *testing.T) {
|
|
||||||
sessionId := "session123"
|
|
||||||
publicKey := "0X13242618721"
|
|
||||||
|
|
||||||
ctx, userStore := InitializeTestStore(t)
|
|
||||||
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
|
||||||
|
|
||||||
spdb := InitializeTestSubPrefixDb(t, ctx)
|
|
||||||
|
|
||||||
// Initialize MenuHandlers
|
|
||||||
h := &MenuHandlers{
|
|
||||||
userdataStore: userStore,
|
|
||||||
prefixDb: spdb,
|
|
||||||
ReplaceSeparatorFunc: mockReplaceSeparator,
|
|
||||||
}
|
|
||||||
|
|
||||||
err := userStore.WriteEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY, []byte(publicKey))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
dateBlock, err := time.Parse(time.RFC3339, "2024-10-03T07:23:12Z")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
mockTXResponse := []dataserviceapi.Last10TxResponse{
|
|
||||||
{
|
|
||||||
Sender: "0X13242618721", Recipient: "0x41c188d63Qa", TransferValue: "1000", ContractAddress: "0X1324262343rfdGW23",
|
|
||||||
TxHash: "0x123wefsf34rf", DateBlock: dateBlock, TokenSymbol: "SRF", TokenDecimals: "2",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Sender: "0x41c188d63Qa", Recipient: "0X13242618721", TransferValue: "2000", ContractAddress: "0X1324262343rfdGW23",
|
|
||||||
TxHash: "0xq34wresfdb44", DateBlock: dateBlock, TokenSymbol: "SRF", TokenDecimals: "2",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
data := store.ProcessTransfers(mockTXResponse)
|
|
||||||
|
|
||||||
// Store all transaction data
|
|
||||||
dataMap := map[storedb.DataTyp]string{
|
|
||||||
storedb.DATA_TX_SENDERS: data.Senders,
|
|
||||||
storedb.DATA_TX_RECIPIENTS: data.Recipients,
|
|
||||||
storedb.DATA_TX_VALUES: data.TransferValues,
|
|
||||||
storedb.DATA_TX_ADDRESSES: data.Addresses,
|
|
||||||
storedb.DATA_TX_HASHES: data.TxHashes,
|
|
||||||
storedb.DATA_TX_DATES: data.Dates,
|
|
||||||
storedb.DATA_TX_SYMBOLS: data.Symbols,
|
|
||||||
storedb.DATA_TX_DECIMALS: data.Decimals,
|
|
||||||
}
|
|
||||||
|
|
||||||
for key, value := range dataMap {
|
|
||||||
if err := h.prefixDb.Put(ctx, []byte(storedb.ToBytes(key)), []byte(value)); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
expectedTransactionList := []byte("1: Sent 10 SRF 2024-10-03\n2: Received 20 SRF 2024-10-03")
|
|
||||||
|
|
||||||
res, err := h.GetTransactionsList(ctx, "", []byte(""))
|
|
||||||
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, res.Content, string(expectedTransactionList))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestViewTransactionStatement(t *testing.T) {
|
|
||||||
ctx, userStore := InitializeTestStore(t)
|
|
||||||
sessionId := "session123"
|
|
||||||
publicKey := "0X13242618721"
|
|
||||||
|
|
||||||
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
|
||||||
spdb := InitializeTestSubPrefixDb(t, ctx)
|
|
||||||
|
|
||||||
fm, err := NewFlagManager(flagsPath)
|
|
||||||
if err != nil {
|
|
||||||
t.Logf(err.Error())
|
|
||||||
}
|
|
||||||
flag_incorrect_statement, _ := fm.GetFlag("flag_incorrect_statement")
|
|
||||||
|
|
||||||
h := &MenuHandlers{
|
|
||||||
userdataStore: userStore,
|
|
||||||
prefixDb: spdb,
|
|
||||||
flagManager: fm,
|
|
||||||
}
|
|
||||||
|
|
||||||
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY, []byte(publicKey))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
dateBlock, err := time.Parse(time.RFC3339, "2024-10-03T07:23:12Z")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
mockTXResponse := []dataserviceapi.Last10TxResponse{
|
|
||||||
{
|
|
||||||
Sender: "0X13242618721", Recipient: "0x41c188d63Qa", TransferValue: "1000", ContractAddress: "0X1324262343rfdGW23",
|
|
||||||
TxHash: "0x123wefsf34rf", DateBlock: dateBlock, TokenSymbol: "SRF", TokenDecimals: "2",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Sender: "0x41c188d63Qa", Recipient: "0X13242618721", TransferValue: "2000", ContractAddress: "0X1324262343rfdGW23",
|
|
||||||
TxHash: "0xq34wresfdb44", DateBlock: dateBlock, TokenSymbol: "SRF", TokenDecimals: "2",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
data := store.ProcessTransfers(mockTXResponse)
|
|
||||||
|
|
||||||
// Store all transaction data
|
|
||||||
dataMap := map[storedb.DataTyp]string{
|
|
||||||
storedb.DATA_TX_SENDERS: data.Senders,
|
|
||||||
storedb.DATA_TX_RECIPIENTS: data.Recipients,
|
|
||||||
storedb.DATA_TX_VALUES: data.TransferValues,
|
|
||||||
storedb.DATA_TX_ADDRESSES: data.Addresses,
|
|
||||||
storedb.DATA_TX_HASHES: data.TxHashes,
|
|
||||||
storedb.DATA_TX_DATES: data.Dates,
|
|
||||||
storedb.DATA_TX_SYMBOLS: data.Symbols,
|
|
||||||
storedb.DATA_TX_DECIMALS: data.Decimals,
|
|
||||||
}
|
|
||||||
|
|
||||||
for key, value := range dataMap {
|
|
||||||
if err := h.prefixDb.Put(ctx, []byte(storedb.ToBytes(key)), []byte(value)); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
input []byte
|
|
||||||
expectedError error
|
|
||||||
expectedResult resource.Result
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "Valid input - index 1",
|
|
||||||
input: []byte("1"),
|
|
||||||
expectedError: nil,
|
|
||||||
expectedResult: resource.Result{
|
|
||||||
Content: "Sent 10 SRF\nTo: 0x41c188d63Qa\nContract address: 0X1324262343rfdGW23\nTxhash: 0x123wefsf34rf\nDate: 2024-10-03 07:23:12 AM",
|
|
||||||
FlagReset: []uint32{flag_incorrect_statement},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Valid input - index 2",
|
|
||||||
input: []byte("2"),
|
|
||||||
expectedError: nil,
|
|
||||||
expectedResult: resource.Result{
|
|
||||||
Content: "Received 20 SRF\nFrom: 0x41c188d63Qa\nContract address: 0X1324262343rfdGW23\nTxhash: 0xq34wresfdb44\nDate: 2024-10-03 07:23:12 AM",
|
|
||||||
FlagReset: []uint32{flag_incorrect_statement},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Invalid input - index 0",
|
|
||||||
input: []byte("0"),
|
|
||||||
expectedError: nil,
|
|
||||||
expectedResult: resource.Result{
|
|
||||||
FlagReset: []uint32{flag_incorrect_statement},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Invalid input - index 12",
|
|
||||||
input: []byte("12"),
|
|
||||||
expectedError: fmt.Errorf("invalid input: index must be between 1 and 10"),
|
|
||||||
expectedResult: resource.Result{},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Invalid input - non-numeric",
|
|
||||||
input: []byte("abc"),
|
|
||||||
expectedError: fmt.Errorf("invalid input: must be a number between 1 and 10"),
|
|
||||||
expectedResult: resource.Result{},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
res, err := h.ViewTransactionStatement(ctx, "view_transaction_statement", tt.input)
|
|
||||||
|
|
||||||
if tt.expectedError != nil {
|
|
||||||
assert.EqualError(t, err, tt.expectedError.Error())
|
|
||||||
} else {
|
|
||||||
assert.NoError(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
assert.Equal(t, tt.expectedResult, res)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRetrieveBlockedNumber(t *testing.T) {
|
func TestRetrieveBlockedNumber(t *testing.T) {
|
||||||
sessionId := "session123"
|
sessionId := "session123"
|
||||||
blockedNumber := "0712345678"
|
blockedNumber := "0712345678"
|
||||||
|
|||||||
265
handlers/application/transactions_test.go
Normal file
265
handlers/application/transactions_test.go
Normal file
@ -0,0 +1,265 @@
|
|||||||
|
package application
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.defalsify.org/vise.git/resource"
|
||||||
|
"git.grassecon.net/grassrootseconomics/sarafu-api/testutil/mocks"
|
||||||
|
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
|
||||||
|
storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db"
|
||||||
|
"github.com/alecthomas/assert/v2"
|
||||||
|
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCheckTransactions(t *testing.T) {
|
||||||
|
mockAccountService := new(mocks.MockAccountService)
|
||||||
|
sessionId := "session123"
|
||||||
|
publicKey := "0X13242618721"
|
||||||
|
|
||||||
|
ctx, userStore := InitializeTestStore(t)
|
||||||
|
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
||||||
|
_, logdb := InitializeTestLogdbStore(t)
|
||||||
|
|
||||||
|
logDb := store.LogDb{
|
||||||
|
Db: logdb,
|
||||||
|
}
|
||||||
|
|
||||||
|
spdb := InitializeTestSubPrefixDb(t, ctx)
|
||||||
|
|
||||||
|
fm, err := NewFlagManager(flagsPath)
|
||||||
|
if err != nil {
|
||||||
|
t.Logf(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
h := &MenuHandlers{
|
||||||
|
userdataStore: userStore,
|
||||||
|
accountService: mockAccountService,
|
||||||
|
prefixDb: spdb,
|
||||||
|
logDb: logDb,
|
||||||
|
flagManager: fm,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY, []byte(publicKey))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mockTXResponse := []dataserviceapi.Last10TxResponse{
|
||||||
|
{
|
||||||
|
Sender: "0X13242618721", Recipient: "0x41c188d63Qa", TransferValue: "100", ContractAddress: "0X1324262343rfdGW23",
|
||||||
|
TxHash: "0x123wefsf34rf", DateBlock: time.Now(), TokenSymbol: "SRF", TokenDecimals: "6",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Sender: "0x41c188d63Qa", Recipient: "0X13242618721", TransferValue: "200", ContractAddress: "0X1324262343rfdGW23",
|
||||||
|
TxHash: "0xq34wresfdb44", DateBlock: time.Now(), TokenSymbol: "SRF", TokenDecimals: "6",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedSenders := []byte("0X13242618721\n0x41c188d63Qa")
|
||||||
|
|
||||||
|
mockAccountService.On("FetchTransactions", string(publicKey)).Return(mockTXResponse, nil)
|
||||||
|
|
||||||
|
_, err = h.CheckTransactions(ctx, "check_transactions", []byte(""))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Read tranfers senders data from the store
|
||||||
|
senderData, err := spdb.Get(ctx, storedb.ToBytes(storedb.DATA_TX_SENDERS))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// assert that the data is stored correctly
|
||||||
|
assert.Equal(t, expectedSenders, senderData)
|
||||||
|
|
||||||
|
mockAccountService.AssertExpectations(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetTransactionsList(t *testing.T) {
|
||||||
|
sessionId := "session123"
|
||||||
|
publicKey := "0X13242618721"
|
||||||
|
|
||||||
|
ctx, userStore := InitializeTestStore(t)
|
||||||
|
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
||||||
|
|
||||||
|
spdb := InitializeTestSubPrefixDb(t, ctx)
|
||||||
|
|
||||||
|
// Initialize MenuHandlers
|
||||||
|
h := &MenuHandlers{
|
||||||
|
userdataStore: userStore,
|
||||||
|
prefixDb: spdb,
|
||||||
|
ReplaceSeparatorFunc: mockReplaceSeparator,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := userStore.WriteEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY, []byte(publicKey))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
dateBlock, err := time.Parse(time.RFC3339, "2024-10-03T07:23:12Z")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mockTXResponse := []dataserviceapi.Last10TxResponse{
|
||||||
|
{
|
||||||
|
Sender: "0X13242618721", Recipient: "0x41c188d63Qa", TransferValue: "1000", ContractAddress: "0X1324262343rfdGW23",
|
||||||
|
TxHash: "0x123wefsf34rf", DateBlock: dateBlock, TokenSymbol: "SRF", TokenDecimals: "2",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Sender: "0x41c188d63Qa", Recipient: "0X13242618721", TransferValue: "2000", ContractAddress: "0X1324262343rfdGW23",
|
||||||
|
TxHash: "0xq34wresfdb44", DateBlock: dateBlock, TokenSymbol: "SRF", TokenDecimals: "2",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
data := store.ProcessTransfers(mockTXResponse)
|
||||||
|
|
||||||
|
// Store all transaction data
|
||||||
|
dataMap := map[storedb.DataTyp]string{
|
||||||
|
storedb.DATA_TX_SENDERS: data.Senders,
|
||||||
|
storedb.DATA_TX_RECIPIENTS: data.Recipients,
|
||||||
|
storedb.DATA_TX_VALUES: data.TransferValues,
|
||||||
|
storedb.DATA_TX_ADDRESSES: data.Addresses,
|
||||||
|
storedb.DATA_TX_HASHES: data.TxHashes,
|
||||||
|
storedb.DATA_TX_DATES: data.Dates,
|
||||||
|
storedb.DATA_TX_SYMBOLS: data.Symbols,
|
||||||
|
storedb.DATA_TX_DECIMALS: data.Decimals,
|
||||||
|
}
|
||||||
|
|
||||||
|
for key, value := range dataMap {
|
||||||
|
if err := h.prefixDb.Put(ctx, []byte(storedb.ToBytes(key)), []byte(value)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedTransactionList := []byte("1: Sent 10 SRF 2024-10-03\n2: Received 20 SRF 2024-10-03")
|
||||||
|
|
||||||
|
res, err := h.GetTransactionsList(ctx, "", []byte(""))
|
||||||
|
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, res.Content, string(expectedTransactionList))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestViewTransactionStatement(t *testing.T) {
|
||||||
|
ctx, userStore := InitializeTestStore(t)
|
||||||
|
sessionId := "session123"
|
||||||
|
publicKey := "0X13242618721"
|
||||||
|
|
||||||
|
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
||||||
|
spdb := InitializeTestSubPrefixDb(t, ctx)
|
||||||
|
|
||||||
|
fm, err := NewFlagManager(flagsPath)
|
||||||
|
if err != nil {
|
||||||
|
t.Logf(err.Error())
|
||||||
|
}
|
||||||
|
flag_incorrect_statement, _ := fm.GetFlag("flag_incorrect_statement")
|
||||||
|
|
||||||
|
h := &MenuHandlers{
|
||||||
|
userdataStore: userStore,
|
||||||
|
prefixDb: spdb,
|
||||||
|
flagManager: fm,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY, []byte(publicKey))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
dateBlock, err := time.Parse(time.RFC3339, "2024-10-03T07:23:12Z")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mockTXResponse := []dataserviceapi.Last10TxResponse{
|
||||||
|
{
|
||||||
|
Sender: "0X13242618721", Recipient: "0x41c188d63Qa", TransferValue: "1000", ContractAddress: "0X1324262343rfdGW23",
|
||||||
|
TxHash: "0x123wefsf34rf", DateBlock: dateBlock, TokenSymbol: "SRF", TokenDecimals: "2",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Sender: "0x41c188d63Qa", Recipient: "0X13242618721", TransferValue: "2000", ContractAddress: "0X1324262343rfdGW23",
|
||||||
|
TxHash: "0xq34wresfdb44", DateBlock: dateBlock, TokenSymbol: "SRF", TokenDecimals: "2",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
data := store.ProcessTransfers(mockTXResponse)
|
||||||
|
|
||||||
|
// Store all transaction data
|
||||||
|
dataMap := map[storedb.DataTyp]string{
|
||||||
|
storedb.DATA_TX_SENDERS: data.Senders,
|
||||||
|
storedb.DATA_TX_RECIPIENTS: data.Recipients,
|
||||||
|
storedb.DATA_TX_VALUES: data.TransferValues,
|
||||||
|
storedb.DATA_TX_ADDRESSES: data.Addresses,
|
||||||
|
storedb.DATA_TX_HASHES: data.TxHashes,
|
||||||
|
storedb.DATA_TX_DATES: data.Dates,
|
||||||
|
storedb.DATA_TX_SYMBOLS: data.Symbols,
|
||||||
|
storedb.DATA_TX_DECIMALS: data.Decimals,
|
||||||
|
}
|
||||||
|
|
||||||
|
for key, value := range dataMap {
|
||||||
|
if err := h.prefixDb.Put(ctx, []byte(storedb.ToBytes(key)), []byte(value)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input []byte
|
||||||
|
expectedError error
|
||||||
|
expectedResult resource.Result
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Valid input - index 1",
|
||||||
|
input: []byte("1"),
|
||||||
|
expectedError: nil,
|
||||||
|
expectedResult: resource.Result{
|
||||||
|
Content: "Sent 10 SRF\nTo: 0x41c188d63Qa\nContract address: 0X1324262343rfdGW23\nTxhash: 0x123wefsf34rf\nDate: 2024-10-03 07:23:12 AM",
|
||||||
|
FlagReset: []uint32{flag_incorrect_statement},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Valid input - index 2",
|
||||||
|
input: []byte("2"),
|
||||||
|
expectedError: nil,
|
||||||
|
expectedResult: resource.Result{
|
||||||
|
Content: "Received 20 SRF\nFrom: 0x41c188d63Qa\nContract address: 0X1324262343rfdGW23\nTxhash: 0xq34wresfdb44\nDate: 2024-10-03 07:23:12 AM",
|
||||||
|
FlagReset: []uint32{flag_incorrect_statement},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Invalid input - index 0",
|
||||||
|
input: []byte("0"),
|
||||||
|
expectedError: nil,
|
||||||
|
expectedResult: resource.Result{
|
||||||
|
FlagReset: []uint32{flag_incorrect_statement},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Invalid input - index 12",
|
||||||
|
input: []byte("12"),
|
||||||
|
expectedError: fmt.Errorf("invalid input: index must be between 1 and 10"),
|
||||||
|
expectedResult: resource.Result{},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Invalid input - non-numeric",
|
||||||
|
input: []byte("abc"),
|
||||||
|
expectedError: fmt.Errorf("invalid input: must be a number between 1 and 10"),
|
||||||
|
expectedResult: resource.Result{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
res, err := h.ViewTransactionStatement(ctx, "view_transaction_statement", tt.input)
|
||||||
|
|
||||||
|
if tt.expectedError != nil {
|
||||||
|
assert.EqualError(t, err, tt.expectedError.Error())
|
||||||
|
} else {
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, tt.expectedResult, res)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
282
handlers/application/vouchers_test.go
Normal file
282
handlers/application/vouchers_test.go
Normal file
@ -0,0 +1,282 @@
|
|||||||
|
package application
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.defalsify.org/vise.git/resource"
|
||||||
|
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
|
||||||
|
"git.grassecon.net/grassrootseconomics/sarafu-api/testutil/mocks"
|
||||||
|
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
|
||||||
|
storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db"
|
||||||
|
"github.com/alecthomas/assert/v2"
|
||||||
|
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestManageVouchers(t *testing.T) {
|
||||||
|
sessionId := "session123"
|
||||||
|
publicKey := "0X13242618721"
|
||||||
|
|
||||||
|
ctx, userStore := InitializeTestStore(t)
|
||||||
|
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
||||||
|
_, logdb := InitializeTestLogdbStore(t)
|
||||||
|
|
||||||
|
logDb := store.LogDb{
|
||||||
|
Db: logdb,
|
||||||
|
}
|
||||||
|
|
||||||
|
fm, err := NewFlagManager(flagsPath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
flag_no_active_voucher, err := fm.GetFlag("flag_no_active_voucher")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
flag_api_error, err := fm.GetFlag("flag_api_call_error")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY, []byte(publicKey))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
vouchersResp []dataserviceapi.TokenHoldings
|
||||||
|
storedActiveVoucher string
|
||||||
|
expectedVoucherSymbols []byte
|
||||||
|
expectedUpdatedAddress []byte
|
||||||
|
expectedResult resource.Result
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "No vouchers available",
|
||||||
|
vouchersResp: []dataserviceapi.TokenHoldings{},
|
||||||
|
expectedVoucherSymbols: []byte(""),
|
||||||
|
expectedUpdatedAddress: []byte(""),
|
||||||
|
expectedResult: resource.Result{
|
||||||
|
FlagSet: []uint32{flag_no_active_voucher},
|
||||||
|
FlagReset: []uint32{flag_api_error},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Set default voucher when no active voucher is set",
|
||||||
|
vouchersResp: []dataserviceapi.TokenHoldings{
|
||||||
|
{
|
||||||
|
TokenAddress: "0x123",
|
||||||
|
TokenSymbol: "TOKEN1",
|
||||||
|
TokenDecimals: "18",
|
||||||
|
Balance: "100",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectedVoucherSymbols: []byte("1:TOKEN1"),
|
||||||
|
expectedUpdatedAddress: []byte("0x123"),
|
||||||
|
expectedResult: resource.Result{
|
||||||
|
FlagReset: []uint32{flag_api_error, flag_no_active_voucher},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Check and update active voucher balance",
|
||||||
|
vouchersResp: []dataserviceapi.TokenHoldings{
|
||||||
|
{TokenAddress: "0xd4c288865Ce", TokenSymbol: "SRF", TokenDecimals: "6", Balance: "100"},
|
||||||
|
{TokenAddress: "0x41c188d63Qa", TokenSymbol: "MILO", TokenDecimals: "4", Balance: "200"},
|
||||||
|
},
|
||||||
|
storedActiveVoucher: "SRF",
|
||||||
|
expectedVoucherSymbols: []byte("1:SRF\n2:MILO"),
|
||||||
|
expectedUpdatedAddress: []byte("0xd4c288865Ce"),
|
||||||
|
expectedResult: resource.Result{
|
||||||
|
FlagReset: []uint32{flag_api_error, flag_no_active_voucher},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
mockAccountService := new(mocks.MockAccountService)
|
||||||
|
|
||||||
|
h := &MenuHandlers{
|
||||||
|
userdataStore: userStore,
|
||||||
|
accountService: mockAccountService,
|
||||||
|
flagManager: fm,
|
||||||
|
logDb: logDb,
|
||||||
|
}
|
||||||
|
|
||||||
|
mockAccountService.On("FetchVouchers", string(publicKey)).Return(tt.vouchersResp, nil)
|
||||||
|
|
||||||
|
// Store active voucher if needed
|
||||||
|
if tt.storedActiveVoucher != "" {
|
||||||
|
err := userStore.WriteEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM, []byte(tt.storedActiveVoucher))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_ACTIVE_ADDRESS, []byte("0x41c188D45rfg6ds"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := h.ManageVouchers(ctx, "manage_vouchers", []byte(""))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, tt.expectedResult, res)
|
||||||
|
|
||||||
|
if tt.storedActiveVoucher != "" {
|
||||||
|
// Validate stored voucher symbols
|
||||||
|
voucherData, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_VOUCHER_SYMBOLS)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, tt.expectedVoucherSymbols, voucherData)
|
||||||
|
|
||||||
|
// Validate stored active contract address
|
||||||
|
updatedAddress, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_ADDRESS)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, tt.expectedUpdatedAddress, updatedAddress)
|
||||||
|
|
||||||
|
mockAccountService.AssertExpectations(t)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetVoucherList(t *testing.T) {
|
||||||
|
sessionId := "session123"
|
||||||
|
|
||||||
|
ctx, store := InitializeTestStore(t)
|
||||||
|
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
||||||
|
|
||||||
|
// Initialize MenuHandlers
|
||||||
|
h := &MenuHandlers{
|
||||||
|
userdataStore: store,
|
||||||
|
ReplaceSeparatorFunc: mockReplaceSeparator,
|
||||||
|
}
|
||||||
|
|
||||||
|
mockSymbols := []byte("1:SRF\n2:MILO")
|
||||||
|
mockBalances := []byte("1:10.099999\n2:40.7")
|
||||||
|
|
||||||
|
// Put voucher symnols and balances data to the store
|
||||||
|
err := store.WriteEntry(ctx, sessionId, storedb.DATA_VOUCHER_SYMBOLS, mockSymbols)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
err = store.WriteEntry(ctx, sessionId, storedb.DATA_VOUCHER_BALANCES, mockBalances)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedList := []byte("1: SRF 10.09\n2: MILO 40.70")
|
||||||
|
|
||||||
|
res, err := h.GetVoucherList(ctx, "", []byte(""))
|
||||||
|
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, res.Content, string(expectedList))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestViewVoucher(t *testing.T) {
|
||||||
|
fm, err := NewFlagManager(flagsPath)
|
||||||
|
if err != nil {
|
||||||
|
t.Logf(err.Error())
|
||||||
|
}
|
||||||
|
ctx, store := InitializeTestStore(t)
|
||||||
|
sessionId := "session123"
|
||||||
|
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
||||||
|
|
||||||
|
h := &MenuHandlers{
|
||||||
|
userdataStore: store,
|
||||||
|
flagManager: fm,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Define mock voucher data
|
||||||
|
mockData := map[storedb.DataTyp][]byte{
|
||||||
|
storedb.DATA_VOUCHER_SYMBOLS: []byte("1:SRF\n2:MILO"),
|
||||||
|
storedb.DATA_VOUCHER_BALANCES: []byte("1:100\n2:200"),
|
||||||
|
storedb.DATA_VOUCHER_DECIMALS: []byte("1:6\n2:4"),
|
||||||
|
storedb.DATA_VOUCHER_ADDRESSES: []byte("1:0xd4c288865Ce\n2:0x41c188d63Qa"),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Put the data
|
||||||
|
for key, value := range mockData {
|
||||||
|
err := store.WriteEntry(ctx, sessionId, key, []byte(value))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := h.ViewVoucher(ctx, "view_voucher", []byte("1"))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, res.Content, "Symbol: SRF\nBalance: 100")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSetVoucher(t *testing.T) {
|
||||||
|
ctx, store := InitializeTestStore(t)
|
||||||
|
sessionId := "session123"
|
||||||
|
|
||||||
|
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
||||||
|
|
||||||
|
h := &MenuHandlers{
|
||||||
|
userdataStore: store,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Define the temporary voucher data
|
||||||
|
tempData := &dataserviceapi.TokenHoldings{
|
||||||
|
TokenSymbol: "SRF",
|
||||||
|
Balance: "200",
|
||||||
|
TokenDecimals: "6",
|
||||||
|
TokenAddress: "0xd4c288865Ce0985a481Eef3be02443dF5E2e4Ea9",
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedData := fmt.Sprintf("%s,%s,%s,%s", tempData.TokenSymbol, tempData.Balance, tempData.TokenDecimals, tempData.TokenAddress)
|
||||||
|
|
||||||
|
// store the expectedData
|
||||||
|
if err := store.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte(expectedData)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := h.SetVoucher(ctx, "set_voucher", []byte(""))
|
||||||
|
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, string(tempData.TokenSymbol), res.Content)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetVoucherDetails(t *testing.T) {
|
||||||
|
ctx, store := InitializeTestStore(t)
|
||||||
|
fm, err := NewFlagManager(flagsPath)
|
||||||
|
if err != nil {
|
||||||
|
t.Logf(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
flag_api_error, _ := fm.GetFlag("flag_api_call_error")
|
||||||
|
mockAccountService := new(mocks.MockAccountService)
|
||||||
|
|
||||||
|
sessionId := "session123"
|
||||||
|
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
||||||
|
expectedResult := resource.Result{}
|
||||||
|
|
||||||
|
tokA_AAddress := "0x0000000000000000000000000000000000000000"
|
||||||
|
|
||||||
|
h := &MenuHandlers{
|
||||||
|
userdataStore: store,
|
||||||
|
flagManager: fm,
|
||||||
|
accountService: mockAccountService,
|
||||||
|
}
|
||||||
|
err = store.WriteEntry(ctx, sessionId, storedb.DATA_ACTIVE_ADDRESS, []byte(tokA_AAddress))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
tokenDetails := &models.VoucherDataResult{
|
||||||
|
TokenName: "Token A",
|
||||||
|
TokenSymbol: "TOKA",
|
||||||
|
TokenLocation: "Kilifi,Kenya",
|
||||||
|
TokenCommodity: "Farming",
|
||||||
|
}
|
||||||
|
expectedResult.Content = fmt.Sprintf(
|
||||||
|
"Name: %s\nSymbol: %s\nCommodity: %s\nLocation: %s", tokenDetails.TokenName, tokenDetails.TokenSymbol, tokenDetails.TokenCommodity, tokenDetails.TokenLocation,
|
||||||
|
)
|
||||||
|
mockAccountService.On("VoucherData", string(tokA_AAddress)).Return(tokenDetails, nil)
|
||||||
|
res, err := h.GetVoucherDetails(ctx, "SessionId", []byte(""))
|
||||||
|
expectedResult.FlagReset = append(expectedResult.FlagReset, flag_api_error)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, expectedResult, res)
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user