alfred/test-updates #15
| @ -3266,3 +3266,94 @@ func TestResetUnregisteredNumber(t *testing.T) { | |||||||
| 
 | 
 | ||||||
| 	assert.Equal(t, expectedResult, res) | 	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) | ||||||
|  | 		}) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user