Commit 0d37fb92 authored by Sylvain Defresne's avatar Sylvain Defresne Committed by Commit Bot

Add API to convert AccountTrackerService::FindAccountInfoByXXX

Add methods to find AccountInfo using either account id, email
address or gaia id to IdentityManager. They are wrapper around
the corresponding method of AccountTrackerService (with a call
to GetAccountInfoForAccountWithValidRefreshToken to get the
logic to deal with kSupervisedUserPseudoEmail).

Add unit tests for the new methods.

Bug: 912170
Change-Id: Ic7da17b0f22c85349b89cb44d272a3a6b4f6d573
Reviewed-on: https://chromium-review.googlesource.com/c/1365245
Commit-Queue: Sylvain Defresne <sdefresne@chromium.org>
Reviewed-by: default avatarColin Blundell <blundell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#615502}
parent 1fe84051
...@@ -124,6 +124,51 @@ bool IdentityManager::HasPrimaryAccountWithRefreshToken() const { ...@@ -124,6 +124,51 @@ bool IdentityManager::HasPrimaryAccountWithRefreshToken() const {
return HasAccountWithRefreshToken(GetPrimaryAccountId()); return HasAccountWithRefreshToken(GetPrimaryAccountId());
} }
base::Optional<AccountInfo>
IdentityManager::FindAccountInfoForAccountWithRefreshTokenByAccountId(
const std::string& account_id) const {
AccountInfo account_info =
account_tracker_service_->GetAccountInfo(account_id);
// AccountTrackerService always returns an AccountInfo, even on failure. In
// case of failure, the AccountInfo will be unpopulated, thus we should not
// be able to find a valid refresh token.
if (!HasAccountWithRefreshToken(account_info.account_id))
return base::nullopt;
return GetAccountInfoForAccountWithRefreshToken(account_info.account_id);
}
base::Optional<AccountInfo>
IdentityManager::FindAccountInfoForAccountWithRefreshTokenByEmailAddress(
const std::string& email_address) const {
AccountInfo account_info =
account_tracker_service_->FindAccountInfoByEmail(email_address);
// AccountTrackerService always returns an AccountInfo, even on failure. In
// case of failure, the AccountInfo will be unpopulated, thus we should not
// be able to find a valid refresh token.
if (!HasAccountWithRefreshToken(account_info.account_id))
return base::nullopt;
return GetAccountInfoForAccountWithRefreshToken(account_info.account_id);
}
base::Optional<AccountInfo>
IdentityManager::FindAccountInfoForAccountWithRefreshTokenByGaiaId(
const std::string& gaia_id) const {
AccountInfo account_info =
account_tracker_service_->FindAccountInfoByGaiaId(gaia_id);
// AccountTrackerService always returns an AccountInfo, even on failure. In
// case of failure, the AccountInfo will be unpopulated, thus we should not
// be able to find a valid refresh token.
if (!HasAccountWithRefreshToken(account_info.account_id))
return base::nullopt;
return GetAccountInfoForAccountWithRefreshToken(account_info.account_id);
}
std::unique_ptr<AccessTokenFetcher> std::unique_ptr<AccessTokenFetcher>
IdentityManager::CreateAccessTokenFetcherForAccount( IdentityManager::CreateAccessTokenFetcherForAccount(
const std::string& account_id, const std::string& account_id,
......
...@@ -219,6 +219,29 @@ class IdentityManager : public SigninManagerBase::Observer, ...@@ -219,6 +219,29 @@ class IdentityManager : public SigninManagerBase::Observer,
// exists for the primary account. // exists for the primary account.
bool HasPrimaryAccountWithRefreshToken() const; bool HasPrimaryAccountWithRefreshToken() const;
// Looks up and returns information for account with given |account_id|. If
// the account cannot be found, return an empty optional. This is equivalent
// to searching on the vector returned by GetAccountsWithRefreshTokens() but
// without allocating memory for the vector.
base::Optional<AccountInfo>
FindAccountInfoForAccountWithRefreshTokenByAccountId(
const std::string& account_id) const;
// Looks up and returns information for account with given |email_address|. If
// the account cannot be found, return an empty optional. This is equivalent
// to searching on the vector returned by GetAccountsWithRefreshTokens() but
// without allocating memory for the vector.
base::Optional<AccountInfo>
FindAccountInfoForAccountWithRefreshTokenByEmailAddress(
const std::string& email_address) const;
// Looks up and returns information for account with given |gaia_id|. If the
// account cannot be found, return an empty optional. This is equivalent to
// searching on the vector returned by GetAccountsWithRefreshTokens() but
// without allocating memory for the vector.
base::Optional<AccountInfo> FindAccountInfoForAccountWithRefreshTokenByGaiaId(
const std::string& gaia_id) const;
// Creates an AccessTokenFetcher given the passed-in information. // Creates an AccessTokenFetcher given the passed-in information.
std::unique_ptr<AccessTokenFetcher> CreateAccessTokenFetcherForAccount( std::unique_ptr<AccessTokenFetcher> CreateAccessTokenFetcherForAccount(
const std::string& account_id, const std::string& account_id,
......
...@@ -1774,4 +1774,80 @@ TEST_F(IdentityManagerTest, GetAccountsMutator) { ...@@ -1774,4 +1774,80 @@ TEST_F(IdentityManagerTest, GetAccountsMutator) {
EXPECT_TRUE(mutator); EXPECT_TRUE(mutator);
} }
// Checks that FindAccountInfoForAccountWithRefreshTokenByAccountId() returns
// information about the account if the account is found or nullopt if there
// are no accounts with requested |account_id|.
TEST_F(IdentityManagerTest,
FindAccountInfoForAccountWithRefreshTokenByAccountId) {
// Add an account (note: cannot use kTestEmail as it is already inserted
// by the fixture common code, so use a different address).
const AccountInfo foo_account_info =
MakeAccountAvailable(identity_manager(), "foo@bar.com");
base::Optional<AccountInfo> maybe_account_info;
maybe_account_info =
identity_manager()->FindAccountInfoForAccountWithRefreshTokenByAccountId(
"dummy_value");
EXPECT_FALSE(maybe_account_info.has_value());
maybe_account_info =
identity_manager()->FindAccountInfoForAccountWithRefreshTokenByAccountId(
foo_account_info.account_id);
EXPECT_TRUE(maybe_account_info.has_value());
EXPECT_EQ(foo_account_info.account_id, maybe_account_info.value().account_id);
EXPECT_EQ(foo_account_info.email, maybe_account_info.value().email);
EXPECT_EQ(foo_account_info.gaia, maybe_account_info.value().gaia);
}
// Checks that FindAccountInfoForAccountWithRefreshTokenByEmailAddress() returns
// information about the account if the account is found or nullopt if there
// are no accounts with requested |email_address|.
TEST_F(IdentityManagerTest,
FindAccountInfoForAccountWithRefreshTokenByEmailAddress) {
// Add an account (note: cannot use kTestEmail as it is already inserted
// by the fixture common code, so use a different address).
const AccountInfo foo_account_info =
MakeAccountAvailable(identity_manager(), "foo@bar.com");
base::Optional<AccountInfo> maybe_account_info;
maybe_account_info =
identity_manager()
->FindAccountInfoForAccountWithRefreshTokenByEmailAddress(
"dummy_value");
EXPECT_FALSE(maybe_account_info.has_value());
maybe_account_info =
identity_manager()
->FindAccountInfoForAccountWithRefreshTokenByEmailAddress(
foo_account_info.email);
EXPECT_TRUE(maybe_account_info.has_value());
EXPECT_EQ(foo_account_info.account_id, maybe_account_info.value().account_id);
EXPECT_EQ(foo_account_info.email, maybe_account_info.value().email);
EXPECT_EQ(foo_account_info.gaia, maybe_account_info.value().gaia);
}
// Checks that FindAccountInfoForAccountWithRefreshTokenByGaiaId() returns
// information about the account if the account is found or nullopt if there
// are no accounts with requested |gaia_id|.
TEST_F(IdentityManagerTest, FindAccountInfoForAccountWithRefreshTokenByGaiaId) {
// Add an account (note: cannot use kTestEmail as it is already inserted
// by the fixture common code, so use a different address).
const AccountInfo foo_account_info =
MakeAccountAvailable(identity_manager(), "foo@bar.com");
base::Optional<AccountInfo> maybe_account_info;
maybe_account_info =
identity_manager()->FindAccountInfoForAccountWithRefreshTokenByGaiaId(
"dummy_value");
EXPECT_FALSE(maybe_account_info.has_value());
maybe_account_info =
identity_manager()->FindAccountInfoForAccountWithRefreshTokenByGaiaId(
foo_account_info.gaia);
EXPECT_TRUE(maybe_account_info.has_value());
EXPECT_EQ(foo_account_info.account_id, maybe_account_info.value().account_id);
EXPECT_EQ(foo_account_info.email, maybe_account_info.value().email);
EXPECT_EQ(foo_account_info.gaia, maybe_account_info.value().gaia);
}
} // namespace identity } // namespace identity
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