Commit fa626b9f authored by Antonio Gomes's avatar Antonio Gomes Committed by Commit Bot

[s13n] Convert identity_apitest.cc to use IdentityTestEnvironment

This bug is a follow up of [1] and [2], where production code
(//c/b/extensions/api/identity/identity_api.cc) is migrated away from
using PO2TS in favor of IdentityManager, and the API is extended to
accommodate the needs of this migration, respectively.

[1] https://crrev.com/b/1340890
[2] https://crrev.com/b/1346609

BUG=906615

Change-Id: Ib41f99f08b7d181bbcff28be97c8b55993aa8c04
Reviewed-on: https://chromium-review.googlesource.com/c/1343019
Commit-Queue: Antonio Gomes <tonikitoo@igalia.com>
Reviewed-by: default avatarColin Blundell <blundell@chromium.org>
Reviewed-by: default avatarMihai Sardarescu <msarda@chromium.org>
Cr-Commit-Position: refs/heads/master@{#610844}
parent da87860c
...@@ -33,13 +33,8 @@ ...@@ -33,13 +33,8 @@
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/account_fetcher_service_factory.h" #include "chrome/browser/signin/account_fetcher_service_factory.h"
#include "chrome/browser/signin/account_tracker_service_factory.h" #include "chrome/browser/signin/account_tracker_service_factory.h"
#include "chrome/browser/signin/fake_gaia_cookie_manager_service_builder.h"
#include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
#include "chrome/browser/signin/fake_signin_manager_builder.h"
#include "chrome/browser/signin/gaia_cookie_manager_service_factory.h"
#include "chrome/browser/signin/identity_manager_factory.h" #include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h" #include "chrome/browser/signin/identity_test_environment_profile_adaptor.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h" #include "chrome/browser/ui/browser_window.h"
#include "chrome/common/chrome_switches.h" #include "chrome/common/chrome_switches.h"
...@@ -50,8 +45,6 @@ ...@@ -50,8 +45,6 @@
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
#include "components/signin/core/browser/account_fetcher_service.h" #include "components/signin/core/browser/account_fetcher_service.h"
#include "components/signin/core/browser/account_tracker_service.h" #include "components/signin/core/browser/account_tracker_service.h"
#include "components/signin/core/browser/fake_gaia_cookie_manager_service.h"
#include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
#include "components/signin/core/browser/signin_buildflags.h" #include "components/signin/core/browser/signin_buildflags.h"
#include "components/signin/core/browser/signin_manager.h" #include "components/signin/core/browser/signin_manager.h"
#include "components/signin/core/browser/signin_pref_names.h" #include "components/signin/core/browser/signin_pref_names.h"
...@@ -365,8 +358,8 @@ class FakeGetAuthTokenFunction : public IdentityGetAuthTokenFunction { ...@@ -365,8 +358,8 @@ class FakeGetAuthTokenFunction : public IdentityGetAuthTokenFunction {
account_state.has_refresh_token = true; account_state.has_refresh_token = true;
account_state.is_primary_account = true; account_state.is_primary_account = true;
OnPrimaryAccountAvailable( OnPrimaryAccountAvailable(
SigninManagerFactory::GetForProfile(GetProfile()) IdentityManagerFactory::GetForProfile(GetProfile())
->GetAuthenticatedAccountInfo(), ->GetPrimaryAccountInfo(),
account_state); account_state);
} else { } else {
SigninFailed(); SigninFailed();
...@@ -437,15 +430,10 @@ class IdentityTestWithSignin : public AsyncExtensionBrowserTest { ...@@ -437,15 +430,10 @@ class IdentityTestWithSignin : public AsyncExtensionBrowserTest {
// Replace the signin manager and token service with fakes. Do this ahead of // Replace the signin manager and token service with fakes. Do this ahead of
// creating the browser so that a bunch of classes don't register as // creating the browser so that a bunch of classes don't register as
// observers and end up needing to unregister when the fake is substituted. // observers and end up needing to unregister when the fake is substituted.
SigninManagerFactory::GetInstance()->SetTestingFactory( IdentityTestEnvironmentProfileAdaptor::
context, base::BindRepeating(&BuildFakeSigninManagerForTesting)); SetIdentityTestEnvironmentFactoriesOnBrowserContext(
ProfileOAuth2TokenServiceFactory::GetInstance()->SetTestingFactory(
context, base::BindRepeating(&BuildFakeProfileOAuth2TokenService));
GaiaCookieManagerServiceFactory::GetInstance()->SetTestingFactory(
context, context,
base::BindRepeating( /*create_fake_url_loader_factory_for_cookie_requests=*/false);
&BuildFakeGaiaCookieManagerServiceWithOptions,
/*create_fake_url_loader_factory_for_cookie_requests=*/false));
// Ensure that AccountFetcherService is (1) created at all and (2) created // Ensure that AccountFetcherService is (1) created at all and (2) created
// early enough for it to observe the Profile initialization process and // early enough for it to observe the Profile initialization process and
...@@ -453,9 +441,6 @@ class IdentityTestWithSignin : public AsyncExtensionBrowserTest { ...@@ -453,9 +441,6 @@ class IdentityTestWithSignin : public AsyncExtensionBrowserTest {
// naturally in production) is necessary for the flow of // naturally in production) is necessary for the flow of
// AccountTrackerService having accounts removed when tokens are revoked // AccountTrackerService having accounts removed when tokens are revoked
// with PO2TS to work as expected in this testing context. // with PO2TS to work as expected in this testing context.
// TODO(blundell): Change these tests to interact with
// IdentityTestEnvironment once the production code is changed to interact
// with IdentityManager.
AccountFetcherServiceFactory::GetInstance()->GetForProfile( AccountFetcherServiceFactory::GetInstance()->GetForProfile(
Profile::FromBrowserContext(context)); Profile::FromBrowserContext(context));
} }
...@@ -463,14 +448,8 @@ class IdentityTestWithSignin : public AsyncExtensionBrowserTest { ...@@ -463,14 +448,8 @@ class IdentityTestWithSignin : public AsyncExtensionBrowserTest {
void SetUpOnMainThread() override { void SetUpOnMainThread() override {
AsyncExtensionBrowserTest::SetUpOnMainThread(); AsyncExtensionBrowserTest::SetUpOnMainThread();
// Grab references to the fake signin manager and token service. identity_test_env_profile_adaptor_ =
signin_manager_ = static_cast<FakeSigninManagerForTesting*>( std::make_unique<IdentityTestEnvironmentProfileAdaptor>(profile());
SigninManagerFactory::GetInstance()->GetForProfile(profile()));
ASSERT_TRUE(signin_manager_);
token_service_ = static_cast<FakeProfileOAuth2TokenService*>(
ProfileOAuth2TokenServiceFactory::GetInstance()->GetForProfile(
profile()));
ASSERT_TRUE(token_service_);
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
// On ChromeOS, ProfileOAuth2TokenService does not fire // On ChromeOS, ProfileOAuth2TokenService does not fire
...@@ -486,18 +465,21 @@ class IdentityTestWithSignin : public AsyncExtensionBrowserTest { ...@@ -486,18 +465,21 @@ class IdentityTestWithSignin : public AsyncExtensionBrowserTest {
#endif #endif
} }
void TearDownOnMainThread() override {
// Must be destroyed before the Profile.
identity_test_env_profile_adaptor_.reset();
}
protected: protected:
// Returns the account ID of the created account. // Returns the account ID of the created account.
std::string SignIn(const std::string& email) { std::string SignIn(const std::string& email) {
identity::IdentityManager* identity_manager = auto account_info = identity_test_env()->MakePrimaryAccountAvailable(email);
IdentityManagerFactory::GetForProfile(profile()); return account_info.account_id;
identity::MakePrimaryAccountAvailable(identity_manager, email);
return identity_manager->GetPrimaryAccountId();
} }
std::string AddAccount(const std::string& email) { std::string AddAccount(const std::string& email) {
std::string account_id = SeedAccountInfo(email); std::string account_id = SeedAccountInfo(email);
token_service_->UpdateCredentials(account_id, "refresh_token"); identity_test_env()->SetRefreshTokenForAccount(account_id);
return account_id; return account_id;
} }
...@@ -513,8 +495,12 @@ class IdentityTestWithSignin : public AsyncExtensionBrowserTest { ...@@ -513,8 +495,12 @@ class IdentityTestWithSignin : public AsyncExtensionBrowserTest {
return IdentityAPI::GetFactoryInstance()->Get(browser()->profile()); return IdentityAPI::GetFactoryInstance()->Get(browser()->profile());
} }
FakeSigninManagerForTesting* signin_manager_; identity::IdentityTestEnvironment* identity_test_env() {
FakeProfileOAuth2TokenService* token_service_; return identity_test_env_profile_adaptor_->identity_test_env();
}
std::unique_ptr<IdentityTestEnvironmentProfileAdaptor>
identity_test_env_profile_adaptor_;
std::unique_ptr< std::unique_ptr<
base::CallbackList<void(content::BrowserContext*)>::Subscription> base::CallbackList<void(content::BrowserContext*)>::Subscription>
...@@ -620,7 +606,7 @@ IN_PROC_BROWSER_TEST_F(IdentityGetAccountsFunctionTest, NoPrimaryAccount) { ...@@ -620,7 +606,7 @@ IN_PROC_BROWSER_TEST_F(IdentityGetAccountsFunctionTest, NoPrimaryAccount) {
IN_PROC_BROWSER_TEST_F(IdentityGetAccountsFunctionTest, IN_PROC_BROWSER_TEST_F(IdentityGetAccountsFunctionTest,
PrimaryAccountHasNoRefreshToken) { PrimaryAccountHasNoRefreshToken) {
std::string primary_account_id = SignIn("primary@example.com"); std::string primary_account_id = SignIn("primary@example.com");
token_service_->RevokeCredentials(primary_account_id); identity_test_env()->RemoveRefreshTokenForAccount(primary_account_id);
EXPECT_TRUE(ExpectGetAccounts({})); EXPECT_TRUE(ExpectGetAccounts({}));
} }
...@@ -729,7 +715,7 @@ IN_PROC_BROWSER_TEST_F(IdentityGetProfileUserInfoFunctionTest, ...@@ -729,7 +715,7 @@ IN_PROC_BROWSER_TEST_F(IdentityGetProfileUserInfoFunctionTest,
class GetAuthTokenFunctionTest class GetAuthTokenFunctionTest
: public IdentityTestWithSignin, : public IdentityTestWithSignin,
public OAuth2TokenService::DiagnosticsObserver { public identity::IdentityManager::DiagnosticsObserver {
public: public:
GetAuthTokenFunctionTest() { GetAuthTokenFunctionTest() {
#if BUILDFLAG(ENABLE_DICE_SUPPORT) #if BUILDFLAG(ENABLE_DICE_SUPPORT)
...@@ -743,7 +729,8 @@ class GetAuthTokenFunctionTest ...@@ -743,7 +729,8 @@ class GetAuthTokenFunctionTest
std::string IssueLoginAccessTokenForAccount(const std::string& account_id) { std::string IssueLoginAccessTokenForAccount(const std::string& account_id) {
std::string access_token = "access_token-" + account_id; std::string access_token = "access_token-" + account_id;
token_service_->IssueAllTokensForAccount( identity_test_env()
->WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
account_id, access_token, account_id, access_token,
base::Time::Now() + base::TimeDelta::FromSeconds(3600)); base::Time::Now() + base::TimeDelta::FromSeconds(3600));
return access_token; return access_token;
...@@ -759,11 +746,11 @@ class GetAuthTokenFunctionTest ...@@ -759,11 +746,11 @@ class GetAuthTokenFunctionTest
void SetUpOnMainThread() override { void SetUpOnMainThread() override {
IdentityTestWithSignin::SetUpOnMainThread(); IdentityTestWithSignin::SetUpOnMainThread();
token_service_->AddDiagnosticsObserver(this); identity_test_env()->identity_manager()->AddDiagnosticsObserver(this);
} }
void TearDownOnMainThread() override { void TearDownOnMainThread() override {
token_service_->RemoveDiagnosticsObserver(this); identity_test_env()->identity_manager()->RemoveDiagnosticsObserver(this);
IdentityTestWithSignin::TearDownOnMainThread(); IdentityTestWithSignin::TearDownOnMainThread();
} }
...@@ -797,9 +784,7 @@ class GetAuthTokenFunctionTest ...@@ -797,9 +784,7 @@ class GetAuthTokenFunctionTest
} }
const std::string& GetPrimaryAccountId() { const std::string& GetPrimaryAccountId() {
SigninManagerBase* signin_manager = return identity_test_env()->identity_manager()->GetPrimaryAccountId();
SigninManagerFactory::GetForProfile(browser()->profile());
return signin_manager->GetAuthenticatedAccountId();
} }
void SetCachedToken(const IdentityTokenCacheValue& token_data) { void SetCachedToken(const IdentityTokenCacheValue& token_data) {
...@@ -829,11 +814,10 @@ class GetAuthTokenFunctionTest ...@@ -829,11 +814,10 @@ class GetAuthTokenFunctionTest
base::OnceClosure on_access_token_requested_; base::OnceClosure on_access_token_requested_;
private: private:
// OAuth2TokenService::DiagnosticsObserver: // identity::IdentityManager::DiagnosticsObserver:
void OnAccessTokenRequested( void OnAccessTokenRequested(const std::string& account_id,
const std::string& account_id,
const std::string& consumer_id, const std::string& consumer_id,
const OAuth2TokenService::ScopeSet& scopes) override { const identity::ScopeSet& scopes) override {
if (on_access_token_requested_.is_null()) if (on_access_token_requested_.is_null())
return; return;
std::move(on_access_token_requested_).Run(); std::move(on_access_token_requested_).Run();
...@@ -1734,7 +1718,7 @@ IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, ManuallyIssueTokenFailure) { ...@@ -1734,7 +1718,7 @@ IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, ManuallyIssueTokenFailure) {
RunFunctionAsync(func.get(), "[{}]"); RunFunctionAsync(func.get(), "[{}]");
run_loop.Run(); run_loop.Run();
token_service_->IssueErrorForAllPendingRequestsForAccount( identity_test_env()->WaitForAccessTokenRequestIfNecessaryAndRespondWithError(
primary_account_id, primary_account_id,
GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE)); GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE));
...@@ -2365,7 +2349,7 @@ IN_PROC_BROWSER_TEST_F(OnSignInChangedEventTest, FireOnPrimaryAccountSignOut) { ...@@ -2365,7 +2349,7 @@ IN_PROC_BROWSER_TEST_F(OnSignInChangedEventTest, FireOnPrimaryAccountSignOut) {
AddExpectedEvent(api::identity::OnSignInChanged::Create(account_info, false)); AddExpectedEvent(api::identity::OnSignInChanged::Create(account_info, false));
// Sign out and verify that the callback fires. // Sign out and verify that the callback fires.
signin_manager_->ForceSignOut(); identity_test_env()->ClearPrimaryAccount();
EXPECT_FALSE(HasExpectedEvent()); EXPECT_FALSE(HasExpectedEvent());
} }
...@@ -2384,7 +2368,7 @@ IN_PROC_BROWSER_TEST_F(OnSignInChangedEventTest, ...@@ -2384,7 +2368,7 @@ IN_PROC_BROWSER_TEST_F(OnSignInChangedEventTest,
AddExpectedEvent(api::identity::OnSignInChanged::Create(account_info, false)); AddExpectedEvent(api::identity::OnSignInChanged::Create(account_info, false));
// Revoke the refresh token and verify that the callback fires. // Revoke the refresh token and verify that the callback fires.
token_service_->RevokeCredentials(primary_account_id); identity_test_env()->RemoveRefreshTokenForAccount(primary_account_id);
EXPECT_FALSE(HasExpectedEvent()); EXPECT_FALSE(HasExpectedEvent());
} }
...@@ -2400,7 +2384,7 @@ IN_PROC_BROWSER_TEST_F(OnSignInChangedEventTest, ...@@ -2400,7 +2384,7 @@ IN_PROC_BROWSER_TEST_F(OnSignInChangedEventTest,
std::string primary_account_id = SignIn("primary@example.com"); std::string primary_account_id = SignIn("primary@example.com");
AddExpectedEvent(api::identity::OnSignInChanged::Create(account_info, false)); AddExpectedEvent(api::identity::OnSignInChanged::Create(account_info, false));
token_service_->RevokeCredentials(primary_account_id); identity_test_env()->RemoveRefreshTokenForAccount(primary_account_id);
account_info.id = "gaia_id_for_primary_example.com"; account_info.id = "gaia_id_for_primary_example.com";
AddExpectedEvent(api::identity::OnSignInChanged::Create(account_info, true)); AddExpectedEvent(api::identity::OnSignInChanged::Create(account_info, true));
...@@ -2432,7 +2416,7 @@ IN_PROC_BROWSER_TEST_F(OnSignInChangedEventTest, FireForSecondaryAccount) { ...@@ -2432,7 +2416,7 @@ IN_PROC_BROWSER_TEST_F(OnSignInChangedEventTest, FireForSecondaryAccount) {
// fires. // fires.
AddExpectedEvent(api::identity::OnSignInChanged::Create(account_info, false)); AddExpectedEvent(api::identity::OnSignInChanged::Create(account_info, false));
token_service_->RevokeCredentials(secondary_account_id); identity_test_env()->RemoveRefreshTokenForAccount(secondary_account_id);
EXPECT_FALSE(HasExpectedEvent()); EXPECT_FALSE(HasExpectedEvent());
} }
......
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