Compare commits
No commits in common. "b6d24bf92938fce74eada07f3e454fcf71e32fa0" and "672eebb8fb43ac8947730532a5d0830c1da93587" have entirely different histories.
b6d24bf929
...
672eebb8fb
@ -233,6 +233,28 @@ func (h *Handlers) SaveTemporaryPin(ctx context.Context, sym string, input []byt
|
||||
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) {
|
||||
var res resource.Result
|
||||
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, 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)
|
||||
@ -1049,13 +1057,13 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte)
|
||||
voucherSymbolList := strings.Join(numberedSymbols, "\n")
|
||||
voucherBalanceList := strings.Join(numberedBalances, "\n")
|
||||
|
||||
prefixdb := storage.NewSubPrefixDb(store, []byte("pfx"))
|
||||
err = prefixdb.Put(ctx, []byte("sym"), []byte(voucherSymbolList))
|
||||
prefixdb := storage.NewSubPrefixDb(store, []byte("token_holdings"))
|
||||
err = prefixdb.Put(ctx, []byte("tokens"), []byte(voucherSymbolList))
|
||||
if err != nil {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
err = prefixdb.Put(ctx, []byte("bal"), []byte(voucherBalanceList))
|
||||
err = prefixdb.Put(ctx, []byte(voucherSymbolList), []byte(voucherBalanceList))
|
||||
if err != nil {
|
||||
return res, nil
|
||||
}
|
||||
@ -1063,24 +1071,6 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte)
|
||||
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
|
||||
func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
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")
|
||||
}
|
||||
|
||||
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("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
|
||||
voucherSymbolList, err := prefixdb.Get(ctx, []byte("sym"))
|
||||
voucherSymbolList, err := prefixdb.Get(ctx, []byte("tokens"))
|
||||
if err != nil {
|
||||
return res, fmt.Errorf("failed to retrieve voucher symbol list: %v", err)
|
||||
}
|
||||
|
||||
// Retrieve the voucher balance list
|
||||
voucherBalanceList, err := prefixdb.Get(ctx, []byte("bal"))
|
||||
voucherBalanceList, err := prefixdb.Get(ctx, []byte(voucherSymbolList))
|
||||
if err != nil {
|
||||
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 != "" {
|
||||
err = store.WriteEntry(ctx, sessionId, utils.DATA_TEMPORARY_SYM, []byte(matchedSymbol))
|
||||
if err != nil {
|
||||
@ -1163,9 +1153,8 @@ func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (r
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// SetVoucher retrieves the temporary sym and balance,
|
||||
// sets them as the active data and
|
||||
// clears the temporary data
|
||||
// SetVoucher retrieves the temporary voucher, sets it as the active voucher and
|
||||
// clears the temporary voucher/sym
|
||||
func (h *Handlers) SetVoucher(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
var res resource.Result
|
||||
var err error
|
||||
|
@ -1560,72 +1560,33 @@ func TestValidateRecipient(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCheckBalance(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
sessionId string
|
||||
publicKey string
|
||||
activeSym string
|
||||
activeBal string
|
||||
expectedResult resource.Result
|
||||
expectError bool
|
||||
}{
|
||||
{
|
||||
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,
|
||||
},
|
||||
|
||||
mockDataStore := new(mocks.MockUserDataStore)
|
||||
sessionId := "session123"
|
||||
publicKey := "0X13242618721"
|
||||
balance := "0.003 CELO"
|
||||
|
||||
expectedResult := resource.Result{
|
||||
Content: "0.003 CELO",
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
mockDataStore := new(mocks.MockUserDataStore)
|
||||
mockAccountService := new(mocks.MockAccountService)
|
||||
ctx := context.WithValue(context.Background(), "SessionId", tt.sessionId)
|
||||
mockCreateAccountService := new(mocks.MockAccountService)
|
||||
|
||||
h := &Handlers{
|
||||
userdataStore: mockDataStore,
|
||||
accountService: mockAccountService,
|
||||
}
|
||||
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
||||
|
||||
// Mock calls for public key
|
||||
mockDataStore.On("ReadEntry", ctx, tt.sessionId, utils.DATA_PUBLIC_KEY).Return([]byte(tt.publicKey), nil)
|
||||
|
||||
if tt.activeSym == "" {
|
||||
// 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)
|
||||
})
|
||||
h := &Handlers{
|
||||
userdataStore: mockDataStore,
|
||||
accountService: mockCreateAccountService,
|
||||
//flagManager: fm.parser,
|
||||
}
|
||||
//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) {
|
||||
|
Loading…
Reference in New Issue
Block a user