Compare commits

..

7 Commits

Author SHA1 Message Date
alfred-mk
40c8cfa874 Use the default vouchersResp 2025-03-20 16:11:59 +03:00
alfred-mk
1bc2ef94e9 Added default voucher response 2025-03-20 16:04:40 +03:00
alfred-mk
e681c9cfca Added debug logs
Some checks failed
release / docker (push) Has been cancelled
2025-03-20 15:23:36 +03:00
alfred-mk
d504571014 Added debug logs
Some checks failed
release / docker (push) Has been cancelled
2025-03-20 15:07:02 +03:00
alfred-mk
3b16e25ebd Added debug logs
Some checks failed
release / docker (push) Has been cancelled
2025-03-20 14:54:06 +03:00
alfred-mk
5f37856927 Added activeSym logs
Some checks failed
release / docker (push) Has been cancelled
2025-03-20 14:35:06 +03:00
Carlosokumu
e564f1328b add fetched vouchers log
Some checks failed
release / docker (push) Has been cancelled
2025-03-20 13:48:34 +03:00
4 changed files with 173 additions and 534 deletions

View File

@@ -1468,7 +1468,6 @@ func loadUserContent(ctx context.Context, activeSym string, balance string, alia
// CheckBalance retrieves the balance of the active voucher and sets
// the balance as the result content.
func (h *MenuHandlers) CheckBalance(ctx context.Context, sym string, input []byte) (resource.Result, error) {
var (
res resource.Result
err error
@@ -1496,19 +1495,22 @@ func (h *MenuHandlers) CheckBalance(ctx context.Context, sym string, input []byt
// get the active sym and active balance
activeSym, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM)
if err != nil {
fmt.Println("could not find the activeSym in checkBalance:", err)
if !db.IsNotFound(err) {
fmt.Println("the err:", err)
logg.ErrorCtxf(ctx, "failed to read activeSym entry with", "key", storedb.DATA_ACTIVE_SYM, "error", err)
return res, err
}
}
fmt.Println("The active data in CheckBalance:", string(activeSym))
activeBal, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_BAL)
if err != nil {
if !db.IsNotFound(err) {
logg.ErrorCtxf(ctx, "failed to read activeBal entry with", "key", storedb.DATA_ACTIVE_BAL, "error", err)
return res, err
}
}
content, err = loadUserContent(ctx, string(activeSym), string(activeBal), alias)
@@ -1930,7 +1932,6 @@ func (h *MenuHandlers) InitiateTransaction(ctx context.Context, sym string, inpu
// and sets the first as the default voucher, if no active voucher is set.
func (h *MenuHandlers) SetDefaultVoucher(ctx context.Context, sym string, input []byte) (resource.Result, error) {
var res resource.Result
var err error
userStore := h.userdataStore
sessionId, ok := ctx.Value("SessionId").(string)
@@ -1941,9 +1942,12 @@ func (h *MenuHandlers) SetDefaultVoucher(ctx context.Context, sym string, input
flag_no_active_voucher, _ := h.flagManager.GetFlag("flag_no_active_voucher")
// check if the user has an active sym
_, err = userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM)
activeSym, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM)
fmt.Println("The activeSym in SetDefaultVoucher:", string(activeSym))
if err != nil {
fmt.Println("The err", err)
fmt.Println("Checking the data as no activeSym", "DATA_ACTIVE_SYM", storedb.DATA_ACTIVE_SYM)
if db.IsNotFound(err) {
publicKey, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY)
if err != nil {
@@ -1954,12 +1958,25 @@ func (h *MenuHandlers) SetDefaultVoucher(ctx context.Context, sym string, input
// Fetch vouchers from the API using the public key
vouchersResp, err := h.accountService.FetchVouchers(ctx, string(publicKey))
if err != nil {
fmt.Println("setting the flag_no_active_voucher")
res.FlagSet = append(res.FlagSet, flag_no_active_voucher)
return res, nil
}
// vouchersResp := []dataserviceapi.TokenHoldings{
// {
// ContractAddress: "0xcB453B742Bc8BE1BAa03Ce1927e287788d0aF065",
// TokenSymbol: "PES",
// TokenDecimals: "6",
// Balance: "10000000",
// },
// }
fmt.Println("fetched user vouchers in SetDefaultVoucher", "public_key", string(publicKey), "vouchers", vouchersResp)
// Return if there is no voucher
if len(vouchersResp) == 0 {
fmt.Println("setting the flag_no_active_voucher in SetDefaultVoucher")
res.FlagSet = append(res.FlagSet, flag_no_active_voucher)
return res, nil
}
@@ -1974,10 +1991,14 @@ func (h *MenuHandlers) SetDefaultVoucher(ctx context.Context, sym string, input
// Scale down the balance
scaledBalance := store.ScaleDownBalance(defaultBal, defaultDec)
fmt.Println("firstVoucher data", "defaultSym", defaultSym, "defaultBal", defaultBal, "defaultDec", defaultDec, "defaultAddr", defaultAddr)
// TODO: implement atomic transaction
// set the active symbol
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM, []byte(defaultSym))
if err != nil {
fmt.Println("got an error in writing DATA_ACTIVE_SYM", "defaultSym", defaultSym)
logg.ErrorCtxf(ctx, "failed to write defaultSym entry with", "key", storedb.DATA_ACTIVE_SYM, "value", defaultSym, "error", err)
return res, err
}
@@ -2003,7 +2024,7 @@ func (h *MenuHandlers) SetDefaultVoucher(ctx context.Context, sym string, input
return res, nil
}
logg.ErrorCtxf(ctx, "failed to read activeSym entry with", "key", storedb.DATA_ACTIVE_SYM, "error", err)
fmt.Println("failed to read activeSym entry with", "key", storedb.DATA_ACTIVE_SYM, "error", err)
return res, err
}
@@ -2034,6 +2055,17 @@ func (h *MenuHandlers) CheckVouchers(ctx context.Context, sym string, input []by
return res, nil
}
// vouchersResp := []dataserviceapi.TokenHoldings{
// {
// ContractAddress: "0xcB453B742Bc8BE1BAa03Ce1927e287788d0aF065",
// TokenSymbol: "PES",
// TokenDecimals: "6",
// Balance: "10000000",
// },
// }
fmt.Println("fetched user vouchers", "public_key", string(publicKey), "vouchers", vouchersResp)
// check the current active sym and update the data
activeSym, _ := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM)
if activeSym != nil {
@@ -2066,8 +2098,15 @@ func (h *MenuHandlers) CheckVouchers(ctx context.Context, sym string, input []by
}
}
activeBal, _ := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_BAL)
activeAddr, _ := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_ADDRESS)
fmt.Println(ctx, "The active data in CheckVouchers:", "activeSym", string(activeSym), string(activeBal), string(activeAddr))
data := store.ProcessVouchers(vouchersResp)
fmt.Println(ctx, "The data in CheckVouchers:", "data", data)
// Store all voucher data
dataMap := map[storedb.DataTyp]string{
storedb.DATA_VOUCHER_SYMBOLS: data.Symbols,

View File

@@ -275,80 +275,36 @@ func TestSaveFirstname(t *testing.T) {
// Set the flag in the State
mockState := state.NewState(128)
mockState.SetFlag(flag_allow_update)
expectedResult := resource.Result{}
// Define test data
firstName := "John"
if err := store.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte(firstName)); err != nil {
t.Fatal(err)
}
expectedResult.FlagSet = []uint32{flag_firstname_set}
// Create the MenuHandlers instance with the mock store
h := &MenuHandlers{
userdataStore: store,
flagManager: fm,
st: mockState,
profile: &profile.Profile{Max: 6},
}
tests := []struct {
name string
setupfunc func()
saveItem string
expectedSavedItem string
expectedResult resource.Result
}{
{
name: "test when `flag_allow_update` flag is set",
saveItem: "John",
setupfunc: func() {
//setup the required flags
mockState.SetFlag(flag_allow_update)
},
expectedSavedItem: "John",
expectedResult: resource.Result{
FlagSet: []uint32{flag_firstname_set},
},
},
{
name: "test when `flag_allow_update flag` is not set but `flag_firstname_set` set ",
setupfunc: func() {
//setup the required flags
mockState.ResetFlag(flag_allow_update)
mockState.SetFlag(flag_firstname_set)
},
saveItem: "John",
expectedSavedItem: "John",
expectedResult: resource.Result{},
},
{
name: "test when both `flag_allow_update flag` `flag_firstname_set` are not set ",
setupfunc: func() {
//setup the required flags
mockState.ResetFlag(flag_allow_update)
mockState.ResetFlag(flag_firstname_set)
},
saveItem: "John",
expectedSavedItem: "John",
expectedResult: resource.Result{},
},
}
// Call the method
res, err := h.SaveFirstname(ctx, "save_firstname", []byte(firstName))
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.setupfunc()
// Assert results
assert.NoError(t, err)
assert.Equal(t, expectedResult, res)
if err := store.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte(tt.saveItem)); err != nil {
t.Fatal(err)
}
// Call the method
res, err := h.SaveFirstname(ctx, "save_firstname", []byte(tt.saveItem))
if err != nil {
t.Fatalf("Failed to save firstname with error %v", err)
}
// Assert results
assert.NoError(t, err)
assert.Equal(t, tt.expectedResult, res)
// Verify that the DATA_FIRST_NAME entry has been updated with the temporary value
storedFirstName, _ := store.ReadEntry(ctx, sessionId, storedb.DATA_FIRST_NAME)
assert.Equal(t, tt.expectedSavedItem, string(storedFirstName))
})
}
// Verify that the DATA_FIRST_NAME entry has been updated with the temporary value
storedFirstName, _ := store.ReadEntry(ctx, sessionId, storedb.DATA_FIRST_NAME)
assert.Equal(t, firstName, string(storedFirstName))
}
func TestSaveFamilyname(t *testing.T) {
@@ -359,84 +315,40 @@ func TestSaveFamilyname(t *testing.T) {
fm, _ := NewFlagManager(flagsPath)
flag_allow_update, _ := fm.GetFlag("flag_allow_update")
flag_familyname_set, _ := fm.GetFlag("flag_familyname_set")
flag_firstname_set, _ := fm.GetFlag("flag_familyname_set")
// Set the flag in the State
mockState := state.NewState(128)
mockState.SetFlag(flag_allow_update)
expectedResult := resource.Result{}
expectedResult.FlagSet = []uint32{flag_firstname_set}
// Define test data
familyName := "Doeee"
if err := store.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte(familyName)); err != nil {
t.Fatal(err)
}
// Create the MenuHandlers instance with the mock store
h := &MenuHandlers{
userdataStore: store,
flagManager: fm,
st: mockState,
profile: &profile.Profile{Max: 6},
flagManager: fm,
}
tests := []struct {
name string
setupfunc func()
saveItem string
expectedSavedItem string
expectedResult resource.Result
}{
{
name: "test when `flag_allow_update` flag is set",
saveItem: "Doe",
setupfunc: func() {
//setup the required flags
mockState.SetFlag(flag_allow_update)
},
expectedSavedItem: "Doe",
expectedResult: resource.Result{
FlagSet: []uint32{flag_familyname_set},
},
},
{
name: "test when `flag_allow_update flag` is not set but `flag_familyname_set` set ",
setupfunc: func() {
//setup the required flags
mockState.ResetFlag(flag_allow_update)
mockState.SetFlag(flag_familyname_set)
},
saveItem: "Doe",
expectedSavedItem: "Doe",
expectedResult: resource.Result{},
},
{
name: "test when both `flag_allow_update flag` `flag_familyname_set` are not set ",
setupfunc: func() {
//setup the required flags
mockState.ResetFlag(flag_allow_update)
mockState.ResetFlag(flag_familyname_set)
},
saveItem: "Doe",
expectedSavedItem: "Doe",
expectedResult: resource.Result{},
},
}
// Call the method
res, err := h.SaveFamilyname(ctx, "save_familyname", []byte(familyName))
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.setupfunc()
// Assert results
assert.NoError(t, err)
assert.Equal(t, expectedResult, res)
if err := store.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte(tt.saveItem)); err != nil {
t.Fatal(err)
}
// Call the method
res, err := h.SaveFamilyname(ctx, "save_yob", []byte(tt.saveItem))
if err != nil {
t.Fatalf("Failed to save family name with error %v", err)
}
// Assert results
assert.NoError(t, err)
assert.Equal(t, tt.expectedResult, res)
// Verify that the DATA_FAMILY_NAME entry has been updated with the temporary value
storedFamilyname, _ := store.ReadEntry(ctx, sessionId, storedb.DATA_FAMILY_NAME)
assert.Equal(t, tt.expectedSavedItem, string(storedFamilyname))
})
}
// Verify that the DATA_FAMILY_NAME entry has been updated with the temporary value
storedFamilyName, _ := store.ReadEntry(ctx, sessionId, storedb.DATA_FAMILY_NAME)
assert.Equal(t, familyName, string(storedFamilyName))
}
func TestSaveYoB(t *testing.T) {
@@ -453,83 +365,37 @@ func TestSaveYoB(t *testing.T) {
mockState := state.NewState(108)
mockState.SetFlag(flag_allow_update)
expectedResult := resource.Result{}
// Define test data
yob := "1980"
if err := store.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte(yob)); err != nil {
t.Fatal(err)
}
expectedResult.FlagSet = []uint32{flag_yob_set}
// Create the MenuHandlers instance with the mock store
h := &MenuHandlers{
userdataStore: store,
flagManager: fm,
st: mockState,
profile: &profile.Profile{Max: 6},
}
tests := []struct {
name string
setupfunc func()
saveItem string
expectedSavedItem string
expectedResult resource.Result
}{
{
name: "test when `flag_allow_update` flag is set",
saveItem: "1980",
setupfunc: func() {
//setup the required flags
mockState.SetFlag(flag_allow_update)
},
expectedSavedItem: "1980",
expectedResult: resource.Result{
FlagSet: []uint32{flag_yob_set},
},
},
{
name: "test when `flag_allow_update flag` is not set but `flag_yob_set` set ",
setupfunc: func() {
//setup the required flags
mockState.ResetFlag(flag_allow_update)
mockState.SetFlag(flag_yob_set)
},
saveItem: "1980",
expectedSavedItem: "1980",
expectedResult: resource.Result{},
},
{
name: "test when both `flag_allow_update flag` `flag_yob_set` are not set ",
setupfunc: func() {
//setup the required flags
mockState.ResetFlag(flag_allow_update)
mockState.ResetFlag(flag_yob_set)
},
saveItem: "1980",
expectedSavedItem: "1980",
expectedResult: resource.Result{},
},
}
// Call the method
res, err := h.SaveYob(ctx, "save_yob", []byte(yob))
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.setupfunc()
if err := store.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte(tt.saveItem)); err != nil {
t.Fatal(err)
}
// Call the method
res, err := h.SaveYob(ctx, "save_yob", []byte(tt.saveItem))
// Assert results
assert.NoError(t, err)
assert.Equal(t, expectedResult, res)
if err != nil {
t.Fatalf("Failed to save location with error %v", err)
}
// Assert results
assert.NoError(t, err)
assert.Equal(t, tt.expectedResult, res)
// Verify that the DATA_YOB entry has been updated with the temporary value
storedYob, _ := store.ReadEntry(ctx, sessionId, storedb.DATA_YOB)
assert.Equal(t, tt.expectedSavedItem, string(storedYob))
})
}
// Verify that the DATA_YOB entry has been updated with the temporary value
storedYob, _ := store.ReadEntry(ctx, sessionId, storedb.DATA_YOB)
assert.Equal(t, yob, string(storedYob))
}
func TestSaveLocation(t *testing.T) {
sessionId := "session123"
ctx, store := InitializeTestStore(t)
ctx = context.WithValue(ctx, "SessionId", sessionId)
@@ -541,80 +407,36 @@ func TestSaveLocation(t *testing.T) {
// Set the flag in the State
mockState := state.NewState(108)
mockState.SetFlag(flag_allow_update)
expectedResult := resource.Result{}
// Define test data
location := "Kilifi"
if err := store.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte(location)); err != nil {
t.Fatal(err)
}
expectedResult.FlagSet = []uint32{flag_location_set}
// Create the MenuHandlers instance with the mock store
h := &MenuHandlers{
userdataStore: store,
flagManager: fm,
profile: &profile.Profile{Max: 6},
st: mockState,
}
tests := []struct {
name string
setupfunc func()
saveItem string
expectedSavedItem string
expectedResult resource.Result
}{
{
name: "test when `flag_allow_update` flag is set",
saveItem: "Kilifi",
setupfunc: func() {
//setup the required flags
mockState.SetFlag(flag_allow_update)
},
expectedSavedItem: "Kilifi",
expectedResult: resource.Result{
FlagSet: []uint32{flag_location_set},
},
},
{
name: "test when `flag_allow_update flag` is not set but `flag_location_set` set ",
setupfunc: func() {
//setup the required flags
mockState.ResetFlag(flag_allow_update)
mockState.SetFlag(flag_location_set)
},
saveItem: "Kilifi",
expectedSavedItem: "Kilifi",
expectedResult: resource.Result{FlagSet: []uint32{flag_location_set}},
},
{
name: "test when both `flag_allow_update flag` `flag_location_set` are not set ",
setupfunc: func() {
//setup the required flags
mockState.ResetFlag(flag_allow_update)
mockState.ResetFlag(flag_location_set)
},
saveItem: "Kilifi",
expectedSavedItem: "Kilifi",
expectedResult: resource.Result{},
},
}
// Call the method
res, err := h.SaveLocation(ctx, "save_location", []byte(location))
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.setupfunc()
if err := store.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte(tt.saveItem)); err != nil {
t.Fatal(err)
}
// Call the method
res, err := h.SaveLocation(ctx, "save_location", []byte(tt.saveItem))
// Assert results
assert.NoError(t, err)
assert.Equal(t, expectedResult, res)
if err != nil {
t.Fatalf("Failed to save location with error %v", err)
}
// Assert results
assert.NoError(t, err)
assert.Equal(t, tt.expectedResult, res)
// Verify that the DATA_LOCATION entry has been updated with the temporary value
storedLocation, _ := store.ReadEntry(ctx, sessionId, storedb.DATA_LOCATION)
assert.Equal(t, tt.expectedSavedItem, string(storedLocation))
})
}
// Verify that the DATA_LOCATION entry has been updated with the temporary value
storedLocation, _ := store.ReadEntry(ctx, sessionId, storedb.DATA_LOCATION)
assert.Equal(t, location, string(storedLocation))
}
func TestSaveOfferings(t *testing.T) {
@@ -629,76 +451,36 @@ func TestSaveOfferings(t *testing.T) {
// Set the flag in the State
mockState := state.NewState(108)
mockState.SetFlag(flag_allow_update)
expectedResult := resource.Result{}
// Define test data
offerings := "Bananas"
if err := store.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte(offerings)); err != nil {
t.Fatal(err)
}
expectedResult.FlagSet = []uint32{flag_offerings_set}
// Create the MenuHandlers instance with the mock store
h := &MenuHandlers{
userdataStore: store,
flagManager: fm,
st: mockState,
profile: &profile.Profile{Max: 6},
}
tests := []struct {
name string
setupfunc func()
saveItem string
expectedSavedItem string
expectedResult resource.Result
}{
{
name: "test when `flag_allow_update` flag is set",
saveItem: "Bananas",
setupfunc: func() {
//setup the required flags
mockState.SetFlag(flag_allow_update)
},
expectedSavedItem: "Bananas",
expectedResult: resource.Result{
FlagSet: []uint32{flag_offerings_set},
},
},
{
name: "test when `flag_allow_update flag` is not set but `flag_offerings_set` set ",
setupfunc: func() {
//setup the required flags
mockState.ResetFlag(flag_allow_update)
mockState.SetFlag(flag_offerings_set)
},
saveItem: "Bananas",
expectedSavedItem: "Bananas",
expectedResult: resource.Result{},
},
{
name: "test when both `flag_allow_update flag` `flag_offerings_set` are not set ",
setupfunc: func() {
//setup the required flags
mockState.ResetFlag(flag_allow_update)
mockState.ResetFlag(flag_offerings_set)
},
saveItem: "Bananas",
expectedSavedItem: "Bananas",
expectedResult: resource.Result{},
},
}
// Call the method
res, err := h.SaveOfferings(ctx, "save_offerings", []byte(offerings))
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.setupfunc()
// Assert results
assert.NoError(t, err)
assert.Equal(t, expectedResult, res)
if err := store.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte(tt.saveItem)); err != nil {
t.Fatal(err)
}
res, err := h.SaveOfferings(ctx, "save_offerings", []byte(tt.saveItem))
// Assert results
assert.NoError(t, err)
assert.Equal(t, tt.expectedResult, res)
// Verify that the DATA_OFFERINGS entry has been updated with the temporary value
storedOfferings, _ := store.ReadEntry(ctx, sessionId, storedb.DATA_OFFERINGS)
assert.Equal(t, tt.expectedSavedItem, string(storedOfferings))
})
}
// Verify that the DATA_OFFERINGS entry has been updated with the temporary value
storedOfferings, _ := store.ReadEntry(ctx, sessionId, storedb.DATA_OFFERINGS)
assert.Equal(t, offerings, string(storedOfferings))
}
func TestSaveGender(t *testing.T) {
@@ -713,89 +495,63 @@ func TestSaveGender(t *testing.T) {
// Set the flag in the State
mockState := state.NewState(108)
mockState.SetFlag(flag_allow_update)
// Define test cases
tests := []struct {
name string
setupfunc func()
input []byte
expectedGender string
expectedResult resource.Result
executingSymbol string
}{
{
name: "Valid Male Input with `flag_allow_update_set` set",
input: []byte("1"),
setupfunc: func() {
mockState.SetFlag(flag_allow_update)
mockState.ExecPath = []string{}
},
name: "Valid Male Input",
input: []byte("1"),
expectedGender: "male",
executingSymbol: "set_male",
expectedResult: resource.Result{
FlagSet: []uint32{flag_gender_set},
},
},
{
name: "Valid Female Input when `flag_allow_update` is not set but `flag_gender_set` is set",
input: []byte("2"),
setupfunc: func() {
mockState.ResetFlag(flag_allow_update)
mockState.SetFlag(flag_gender_set)
mockState.ExecPath = []string{}
},
expectedResult: resource.Result{},
name: "Valid Female Input",
input: []byte("2"),
expectedGender: "female",
executingSymbol: "set_female",
},
{
name: "Valid Unspecified Input when both `flag_allow_update` and `flag_gender_set` are not set",
setupfunc: func() {
mockState.ResetFlag(flag_allow_update)
mockState.ResetFlag(flag_gender_set)
mockState.ExecPath = []string{}
},
name: "Valid Unspecified Input",
input: []byte("3"),
executingSymbol: "set_unspecified",
expectedResult: resource.Result{},
expectedGender: "unspecified",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.setupfunc()
mockState.ExecPath = append(mockState.ExecPath, tt.executingSymbol)
if err := store.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte(tt.expectedGender)); err != nil {
t.Fatal(err)
}
mockState.ExecPath = append(mockState.ExecPath, tt.executingSymbol)
// Create the MenuHandlers instance with the mock store
h := &MenuHandlers{
userdataStore: store,
st: mockState,
flagManager: fm,
profile: &profile.Profile{Max: 6},
}
allowUpdate := h.st.MatchFlag(flag_allow_update, true)
expectedResult := resource.Result{}
// Call the method
res, err := h.SaveGender(ctx, "save_gender", tt.input)
expectedResult.FlagSet = []uint32{flag_gender_set}
// Assert results
assert.NoError(t, err)
assert.Equal(t, tt.expectedResult, res)
assert.Equal(t, expectedResult, res)
// Verify that the DATA_GENDER entry if the flag_allow_update is set has been updated with the temporary value
if allowUpdate {
storedGender, _ := store.ReadEntry(ctx, sessionId, storedb.DATA_GENDER)
assert.Equal(t, tt.expectedGender, string(storedGender))
} else {
// Verify that DATA_TEMPORARY_VALUE is updated with the latest user input
temporaryGenderValue, _ := store.ReadEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE)
assert.Equal(t, tt.expectedGender, string(temporaryGenderValue))
}
// Verify that the DATA_GENDER entry has been updated with the temporary value
storedGender, _ := store.ReadEntry(ctx, sessionId, storedb.DATA_GENDER)
assert.Equal(t, tt.expectedGender, string(storedGender))
})
}
}
@@ -1315,15 +1071,8 @@ func TestAuthorize(t *testing.T) {
},
},
{
name: "Test with PIN that is more than 4 digits",
input: []byte("12357"),
expectedResult: resource.Result{
FlagSet: []uint32{flag_invalid_pin},
},
},
{
name: "Test with pin that is less than 4 digit",
input: []byte("123"),
name: "Test with pin that is not a 4 digit",
input: []byte("1235aqds"),
expectedResult: resource.Result{
FlagSet: []uint32{flag_invalid_pin},
},
@@ -1901,7 +1650,7 @@ func TestValidateRecipient(t *testing.T) {
},
},
{
name: "Test with valid unregistered recipient",
name: "Test with valid unregistered recepient",
input: []byte("0712345678"),
expectedResult: resource.Result{
FlagSet: []uint32{flag_invalid_recipient_with_invite},
@@ -1909,7 +1658,7 @@ func TestValidateRecipient(t *testing.T) {
},
},
{
name: "Test with valid registered recipient",
name: "Test with valid registered recepient",
input: []byte("0711223344"),
expectedResult: resource.Result{},
},
@@ -1919,12 +1668,7 @@ func TestValidateRecipient(t *testing.T) {
expectedResult: resource.Result{},
},
{
name: "Test with alias recipient",
input: []byte("foobar.sarafu.eth"),
expectedResult: resource.Result{},
},
{
name: "Test with alias recipient",
name: "Test with alias recepient",
input: []byte("alias123.sarafu.local"),
expectedResult: resource.Result{},
},
@@ -1953,7 +1697,7 @@ func TestValidateRecipient(t *testing.T) {
mockAccountService.On("CheckAliasAddress", string(tt.input)).Return(aliasResponse, nil)
// Call the method
res, err := h.ValidateRecipient(ctx, "validate_recipient", tt.input)
res, err := h.ValidateRecipient(ctx, "validate_recepient", tt.input)
if err != nil {
t.Error(err)
@@ -3336,53 +3080,6 @@ func TestCheckBlockedNumPinMisMatch(t *testing.T) {
}
}
func TestSetBack(t *testing.T) {
ctx, store := InitializeTestStore(t)
fm, err := NewFlagManager(flagsPath)
if err != nil {
t.Fatal(err)
}
h := &MenuHandlers{
userdataStore: store,
flagManager: fm,
st: state.NewState(16),
}
flag_back_set, _ := h.flagManager.GetFlag("flag_back_set")
tests := []struct {
name string
input []byte
expectedResult resource.Result
}{
{
name: "Test with `0` input for back navigation",
input: []byte("0"),
expectedResult: resource.Result{
FlagSet: []uint32{flag_back_set},
},
},
{
name: "Test with input that is not back",
input: []byte("1345"),
expectedResult: resource.Result{
FlagReset: []uint32{flag_back_set},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
//Call the function under test
res, _ := h.SetBack(ctx, "set_back", tt.input)
//Assert that the result set to content is what was expected
assert.Equal(t, res, tt.expectedResult, "Result should contain flags set according to user input")
})
}
}
func TestGetCurrentProfileInfo(t *testing.T) {
sessionId := "session123"
ctx, store := InitializeTestStore(t)
@@ -3834,70 +3531,3 @@ func TestClearTemporaryValue(t *testing.T) {
// assert that the temp value is empty
assert.Equal(t, currentTempValue, []byte(""))
}
func TestGetSuggestedAlias(t *testing.T) {
ctx, store := InitializeTestStore(t)
sessionId := "session123"
alias := "foo.sarafu.eth"
ctx = context.WithValue(ctx, "SessionId", sessionId)
h := &MenuHandlers{
userdataStore: store,
}
//Set a suggested alias a temporary value that will be expected to be in the result content
err := store.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte(alias))
if err != nil {
t.Fatal(err)
}
res, err := h.GetSuggestedAlias(ctx, "getsuggested_alias", []byte(""))
if err != nil {
t.Fail()
}
assert.Equal(t, res.Content, alias)
}
func TestConfirmNewAlias(t *testing.T) {
ctx, store := InitializeTestStore(t)
sessionId := "session123"
expectedSavedAlias := "fooo.sarafu.eth"
ctx = context.WithValue(ctx, "SessionId", sessionId)
//Set a temporary alias that is suggested,confirm if the the current new alias after confirmation
err := store.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte(expectedSavedAlias))
if err != nil {
t.Fatal(err)
}
mockState := state.NewState(128)
fm, err := NewFlagManager(flagsPath)
if err != nil {
t.Fatal(err)
}
h := &MenuHandlers{
userdataStore: store,
st: mockState,
flagManager: fm,
}
flag_alias_set, _ := h.flagManager.GetFlag("flag_alias_set")
expectedResult := resource.Result{
FlagSet: []uint32{flag_alias_set},
}
res, err := h.ConfirmNewAlias(ctx, "confirm_new_alias", []byte(""))
if err != nil {
t.Fail()
}
accAlias, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACCOUNT_ALIAS)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, expectedResult, res)
assert.Equal(t, expectedSavedAlias, string(accAlias))
}

View File

@@ -140,39 +140,6 @@
}
]
},
{
"name": "menu_my_account_my_alias",
"steps": [
{
"input": "",
"expectedContent": "{balance}\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit"
},
{
"input": "3",
"expectedContent": "My Account\n1:Profile\n2:Change language\n3:Check balances\n4:Check statement\n5:PIN options\n6:My Address\n7:My Alias\n0:Back"
},
{
"input": "",
"expectedContent": "My Account\n1:Profile\n2:Change language\n3:Check balances\n4:Check statement\n5:PIN options\n6:My Address\n7:My Alias\n0:Back"
},
{
"input": "7",
"expectedContent": "Current alias: Not Provided\nEdit my alias:\n0:Back"
},
{
"input": "foo",
"expectedContent": "Your full alias will be: \nPlease enter your PIN to confirm: \n\n0:Back"
},
{
"input": "1234",
"expectedContent": "Your alias has been updated successfully\n0:Back\n9:Quit"
},
{
"input": "0",
"expectedContent": "{balance}\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit"
}
]
},
{
"name": "menu_my_account_reset_others_pin_with_unregistered_number",
"steps": [

View File

@@ -33,11 +33,14 @@ func (s *SubPrefixDb) toKey(k []byte) []byte {
func (s *SubPrefixDb) Get(ctx context.Context, key []byte) ([]byte, error) {
s.store.SetPrefix(db.DATATYPE_USERDATA)
key = s.toKey(key)
logg.InfoCtxf(ctx, "SubPrefixDb Get log", "key", string(key))
return s.store.Get(ctx, key)
}
func (s *SubPrefixDb) Put(ctx context.Context, key []byte, val []byte) error {
s.store.SetPrefix(db.DATATYPE_USERDATA)
key = s.toKey(key)
logg.InfoCtxf(ctx, "SubPrefixDb Put log", "key", string(key))
return s.store.Put(ctx, key, val)
}