Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make UpdateUserProfile consistent on clearing fields on Android #1654

Merged
merged 9 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions auth/integration_test/src/integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,38 @@ TEST_F(FirebaseAuthTest, TestUpdateUserProfile) {
auth_->CreateUserWithEmailAndPassword(email.c_str(), kTestPassword);
WaitForCompletion(create_user, "CreateUserWithEmailAndPassword");
EXPECT_TRUE(auth_->current_user().is_valid());

// Set some user profile properties.
firebase::auth::User user = create_user.result()->user;
const char kDisplayName[] = "Hello World";
const char kPhotoUrl[] = "http://example.com/image.jpg";
firebase::auth::User::UserProfile user_profile;
user_profile.display_name = kDisplayName;
user_profile.photo_url = kPhotoUrl;
firebase::Future<void> update_profile = user.UpdateUserProfile(user_profile);
WaitForCompletion(update_profile, "UpdateUserProfile");
user = auth_->current_user();
EXPECT_EQ(user.display_name(), kDisplayName);
EXPECT_EQ(user.photo_url(), kPhotoUrl);

// Validate that the new properties are present after signing out and in.
SignOut();
WaitForCompletion(
auth_->SignInWithEmailAndPassword(email.c_str(), kTestPassword),
"SignInWithEmailAndPassword");
user = auth_->current_user();
EXPECT_EQ(user.display_name(), kDisplayName);
EXPECT_EQ(user.photo_url(), kPhotoUrl);
DeleteUser();
}

TEST_F(FirebaseAuthTest, TestUpdateUserProfileNull) {
a-maurice marked this conversation as resolved.
Show resolved Hide resolved
std::string email = GenerateEmailAddress();
firebase::Future<firebase::auth::AuthResult> create_user =
auth_->CreateUserWithEmailAndPassword(email.c_str(), kTestPassword);
WaitForCompletion(create_user, "CreateUserWithEmailAndPassword");
EXPECT_TRUE(auth_->current_user().is_valid());

// Set some user profile properties.
firebase::auth::User user = create_user.result()->user;
const char kDisplayName[] = "Hello World";
Expand All @@ -661,6 +693,19 @@ TEST_F(FirebaseAuthTest, TestUpdateUserProfile) {
user = auth_->current_user();
EXPECT_EQ(user.display_name(), kDisplayName);
EXPECT_EQ(user.photo_url(), kPhotoUrl);

// Setting the entries to null should leave the old values
firebase::auth::User::UserProfile user_profile_null;
user_profile_null.display_name = nullptr;
user_profile_null.photo_url = nullptr;
firebase::Future<void> update_profile_null =
user.UpdateUserProfile(user_profile_null);
WaitForCompletion(update_profile_null, "UpdateUserProfileNull");
user = auth_->current_user();
EXPECT_EQ(user.display_name(), kDisplayName);
EXPECT_EQ(user.photo_url(), kPhotoUrl);

// Validate that the new properties are present after signing out and in.
SignOut();
WaitForCompletion(
auth_->SignInWithEmailAndPassword(email.c_str(), kTestPassword),
Expand All @@ -671,6 +716,48 @@ TEST_F(FirebaseAuthTest, TestUpdateUserProfile) {
DeleteUser();
}

TEST_F(FirebaseAuthTest, TestUpdateUserProfileEmpty) {
std::string email = GenerateEmailAddress();
firebase::Future<firebase::auth::AuthResult> create_user =
auth_->CreateUserWithEmailAndPassword(email.c_str(), kTestPassword);
WaitForCompletion(create_user, "CreateUserWithEmailAndPassword");
EXPECT_TRUE(auth_->current_user().is_valid());

// Set some user profile properties.
firebase::auth::User user = create_user.result()->user;
const char kDisplayName[] = "Hello World";
const char kPhotoUrl[] = "http://example.com/image.jpg";
firebase::auth::User::UserProfile user_profile;
user_profile.display_name = kDisplayName;
user_profile.photo_url = kPhotoUrl;
firebase::Future<void> update_profile = user.UpdateUserProfile(user_profile);
WaitForCompletion(update_profile, "UpdateUserProfile");
user = auth_->current_user();
EXPECT_EQ(user.display_name(), kDisplayName);
EXPECT_EQ(user.photo_url(), kPhotoUrl);

// Setting the fields to empty should clear it.
firebase::auth::User::UserProfile user_profile_empty;
user_profile_empty.display_name = "";
user_profile_empty.photo_url = "";
firebase::Future<void> update_profile_empty =
user.UpdateUserProfile(user_profile_empty);
WaitForCompletion(update_profile_empty, "UpdateUserProfileEmpty");
user = auth_->current_user();
EXPECT_EQ(user.display_name(), "");
EXPECT_EQ(user.photo_url(), "");

// Validate that the properties are cleared out after signing out and in.
SignOut();
WaitForCompletion(
auth_->SignInWithEmailAndPassword(email.c_str(), kTestPassword),
"SignInWithEmailAndPassword");
user = auth_->current_user();
EXPECT_EQ(user.display_name(), "");
EXPECT_EQ(user.photo_url(), "");
DeleteUser();
}

TEST_F(FirebaseAuthTest, TestUpdateEmailAndPassword) {
std::string email = GenerateEmailAddress();
WaitForCompletion(
Expand Down
9 changes: 7 additions & 2 deletions auth/src/android/user_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,10 @@ Future<void> User::UpdateUserProfile(const UserProfile& profile) {

// Extra painfully call UserProfileChangeRequest.Builder.setPhotoUri.
if (error == kAuthErrorNone && profile.photo_url != nullptr) {
jobject j_uri = CharsToJniUri(env, profile.photo_url);
jobject j_uri = nullptr;
if (strlen(profile.photo_url) > 0) {
j_uri = CharsToJniUri(env, profile.photo_url);
}
jobject j_builder_discard = env->CallObjectMethod(
j_user_profile_builder,
userprofilebuilder::GetMethodId(userprofilebuilder::kSetPhotoUri),
Expand All @@ -434,7 +437,9 @@ Future<void> User::UpdateUserProfile(const UserProfile& profile) {
if (j_builder_discard) {
env->DeleteLocalRef(j_builder_discard);
}
env->DeleteLocalRef(j_uri);
if (j_uri) {
env->DeleteLocalRef(j_uri);
}
}

jobject j_user_profile_request = nullptr;
Expand Down
5 changes: 5 additions & 0 deletions release_build_files/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,11 @@ workflow use only during the development of your app, not for publicly shipping
code.

## Release Notes
### Upcoming Release
- Changes
- Auth (Android): Setting photo_url to empty string with UpdateUserProfile
clears the field, making it consistent with the other platforms.

### 12.3.0
- Changes
- General (iOS): Update to Firebase Cocoapods version 11.2.0.
Expand Down
Loading