Commit 39ba6f46 authored by Colin Blundell's avatar Colin Blundell Committed by Commit Bot

Make account components explicit in AccountTracker unittest

The AccountTracker unittest currently plays fast-and-loose with the
notion of an "account ID", which can be either the Gaia ID or the
email address for a given account depending on the platform. This is a
blocker to porting AccountTracker to use IdentityManager, as the
IdentityManager test infrastructure deliberately makes a much more
explicit and enforced separation between email address, Gaia ID, and
account ID, reflecting how these elements are used in production.
In particular:
- Adding an account takes in the gaia ID and email and returns an
  account ID.
- Calls that interact with the token service take in the account ID.

This CL paves the way for that porting by changing the
AccountTracker unittest to be explicit about its usage of the
email address, Gaia ID, and account ID of a given account. The test now
adds accounts by passing the gaia ID/email, obtains the account IDs of
the added accounts, and interacts with the token service via those
account IDs.

Bug: 809923
Change-Id: Ia0bf7fc90c035976f8c578563e7d81c597d4d635
Reviewed-on: https://chromium-review.googlesource.com/1138082
Commit-Queue: Colin Blundell <blundell@chromium.org>
Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#577444}
parent d0994aa8
...@@ -31,7 +31,8 @@ using SigninManagerForTest = FakeSigninManagerBase; ...@@ -31,7 +31,8 @@ using SigninManagerForTest = FakeSigninManagerBase;
using SigninManagerForTest = FakeSigninManager; using SigninManagerForTest = FakeSigninManager;
#endif // OS_CHROMEOS #endif // OS_CHROMEOS
const char kPrimaryAccountKey[] = "primary_account@example.com"; const char kPrimaryAccountEmail[] = "primary_account@example.com";
const char kPrimaryAccountGaiaId[] = "primary_account";
enum TrackingEventType { SIGN_IN, SIGN_OUT }; enum TrackingEventType { SIGN_IN, SIGN_OUT };
...@@ -288,13 +289,17 @@ class AccountTrackerTest : public testing::Test { ...@@ -288,13 +289,17 @@ class AccountTrackerTest : public testing::Test {
// Sets the primary account info but carries no guarantee of firing the // Sets the primary account info but carries no guarantee of firing the
// callback that signin occurred (see NotifyLogin() below if exercising a // callback that signin occurred (see NotifyLogin() below if exercising a
// true signin flow in a non-ChromeOS context). // true signin flow in a non-ChromeOS context). Returns the account ID of the
void SetActiveAccount(const std::string& account_key) { // newly-set account.
std::string SetActiveAccount(const std::string& gaia_id,
const std::string& email) {
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
fake_signin_manager_->SignIn(account_key); fake_signin_manager_->SignIn(email);
#else #else
fake_signin_manager_->SignIn(account_key, account_key, "" /* password */); fake_signin_manager_->SignIn(gaia_id, email, "" /* password */);
#endif #endif
return fake_signin_manager_->GetAuthenticatedAccountId();
} }
// NOTE: On ChromeOS, the login callback is never fired in production (since the // NOTE: On ChromeOS, the login callback is never fired in production (since the
...@@ -302,8 +307,12 @@ class AccountTrackerTest : public testing::Test { ...@@ -302,8 +307,12 @@ class AccountTrackerTest : public testing::Test {
// exercise functionality dependent on that callback firing are not relevant // exercise functionality dependent on that callback firing are not relevant
// on ChromeOS and should simply not run on that platform. // on ChromeOS and should simply not run on that platform.
#if !defined(OS_CHROMEOS) #if !defined(OS_CHROMEOS)
void NotifyLogin(const std::string& account_key) { // Sets the primary account info and fires the callback that signin occurred.
fake_signin_manager_->SignIn(account_key, account_key, "" /* password */); // Returns the account ID of the newly-set account.
std::string NotifyLogin(const std::string& gaia_id,
const std::string& email) {
fake_signin_manager_->SignIn(gaia_id, email, "" /* password */);
return fake_signin_manager_->GetAuthenticatedAccountId();
} }
void NotifyLogoutOfPrimaryAccountOnly() { void NotifyLogoutOfPrimaryAccountOnly() {
...@@ -319,23 +328,32 @@ class AccountTrackerTest : public testing::Test { ...@@ -319,23 +328,32 @@ class AccountTrackerTest : public testing::Test {
} }
#endif #endif
void NotifyTokenAvailable(const std::string& username) { std::string AddAccountWithToken(const std::string& gaia,
fake_oauth2_token_service_->UpdateCredentials(username, const std::string& email) {
std::string account_id =
account_tracker_service_.SeedAccountInfo(gaia, email);
fake_oauth2_token_service_->UpdateCredentials(account_id,
"fake_refresh_token"); "fake_refresh_token");
return account_id;
} }
void NotifyTokenRevoked(const std::string& username) { void NotifyTokenAvailable(const std::string& account_id) {
fake_oauth2_token_service_->RevokeCredentials(username); fake_oauth2_token_service_->UpdateCredentials(account_id,
"fake_refresh_token");
}
void NotifyTokenRevoked(const std::string& account_id) {
fake_oauth2_token_service_->RevokeCredentials(account_id);
} }
// Helpers to fake access token and user info fetching // Helpers to fake access token and user info fetching
void IssueAccessToken(const std::string& username) { void IssueAccessToken(const std::string& account_id) {
fake_oauth2_token_service_->IssueAllTokensForAccount( fake_oauth2_token_service_->IssueAllTokensForAccount(
username, "access_token-" + username, base::Time::Max()); account_id, "access_token-" + account_id, base::Time::Max());
} }
std::string GetValidTokenInfoResponse(const std::string& account_key) { std::string GetValidTokenInfoResponse(const std::string& account_id) {
return std::string("{ \"id\": \"") + AccountKeyToObfuscatedId(account_key) + return std::string("{ \"id\": \"") + AccountKeyToObfuscatedId(account_id) +
"\" }"; "\" }";
} }
...@@ -346,12 +364,15 @@ class AccountTrackerTest : public testing::Test { ...@@ -346,12 +364,15 @@ class AccountTrackerTest : public testing::Test {
void ReturnOAuthUrlFetchSuccess(const std::string& account_key); void ReturnOAuthUrlFetchSuccess(const std::string& account_key);
void ReturnOAuthUrlFetchFailure(const std::string& account_key); void ReturnOAuthUrlFetchFailure(const std::string& account_key);
void SetupPrimaryLogin() { std::string SetupPrimaryLogin() {
// Initial setup for tests that start with a signed in profile. // Initial setup for tests that start with a signed in profile.
SetActiveAccount(kPrimaryAccountKey); std::string primary_account_id =
NotifyTokenAvailable(kPrimaryAccountKey); SetActiveAccount(kPrimaryAccountGaiaId, kPrimaryAccountEmail);
ReturnOAuthUrlFetchSuccess(kPrimaryAccountKey); NotifyTokenAvailable(primary_account_id);
ReturnOAuthUrlFetchSuccess(primary_account_id);
observer()->Clear(); observer()->Clear();
return primary_account_id;
} }
private: private:
...@@ -380,15 +401,15 @@ void AccountTrackerTest::ReturnOAuthUrlFetchResults( ...@@ -380,15 +401,15 @@ void AccountTrackerTest::ReturnOAuthUrlFetchResults(
} }
void AccountTrackerTest::ReturnOAuthUrlFetchSuccess( void AccountTrackerTest::ReturnOAuthUrlFetchSuccess(
const std::string& account_key) { const std::string& account_id) {
IssueAccessToken(account_key); IssueAccessToken(account_id);
ReturnOAuthUrlFetchResults(gaia::GaiaOAuthClient::kUrlFetcherId, net::HTTP_OK, ReturnOAuthUrlFetchResults(gaia::GaiaOAuthClient::kUrlFetcherId, net::HTTP_OK,
GetValidTokenInfoResponse(account_key)); GetValidTokenInfoResponse(account_id));
} }
void AccountTrackerTest::ReturnOAuthUrlFetchFailure( void AccountTrackerTest::ReturnOAuthUrlFetchFailure(
const std::string& account_key) { const std::string& account_id) {
IssueAccessToken(account_key); IssueAccessToken(account_id);
ReturnOAuthUrlFetchResults(gaia::GaiaOAuthClient::kUrlFetcherId, ReturnOAuthUrlFetchResults(gaia::GaiaOAuthClient::kUrlFetcherId,
net::HTTP_BAD_REQUEST, ""); net::HTTP_BAD_REQUEST, "");
} }
...@@ -396,8 +417,8 @@ void AccountTrackerTest::ReturnOAuthUrlFetchFailure( ...@@ -396,8 +417,8 @@ void AccountTrackerTest::ReturnOAuthUrlFetchFailure(
// Primary tests just involve the Active account // Primary tests just involve the Active account
TEST_F(AccountTrackerTest, PrimaryNoEventsBeforeLogin) { TEST_F(AccountTrackerTest, PrimaryNoEventsBeforeLogin) {
NotifyTokenAvailable(kPrimaryAccountKey); NotifyTokenAvailable("dummy_account_id");
NotifyTokenRevoked(kPrimaryAccountKey); NotifyTokenRevoked("dummy_account_id");
// Logout is not possible on ChromeOS. // Logout is not possible on ChromeOS.
#if !defined(OS_CHROMEOS) #if !defined(OS_CHROMEOS)
...@@ -408,101 +429,108 @@ TEST_F(AccountTrackerTest, PrimaryNoEventsBeforeLogin) { ...@@ -408,101 +429,108 @@ TEST_F(AccountTrackerTest, PrimaryNoEventsBeforeLogin) {
} }
TEST_F(AccountTrackerTest, PrimaryLoginThenTokenAvailable) { TEST_F(AccountTrackerTest, PrimaryLoginThenTokenAvailable) {
SetActiveAccount(kPrimaryAccountKey); std::string primary_account_id =
NotifyTokenAvailable(kPrimaryAccountKey); SetActiveAccount(kPrimaryAccountGaiaId, kPrimaryAccountEmail);
NotifyTokenAvailable(primary_account_id);
EXPECT_TRUE(observer()->CheckEvents()); EXPECT_TRUE(observer()->CheckEvents());
ReturnOAuthUrlFetchSuccess(kPrimaryAccountKey); ReturnOAuthUrlFetchSuccess(primary_account_id);
EXPECT_TRUE( EXPECT_TRUE(
observer()->CheckEvents(TrackingEvent(SIGN_IN, kPrimaryAccountKey))); observer()->CheckEvents(TrackingEvent(SIGN_IN, primary_account_id)));
} }
// These tests exercise true login/logout, which are not possible on ChromeOS.
#if !defined(OS_CHROMEOS) #if !defined(OS_CHROMEOS)
TEST_F(AccountTrackerTest, PrimaryTokenAvailableThenLogin) { TEST_F(AccountTrackerTest, PrimaryTokenAvailableThenLogin) {
NotifyTokenAvailable(kPrimaryAccountKey); AddAccountWithToken(kPrimaryAccountGaiaId, kPrimaryAccountEmail);
EXPECT_TRUE(observer()->CheckEvents()); EXPECT_TRUE(observer()->CheckEvents());
NotifyLogin(kPrimaryAccountKey); std::string primary_account_id =
ReturnOAuthUrlFetchSuccess(kPrimaryAccountKey); NotifyLogin(kPrimaryAccountGaiaId, kPrimaryAccountEmail);
ReturnOAuthUrlFetchSuccess(primary_account_id);
EXPECT_TRUE( EXPECT_TRUE(
observer()->CheckEvents(TrackingEvent(SIGN_IN, kPrimaryAccountKey))); observer()->CheckEvents(TrackingEvent(SIGN_IN, primary_account_id)));
} }
TEST_F(AccountTrackerTest, PrimaryTokenAvailableAndRevokedThenLogin) { TEST_F(AccountTrackerTest, PrimaryTokenAvailableAndRevokedThenLogin) {
NotifyTokenAvailable(kPrimaryAccountKey); std::string primary_account_id =
AddAccountWithToken(kPrimaryAccountGaiaId, kPrimaryAccountEmail);
EXPECT_TRUE(observer()->CheckEvents()); EXPECT_TRUE(observer()->CheckEvents());
NotifyTokenRevoked(kPrimaryAccountKey); NotifyTokenRevoked(primary_account_id);
EXPECT_TRUE(observer()->CheckEvents()); EXPECT_TRUE(observer()->CheckEvents());
NotifyLogin(kPrimaryAccountKey); NotifyLogin(kPrimaryAccountGaiaId, kPrimaryAccountEmail);
EXPECT_TRUE(observer()->CheckEvents()); EXPECT_TRUE(observer()->CheckEvents());
} }
#endif #endif
TEST_F(AccountTrackerTest, PrimaryRevoke) { TEST_F(AccountTrackerTest, PrimaryRevoke) {
SetActiveAccount(kPrimaryAccountKey); std::string primary_account_id =
NotifyTokenAvailable(kPrimaryAccountKey); SetActiveAccount(kPrimaryAccountGaiaId, kPrimaryAccountEmail);
ReturnOAuthUrlFetchSuccess(kPrimaryAccountKey); NotifyTokenAvailable(primary_account_id);
ReturnOAuthUrlFetchSuccess(primary_account_id);
observer()->Clear(); observer()->Clear();
NotifyTokenRevoked(kPrimaryAccountKey); NotifyTokenRevoked(primary_account_id);
EXPECT_TRUE( EXPECT_TRUE(
observer()->CheckEvents(TrackingEvent(SIGN_OUT, kPrimaryAccountKey))); observer()->CheckEvents(TrackingEvent(SIGN_OUT, primary_account_id)));
} }
TEST_F(AccountTrackerTest, PrimaryRevokeThenLogin) { TEST_F(AccountTrackerTest, PrimaryRevokeThenLogin) {
SetActiveAccount(kPrimaryAccountKey); std::string primary_account_id =
NotifyTokenAvailable(kPrimaryAccountKey); SetActiveAccount(kPrimaryAccountGaiaId, kPrimaryAccountEmail);
ReturnOAuthUrlFetchSuccess(kPrimaryAccountKey); NotifyTokenAvailable(primary_account_id);
NotifyTokenRevoked(kPrimaryAccountKey); ReturnOAuthUrlFetchSuccess(primary_account_id);
NotifyTokenRevoked(primary_account_id);
observer()->Clear(); observer()->Clear();
SetActiveAccount(kPrimaryAccountKey); SetActiveAccount(kPrimaryAccountGaiaId, kPrimaryAccountEmail);
EXPECT_TRUE(observer()->CheckEvents()); EXPECT_TRUE(observer()->CheckEvents());
} }
TEST_F(AccountTrackerTest, PrimaryRevokeThenTokenAvailable) { TEST_F(AccountTrackerTest, PrimaryRevokeThenTokenAvailable) {
SetActiveAccount(kPrimaryAccountKey); std::string primary_account_id =
NotifyTokenAvailable(kPrimaryAccountKey); SetActiveAccount(kPrimaryAccountGaiaId, kPrimaryAccountEmail);
ReturnOAuthUrlFetchSuccess(kPrimaryAccountKey); NotifyTokenAvailable(primary_account_id);
NotifyTokenRevoked(kPrimaryAccountKey); ReturnOAuthUrlFetchSuccess(primary_account_id);
NotifyTokenRevoked(primary_account_id);
observer()->Clear(); observer()->Clear();
NotifyTokenAvailable(kPrimaryAccountKey); NotifyTokenAvailable(primary_account_id);
EXPECT_TRUE( EXPECT_TRUE(
observer()->CheckEvents(TrackingEvent(SIGN_IN, kPrimaryAccountKey))); observer()->CheckEvents(TrackingEvent(SIGN_IN, primary_account_id)));
} }
// These tests exercise true login/logout, which are not possible on ChromeOS. // These tests exercise true login/logout, which are not possible on ChromeOS.
#if !defined(OS_CHROMEOS) #if !defined(OS_CHROMEOS)
TEST_F(AccountTrackerTest, PrimaryLogoutThenRevoke) { TEST_F(AccountTrackerTest, PrimaryLogoutThenRevoke) {
NotifyLogin(kPrimaryAccountKey); std::string primary_account_id =
NotifyTokenAvailable(kPrimaryAccountKey); NotifyLogin(kPrimaryAccountGaiaId, kPrimaryAccountEmail);
ReturnOAuthUrlFetchSuccess(kPrimaryAccountKey); NotifyTokenAvailable(primary_account_id);
ReturnOAuthUrlFetchSuccess(primary_account_id);
observer()->Clear(); observer()->Clear();
NotifyLogoutOfAllAccounts(); NotifyLogoutOfAllAccounts();
EXPECT_TRUE( EXPECT_TRUE(
observer()->CheckEvents(TrackingEvent(SIGN_OUT, kPrimaryAccountKey))); observer()->CheckEvents(TrackingEvent(SIGN_OUT, primary_account_id)));
NotifyTokenRevoked(kPrimaryAccountKey); NotifyTokenRevoked(primary_account_id);
EXPECT_TRUE(observer()->CheckEvents()); EXPECT_TRUE(observer()->CheckEvents());
} }
TEST_F(AccountTrackerTest, PrimaryLogoutFetchCancelAvailable) { TEST_F(AccountTrackerTest, PrimaryLogoutFetchCancelAvailable) {
NotifyLogin(kPrimaryAccountKey); std::string primary_account_id =
NotifyTokenAvailable(kPrimaryAccountKey); NotifyLogin(kPrimaryAccountGaiaId, kPrimaryAccountEmail);
NotifyTokenAvailable(primary_account_id);
// TokenAvailable kicks off a fetch. Logout without satisfying it. // TokenAvailable kicks off a fetch. Logout without satisfying it.
NotifyLogoutOfAllAccounts(); NotifyLogoutOfAllAccounts();
EXPECT_TRUE(observer()->CheckEvents()); EXPECT_TRUE(observer()->CheckEvents());
SetActiveAccount(kPrimaryAccountKey); SetActiveAccount(kPrimaryAccountGaiaId, kPrimaryAccountEmail);
NotifyTokenAvailable(kPrimaryAccountKey); NotifyTokenAvailable(primary_account_id);
ReturnOAuthUrlFetchSuccess(kPrimaryAccountKey); ReturnOAuthUrlFetchSuccess(primary_account_id);
EXPECT_TRUE( EXPECT_TRUE(
observer()->CheckEvents(TrackingEvent(SIGN_IN, kPrimaryAccountKey))); observer()->CheckEvents(TrackingEvent(SIGN_IN, primary_account_id)));
} }
#endif #endif
...@@ -511,162 +539,156 @@ TEST_F(AccountTrackerTest, PrimaryLogoutFetchCancelAvailable) { ...@@ -511,162 +539,156 @@ TEST_F(AccountTrackerTest, PrimaryLogoutFetchCancelAvailable) {
TEST_F(AccountTrackerTest, Available) { TEST_F(AccountTrackerTest, Available) {
SetupPrimaryLogin(); SetupPrimaryLogin();
NotifyTokenAvailable("user@example.com"); std::string account_id = AddAccountWithToken("user", "user@example.com");
EXPECT_TRUE(observer()->CheckEvents()); EXPECT_TRUE(observer()->CheckEvents());
ReturnOAuthUrlFetchSuccess("user@example.com"); ReturnOAuthUrlFetchSuccess(account_id);
EXPECT_TRUE( EXPECT_TRUE(observer()->CheckEvents(TrackingEvent(SIGN_IN, account_id)));
observer()->CheckEvents(TrackingEvent(SIGN_IN, "user@example.com")));
} }
TEST_F(AccountTrackerTest, AvailableRevokeAvailable) { TEST_F(AccountTrackerTest, AvailableRevokeAvailable) {
SetupPrimaryLogin(); SetupPrimaryLogin();
NotifyTokenAvailable("user@example.com"); std::string account_id = AddAccountWithToken("user", "user@example.com");
ReturnOAuthUrlFetchSuccess("user@example.com"); ReturnOAuthUrlFetchSuccess(account_id);
NotifyTokenRevoked("user@example.com"); NotifyTokenRevoked(account_id);
EXPECT_TRUE( EXPECT_TRUE(observer()->CheckEvents(TrackingEvent(SIGN_IN, account_id),
observer()->CheckEvents(TrackingEvent(SIGN_IN, "user@example.com"), TrackingEvent(SIGN_OUT, account_id)));
TrackingEvent(SIGN_OUT, "user@example.com")));
NotifyTokenAvailable("user@example.com"); NotifyTokenAvailable(account_id);
EXPECT_TRUE( EXPECT_TRUE(observer()->CheckEvents(TrackingEvent(SIGN_IN, account_id)));
observer()->CheckEvents(TrackingEvent(SIGN_IN, "user@example.com")));
} }
TEST_F(AccountTrackerTest, AvailableRevokeAvailableWithPendingFetch) { TEST_F(AccountTrackerTest, AvailableRevokeAvailableWithPendingFetch) {
SetupPrimaryLogin(); SetupPrimaryLogin();
NotifyTokenAvailable("user@example.com"); std::string account_id = AddAccountWithToken("user", "user@example.com");
NotifyTokenRevoked("user@example.com"); NotifyTokenRevoked(account_id);
EXPECT_TRUE(observer()->CheckEvents()); EXPECT_TRUE(observer()->CheckEvents());
NotifyTokenAvailable("user@example.com"); NotifyTokenAvailable(account_id);
ReturnOAuthUrlFetchSuccess("user@example.com"); ReturnOAuthUrlFetchSuccess(account_id);
EXPECT_TRUE( EXPECT_TRUE(observer()->CheckEvents(TrackingEvent(SIGN_IN, account_id)));
observer()->CheckEvents(TrackingEvent(SIGN_IN, "user@example.com")));
} }
TEST_F(AccountTrackerTest, AvailableRevokeRevoke) { TEST_F(AccountTrackerTest, AvailableRevokeRevoke) {
SetupPrimaryLogin(); SetupPrimaryLogin();
NotifyTokenAvailable("user@example.com"); std::string account_id = AddAccountWithToken("user", "user@example.com");
ReturnOAuthUrlFetchSuccess("user@example.com"); ReturnOAuthUrlFetchSuccess(account_id);
NotifyTokenRevoked("user@example.com"); NotifyTokenRevoked(account_id);
EXPECT_TRUE( EXPECT_TRUE(observer()->CheckEvents(TrackingEvent(SIGN_IN, account_id),
observer()->CheckEvents(TrackingEvent(SIGN_IN, "user@example.com"), TrackingEvent(SIGN_OUT, account_id)));
TrackingEvent(SIGN_OUT, "user@example.com")));
NotifyTokenRevoked("user@example.com"); NotifyTokenRevoked(account_id);
EXPECT_TRUE(observer()->CheckEvents()); EXPECT_TRUE(observer()->CheckEvents());
} }
TEST_F(AccountTrackerTest, AvailableAvailable) { TEST_F(AccountTrackerTest, AvailableAvailable) {
SetupPrimaryLogin(); SetupPrimaryLogin();
NotifyTokenAvailable("user@example.com"); std::string account_id = AddAccountWithToken("user", "user@example.com");
ReturnOAuthUrlFetchSuccess("user@example.com"); ReturnOAuthUrlFetchSuccess(account_id);
EXPECT_TRUE( EXPECT_TRUE(observer()->CheckEvents(TrackingEvent(SIGN_IN, account_id)));
observer()->CheckEvents(TrackingEvent(SIGN_IN, "user@example.com")));
NotifyTokenAvailable("user@example.com"); NotifyTokenAvailable(account_id);
EXPECT_TRUE(observer()->CheckEvents()); EXPECT_TRUE(observer()->CheckEvents());
} }
TEST_F(AccountTrackerTest, TwoAccounts) { TEST_F(AccountTrackerTest, TwoAccounts) {
SetupPrimaryLogin(); SetupPrimaryLogin();
NotifyTokenAvailable("alpha@example.com"); std::string alpha_account_id =
ReturnOAuthUrlFetchSuccess("alpha@example.com"); AddAccountWithToken("alpha", "alpha@example.com");
ReturnOAuthUrlFetchSuccess(alpha_account_id);
EXPECT_TRUE( EXPECT_TRUE(
observer()->CheckEvents(TrackingEvent(SIGN_IN, "alpha@example.com"))); observer()->CheckEvents(TrackingEvent(SIGN_IN, alpha_account_id)));
NotifyTokenAvailable("beta@example.com"); std::string beta_account_id = AddAccountWithToken("beta", "beta@example.com");
ReturnOAuthUrlFetchSuccess("beta@example.com"); ReturnOAuthUrlFetchSuccess(beta_account_id);
EXPECT_TRUE( EXPECT_TRUE(observer()->CheckEvents(TrackingEvent(SIGN_IN, beta_account_id)));
observer()->CheckEvents(TrackingEvent(SIGN_IN, "beta@example.com")));
NotifyTokenRevoked("alpha@example.com"); NotifyTokenRevoked(alpha_account_id);
EXPECT_TRUE( EXPECT_TRUE(
observer()->CheckEvents(TrackingEvent(SIGN_OUT, "alpha@example.com"))); observer()->CheckEvents(TrackingEvent(SIGN_OUT, alpha_account_id)));
NotifyTokenRevoked("beta@example.com"); NotifyTokenRevoked(beta_account_id);
EXPECT_TRUE( EXPECT_TRUE(
observer()->CheckEvents(TrackingEvent(SIGN_OUT, "beta@example.com"))); observer()->CheckEvents(TrackingEvent(SIGN_OUT, beta_account_id)));
} }
TEST_F(AccountTrackerTest, AvailableTokenFetchFailAvailable) { TEST_F(AccountTrackerTest, AvailableTokenFetchFailAvailable) {
SetupPrimaryLogin(); SetupPrimaryLogin();
NotifyTokenAvailable("user@example.com"); std::string account_id = AddAccountWithToken("user", "user@example.com");
ReturnOAuthUrlFetchFailure("user@example.com"); ReturnOAuthUrlFetchFailure(account_id);
EXPECT_TRUE(observer()->CheckEvents()); EXPECT_TRUE(observer()->CheckEvents());
NotifyTokenAvailable("user@example.com"); NotifyTokenAvailable(account_id);
ReturnOAuthUrlFetchSuccess("user@example.com"); ReturnOAuthUrlFetchSuccess(account_id);
EXPECT_TRUE( EXPECT_TRUE(observer()->CheckEvents(TrackingEvent(SIGN_IN, account_id)));
observer()->CheckEvents(TrackingEvent(SIGN_IN, "user@example.com")));
} }
// These tests exercise true login/logout, which are not possible on ChromeOS. // These tests exercise true login/logout, which are not possible on ChromeOS.
#if !defined(OS_CHROMEOS) #if !defined(OS_CHROMEOS)
TEST_F(AccountTrackerTest, MultiSignOutSignIn) { TEST_F(AccountTrackerTest, MultiSignOutSignIn) {
SetupPrimaryLogin(); std::string primary_account_id = SetupPrimaryLogin();
NotifyTokenAvailable("alpha@example.com"); std::string alpha_account_id =
ReturnOAuthUrlFetchSuccess("alpha@example.com"); AddAccountWithToken("alpha", "alpha@example.com");
NotifyTokenAvailable("beta@example.com"); ReturnOAuthUrlFetchSuccess(alpha_account_id);
ReturnOAuthUrlFetchSuccess("beta@example.com"); std::string beta_account_id = AddAccountWithToken("beta", "beta@example.com");
ReturnOAuthUrlFetchSuccess(beta_account_id);
observer()->SortEventsByUser(); observer()->SortEventsByUser();
EXPECT_TRUE( EXPECT_TRUE(observer()->CheckEvents(TrackingEvent(SIGN_IN, alpha_account_id),
observer()->CheckEvents(TrackingEvent(SIGN_IN, "alpha@example.com"), TrackingEvent(SIGN_IN, beta_account_id)));
TrackingEvent(SIGN_IN, "beta@example.com")));
// Log out of the primary account only (allows for testing that the account // Log out of the primary account only (allows for testing that the account
// tracker preserves knowledge of "beta@example.com"). // tracker preserves knowledge of "beta@example.com").
NotifyLogoutOfPrimaryAccountOnly(); NotifyLogoutOfPrimaryAccountOnly();
observer()->SortEventsByUser(); observer()->SortEventsByUser();
EXPECT_TRUE( EXPECT_TRUE(
observer()->CheckEvents(TrackingEvent(SIGN_OUT, "alpha@example.com"), observer()->CheckEvents(TrackingEvent(SIGN_OUT, alpha_account_id),
TrackingEvent(SIGN_OUT, "beta@example.com"), TrackingEvent(SIGN_OUT, beta_account_id),
TrackingEvent(SIGN_OUT, kPrimaryAccountKey))); TrackingEvent(SIGN_OUT, primary_account_id)));
// No events fire at all while profile is signed out. // No events fire at all while profile is signed out.
NotifyTokenRevoked("alpha@example.com"); NotifyTokenRevoked(alpha_account_id);
NotifyTokenAvailable("gamma@example.com"); std::string gamma_account_id =
AddAccountWithToken("gamma", "gamma@example.com");
EXPECT_TRUE(observer()->CheckEvents()); EXPECT_TRUE(observer()->CheckEvents());
// Signing the profile in again will resume tracking all accounts. // Signing the profile in again will resume tracking all accounts.
NotifyLogin(kPrimaryAccountKey); NotifyLogin(kPrimaryAccountGaiaId, kPrimaryAccountEmail);
NotifyTokenAvailable(kPrimaryAccountKey); NotifyTokenAvailable(primary_account_id);
ReturnOAuthUrlFetchSuccess("beta@example.com"); ReturnOAuthUrlFetchSuccess(beta_account_id);
ReturnOAuthUrlFetchSuccess("gamma@example.com"); ReturnOAuthUrlFetchSuccess(gamma_account_id);
ReturnOAuthUrlFetchSuccess(kPrimaryAccountKey); ReturnOAuthUrlFetchSuccess(primary_account_id);
observer()->SortEventsByUser(); observer()->SortEventsByUser();
EXPECT_TRUE( EXPECT_TRUE(
observer()->CheckEvents(TrackingEvent(SIGN_IN, "beta@example.com"), observer()->CheckEvents(TrackingEvent(SIGN_IN, beta_account_id),
TrackingEvent(SIGN_IN, "gamma@example.com"), TrackingEvent(SIGN_IN, gamma_account_id),
TrackingEvent(SIGN_IN, kPrimaryAccountKey))); TrackingEvent(SIGN_IN, primary_account_id)));
// Revoking the primary token does not affect other accounts. // Revoking the primary token does not affect other accounts.
NotifyTokenRevoked(kPrimaryAccountKey); NotifyTokenRevoked(primary_account_id);
EXPECT_TRUE( EXPECT_TRUE(
observer()->CheckEvents(TrackingEvent(SIGN_OUT, kPrimaryAccountKey))); observer()->CheckEvents(TrackingEvent(SIGN_OUT, primary_account_id)));
NotifyTokenAvailable(kPrimaryAccountKey); NotifyTokenAvailable(primary_account_id);
EXPECT_TRUE( EXPECT_TRUE(
observer()->CheckEvents(TrackingEvent(SIGN_IN, kPrimaryAccountKey))); observer()->CheckEvents(TrackingEvent(SIGN_IN, primary_account_id)));
} }
#endif #endif
// Primary/non-primary interactions // Primary/non-primary interactions
TEST_F(AccountTrackerTest, MultiNoEventsBeforeLogin) { TEST_F(AccountTrackerTest, MultiNoEventsBeforeLogin) {
NotifyTokenAvailable(kPrimaryAccountKey); NotifyTokenAvailable("dummy_account_id");
NotifyTokenAvailable("user@example.com"); std::string account_id = AddAccountWithToken("user", "user@example.com");
NotifyTokenRevoked("user@example.com"); NotifyTokenRevoked(account_id);
NotifyTokenRevoked(kPrimaryAccountKey); NotifyTokenRevoked("dummy_account_id");
// Logout is not possible on ChromeOS. // Logout is not possible on ChromeOS.
#if !defined(OS_CHROMEOS) #if !defined(OS_CHROMEOS)
...@@ -679,42 +701,44 @@ TEST_F(AccountTrackerTest, MultiNoEventsBeforeLogin) { ...@@ -679,42 +701,44 @@ TEST_F(AccountTrackerTest, MultiNoEventsBeforeLogin) {
// This test exercises true login/logout, which are not possible on ChromeOS. // This test exercises true login/logout, which are not possible on ChromeOS.
#if !defined(OS_CHROMEOS) #if !defined(OS_CHROMEOS)
TEST_F(AccountTrackerTest, MultiLogoutRemovesAllAccounts) { TEST_F(AccountTrackerTest, MultiLogoutRemovesAllAccounts) {
NotifyLogin(kPrimaryAccountKey); std::string primary_account_id =
NotifyTokenAvailable(kPrimaryAccountKey); NotifyLogin(kPrimaryAccountGaiaId, kPrimaryAccountEmail);
ReturnOAuthUrlFetchSuccess(kPrimaryAccountKey); NotifyTokenAvailable(primary_account_id);
NotifyTokenAvailable("user@example.com"); ReturnOAuthUrlFetchSuccess(primary_account_id);
ReturnOAuthUrlFetchSuccess("user@example.com"); std::string account_id = AddAccountWithToken("user", "user@example.com");
ReturnOAuthUrlFetchSuccess(account_id);
observer()->Clear(); observer()->Clear();
NotifyLogoutOfAllAccounts(); NotifyLogoutOfAllAccounts();
observer()->SortEventsByUser(); observer()->SortEventsByUser();
EXPECT_TRUE( EXPECT_TRUE(
observer()->CheckEvents(TrackingEvent(SIGN_OUT, kPrimaryAccountKey), observer()->CheckEvents(TrackingEvent(SIGN_OUT, primary_account_id),
TrackingEvent(SIGN_OUT, "user@example.com"))); TrackingEvent(SIGN_OUT, account_id)));
} }
#endif #endif
TEST_F(AccountTrackerTest, MultiRevokePrimaryDoesNotRemoveAllAccounts) { TEST_F(AccountTrackerTest, MultiRevokePrimaryDoesNotRemoveAllAccounts) {
SetActiveAccount(kPrimaryAccountKey); std::string primary_account_id =
NotifyTokenAvailable(kPrimaryAccountKey); SetActiveAccount(kPrimaryAccountGaiaId, kPrimaryAccountEmail);
ReturnOAuthUrlFetchSuccess(kPrimaryAccountKey); NotifyTokenAvailable(primary_account_id);
NotifyTokenAvailable("user@example.com"); ReturnOAuthUrlFetchSuccess(primary_account_id);
ReturnOAuthUrlFetchSuccess("user@example.com"); std::string account_id = AddAccountWithToken("user", "user@example.com");
ReturnOAuthUrlFetchSuccess(account_id);
observer()->Clear(); observer()->Clear();
NotifyTokenRevoked(kPrimaryAccountKey); NotifyTokenRevoked(primary_account_id);
observer()->SortEventsByUser(); observer()->SortEventsByUser();
EXPECT_TRUE( EXPECT_TRUE(
observer()->CheckEvents(TrackingEvent(SIGN_OUT, kPrimaryAccountKey))); observer()->CheckEvents(TrackingEvent(SIGN_OUT, primary_account_id)));
} }
TEST_F(AccountTrackerTest, GetAccountsPrimary) { TEST_F(AccountTrackerTest, GetAccountsPrimary) {
SetupPrimaryLogin(); std::string primary_account_id = SetupPrimaryLogin();
std::vector<AccountIds> ids = account_tracker()->GetAccounts(); std::vector<AccountIds> ids = account_tracker()->GetAccounts();
EXPECT_EQ(1ul, ids.size()); EXPECT_EQ(1ul, ids.size());
EXPECT_EQ(kPrimaryAccountKey, ids[0].account_key); EXPECT_EQ(primary_account_id, ids[0].account_key);
EXPECT_EQ(AccountKeyToObfuscatedId(kPrimaryAccountKey), ids[0].gaia); EXPECT_EQ(AccountKeyToObfuscatedId(primary_account_id), ids[0].gaia);
} }
TEST_F(AccountTrackerTest, GetAccountsSignedOut) { TEST_F(AccountTrackerTest, GetAccountsSignedOut) {
...@@ -723,49 +747,52 @@ TEST_F(AccountTrackerTest, GetAccountsSignedOut) { ...@@ -723,49 +747,52 @@ TEST_F(AccountTrackerTest, GetAccountsSignedOut) {
} }
TEST_F(AccountTrackerTest, GetAccountsOnlyReturnAccountsWithTokens) { TEST_F(AccountTrackerTest, GetAccountsOnlyReturnAccountsWithTokens) {
SetupPrimaryLogin(); std::string primary_account_id = SetupPrimaryLogin();
NotifyTokenAvailable("alpha@example.com"); std::string alpha_account_id =
NotifyTokenAvailable("beta@example.com"); AddAccountWithToken("alpha", "alpha@example.com");
ReturnOAuthUrlFetchSuccess("beta@example.com"); std::string beta_account_id = AddAccountWithToken("beta", "beta@example.com");
ReturnOAuthUrlFetchSuccess(beta_account_id);
std::vector<AccountIds> ids = account_tracker()->GetAccounts(); std::vector<AccountIds> ids = account_tracker()->GetAccounts();
EXPECT_EQ(2ul, ids.size()); EXPECT_EQ(2ul, ids.size());
EXPECT_EQ(kPrimaryAccountKey, ids[0].account_key); EXPECT_EQ(primary_account_id, ids[0].account_key);
EXPECT_EQ(AccountKeyToObfuscatedId(kPrimaryAccountKey), ids[0].gaia); EXPECT_EQ(AccountKeyToObfuscatedId(primary_account_id), ids[0].gaia);
EXPECT_EQ("beta@example.com", ids[1].account_key); EXPECT_EQ(beta_account_id, ids[1].account_key);
EXPECT_EQ(AccountKeyToObfuscatedId("beta@example.com"), ids[1].gaia); EXPECT_EQ(AccountKeyToObfuscatedId(beta_account_id), ids[1].gaia);
} }
TEST_F(AccountTrackerTest, GetAccountsSortOrder) { TEST_F(AccountTrackerTest, GetAccountsSortOrder) {
SetupPrimaryLogin(); std::string primary_account_id = SetupPrimaryLogin();
NotifyTokenAvailable("zeta@example.com"); std::string zeta_account_id = AddAccountWithToken("zeta", "zeta@example.com");
ReturnOAuthUrlFetchSuccess("zeta@example.com"); ReturnOAuthUrlFetchSuccess(zeta_account_id);
NotifyTokenAvailable("alpha@example.com"); std::string alpha_account_id =
ReturnOAuthUrlFetchSuccess("alpha@example.com"); AddAccountWithToken("alpha", "alpha@example.com");
ReturnOAuthUrlFetchSuccess(alpha_account_id);
// The primary account will be first in the vector. Remaining accounts // The primary account will be first in the vector. Remaining accounts
// will be sorted by gaia ID. // will be sorted by gaia ID.
std::vector<AccountIds> ids = account_tracker()->GetAccounts(); std::vector<AccountIds> ids = account_tracker()->GetAccounts();
EXPECT_EQ(3ul, ids.size()); EXPECT_EQ(3ul, ids.size());
EXPECT_EQ(kPrimaryAccountKey, ids[0].account_key); EXPECT_EQ(primary_account_id, ids[0].account_key);
EXPECT_EQ(AccountKeyToObfuscatedId(kPrimaryAccountKey), ids[0].gaia); EXPECT_EQ(AccountKeyToObfuscatedId(primary_account_id), ids[0].gaia);
EXPECT_EQ("alpha@example.com", ids[1].account_key); EXPECT_EQ(alpha_account_id, ids[1].account_key);
EXPECT_EQ(AccountKeyToObfuscatedId("alpha@example.com"), ids[1].gaia); EXPECT_EQ(AccountKeyToObfuscatedId(alpha_account_id), ids[1].gaia);
EXPECT_EQ("zeta@example.com", ids[2].account_key); EXPECT_EQ(zeta_account_id, ids[2].account_key);
EXPECT_EQ(AccountKeyToObfuscatedId("zeta@example.com"), ids[2].gaia); EXPECT_EQ(AccountKeyToObfuscatedId(zeta_account_id), ids[2].gaia);
} }
TEST_F(AccountTrackerTest, GetAccountsReturnNothingWhenPrimarySignedOut) { TEST_F(AccountTrackerTest, GetAccountsReturnNothingWhenPrimarySignedOut) {
SetupPrimaryLogin(); std::string primary_account_id = SetupPrimaryLogin();
NotifyTokenAvailable("zeta@example.com"); std::string zeta_account_id = AddAccountWithToken("zeta", "zeta@example.com");
ReturnOAuthUrlFetchSuccess("zeta@example.com"); ReturnOAuthUrlFetchSuccess(zeta_account_id);
NotifyTokenAvailable("alpha@example.com"); std::string alpha_account_id =
ReturnOAuthUrlFetchSuccess("alpha@example.com"); AddAccountWithToken("alpha", "alpha@example.com");
ReturnOAuthUrlFetchSuccess(alpha_account_id);
NotifyTokenRevoked(kPrimaryAccountKey); NotifyTokenRevoked(primary_account_id);
std::vector<AccountIds> ids = account_tracker()->GetAccounts(); std::vector<AccountIds> ids = account_tracker()->GetAccounts();
EXPECT_EQ(0ul, ids.size()); EXPECT_EQ(0ul, ids.size());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment