Skip to content
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
3 changes: 3 additions & 0 deletions auth/tests/auth_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ class AuthTest : public ::testing::Test {
void MakeAuth() {
firebase_app_ = testing::CreateApp();
firebase_auth_ = Auth::GetAuth(firebase_app_);
if(firebase_auth_->current_user()) {
firebase_auth_->SignOut();
}
}

App* firebase_app_ = nullptr;
Expand Down
20 changes: 20 additions & 0 deletions auth/tests/desktop/auth_desktop_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ using test::GetUrlForApi;
using test::InitializeConfigWithAFake;
using test::InitializeConfigWithFakes;
using test::OAuthProviderTestHandler;
using test::SleepUponDestruction;
using test::VerifySignInResult;
using test::WaitForFuture;
using ::testing::IsEmpty;
Expand Down Expand Up @@ -457,6 +458,7 @@ TEST_F(AuthDesktopTest, TestGetAccountInfo) {
// getAccountInfo never returns new tokens, and can't change current user.
id_token_listener.ExpectChanges(1);
auth_state_listener.ExpectChanges(1);
SleepUponDestruction sleep_for_listeners;

// Call the function and verify results.
AuthData auth_data;
Expand All @@ -474,6 +476,7 @@ TEST_F(AuthDesktopTest, TestGetAccountInfo) {
EXPECT_EQ("519", user.phone_number);
EXPECT_FALSE(user.is_email_verified);
EXPECT_TRUE(user.has_email_password_credential);

}

// Test the helper function CompleteSignIn. Since we do not have the access to
Expand All @@ -488,6 +491,7 @@ TEST_F(AuthDesktopTest, CompleteSignInWithFailedResponse) {
// Because the API call fails, current user shouldn't have changed.
id_token_listener.ExpectChanges(1);
auth_state_listener.ExpectChanges(1);
SleepUponDestruction sleep_for_listeners;

// Call the function and verify results.
const User* const user =
Expand All @@ -514,6 +518,7 @@ TEST_F(AuthDesktopTest, CompleteSignInWithGetAccountInfoFailure) {
// getAccountInfo fails, current user shouldn't have changed.
id_token_listener.ExpectChanges(1);
auth_state_listener.ExpectChanges(1);
SleepUponDestruction sleep_for_listeners;

// Call the function and verify results.
const User* const user =
Expand Down Expand Up @@ -546,6 +551,7 @@ TEST_F(AuthDesktopTest, TestSignInAnonymously) {

id_token_listener.ExpectChanges(2);
auth_state_listener.ExpectChanges(2);
SleepUponDestruction sleep_for_listeners;

const User* const user = WaitForFuture(firebase_auth_->SignInAnonymously());
EXPECT_TRUE(user->is_anonymous());
Expand Down Expand Up @@ -574,6 +580,7 @@ TEST_F(AuthDesktopTest, TestSignInWithEmailAndPassword) {

id_token_listener.ExpectChanges(2);
auth_state_listener.ExpectChanges(2);
SleepUponDestruction sleep_for_listeners;

// Call the function and verify results.
const Future<User*> future = firebase_auth_->SignInWithEmailAndPassword(
Expand Down Expand Up @@ -610,6 +617,7 @@ TEST_F(AuthDesktopTest, TestCreateUserWithEmailAndPassword) {

id_token_listener.ExpectChanges(2);
auth_state_listener.ExpectChanges(2);
SleepUponDestruction sleep_for_listeners;

const Future<User*> future = firebase_auth_->CreateUserWithEmailAndPassword(
"testsignin@example.com", "testsignin");
Expand All @@ -633,6 +641,7 @@ TEST_F(AuthDesktopTest, TestSignInWithCustomToken) {

id_token_listener.ExpectChanges(2);
auth_state_listener.ExpectChanges(2);
SleepUponDestruction sleep_for_listeners;

const User* const user =
WaitForFuture(firebase_auth_->SignInWithCustomToken("fake_custom_token"));
Expand All @@ -647,6 +656,7 @@ TEST_F(AuthDesktopTest, TestSignInWithCredential_GoogleIdToken) {

id_token_listener.ExpectChanges(2);
auth_state_listener.ExpectChanges(2);
SleepUponDestruction sleep_for_listeners;

const Credential credential =
GoogleAuthProvider::GetCredential("fake_id_token", "");
Expand All @@ -661,6 +671,7 @@ TEST_F(AuthDesktopTest, TestSignInWithCredential_GoogleAccessToken) {

id_token_listener.ExpectChanges(2);
auth_state_listener.ExpectChanges(2);
SleepUponDestruction sleep_for_listeners;

const Credential credential =
GoogleAuthProvider::GetCredential("", "fake_access_token");
Expand All @@ -679,6 +690,7 @@ TEST_F(AuthDesktopTest,

id_token_listener.ExpectChanges(1);
auth_state_listener.ExpectChanges(1);
SleepUponDestruction sleep_for_listeners;

const Credential credential =
GoogleAuthProvider::GetCredential("", "fake_access_token");
Expand All @@ -697,6 +709,7 @@ TEST_F(AuthDesktopTest,

id_token_listener.ExpectChanges(1);
auth_state_listener.ExpectChanges(1);
SleepUponDestruction sleep_for_listeners;

const Credential credential =
GoogleAuthProvider::GetCredential("", "fake_access_token");
Expand All @@ -714,6 +727,7 @@ TEST_F(AuthDesktopTest, TestSignInWithCredential_NeedsConfirmation) {
// shouldn't have been updated.
id_token_listener.ExpectChanges(1);
auth_state_listener.ExpectChanges(1);
SleepUponDestruction sleep_for_listeners;

const Credential credential =
GoogleAuthProvider::GetCredential("fake_id_token", "");
Expand All @@ -731,6 +745,7 @@ TEST_F(AuthDesktopTest, TestSignInAndRetrieveDataWithCredential_GitHub) {

id_token_listener.ExpectChanges(2);
auth_state_listener.ExpectChanges(2);
SleepUponDestruction sleep_for_listeners;

const Credential credential =
GitHubAuthProvider::GetCredential("fake_access_token");
Expand Down Expand Up @@ -760,6 +775,7 @@ TEST_F(AuthDesktopTest, TestSignInAndRetrieveDataWithCredential_Twitter) {

id_token_listener.ExpectChanges(2);
auth_state_listener.ExpectChanges(2);
SleepUponDestruction sleep_for_listeners;

const Credential credential = TwitterAuthProvider::GetCredential(
"fake_access_token", "fake_oauth_token");
Expand All @@ -780,6 +796,7 @@ TEST_F(AuthDesktopTest,

id_token_listener.ExpectChanges(2);
auth_state_listener.ExpectChanges(2);
SleepUponDestruction sleep_for_listeners;

const Credential credential = TwitterAuthProvider::GetCredential(
"fake_access_token", "fake_oauth_token");
Expand All @@ -803,6 +820,7 @@ TEST_F(AuthDesktopTest,

id_token_listener.ExpectChanges(2);
auth_state_listener.ExpectChanges(2);
SleepUponDestruction sleep_for_listeners;

const Credential credential = TwitterAuthProvider::GetCredential(
"fake_access_token", "fake_oauth_token");
Expand All @@ -827,6 +845,7 @@ TEST_F(AuthDesktopTest, TestFetchProvidersForEmail) {
// Fetch providers flow shouldn't affect current user in any way.
id_token_listener.ExpectChanges(1);
auth_state_listener.ExpectChanges(1);
SleepUponDestruction sleep_for_listeners;

const Auth::FetchProvidersResult result = WaitForFuture(
firebase_auth_->FetchProvidersForEmail("fake_email@example.com"));
Expand All @@ -844,6 +863,7 @@ TEST_F(AuthDesktopTest, TestSendPasswordResetEmail) {
// Sending password reset email shouldn't affect current user in any way.
id_token_listener.ExpectChanges(1);
auth_state_listener.ExpectChanges(1);
SleepUponDestruction sleep_for_listeners;

WaitForFuture(
firebase_auth_->SendPasswordResetEmail("fake_email@example.com"));
Expand Down
10 changes: 10 additions & 0 deletions auth/tests/desktop/test_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,16 @@ class AuthStateChangesCounter : public detail::ListenerChangeCounter,
void OnAuthStateChanged(Auth* /*unused*/) override;
};

// Class that when destroyed will momentarly sleep. Used to ensure that
// listener callbacks have time to be invoked before they're verified.
class SleepUponDestruction {
public:
~SleepUponDestruction() {
firebase::internal::Sleep(200);
}
};


// Waits until the given future is complete and asserts that it completed with
// the given error (no error by default). Returns the future's result.
template <typename T>
Expand Down
4 changes: 4 additions & 0 deletions auth/tests/desktop/user_desktop_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ using test::GetUrlForApi;
using test::InitializeConfigWithAFake;
using test::InitializeConfigWithFakes;
using test::OAuthProviderTestHandler;
using test::SleepUponDestruction;
using test::VerifySignInResult;
using test::WaitForFuture;

Expand Down Expand Up @@ -287,6 +288,9 @@ class UserDesktopTest : public ::testing::Test {
}

void TearDown() override {
{
SleepUponDestruction sleep_for_listeners;
}
// Reset listeners before signing out.
id_token_listener.VerifyAndReset();
auth_state_listener.VerifyAndReset();
Expand Down
9 changes: 6 additions & 3 deletions auth/tests/user_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,12 @@ TEST_F(UserTest, TestReauthenticate) {
"}";
firebase::testing::cppsdk::ConfigSet(config.c_str());

Future<void> result = firebase_user_->Reauthenticate(
EmailAuthProvider::GetCredential("i@email.com", "pw"));
Verify(result);
Credential credential = EmailAuthProvider::GetCredential("i@email.com", "pw");
Future<User*> sign_in_result = firebase_auth_->SignInWithCredential(credential);
Verify(sign_in_result);

Future<void> reauthenticate_result = firebase_user_->Reauthenticate(credential);
Verify(reauthenticate_result);
}

#if !defined(__APPLE__) && !defined(FIREBASE_WAIT_ASYNC_IN_TEST)
Expand Down