Skip to content

Commit d8ffa49

Browse files
authored
Make UpdateUserProfile consistent on clearing fields on Android (#1654)
* Add tests around updating user profile * Update integration_test.cc * Update integration_test.cc * Update integration_test.cc * Handle empty string in profile change * Formatting * Update readme.md * Update readme.md * Update integration_test.cc
1 parent 1431a42 commit d8ffa49

File tree

3 files changed

+99
-2
lines changed

3 files changed

+99
-2
lines changed

auth/integration_test/src/integration_test.cc

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,38 @@ TEST_F(FirebaseAuthTest, TestUpdateUserProfile) {
649649
auth_->CreateUserWithEmailAndPassword(email.c_str(), kTestPassword);
650650
WaitForCompletion(create_user, "CreateUserWithEmailAndPassword");
651651
EXPECT_TRUE(auth_->current_user().is_valid());
652+
653+
// Set some user profile properties.
654+
firebase::auth::User user = create_user.result()->user;
655+
const char kDisplayName[] = "Hello World";
656+
const char kPhotoUrl[] = "http://example.com/image.jpg";
657+
firebase::auth::User::UserProfile user_profile;
658+
user_profile.display_name = kDisplayName;
659+
user_profile.photo_url = kPhotoUrl;
660+
firebase::Future<void> update_profile = user.UpdateUserProfile(user_profile);
661+
WaitForCompletion(update_profile, "UpdateUserProfile");
662+
user = auth_->current_user();
663+
EXPECT_EQ(user.display_name(), kDisplayName);
664+
EXPECT_EQ(user.photo_url(), kPhotoUrl);
665+
666+
// Validate that the new properties are present after signing out and in.
667+
SignOut();
668+
WaitForCompletion(
669+
auth_->SignInWithEmailAndPassword(email.c_str(), kTestPassword),
670+
"SignInWithEmailAndPassword");
671+
user = auth_->current_user();
672+
EXPECT_EQ(user.display_name(), kDisplayName);
673+
EXPECT_EQ(user.photo_url(), kPhotoUrl);
674+
DeleteUser();
675+
}
676+
677+
TEST_F(FirebaseAuthTest, TestUpdateUserProfileNull) {
678+
std::string email = GenerateEmailAddress();
679+
firebase::Future<firebase::auth::AuthResult> create_user =
680+
auth_->CreateUserWithEmailAndPassword(email.c_str(), kTestPassword);
681+
WaitForCompletion(create_user, "CreateUserWithEmailAndPassword");
682+
EXPECT_TRUE(auth_->current_user().is_valid());
683+
652684
// Set some user profile properties.
653685
firebase::auth::User user = create_user.result()->user;
654686
const char kDisplayName[] = "Hello World";
@@ -661,6 +693,19 @@ TEST_F(FirebaseAuthTest, TestUpdateUserProfile) {
661693
user = auth_->current_user();
662694
EXPECT_EQ(user.display_name(), kDisplayName);
663695
EXPECT_EQ(user.photo_url(), kPhotoUrl);
696+
697+
// Setting the entries to null should leave the old values
698+
firebase::auth::User::UserProfile user_profile_null;
699+
user_profile_null.display_name = nullptr;
700+
user_profile_null.photo_url = nullptr;
701+
firebase::Future<void> update_profile_null =
702+
user.UpdateUserProfile(user_profile_null);
703+
WaitForCompletion(update_profile_null, "UpdateUserProfileNull");
704+
user = auth_->current_user();
705+
EXPECT_EQ(user.display_name(), kDisplayName);
706+
EXPECT_EQ(user.photo_url(), kPhotoUrl);
707+
708+
// Validate that the new properties are present after signing out and in.
664709
SignOut();
665710
WaitForCompletion(
666711
auth_->SignInWithEmailAndPassword(email.c_str(), kTestPassword),
@@ -671,6 +716,48 @@ TEST_F(FirebaseAuthTest, TestUpdateUserProfile) {
671716
DeleteUser();
672717
}
673718

719+
TEST_F(FirebaseAuthTest, TestUpdateUserProfileEmpty) {
720+
std::string email = GenerateEmailAddress();
721+
firebase::Future<firebase::auth::AuthResult> create_user =
722+
auth_->CreateUserWithEmailAndPassword(email.c_str(), kTestPassword);
723+
WaitForCompletion(create_user, "CreateUserWithEmailAndPassword");
724+
EXPECT_TRUE(auth_->current_user().is_valid());
725+
726+
// Set some user profile properties.
727+
firebase::auth::User user = create_user.result()->user;
728+
const char kDisplayName[] = "Hello World";
729+
const char kPhotoUrl[] = "http://example.com/image.jpg";
730+
firebase::auth::User::UserProfile user_profile;
731+
user_profile.display_name = kDisplayName;
732+
user_profile.photo_url = kPhotoUrl;
733+
firebase::Future<void> update_profile = user.UpdateUserProfile(user_profile);
734+
WaitForCompletion(update_profile, "UpdateUserProfile");
735+
user = auth_->current_user();
736+
EXPECT_EQ(user.display_name(), kDisplayName);
737+
EXPECT_EQ(user.photo_url(), kPhotoUrl);
738+
739+
// Setting the fields to empty should clear it.
740+
firebase::auth::User::UserProfile user_profile_empty;
741+
user_profile_empty.display_name = "";
742+
user_profile_empty.photo_url = "";
743+
firebase::Future<void> update_profile_empty =
744+
user.UpdateUserProfile(user_profile_empty);
745+
WaitForCompletion(update_profile_empty, "UpdateUserProfileEmpty");
746+
user = auth_->current_user();
747+
EXPECT_EQ(user.display_name(), "");
748+
EXPECT_EQ(user.photo_url(), "");
749+
750+
// Validate that the properties are cleared out after signing out and in.
751+
SignOut();
752+
WaitForCompletion(
753+
auth_->SignInWithEmailAndPassword(email.c_str(), kTestPassword),
754+
"SignInWithEmailAndPassword");
755+
user = auth_->current_user();
756+
EXPECT_EQ(user.display_name(), "");
757+
EXPECT_EQ(user.photo_url(), "");
758+
DeleteUser();
759+
}
760+
674761
TEST_F(FirebaseAuthTest, TestUpdateEmailAndPassword) {
675762
std::string email = GenerateEmailAddress();
676763
WaitForCompletion(

auth/src/android/user_android.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,10 @@ Future<void> User::UpdateUserProfile(const UserProfile& profile) {
425425

426426
// Extra painfully call UserProfileChangeRequest.Builder.setPhotoUri.
427427
if (error == kAuthErrorNone && profile.photo_url != nullptr) {
428-
jobject j_uri = CharsToJniUri(env, profile.photo_url);
428+
jobject j_uri = nullptr;
429+
if (strlen(profile.photo_url) > 0) {
430+
j_uri = CharsToJniUri(env, profile.photo_url);
431+
}
429432
jobject j_builder_discard = env->CallObjectMethod(
430433
j_user_profile_builder,
431434
userprofilebuilder::GetMethodId(userprofilebuilder::kSetPhotoUri),
@@ -434,7 +437,9 @@ Future<void> User::UpdateUserProfile(const UserProfile& profile) {
434437
if (j_builder_discard) {
435438
env->DeleteLocalRef(j_builder_discard);
436439
}
437-
env->DeleteLocalRef(j_uri);
440+
if (j_uri) {
441+
env->DeleteLocalRef(j_uri);
442+
}
438443
}
439444

440445
jobject j_user_profile_request = nullptr;

release_build_files/readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,11 @@ workflow use only during the development of your app, not for publicly shipping
631631
code.
632632

633633
## Release Notes
634+
### Upcoming Release
635+
- Changes
636+
- Auth (Android): Setting photo_url to empty string with UpdateUserProfile
637+
clears the field, making it consistent with the other platforms.
638+
634639
### 12.3.0
635640
- Changes
636641
- General (iOS): Update to Firebase Cocoapods version 11.2.0.

0 commit comments

Comments
 (0)