add functionality to Update the Alias #88
| @ -2561,52 +2561,6 @@ func (h *MenuHandlers) persistLanguageCode(ctx context.Context, code string) err | ||||
| 	return h.persistInitialLanguageCode(ctx, sessionId, code) | ||||
| } | ||||
| 
 | ||||
| // constructAccountAlias retrieves and alias based on the first and family name
 | ||||
| // and writes the result in DATA_ACCOUNT_ALIAS
 | ||||
| func (h *MenuHandlers) constructAccountAlias(ctx context.Context) error { | ||||
| 	var alias string | ||||
| 	store := h.userdataStore | ||||
| 	sessionId, ok := ctx.Value("SessionId").(string) | ||||
| 	if !ok { | ||||
| 		return fmt.Errorf("missing session") | ||||
| 	} | ||||
| 	firstName, err := store.ReadEntry(ctx, sessionId, storedb.DATA_FIRST_NAME) | ||||
| 	if err != nil { | ||||
| 		if db.IsNotFound(err) { | ||||
| 			return nil | ||||
| 		} | ||||
| 		return err | ||||
| 	} | ||||
| 	familyName, err := store.ReadEntry(ctx, sessionId, storedb.DATA_FAMILY_NAME) | ||||
| 	if err != nil { | ||||
| 		if db.IsNotFound(err) { | ||||
| 			return nil | ||||
| 		} | ||||
| 		return err | ||||
| 	} | ||||
| 	pubKey, err := store.ReadEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY) | ||||
| 	if err != nil { | ||||
| 		if db.IsNotFound(err) { | ||||
| 			return nil | ||||
| 		} | ||||
| 		return err | ||||
| 	} | ||||
| 	aliasInput := fmt.Sprintf("%s%s", firstName, familyName) | ||||
| 	aliasResult, err := h.accountService.RequestAlias(ctx, string(pubKey), aliasInput) | ||||
| 	if err != nil { | ||||
| 		logg.ErrorCtxf(ctx, "failed to retrieve alias", "alias", aliasInput, "error_alias_request", err) | ||||
| 		return fmt.Errorf("Failed to retrieve alias: %s", err.Error()) | ||||
| 	} | ||||
| 	alias = aliasResult.Alias | ||||
| 	//Store the alias
 | ||||
| 	err = store.WriteEntry(ctx, sessionId, storedb.DATA_ACCOUNT_ALIAS, []byte(alias)) | ||||
| 	if err != nil { | ||||
| 		logg.ErrorCtxf(ctx, "failed to write account alias", "key", storedb.DATA_ACCOUNT_ALIAS, "value", alias, "error", err) | ||||
| 		return err | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
| 
 | ||||
| // RequestCustomAlias requests an ENS based alias name based on a user's input,then saves it as temporary value
 | ||||
| func (h *MenuHandlers) RequestCustomAlias(ctx context.Context, sym string, input []byte) (resource.Result, error) { | ||||
| 	var res resource.Result | ||||
|  | ||||
| @ -3154,97 +3154,6 @@ func TestResetUnregisteredNumber(t *testing.T) { | ||||
| 	assert.Equal(t, expectedResult, res) | ||||
| } | ||||
| 
 | ||||
| func TestConstructAccountAlias(t *testing.T) { | ||||
| 	ctx, store := InitializeTestStore(t) | ||||
| 	sessionId := "session123" | ||||
| 	mockAccountService := new(mocks.MockAccountService) | ||||
| 
 | ||||
| 	ctx = context.WithValue(ctx, "SessionId", sessionId) | ||||
| 
 | ||||
| 	h := &MenuHandlers{ | ||||
| 		userdataStore:  store, | ||||
| 		accountService: mockAccountService, | ||||
| 	} | ||||
| 
 | ||||
| 	tests := []struct { | ||||
| 		name          string | ||||
| 		firstName     string | ||||
| 		familyName    string | ||||
| 		publicKey     string | ||||
| 		expectedAlias string | ||||
| 		aliasResponse *models.RequestAliasResult | ||||
| 		aliasError    error | ||||
| 		expectedError error | ||||
| 	}{ | ||||
| 		{ | ||||
| 			name:          "Valid alias construction", | ||||
| 			firstName:     "John", | ||||
| 			familyName:    "Doe", | ||||
| 			publicKey:     "pubkey123", | ||||
| 			expectedAlias: "JohnDoeAlias", | ||||
| 			aliasResponse: &models.RequestAliasResult{Alias: "JohnDoeAlias"}, | ||||
| 			aliasError:    nil, | ||||
| 			expectedError: nil, | ||||
| 		}, | ||||
| 		{ | ||||
| 			name:          "Account service fails to return alias", | ||||
| 			firstName:     "Jane", | ||||
| 			familyName:    "Smith", | ||||
| 			publicKey:     "pubkey456", | ||||
| 			expectedAlias: "", | ||||
| 			aliasResponse: nil, | ||||
| 			aliasError:    fmt.Errorf("service unavailable"), | ||||
| 			expectedError: fmt.Errorf("Failed to retrieve alias: service unavailable"), | ||||
| 		}, | ||||
| 	} | ||||
| 
 | ||||
| 	for _, tt := range tests { | ||||
| 		t.Run(tt.name, func(t *testing.T) { | ||||
| 			if tt.firstName != "" { | ||||
| 				err := store.WriteEntry(ctx, sessionId, storedb.DATA_FIRST_NAME, []byte(tt.firstName)) | ||||
| 				require.NoError(t, err) | ||||
| 			} | ||||
| 
 | ||||
| 			if tt.familyName != "" { | ||||
| 				err := store.WriteEntry(ctx, sessionId, storedb.DATA_FAMILY_NAME, []byte(tt.familyName)) | ||||
| 				require.NoError(t, err) | ||||
| 			} | ||||
| 
 | ||||
| 			if tt.publicKey != "" { | ||||
| 				err := store.WriteEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY, []byte(tt.publicKey)) | ||||
| 				require.NoError(t, err) | ||||
| 			} | ||||
| 
 | ||||
| 			aliasInput := fmt.Sprintf("%s%s", tt.firstName, tt.familyName) | ||||
| 
 | ||||
| 			// Mock service behavior
 | ||||
| 			mockAccountService.On( | ||||
| 				"RequestAlias", | ||||
| 				tt.publicKey, | ||||
| 				aliasInput, | ||||
| 			).Return(tt.aliasResponse, tt.aliasError) | ||||
| 
 | ||||
| 			// Call the function under test
 | ||||
| 			err := h.constructAccountAlias(ctx) | ||||
| 
 | ||||
| 			// Assertions
 | ||||
| 			if tt.expectedError != nil { | ||||
| 				assert.EqualError(t, err, tt.expectedError.Error()) | ||||
| 			} else { | ||||
| 				assert.NoError(t, err) | ||||
| 				if tt.expectedAlias != "" { | ||||
| 					storedAlias, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACCOUNT_ALIAS) | ||||
| 					require.NoError(t, err) | ||||
| 					assert.Equal(t, tt.expectedAlias, string(storedAlias)) | ||||
| 				} | ||||
| 			} | ||||
| 
 | ||||
| 			// Ensure mock expectations were met
 | ||||
| 			mockAccountService.AssertExpectations(t) | ||||
| 		}) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestInsertProfileItems(t *testing.T) { | ||||
| 	ctx, store := InitializeTestStore(t) | ||||
| 	sessionId := "session123" | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user