Skip to content

Commit 7beccb3

Browse files
authored
Fix issues with Auth integration tests (#1851)
* Add more information to the Auth tests * Update integration_test.cc * Update integration_test.cc * Update integration_test.cc * Update integration_test.cc
1 parent 5d1f3e3 commit 7beccb3

1 file changed

Lines changed: 70 additions & 45 deletions

File tree

auth/integration_test/src/integration_test.cc

Lines changed: 70 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <inttypes.h>
1616

1717
#include <algorithm>
18+
#include <cctype>
1819
#include <cstdio>
1920
#include <cstdlib>
2021
#include <cstring>
@@ -296,6 +297,8 @@ void FirebaseAuthTest::DeleteUser() {
296297
FirebaseTest::WaitForCompletion(auth_->current_user().Delete(),
297298
"Delete User");
298299
ProcessEvents(100);
300+
} else {
301+
LogDebug("Failed to Delete User, not Signed In");
299302
}
300303
}
301304

@@ -425,19 +428,52 @@ TEST_F(FirebaseAuthTest, TestTokensAndAuthStateListeners) {
425428
ElementsAre("", Not(""), Not(""), Not(""), "")));
426429
}
427430

428-
static std::string GenerateEmailAddress() {
429-
char time_string[22];
430-
snprintf(time_string, sizeof(time_string), "%lld",
431-
app_framework::GetCurrentTimeInMicroseconds());
432-
std::string email = "random_user_";
433-
email.append(time_string);
434-
email.append("@gmail.com");
431+
#if FIREBASE_PLATFORM_ANDROID
432+
#define INTEGRATION_TEST_PLATFORM_NAME "Android"
433+
#elif FIREBASE_PLATFORM_IOS
434+
#define INTEGRATION_TEST_PLATFORM_NAME "iOS"
435+
#elif FIREBASE_PLATFORM_TVOS
436+
#define INTEGRATION_TEST_PLATFORM_NAME "tvOS"
437+
#elif FIREBASE_PLATFORM_OSX
438+
#define INTEGRATION_TEST_PLATFORM_NAME "MacOS"
439+
#elif FIREBASE_PLATFORM_WINDOWS
440+
#define INTEGRATION_TEST_PLATFORM_NAME "Windows"
441+
#elif FIREBASE_PLATFORM_LINUX
442+
#define INTEGRATION_TEST_PLATFORM_NAME "Linux"
443+
#else
444+
#define INTEGRATION_TEST_PLATFORM_NAME "Unknown"
445+
#endif
446+
447+
static std::string GenerateEmailAddress(const std::string& test_name) {
448+
std::string time_string =
449+
std::to_string(app_framework::GetCurrentTimeInMicroseconds());
450+
451+
std::string sanitized_test_name = test_name;
452+
for (char& c : sanitized_test_name) {
453+
if (!std::isalnum(static_cast<unsigned char>(c)) && c != '_') {
454+
c = '_';
455+
}
456+
}
457+
458+
if (sanitized_test_name.length() > 25) {
459+
sanitized_test_name = sanitized_test_name.substr(0, 25);
460+
}
461+
462+
std::string email = "user_" + std::string(INTEGRATION_TEST_PLATFORM_NAME) +
463+
"_" + sanitized_test_name + "_" + time_string +
464+
"@gmail.com";
465+
466+
// The backend generally returns the string as lowercase, so just make it
467+
// lowercase to avoid confusion when comparing later.
468+
std::transform(email.begin(), email.end(), email.begin(),
469+
[](unsigned char c) { return std::tolower(c); });
470+
435471
LogDebug("Generated email address: %s", email.c_str());
436472
return email;
437473
}
438474

439475
TEST_F(FirebaseAuthTest, TestEmailAndPasswordSignin) {
440-
std::string email = GenerateEmailAddress();
476+
std::string email = GenerateEmailAddress("EmailAndPasswordSignin");
441477
// Register a random email and password. This signs us in as that user.
442478
std::string password = kTestPassword;
443479
firebase::Future<firebase::auth::AuthResult> auth_result_future =
@@ -529,7 +565,7 @@ TEST_F(FirebaseAuthTest, TestCopyUser) {
529565
}
530566

531567
TEST_F(FirebaseAuthTest, TestRetainedUser) {
532-
std::string email = GenerateEmailAddress();
568+
std::string email = GenerateEmailAddress("RetainedUser");
533569
// Register a random email and password. This signs us in as that user.
534570
std::string password = kTestPassword;
535571
firebase::Future<firebase::auth::AuthResult> auth_result_future =
@@ -546,7 +582,7 @@ TEST_F(FirebaseAuthTest, TestRetainedUser) {
546582
EXPECT_EQ(retained_user.email(), "");
547583

548584
// Sign in a new account.
549-
email = GenerateEmailAddress();
585+
email = GenerateEmailAddress("RetainedUserNew");
550586
auth_result_future =
551587
auth_->CreateUserWithEmailAndPassword(email.c_str(), password.c_str());
552588
WaitForCompletion(auth_result_future, "CreateUserWithEmailAndPassword");
@@ -587,7 +623,8 @@ TEST_F(FirebaseAuthTest, TestOperationsOnInvalidUser) {
587623

588624
firebase::auth::Credential email_cred =
589625
firebase::auth::EmailAuthProvider::GetCredential(
590-
GenerateEmailAddress().c_str(), kTestPasswordUpdated);
626+
GenerateEmailAddress("InvalidUserReauth").c_str(),
627+
kTestPasswordUpdated);
591628
LogDebug("Reauthenticate");
592629
void_future = invalid_user.Reauthenticate(email_cred);
593630
WaitForCompletionOrInvalidStatus(void_future, "Reauthenticate");
@@ -606,7 +643,7 @@ TEST_F(FirebaseAuthTest, TestOperationsOnInvalidUser) {
606643

607644
LogDebug("SendEmailVerificationBeforeUpdatingEmail");
608645
void_future = invalid_user.SendEmailVerificationBeforeUpdatingEmail(
609-
GenerateEmailAddress().c_str());
646+
GenerateEmailAddress("InvalidUserVerify").c_str());
610647
WaitForCompletionOrInvalidStatus(void_future,
611648
"SendEmailVerificationBeforeUpdatingEmail");
612649
EXPECT_NE(void_future.error(), firebase::auth::kAuthErrorNone);
@@ -639,7 +676,7 @@ TEST_F(FirebaseAuthTest, TestOperationsOnInvalidUser) {
639676
}
640677

641678
TEST_F(FirebaseAuthTest, TestUpdateUserProfile) {
642-
std::string email = GenerateEmailAddress();
679+
std::string email = GenerateEmailAddress("UpdateUserProfile");
643680
firebase::Future<firebase::auth::AuthResult> create_user =
644681
auth_->CreateUserWithEmailAndPassword(email.c_str(), kTestPassword);
645682
WaitForCompletion(create_user, "CreateUserWithEmailAndPassword");
@@ -670,7 +707,7 @@ TEST_F(FirebaseAuthTest, TestUpdateUserProfile) {
670707
}
671708

672709
TEST_F(FirebaseAuthTest, TestUpdateUserProfileNull) {
673-
std::string email = GenerateEmailAddress();
710+
std::string email = GenerateEmailAddress("UpdateUserProfileNull");
674711
firebase::Future<firebase::auth::AuthResult> create_user =
675712
auth_->CreateUserWithEmailAndPassword(email.c_str(), kTestPassword);
676713
WaitForCompletion(create_user, "CreateUserWithEmailAndPassword");
@@ -712,7 +749,7 @@ TEST_F(FirebaseAuthTest, TestUpdateUserProfileNull) {
712749
}
713750

714751
TEST_F(FirebaseAuthTest, TestUpdateUserProfileEmpty) {
715-
std::string email = GenerateEmailAddress();
752+
std::string email = GenerateEmailAddress("UpdateUserProfileEmpty");
716753
firebase::Future<firebase::auth::AuthResult> create_user =
717754
auth_->CreateUserWithEmailAndPassword(email.c_str(), kTestPassword);
718755
WaitForCompletion(create_user, "CreateUserWithEmailAndPassword");
@@ -754,7 +791,7 @@ TEST_F(FirebaseAuthTest, TestUpdateUserProfileEmpty) {
754791
}
755792

756793
TEST_F(FirebaseAuthTest, TestUpdateEmailAndPassword) {
757-
std::string email = GenerateEmailAddress();
794+
std::string email = GenerateEmailAddress("UpdateEmailAndPassword");
758795
WaitForCompletion(
759796
auth_->CreateUserWithEmailAndPassword(email.c_str(), kTestPassword),
760797
"CreateUserWithEmailAndPassword");
@@ -773,7 +810,7 @@ TEST_F(FirebaseAuthTest, TestUpdateEmailAndPassword) {
773810
}
774811

775812
TEST_F(FirebaseAuthTest, TestVerifyBeforeUpdatingEmail) {
776-
std::string email = GenerateEmailAddress();
813+
std::string email = GenerateEmailAddress("VerifyBeforeUpdatingEmail");
777814
WaitForCompletion(
778815
auth_->CreateUserWithEmailAndPassword(email.c_str(), kTestPassword),
779816
"CreateUserWithEmailAndPassword");
@@ -796,33 +833,28 @@ TEST_F(FirebaseAuthTest, TestLinkAnonymousUserWithEmailCredential) {
796833

797834
firebase::auth::User user = auth_->current_user();
798835
EXPECT_TRUE(user.is_valid());
799-
std::string email = GenerateEmailAddress();
836+
std::string email = GenerateEmailAddress("LinkAnonEmail");
800837
firebase::auth::Credential credential =
801838
firebase::auth::EmailAuthProvider::GetCredential(email.c_str(),
802839
kTestPassword);
803840
WaitForCompletion(user.LinkWithCredential(credential), "LinkWithCredential");
804-
WaitForCompletion(user.Unlink(credential.provider().c_str()), "Unlink");
805-
SignOut();
806-
WaitForCompletion(auth_->SignInAnonymously(), "SignInAnonymously");
807-
user = auth_->current_user();
808-
EXPECT_TRUE(user.is_valid());
809-
std::string email1 = GenerateEmailAddress();
841+
842+
// At this point, the user is linked to email, so linking again should fail.
843+
std::string email1 = GenerateEmailAddress("LinkAnonEmail1");
810844
firebase::auth::Credential credential1 =
811845
firebase::auth::EmailAuthProvider::GetCredential(email1.c_str(),
812846
kTestPassword);
813847
WaitForCompletion(user.LinkWithCredential(credential1),
814-
"LinkWithCredential 1");
815-
user = auth_->current_user();
848+
"LinkWithCredential (ProviderAlreadyLinked)",
849+
firebase::auth::kAuthErrorProviderAlreadyLinked);
850+
851+
// Next, we unlink the first provider, then link again.
852+
WaitForCompletion(user.Unlink(credential.provider().c_str()), "Unlink");
853+
WaitForCompletion(user.LinkWithCredential(credential1),
854+
"LinkWithCredential (Second attempt)");
816855
EXPECT_TRUE(user.is_valid());
817856

818-
std::string email2 = GenerateEmailAddress();
819-
firebase::auth::Credential credential2 =
820-
firebase::auth::EmailAuthProvider::GetCredential(email2.c_str(),
821-
kTestPassword);
822-
WaitForCompletion(user.LinkWithCredential(credential2),
823-
"LinkWithCredential 2",
824-
firebase::auth::kAuthErrorProviderAlreadyLinked);
825-
WaitForCompletion(user.Unlink(credential.provider().c_str()), "Unlink 2");
857+
// Finally, delete the user.
826858
DeleteUser();
827859

828860
// In case any operations failed, force signout before retrying the test.
@@ -847,15 +879,8 @@ TEST_F(FirebaseAuthTest, TestLinkAnonymousUserWithBadCredential) {
847879
DeleteUser();
848880
}
849881

850-
TEST_F(FirebaseAuthTest, TestSignInWithBadEmailFails) {
851-
WaitForCompletion(
852-
auth_->SignInWithEmailAndPassword(kTestEmailBad, kTestPassword),
853-
"SignInWithEmailAndPassword", firebase::auth::kAuthErrorUserNotFound);
854-
EXPECT_FALSE(auth_->current_user().is_valid());
855-
}
856-
857882
TEST_F(FirebaseAuthTest, TestSignInWithBadPasswordFails) {
858-
std::string email = GenerateEmailAddress();
883+
std::string email = GenerateEmailAddress("SignInBadPassword");
859884
WaitForCompletion(
860885
auth_->CreateUserWithEmailAndPassword(email.c_str(), kTestPassword),
861886
"CreateUserWithEmailAndPassword");
@@ -875,7 +900,7 @@ TEST_F(FirebaseAuthTest, TestSignInWithBadPasswordFails) {
875900
}
876901

877902
TEST_F(FirebaseAuthTest, TestCreateUserWithExistingEmailFails) {
878-
std::string email = GenerateEmailAddress();
903+
std::string email = GenerateEmailAddress("CreateUserExistingEmail");
879904
WaitForCompletion(
880905
auth_->CreateUserWithEmailAndPassword(email.c_str(), kTestPassword),
881906
"CreateUserWithEmailAndPassword 1");
@@ -996,7 +1021,7 @@ TEST_F(FirebaseAuthTest, TestGameCenterSignIn) {
9961021

9971022
TEST_F(FirebaseAuthTest, TestSendPasswordResetEmail) {
9981023
// Test Auth::SendPasswordResetEmail().
999-
std::string email = GenerateEmailAddress();
1024+
std::string email = GenerateEmailAddress("SendPasswordResetEmail");
10001025
WaitForCompletion(
10011026
auth_->CreateUserWithEmailAndPassword(email.c_str(), kTestPassword),
10021027
"CreateUserWithEmailAndPassword");
@@ -1059,7 +1084,7 @@ TEST_F(FirebaseAuthTest, TestAuthPersistenceWithEmailSignin) {
10591084

10601085
FLAKY_TEST_SECTION_BEGIN();
10611086

1062-
std::string email = GenerateEmailAddress();
1087+
std::string email = GenerateEmailAddress("AuthPersistenceEmail");
10631088
WaitForCompletion(
10641089
auth_->CreateUserWithEmailAndPassword(email.c_str(), kTestPassword),
10651090
"CreateUserWithEmailAndPassword");

0 commit comments

Comments
 (0)