Skip to content
Open
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
108 changes: 68 additions & 40 deletions auth/integration_test/src/integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <inttypes.h>

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
Expand Down Expand Up @@ -296,6 +297,8 @@ void FirebaseAuthTest::DeleteUser() {
FirebaseTest::WaitForCompletion(auth_->current_user().Delete(),
"Delete User");
ProcessEvents(100);
} else {
LogDebug("Failed to Delete User, not Signed In");
}
}

Expand Down Expand Up @@ -425,19 +428,55 @@ TEST_F(FirebaseAuthTest, TestTokensAndAuthStateListeners) {
ElementsAre("", Not(""), Not(""), Not(""), "")));
}

static std::string GenerateEmailAddress() {
#if FIREBASE_PLATFORM_ANDROID
#define INTEGRATION_TEST_PLATFORM_NAME "Android"
#elif FIREBASE_PLATFORM_IOS
#define INTEGRATION_TEST_PLATFORM_NAME "iOS"
#elif FIREBASE_PLATFORM_TVOS
#define INTEGRATION_TEST_PLATFORM_NAME "tvOS"
#elif FIREBASE_PLATFORM_OSX
#define INTEGRATION_TEST_PLATFORM_NAME "MacOS"
#elif FIREBASE_PLATFORM_WINDOWS
#define INTEGRATION_TEST_PLATFORM_NAME "Windows"
#elif FIREBASE_PLATFORM_LINUX
#define INTEGRATION_TEST_PLATFORM_NAME "Linux"
#else
#define INTEGRATION_TEST_PLATFORM_NAME "Unknown"
#endif

static std::string GenerateEmailAddress(const std::string& test_name) {
char time_string[22];
snprintf(time_string, sizeof(time_string), "%lld",
app_framework::GetCurrentTimeInMicroseconds());
Comment on lines 448 to 450
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using snprintf with %lld for an int64_t value can lead to portability issues or compiler warnings if int64_t is not exactly long long on all target platforms. Since this is C++, using std::to_string is a safer and more idiomatic way to convert the timestamp to a string.

Suggested change
char time_string[22];
snprintf(time_string, sizeof(time_string), "%lld",
app_framework::GetCurrentTimeInMicroseconds());
std::string time_string = std::to_string(app_framework::GetCurrentTimeInMicroseconds());

std::string email = "random_user_";

std::string sanitized_test_name = test_name;
for (char& c : sanitized_test_name) {
if (!std::isalnum(static_cast<unsigned char>(c)) && c != '_') {
c = '_';
}
}

if (sanitized_test_name.length() > 25) {
sanitized_test_name = sanitized_test_name.substr(0, 25);
}

std::string email = "user_";
email.append(INTEGRATION_TEST_PLATFORM_NAME);
email.append("_");
email.append(sanitized_test_name);
email.append("_");
email.append(time_string);
email.append("@gmail.com");
Comment on lines +463 to 469
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The email construction logic can be simplified by using string concatenation or a single append call, which improves readability. Additionally, since time_string is now a std::string (per the previous suggestion), the append calls are straightforward.

Suggested change
std::string email = "user_";
email.append(INTEGRATION_TEST_PLATFORM_NAME);
email.append("_");
email.append(sanitized_test_name);
email.append("_");
email.append(time_string);
email.append("@gmail.com");
std::string email = "user_" + std::string(INTEGRATION_TEST_PLATFORM_NAME) + "_" +
sanitized_test_name + "_" + time_string + "@gmail.com";


std::transform(email.begin(), email.end(), email.begin(),
[](unsigned char c) { return std::tolower(c); });

LogDebug("Generated email address: %s", email.c_str());
return email;
}

TEST_F(FirebaseAuthTest, TestEmailAndPasswordSignin) {
std::string email = GenerateEmailAddress();
std::string email = GenerateEmailAddress("EmailAndPasswordSignin");
// Register a random email and password. This signs us in as that user.
std::string password = kTestPassword;
firebase::Future<firebase::auth::AuthResult> auth_result_future =
Expand Down Expand Up @@ -529,7 +568,7 @@ TEST_F(FirebaseAuthTest, TestCopyUser) {
}

TEST_F(FirebaseAuthTest, TestRetainedUser) {
std::string email = GenerateEmailAddress();
std::string email = GenerateEmailAddress("RetainedUser");
// Register a random email and password. This signs us in as that user.
std::string password = kTestPassword;
firebase::Future<firebase::auth::AuthResult> auth_result_future =
Expand All @@ -546,7 +585,7 @@ TEST_F(FirebaseAuthTest, TestRetainedUser) {
EXPECT_EQ(retained_user.email(), "");

// Sign in a new account.
email = GenerateEmailAddress();
email = GenerateEmailAddress("RetainedUserNew");
auth_result_future =
auth_->CreateUserWithEmailAndPassword(email.c_str(), password.c_str());
WaitForCompletion(auth_result_future, "CreateUserWithEmailAndPassword");
Expand Down Expand Up @@ -587,7 +626,8 @@ TEST_F(FirebaseAuthTest, TestOperationsOnInvalidUser) {

firebase::auth::Credential email_cred =
firebase::auth::EmailAuthProvider::GetCredential(
GenerateEmailAddress().c_str(), kTestPasswordUpdated);
GenerateEmailAddress("InvalidUserReauth").c_str(),
kTestPasswordUpdated);
LogDebug("Reauthenticate");
void_future = invalid_user.Reauthenticate(email_cred);
WaitForCompletionOrInvalidStatus(void_future, "Reauthenticate");
Expand All @@ -606,7 +646,7 @@ TEST_F(FirebaseAuthTest, TestOperationsOnInvalidUser) {

LogDebug("SendEmailVerificationBeforeUpdatingEmail");
void_future = invalid_user.SendEmailVerificationBeforeUpdatingEmail(
GenerateEmailAddress().c_str());
GenerateEmailAddress("InvalidUserVerify").c_str());
WaitForCompletionOrInvalidStatus(void_future,
"SendEmailVerificationBeforeUpdatingEmail");
EXPECT_NE(void_future.error(), firebase::auth::kAuthErrorNone);
Expand Down Expand Up @@ -639,7 +679,7 @@ TEST_F(FirebaseAuthTest, TestOperationsOnInvalidUser) {
}

TEST_F(FirebaseAuthTest, TestUpdateUserProfile) {
std::string email = GenerateEmailAddress();
std::string email = GenerateEmailAddress("UpdateUserProfile");
firebase::Future<firebase::auth::AuthResult> create_user =
auth_->CreateUserWithEmailAndPassword(email.c_str(), kTestPassword);
WaitForCompletion(create_user, "CreateUserWithEmailAndPassword");
Expand Down Expand Up @@ -670,7 +710,7 @@ TEST_F(FirebaseAuthTest, TestUpdateUserProfile) {
}

TEST_F(FirebaseAuthTest, TestUpdateUserProfileNull) {
std::string email = GenerateEmailAddress();
std::string email = GenerateEmailAddress("UpdateUserProfileNull");
firebase::Future<firebase::auth::AuthResult> create_user =
auth_->CreateUserWithEmailAndPassword(email.c_str(), kTestPassword);
WaitForCompletion(create_user, "CreateUserWithEmailAndPassword");
Expand Down Expand Up @@ -712,7 +752,7 @@ TEST_F(FirebaseAuthTest, TestUpdateUserProfileNull) {
}

TEST_F(FirebaseAuthTest, TestUpdateUserProfileEmpty) {
std::string email = GenerateEmailAddress();
std::string email = GenerateEmailAddress("UpdateUserProfileEmpty");
firebase::Future<firebase::auth::AuthResult> create_user =
auth_->CreateUserWithEmailAndPassword(email.c_str(), kTestPassword);
WaitForCompletion(create_user, "CreateUserWithEmailAndPassword");
Expand Down Expand Up @@ -754,7 +794,7 @@ TEST_F(FirebaseAuthTest, TestUpdateUserProfileEmpty) {
}

TEST_F(FirebaseAuthTest, TestUpdateEmailAndPassword) {
std::string email = GenerateEmailAddress();
std::string email = GenerateEmailAddress("UpdateEmailAndPassword");
WaitForCompletion(
auth_->CreateUserWithEmailAndPassword(email.c_str(), kTestPassword),
"CreateUserWithEmailAndPassword");
Expand All @@ -773,7 +813,7 @@ TEST_F(FirebaseAuthTest, TestUpdateEmailAndPassword) {
}

TEST_F(FirebaseAuthTest, TestVerifyBeforeUpdatingEmail) {
std::string email = GenerateEmailAddress();
std::string email = GenerateEmailAddress("VerifyBeforeUpdatingEmail");
WaitForCompletion(
auth_->CreateUserWithEmailAndPassword(email.c_str(), kTestPassword),
"CreateUserWithEmailAndPassword");
Expand All @@ -796,33 +836,28 @@ TEST_F(FirebaseAuthTest, TestLinkAnonymousUserWithEmailCredential) {

firebase::auth::User user = auth_->current_user();
EXPECT_TRUE(user.is_valid());
std::string email = GenerateEmailAddress();
std::string email = GenerateEmailAddress("LinkAnonEmail");
firebase::auth::Credential credential =
firebase::auth::EmailAuthProvider::GetCredential(email.c_str(),
kTestPassword);
WaitForCompletion(user.LinkWithCredential(credential), "LinkWithCredential");
WaitForCompletion(user.Unlink(credential.provider().c_str()), "Unlink");
SignOut();
WaitForCompletion(auth_->SignInAnonymously(), "SignInAnonymously");
user = auth_->current_user();
EXPECT_TRUE(user.is_valid());
std::string email1 = GenerateEmailAddress();

// At this point, the user is linked to email, so linking again should fail.
std::string email1 = GenerateEmailAddress("LinkAnonEmail1");
firebase::auth::Credential credential1 =
firebase::auth::EmailAuthProvider::GetCredential(email1.c_str(),
kTestPassword);
WaitForCompletion(user.LinkWithCredential(credential1),
"LinkWithCredential 1");
user = auth_->current_user();
"LinkWithCredential (ProviderAlreadyLinked)",
firebase::auth::kAuthErrorProviderAlreadyLinked);

// Next, we unlink the first provider, then link again.
WaitForCompletion(user.Unlink(credential.provider().c_str()), "Unlink");
WaitForCompletion(user.LinkWithCredential(credential1),
"LinkWithCredential (Second attempt)");
EXPECT_TRUE(user.is_valid());

std::string email2 = GenerateEmailAddress();
firebase::auth::Credential credential2 =
firebase::auth::EmailAuthProvider::GetCredential(email2.c_str(),
kTestPassword);
WaitForCompletion(user.LinkWithCredential(credential2),
"LinkWithCredential 2",
firebase::auth::kAuthErrorProviderAlreadyLinked);
WaitForCompletion(user.Unlink(credential.provider().c_str()), "Unlink 2");
// Finally, delete the user.
DeleteUser();

// In case any operations failed, force signout before retrying the test.
Expand All @@ -847,15 +882,8 @@ TEST_F(FirebaseAuthTest, TestLinkAnonymousUserWithBadCredential) {
DeleteUser();
}

TEST_F(FirebaseAuthTest, TestSignInWithBadEmailFails) {
WaitForCompletion(
auth_->SignInWithEmailAndPassword(kTestEmailBad, kTestPassword),
"SignInWithEmailAndPassword", firebase::auth::kAuthErrorUserNotFound);
EXPECT_FALSE(auth_->current_user().is_valid());
}

TEST_F(FirebaseAuthTest, TestSignInWithBadPasswordFails) {
std::string email = GenerateEmailAddress();
std::string email = GenerateEmailAddress("SignInBadPassword");
WaitForCompletion(
auth_->CreateUserWithEmailAndPassword(email.c_str(), kTestPassword),
"CreateUserWithEmailAndPassword");
Expand All @@ -875,7 +903,7 @@ TEST_F(FirebaseAuthTest, TestSignInWithBadPasswordFails) {
}

TEST_F(FirebaseAuthTest, TestCreateUserWithExistingEmailFails) {
std::string email = GenerateEmailAddress();
std::string email = GenerateEmailAddress("CreateUserExistingEmail");
WaitForCompletion(
auth_->CreateUserWithEmailAndPassword(email.c_str(), kTestPassword),
"CreateUserWithEmailAndPassword 1");
Expand Down Expand Up @@ -996,7 +1024,7 @@ TEST_F(FirebaseAuthTest, TestGameCenterSignIn) {

TEST_F(FirebaseAuthTest, TestSendPasswordResetEmail) {
// Test Auth::SendPasswordResetEmail().
std::string email = GenerateEmailAddress();
std::string email = GenerateEmailAddress("SendPasswordResetEmail");
WaitForCompletion(
auth_->CreateUserWithEmailAndPassword(email.c_str(), kTestPassword),
"CreateUserWithEmailAndPassword");
Expand Down Expand Up @@ -1059,7 +1087,7 @@ TEST_F(FirebaseAuthTest, TestAuthPersistenceWithEmailSignin) {

FLAKY_TEST_SECTION_BEGIN();

std::string email = GenerateEmailAddress();
std::string email = GenerateEmailAddress("AuthPersistenceEmail");
WaitForCompletion(
auth_->CreateUserWithEmailAndPassword(email.c_str(), kTestPassword),
"CreateUserWithEmailAndPassword");
Expand Down
Loading