Commit 56bc73a9 authored by Colin Blundell's avatar Colin Blundell Committed by Commit Bot

IdentityTestEnvironment: Add MakeAccountAvailable() API

This API allows test clients to populate an account from an email
address with IdentityManager and its backing classes. It is conceptually
similar to IdentityTestEnvironment::MakePrimaryAccountAvailable().

Bug: 798699
Change-Id: Iffaae9f989a37de3e406ccc10166ce44c7ed6bdb
Reviewed-on: https://chromium-review.googlesource.com/1099057
Commit-Queue: Colin Blundell <blundell@chromium.org>
Reviewed-by: default avatarMarc Treib <treib@chromium.org>
Cr-Commit-Position: refs/heads/master@{#567221}
parent 18a428ba
...@@ -30,6 +30,8 @@ class IdentityTestEnvironmentInternal { ...@@ -30,6 +30,8 @@ class IdentityTestEnvironmentInternal {
// The IdentityManager instance created and owned by this instance. // The IdentityManager instance created and owned by this instance.
IdentityManager* identity_manager(); IdentityManager* identity_manager();
AccountTrackerService* account_tracker_service();
SigninManagerForTest* signin_manager(); SigninManagerForTest* signin_manager();
FakeProfileOAuth2TokenService* token_service(); FakeProfileOAuth2TokenService* token_service();
...@@ -73,6 +75,11 @@ IdentityManager* IdentityTestEnvironmentInternal::identity_manager() { ...@@ -73,6 +75,11 @@ IdentityManager* IdentityTestEnvironmentInternal::identity_manager() {
return identity_manager_.get(); return identity_manager_.get();
} }
AccountTrackerService*
IdentityTestEnvironmentInternal::account_tracker_service() {
return &account_tracker_;
}
SigninManagerForTest* IdentityTestEnvironmentInternal::signin_manager() { SigninManagerForTest* IdentityTestEnvironmentInternal::signin_manager() {
return &signin_manager_; return &signin_manager_;
} }
...@@ -128,6 +135,13 @@ void IdentityTestEnvironment::ClearPrimaryAccount() { ...@@ -128,6 +135,13 @@ void IdentityTestEnvironment::ClearPrimaryAccount() {
internals_->identity_manager()); internals_->identity_manager());
} }
std::string IdentityTestEnvironment::MakeAccountAvailable(
const std::string& email) {
return identity::MakeAccountAvailable(internals_->account_tracker_service(),
internals_->token_service(),
internals_->identity_manager(), email);
}
void IdentityTestEnvironment::SetAutomaticIssueOfAccessTokens(bool grant) { void IdentityTestEnvironment::SetAutomaticIssueOfAccessTokens(bool grant) {
internals_->token_service()->set_auto_post_fetch_response_on_message_loop( internals_->token_service()->set_auto_post_fetch_response_on_message_loop(
grant); grant);
......
...@@ -54,6 +54,12 @@ class IdentityTestEnvironment : public IdentityManager::DiagnosticsObserver { ...@@ -54,6 +54,12 @@ class IdentityTestEnvironment : public IdentityManager::DiagnosticsObserver {
// primary account is cleared. // primary account is cleared.
void ClearPrimaryAccount(); void ClearPrimaryAccount();
// Makes an account available for the given email address, generating a GAIA
// ID and refresh token that correspond uniquely to that email address. Blocks
// until the account is available. Returns the account ID of the
// newly-available account.
std::string MakeAccountAvailable(const std::string& email);
// When this is set, access token requests will be automatically granted with // When this is set, access token requests will be automatically granted with
// an access token value of "access_token". // an access token value of "access_token".
void SetAutomaticIssueOfAccessTokens(bool grant); void SetAutomaticIssueOfAccessTokens(bool grant);
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/strings/string_split.h" #include "base/strings/string_split.h"
#include "components/signin/core/browser/account_tracker_service.h"
#include "components/signin/core/browser/fake_signin_manager.h" #include "components/signin/core/browser/fake_signin_manager.h"
#include "components/signin/core/browser/profile_oauth2_token_service.h" #include "components/signin/core/browser/profile_oauth2_token_service.h"
#include "services/identity/public/cpp/identity_manager.h" #include "services/identity/public/cpp/identity_manager.h"
...@@ -96,15 +97,12 @@ void OneShotIdentityManagerObserver::OnRefreshTokenRemovedForAccount( ...@@ -96,15 +97,12 @@ void OneShotIdentityManagerObserver::OnRefreshTokenRemovedForAccount(
std::move(done_closure_).Run(); std::move(done_closure_).Run();
} }
// Helper function that updates the refresh token for the primary account to // Helper function that updates the refresh token for |account_id| to
// |new_token|. Blocks until the update is processed by |identity_manager|. // |new_token|. Blocks until the update is processed by |identity_manager|.
void UpdateRefreshTokenForPrimaryAccount( void UpdateRefreshTokenForAccount(ProfileOAuth2TokenService* token_service,
ProfileOAuth2TokenService* token_service, IdentityManager* identity_manager,
IdentityManager* identity_manager, const std::string& account_id,
const std::string& new_token) { const std::string& new_token) {
DCHECK(identity_manager->HasPrimaryAccount());
std::string account_id = identity_manager->GetPrimaryAccountInfo().account_id;
base::RunLoop run_loop; base::RunLoop run_loop;
OneShotIdentityManagerObserver token_updated_observer( OneShotIdentityManagerObserver token_updated_observer(
identity_manager, run_loop.QuitClosure(), identity_manager, run_loop.QuitClosure(),
...@@ -164,15 +162,18 @@ void SetRefreshTokenForPrimaryAccount(ProfileOAuth2TokenService* token_service, ...@@ -164,15 +162,18 @@ void SetRefreshTokenForPrimaryAccount(ProfileOAuth2TokenService* token_service,
std::string account_id = identity_manager->GetPrimaryAccountInfo().account_id; std::string account_id = identity_manager->GetPrimaryAccountInfo().account_id;
std::string refresh_token = "refresh_token_for_" + account_id; std::string refresh_token = "refresh_token_for_" + account_id;
UpdateRefreshTokenForPrimaryAccount(token_service, identity_manager, UpdateRefreshTokenForAccount(token_service, identity_manager, account_id,
refresh_token); refresh_token);
} }
void SetInvalidRefreshTokenForPrimaryAccount( void SetInvalidRefreshTokenForPrimaryAccount(
ProfileOAuth2TokenService* token_service, ProfileOAuth2TokenService* token_service,
IdentityManager* identity_manager) { IdentityManager* identity_manager) {
UpdateRefreshTokenForPrimaryAccount( DCHECK(identity_manager->HasPrimaryAccount());
token_service, identity_manager, std::string account_id = identity_manager->GetPrimaryAccountInfo().account_id;
UpdateRefreshTokenForAccount(
token_service, identity_manager, account_id,
OAuth2TokenServiceDelegate::kInvalidRefreshToken); OAuth2TokenServiceDelegate::kInvalidRefreshToken);
} }
...@@ -222,4 +223,24 @@ void ClearPrimaryAccount(SigninManagerForTest* signin_manager, ...@@ -222,4 +223,24 @@ void ClearPrimaryAccount(SigninManagerForTest* signin_manager,
#endif #endif
} }
std::string MakeAccountAvailable(AccountTrackerService* account_tracker_service,
ProfileOAuth2TokenService* token_service,
IdentityManager* identity_manager,
const std::string& email) {
DCHECK(account_tracker_service->FindAccountInfoByEmail(email).IsEmpty());
std::string gaia_id = "gaia_id_for_" + email;
account_tracker_service->SeedAccountInfo(gaia_id, email);
std::string account_id =
account_tracker_service->FindAccountInfoByEmail(email).account_id;
DCHECK(!account_id.empty());
std::string refresh_token = "refresh_token_for_" + account_id;
UpdateRefreshTokenForAccount(token_service, identity_manager, account_id,
refresh_token);
return account_id;
}
} // namespace identity } // namespace identity
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "build/build_config.h" #include "build/build_config.h"
class AccountTrackerService;
class FakeSigninManagerBase; class FakeSigninManagerBase;
class FakeSigninManager; class FakeSigninManager;
class ProfileOAuth2TokenService; class ProfileOAuth2TokenService;
...@@ -84,6 +85,16 @@ std::string MakePrimaryAccountAvailable( ...@@ -84,6 +85,16 @@ std::string MakePrimaryAccountAvailable(
void ClearPrimaryAccount(SigninManagerForTest* signin_manager, void ClearPrimaryAccount(SigninManagerForTest* signin_manager,
IdentityManager* identity_manager); IdentityManager* identity_manager);
// Makes an account available for the given email address, generating a GAIA ID
// and refresh token that correspond uniquely to that email address. Blocks
// until the account is available. Returns the account ID of the
// newly-available account.
// NOTE: See disclaimer at top of file re: direct usage.
std::string MakeAccountAvailable(AccountTrackerService* account_tracker_service,
ProfileOAuth2TokenService* token_service,
IdentityManager* identity_manager,
const std::string& email);
} // namespace identity } // namespace identity
#endif // SERVICES_IDENTITY_PUBLIC_CPP_IDENTITY_TEST_UTILS_H_ #endif // SERVICES_IDENTITY_PUBLIC_CPP_IDENTITY_TEST_UTILS_H_
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