From d00d06a42118f7cda5e3cb66f67a24abcda698a1 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Tue, 4 Mar 2025 22:24:07 +0300 Subject: [PATCH 1/8] test: update profile update tests --- handlers/application/menuhandler_test.go | 478 ++++++++++++++++++----- 1 file changed, 374 insertions(+), 104 deletions(-) diff --git a/handlers/application/menuhandler_test.go b/handlers/application/menuhandler_test.go index f008085..f68f1c3 100644 --- a/handlers/application/menuhandler_test.go +++ b/handlers/application/menuhandler_test.go @@ -275,36 +275,80 @@ 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}, } - // Call the method - res, err := h.SaveFirstname(ctx, "save_firstname", []byte(firstName)) + 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{}, + }, + } - // Assert results - assert.NoError(t, err) - assert.Equal(t, expectedResult, res) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.setupfunc() - // 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)) + 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)) + + }) + } } func TestSaveFamilyname(t *testing.T) { @@ -315,40 +359,84 @@ func TestSaveFamilyname(t *testing.T) { fm, _ := NewFlagManager(flagsPath) flag_allow_update, _ := fm.GetFlag("flag_allow_update") - flag_firstname_set, _ := fm.GetFlag("flag_familyname_set") + flag_familyname_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, - st: mockState, flagManager: fm, + st: mockState, + profile: &profile.Profile{Max: 6}, } - // Call the method - res, err := h.SaveFamilyname(ctx, "save_familyname", []byte(familyName)) + 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{}, + }, + } - // Assert results - assert.NoError(t, err) - assert.Equal(t, expectedResult, res) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.setupfunc() - // 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)) + 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)) + + }) + } } func TestSaveYoB(t *testing.T) { @@ -365,37 +453,83 @@ 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}, } - // Call the method - res, err := h.SaveYob(ctx, "save_yob", []byte(yob)) + 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{}, + }, + } - // Assert results - assert.NoError(t, err) - assert.Equal(t, expectedResult, res) + 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)) - // 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)) + 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)) + + }) + } } func TestSaveLocation(t *testing.T) { + sessionId := "session123" ctx, store := InitializeTestStore(t) ctx = context.WithValue(ctx, "SessionId", sessionId) @@ -407,36 +541,80 @@ 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, } - // Call the method - res, err := h.SaveLocation(ctx, "save_location", []byte(location)) + 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{}, + }, + } - // Assert results - assert.NoError(t, err) - assert.Equal(t, expectedResult, res) + 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)) - // 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)) + 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)) + + }) + } } func TestSaveOfferings(t *testing.T) { @@ -451,36 +629,76 @@ 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}, } - // Call the method - res, err := h.SaveOfferings(ctx, "save_offerings", []byte(offerings)) + 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{}, + }, + } - // Assert results - assert.NoError(t, err) - assert.Equal(t, expectedResult, res) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.setupfunc() - // 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)) + 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)) + }) + } } func TestSaveGender(t *testing.T) { @@ -1664,6 +1882,11 @@ func TestValidateRecipient(t *testing.T) { input: []byte("0xd4c288865Ce0985a481Eef3be02443dF5E2e4Ea9"), expectedResult: resource.Result{}, }, + { + name: "Test with alias recepient", + input: []byte("foobar"), + expectedResult: resource.Result{}, + }, { name: "Test with alias recepient", input: []byte("alias123.sarafu.local"), @@ -3077,6 +3300,53 @@ 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) -- 2.45.2 From f7b81ab62919e1d23bd23f066ea8dd456a8db47e Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Wed, 5 Mar 2025 07:43:08 +0300 Subject: [PATCH 2/8] test: test aliases ending with .eth ,correct typo on recipient --- handlers/application/menuhandler_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/handlers/application/menuhandler_test.go b/handlers/application/menuhandler_test.go index f68f1c3..8a68c74 100644 --- a/handlers/application/menuhandler_test.go +++ b/handlers/application/menuhandler_test.go @@ -1865,7 +1865,7 @@ func TestValidateRecipient(t *testing.T) { }, }, { - name: "Test with valid unregistered recepient", + name: "Test with valid unregistered recipient", input: []byte("0712345678"), expectedResult: resource.Result{ FlagSet: []uint32{flag_invalid_recipient_with_invite}, @@ -1873,7 +1873,7 @@ func TestValidateRecipient(t *testing.T) { }, }, { - name: "Test with valid registered recepient", + name: "Test with valid registered recipient", input: []byte("0711223344"), expectedResult: resource.Result{}, }, @@ -1883,12 +1883,12 @@ func TestValidateRecipient(t *testing.T) { expectedResult: resource.Result{}, }, { - name: "Test with alias recepient", - input: []byte("foobar"), + name: "Test with alias recipient", + input: []byte("foobar.sarafu.eth"), expectedResult: resource.Result{}, }, { - name: "Test with alias recepient", + name: "Test with alias recipient", input: []byte("alias123.sarafu.local"), expectedResult: resource.Result{}, }, @@ -1917,7 +1917,7 @@ func TestValidateRecipient(t *testing.T) { mockAccountService.On("CheckAliasAddress", string(tt.input)).Return(aliasResponse, nil) // Call the method - res, err := h.ValidateRecipient(ctx, "validate_recepient", tt.input) + res, err := h.ValidateRecipient(ctx, "validate_recipient", tt.input) if err != nil { t.Error(err) -- 2.45.2 From 4dbe69954c68c8f9b4765560f8711ca5987386eb Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Wed, 5 Mar 2025 08:27:44 +0300 Subject: [PATCH 3/8] test: improve test coverage on save gender --- handlers/application/menuhandler_test.go | 39 ++++++++++++++++-------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/handlers/application/menuhandler_test.go b/handlers/application/menuhandler_test.go index 8a68c74..3b2641b 100644 --- a/handlers/application/menuhandler_test.go +++ b/handlers/application/menuhandler_test.go @@ -713,59 +713,74 @@ 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", - input: []byte("1"), + name: "Valid Male Input with `flag_allow_update_set` set", + input: []byte("1"), + setupfunc: func() { + mockState.SetFlag(flag_allow_update) + }, expectedGender: "male", executingSymbol: "set_male", + expectedResult: resource.Result{ + FlagSet: []uint32{flag_gender_set}, + }, }, { - name: "Valid Female Input", - input: []byte("2"), + 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) + }, + expectedResult: resource.Result{}, expectedGender: "female", executingSymbol: "set_female", }, { - name: "Valid Unspecified Input", + 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) + }, 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}, } - 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, expectedResult, res) + assert.Equal(t, tt.expectedResult, res) // Verify that the DATA_GENDER entry has been updated with the temporary value storedGender, _ := store.ReadEntry(ctx, sessionId, storedb.DATA_GENDER) -- 2.45.2 From 2b16f57aad13d07b0f869cc8a75ddeae6dd709e9 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Wed, 5 Mar 2025 10:59:00 +0300 Subject: [PATCH 4/8] test: save_gender add check for values in respective data keys when flag_allow update is set --- handlers/application/menuhandler_test.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/handlers/application/menuhandler_test.go b/handlers/application/menuhandler_test.go index 3b2641b..75b9de2 100644 --- a/handlers/application/menuhandler_test.go +++ b/handlers/application/menuhandler_test.go @@ -728,6 +728,7 @@ func TestSaveGender(t *testing.T) { input: []byte("1"), setupfunc: func() { mockState.SetFlag(flag_allow_update) + mockState.ExecPath = []string{} }, expectedGender: "male", executingSymbol: "set_male", @@ -741,6 +742,7 @@ func TestSaveGender(t *testing.T) { setupfunc: func() { mockState.ResetFlag(flag_allow_update) mockState.SetFlag(flag_gender_set) + mockState.ExecPath = []string{} }, expectedResult: resource.Result{}, expectedGender: "female", @@ -751,6 +753,7 @@ func TestSaveGender(t *testing.T) { setupfunc: func() { mockState.ResetFlag(flag_allow_update) mockState.ResetFlag(flag_gender_set) + mockState.ExecPath = []string{} }, input: []byte("3"), executingSymbol: "set_unspecified", @@ -763,6 +766,7 @@ func TestSaveGender(t *testing.T) { 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) } @@ -774,6 +778,7 @@ func TestSaveGender(t *testing.T) { flagManager: fm, profile: &profile.Profile{Max: 6}, } + allowUpdate := h.st.MatchFlag(flag_allow_update, true) // Call the method res, err := h.SaveGender(ctx, "save_gender", tt.input) @@ -782,9 +787,15 @@ func TestSaveGender(t *testing.T) { assert.NoError(t, err) assert.Equal(t, tt.expectedResult, res) - // 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)) + // 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)) + } }) } } -- 2.45.2 From 47dbf176da24613a51c6c7df77a455f6c849ef36 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Wed, 12 Mar 2025 09:21:45 +0300 Subject: [PATCH 5/8] add my alias traversal test --- menutraversal_test/group_test.json | 33 ++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/menutraversal_test/group_test.json b/menutraversal_test/group_test.json index b3177a9..6f4accb 100644 --- a/menutraversal_test/group_test.json +++ b/menutraversal_test/group_test.json @@ -140,6 +140,39 @@ } ] }, + { + "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": [ -- 2.45.2 From e0636593fbffb25d72b76df0ec2fec6bde23b918 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Wed, 12 Mar 2025 09:33:22 +0300 Subject: [PATCH 6/8] add test for getsuggested alias --- handlers/application/menuhandler_test.go | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/handlers/application/menuhandler_test.go b/handlers/application/menuhandler_test.go index c3b27c4..6b1261e 100644 --- a/handlers/application/menuhandler_test.go +++ b/handlers/application/menuhandler_test.go @@ -3827,3 +3827,28 @@ 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) + +} -- 2.45.2 From 5317ed3bd571f2ec981f20591a9bf692ec235ea5 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Wed, 12 Mar 2025 10:31:10 +0300 Subject: [PATCH 7/8] add test to confirm new alias --- handlers/application/menuhandler_test.go | 42 ++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/handlers/application/menuhandler_test.go b/handlers/application/menuhandler_test.go index 6b1261e..a4501aa 100644 --- a/handlers/application/menuhandler_test.go +++ b/handlers/application/menuhandler_test.go @@ -3852,3 +3852,45 @@ func TestGetSuggestedAlias(t *testing.T) { 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)) +} -- 2.45.2 From 4b1e3f09d097b157f1f8f63b719e6842be8520da Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Wed, 19 Mar 2025 09:05:19 +0300 Subject: [PATCH 8/8] add test cases for non-4 digit entries --- handlers/application/menuhandler_test.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/handlers/application/menuhandler_test.go b/handlers/application/menuhandler_test.go index a4501aa..6ddc1c2 100644 --- a/handlers/application/menuhandler_test.go +++ b/handlers/application/menuhandler_test.go @@ -1315,8 +1315,15 @@ func TestAuthorize(t *testing.T) { }, }, { - name: "Test with pin that is not a 4 digit", - input: []byte("1235aqds"), + 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"), expectedResult: resource.Result{ FlagSet: []uint32{flag_invalid_pin}, }, -- 2.45.2