Compare commits

..

No commits in common. "b6d24bf92938fce74eada07f3e454fcf71e32fa0" and "672eebb8fb43ac8947730532a5d0830c1da93587" have entirely different histories.

2 changed files with 56 additions and 106 deletions

View File

@ -233,6 +233,28 @@ func (h *Handlers) SaveTemporaryPin(ctx context.Context, sym string, input []byt
return res, nil return res, nil
} }
// GetVoucherList fetches the list of vouchers and formats them
// checks whether they are stored internally before calling the API
func (h *Handlers) GetVoucherList(ctx context.Context, sym string, input []byte) (resource.Result, error) {
var res resource.Result
// check if the vouchers exist internally and if not
// fetch from the API
// Read vouchers from the store
store := h.userdataStore
prefixdb := storage.NewSubPrefixDb(store, []byte("token_holdings"))
voucherData, err := prefixdb.Get(ctx, []byte("tokens"))
if err != nil {
return res, nil
}
res.Content = string(voucherData)
return res, nil
}
func (h *Handlers) ConfirmPinChange(ctx context.Context, sym string, input []byte) (resource.Result, error) { func (h *Handlers) ConfirmPinChange(ctx context.Context, sym string, input []byte) (resource.Result, error) {
var res resource.Result var res resource.Result
sessionId, ok := ctx.Value("SessionId").(string) sessionId, ok := ctx.Value("SessionId").(string)
@ -652,20 +674,6 @@ func (h *Handlers) CheckBalance(ctx context.Context, sym string, input []byte) (
return res, nil return res, nil
} }
return res, err
}
if len(activeSym) == 0 {
logg.Printf(logging.LVL_INFO, "Using the default sym to fetch balance")
balance, err := h.accountService.CheckBalance(string(publicKey))
if err != nil {
return res, err
}
res.Content = balance
return res, nil
} }
activeBal, err := store.ReadEntry(ctx, sessionId, utils.DATA_ACTIVE_BAL) activeBal, err := store.ReadEntry(ctx, sessionId, utils.DATA_ACTIVE_BAL)
@ -1049,13 +1057,13 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte)
voucherSymbolList := strings.Join(numberedSymbols, "\n") voucherSymbolList := strings.Join(numberedSymbols, "\n")
voucherBalanceList := strings.Join(numberedBalances, "\n") voucherBalanceList := strings.Join(numberedBalances, "\n")
prefixdb := storage.NewSubPrefixDb(store, []byte("pfx")) prefixdb := storage.NewSubPrefixDb(store, []byte("token_holdings"))
err = prefixdb.Put(ctx, []byte("sym"), []byte(voucherSymbolList)) err = prefixdb.Put(ctx, []byte("tokens"), []byte(voucherSymbolList))
if err != nil { if err != nil {
return res, nil return res, nil
} }
err = prefixdb.Put(ctx, []byte("bal"), []byte(voucherBalanceList)) err = prefixdb.Put(ctx, []byte(voucherSymbolList), []byte(voucherBalanceList))
if err != nil { if err != nil {
return res, nil return res, nil
} }
@ -1063,24 +1071,6 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte)
return res, nil return res, nil
} }
// 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("pfx"))
voucherData, err := prefixdb.Get(ctx, []byte("sym"))
if err != nil {
return res, nil
}
res.Content = string(voucherData)
return res, nil
}
// ViewVoucher retrieves the token holding and balance from the subprefixDB // ViewVoucher retrieves the token holding and balance from the subprefixDB
func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (resource.Result, error) { func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (resource.Result, error) {
var res resource.Result var res resource.Result
@ -1092,25 +1082,25 @@ func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (r
return res, fmt.Errorf("missing session") return res, fmt.Errorf("missing session")
} }
flag_incorrect_voucher, _ := h.flagManager.GetFlag("flag_incorrect_voucher")
inputStr := string(input) inputStr := string(input)
if inputStr == "0" || inputStr == "99" { if inputStr == "0" || inputStr == "99" {
res.FlagReset = append(res.FlagReset, flag_incorrect_voucher)
return res, nil return res, nil
} }
prefixdb := storage.NewSubPrefixDb(store, []byte("pfx")) flag_incorrect_voucher, _ := h.flagManager.GetFlag("flag_incorrect_voucher")
// Initialize the store and prefix database
prefixdb := storage.NewSubPrefixDb(store, []byte("token_holdings"))
// Retrieve the voucher symbol list // Retrieve the voucher symbol list
voucherSymbolList, err := prefixdb.Get(ctx, []byte("sym")) voucherSymbolList, err := prefixdb.Get(ctx, []byte("tokens"))
if err != nil { if err != nil {
return res, fmt.Errorf("failed to retrieve voucher symbol list: %v", err) return res, fmt.Errorf("failed to retrieve voucher symbol list: %v", err)
} }
// Retrieve the voucher balance list // Retrieve the voucher balance list
voucherBalanceList, err := prefixdb.Get(ctx, []byte("bal")) voucherBalanceList, err := prefixdb.Get(ctx, []byte(voucherSymbolList))
if err != nil { if err != nil {
return res, fmt.Errorf("failed to retrieve voucher balance list: %v", err) return res, fmt.Errorf("failed to retrieve voucher balance list: %v", err)
} }
@ -1144,7 +1134,7 @@ func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (r
} }
} }
// If a match is found, write the temporary sym, then return the symbol and balance // If a match is found, write the temporary sym , then return the symbol and balance
if matchedSymbol != "" && matchedBalance != "" { if matchedSymbol != "" && matchedBalance != "" {
err = store.WriteEntry(ctx, sessionId, utils.DATA_TEMPORARY_SYM, []byte(matchedSymbol)) err = store.WriteEntry(ctx, sessionId, utils.DATA_TEMPORARY_SYM, []byte(matchedSymbol))
if err != nil { if err != nil {
@ -1163,9 +1153,8 @@ func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (r
return res, nil return res, nil
} }
// SetVoucher retrieves the temporary sym and balance, // SetVoucher retrieves the temporary voucher, sets it as the active voucher and
// sets them as the active data and // clears the temporary voucher/sym
// clears the temporary data
func (h *Handlers) SetVoucher(ctx context.Context, sym string, input []byte) (resource.Result, error) { func (h *Handlers) SetVoucher(ctx context.Context, sym string, input []byte) (resource.Result, error) {
var res resource.Result var res resource.Result
var err error var err error

View File

@ -1560,72 +1560,33 @@ func TestValidateRecipient(t *testing.T) {
} }
func TestCheckBalance(t *testing.T) { func TestCheckBalance(t *testing.T) {
tests := []struct {
name string mockDataStore := new(mocks.MockUserDataStore)
sessionId string sessionId := "session123"
publicKey string publicKey := "0X13242618721"
activeSym string balance := "0.003 CELO"
activeBal string
expectedResult resource.Result expectedResult := resource.Result{
expectError bool Content: "0.003 CELO",
}{
{
name: "User with active sym",
sessionId: "session456",
publicKey: "0X98765432109",
activeSym: "ETH",
activeBal: "1.5",
expectedResult: resource.Result{Content: "1.5 ETH"},
expectError: false,
},
{
name: "User without active sym",
sessionId: "session123",
publicKey: "0X13242618721",
activeSym: "",
activeBal: "",
expectedResult: resource.Result{Content: "0.003 CELO"},
expectError: false,
},
} }
for _, tt := range tests { mockCreateAccountService := new(mocks.MockAccountService)
t.Run(tt.name, func(t *testing.T) {
mockDataStore := new(mocks.MockUserDataStore)
mockAccountService := new(mocks.MockAccountService)
ctx := context.WithValue(context.Background(), "SessionId", tt.sessionId)
h := &Handlers{ ctx := context.WithValue(context.Background(), "SessionId", sessionId)
userdataStore: mockDataStore,
accountService: mockAccountService,
}
// Mock calls for public key h := &Handlers{
mockDataStore.On("ReadEntry", ctx, tt.sessionId, utils.DATA_PUBLIC_KEY).Return([]byte(tt.publicKey), nil) userdataStore: mockDataStore,
accountService: mockCreateAccountService,
if tt.activeSym == "" { //flagManager: fm.parser,
// Mock for user without active sym
mockDataStore.On("ReadEntry", ctx, tt.sessionId, utils.DATA_ACTIVE_SYM).Return([]byte{}, db.ErrNotFound{})
mockAccountService.On("CheckBalance", tt.publicKey).Return("0.003 CELO", nil)
} else {
// Mock for user with active sym
mockDataStore.On("ReadEntry", ctx, tt.sessionId, utils.DATA_ACTIVE_SYM).Return([]byte(tt.activeSym), nil)
mockDataStore.On("ReadEntry", ctx, tt.sessionId, utils.DATA_ACTIVE_BAL).Return([]byte(tt.activeBal), nil)
}
res, err := h.CheckBalance(ctx, "check_balance", []byte("123456"))
if tt.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expectedResult, res, "Result should match expected output")
}
mockDataStore.AssertExpectations(t)
mockAccountService.AssertExpectations(t)
})
} }
//mock call operations
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return([]byte(publicKey), nil)
mockCreateAccountService.On("CheckBalance", string(publicKey)).Return(balance, nil)
res, _ := h.CheckBalance(ctx, "check_balance", []byte("123456"))
assert.Equal(t, res, expectedResult, "Result should contain flag(s) that have been reset")
} }
func TestGetProfile(t *testing.T) { func TestGetProfile(t *testing.T) {