OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ios/chrome/test/app/signin_test_util.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/test/ios/wait_util.h" |
| 10 #include "components/prefs/pref_service.h" |
| 11 #include "components/signin/core/browser/account_tracker_service.h" |
| 12 #include "components/signin/core/common/signin_pref_names.h" |
| 13 #include "google_apis/gaia/gaia_constants.h" |
| 14 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 15 #include "ios/chrome/browser/signin/account_tracker_service_factory.h" |
| 16 #include "ios/chrome/browser/signin/authentication_service.h" |
| 17 #include "ios/chrome/browser/signin/authentication_service_factory.h" |
| 18 #include "ios/chrome/browser/signin/gaia_auth_fetcher_ios.h" |
| 19 #import "ios/chrome/test/app/chrome_test_util.h" |
| 20 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" |
| 21 #import "ios/public/provider/chrome/browser/signin/fake_chrome_identity_service.
h" |
| 22 #include "net/http/http_status_code.h" |
| 23 #include "net/url_request/test_url_fetcher_factory.h" |
| 24 #include "net/url_request/url_fetcher_delegate.h" |
| 25 #include "net/url_request/url_request_status.h" |
| 26 |
| 27 namespace { |
| 28 |
| 29 net::FakeURLFetcherFactory* gFakeURLFetcherFactory = nullptr; |
| 30 |
| 31 // Specialization of the FakeURLFetcherFactory that will recognize GET requests |
| 32 // for MergeSession and answer those requests correctly. |
| 33 class MergeSessionFakeURLFetcherFactory : public net::FakeURLFetcherFactory { |
| 34 public: |
| 35 explicit MergeSessionFakeURLFetcherFactory(URLFetcherFactory* default_factory) |
| 36 : net::FakeURLFetcherFactory(default_factory) {} |
| 37 std::unique_ptr<net::URLFetcher> CreateURLFetcher( |
| 38 int id, |
| 39 const GURL& url, |
| 40 net::URLFetcher::RequestType request_type, |
| 41 net::URLFetcherDelegate* d) override { |
| 42 const GURL kMergeSessionURL = |
| 43 GURL("https://accounts.google.com/MergeSession"); |
| 44 url::Replacements<char> replacements; |
| 45 replacements.ClearRef(); |
| 46 replacements.ClearQuery(); |
| 47 if (url.ReplaceComponents(replacements) != kMergeSessionURL) { |
| 48 // URL is not a MergeSession GET. Use the default method. |
| 49 return net::FakeURLFetcherFactory::CreateURLFetcher(id, url, request_type, |
| 50 d); |
| 51 } |
| 52 // Actual MergeSession request. Answer is ignored by the AccountReconcilor, |
| 53 // so it can also be empty. |
| 54 return std::unique_ptr<net::FakeURLFetcher>(new net::FakeURLFetcher( |
| 55 url, d, "", net::HTTP_OK, net::URLRequestStatus::SUCCESS)); |
| 56 } |
| 57 }; |
| 58 |
| 59 } // namespace |
| 60 |
| 61 namespace chrome_test_util { |
| 62 |
| 63 void SetUpMockAuthentication() { |
| 64 ios::ChromeBrowserProvider* provider = ios::GetChromeBrowserProvider(); |
| 65 std::unique_ptr<ios::FakeChromeIdentityService> service( |
| 66 new ios::FakeChromeIdentityService()); |
| 67 service->SetUpForIntegrationTests(); |
| 68 provider->SetChromeIdentityServiceForTesting(std::move(service)); |
| 69 AuthenticationServiceFactory::GetForBrowserState(GetOriginalBrowserState()) |
| 70 ->ResetChromeIdentityServiceObserverForTesting(); |
| 71 } |
| 72 |
| 73 void TearDownMockAuthentication() { |
| 74 ios::ChromeBrowserProvider* provider = ios::GetChromeBrowserProvider(); |
| 75 provider->SetChromeIdentityServiceForTesting(nullptr); |
| 76 AuthenticationServiceFactory::GetForBrowserState(GetOriginalBrowserState()) |
| 77 ->ResetChromeIdentityServiceObserverForTesting(); |
| 78 } |
| 79 |
| 80 void SetUpMockAccountReconcilor() { |
| 81 gFakeURLFetcherFactory = |
| 82 new MergeSessionFakeURLFetcherFactory(new net::URLFetcherImplFactory()); |
| 83 GaiaAuthFetcherIOS::SetShouldUseGaiaAuthFetcherIOSForTesting(false); |
| 84 |
| 85 // Answer is URLs in JSON that will be fetched and used in MergeSession that |
| 86 // we also intercept, so it can be empty. |
| 87 const GURL kCheckConnectionInfoURL = GURL(base::StringPrintf( |
| 88 "https://accounts.google.com/GetCheckConnectionInfo?source=%s", |
| 89 GaiaConstants::kChromeSource)); |
| 90 gFakeURLFetcherFactory->SetFakeResponse(kCheckConnectionInfoURL, "[]", |
| 91 net::HTTP_OK, |
| 92 net::URLRequestStatus::SUCCESS); |
| 93 |
| 94 // No accounts in the cookie jar, will trigger a MergeSession for all of |
| 95 // them. |
| 96 const GURL kListAccountsURL = GURL(base::StringPrintf( |
| 97 "https://accounts.google.com/ListAccounts?source=%s&json=standard", |
| 98 GaiaConstants::kChromeSource)); |
| 99 gFakeURLFetcherFactory->SetFakeResponse(kListAccountsURL, "[\"\",[]]", |
| 100 net::HTTP_OK, |
| 101 net::URLRequestStatus::SUCCESS); |
| 102 |
| 103 // Ubertoken, which is sent for the MergeSession that we also intercept, so |
| 104 // it can be any bogus token. |
| 105 const GURL kUbertokenURL = GURL(base::StringPrintf( |
| 106 "https://accounts.google.com/OAuthLogin?source=%s&issueuberauth=1", |
| 107 GaiaConstants::kChromeSource)); |
| 108 gFakeURLFetcherFactory->SetFakeResponse( |
| 109 kUbertokenURL, "1234-asdf-fake-ubertoken", net::HTTP_OK, |
| 110 net::URLRequestStatus::SUCCESS); |
| 111 |
| 112 // MergeSession request is handled by MergeSessionFakeURLFetcherFactory. |
| 113 |
| 114 // If a profile was previously signed in without chrome identity, a logout |
| 115 // action is done and will block all other requests until it has been dealt |
| 116 // with. |
| 117 const GURL kLogoutURL = |
| 118 GURL(base::StringPrintf("https://accounts.google.com/Logout?source=%s", |
| 119 GaiaConstants::kChromeSource)); |
| 120 gFakeURLFetcherFactory->SetFakeResponse(kLogoutURL, "", net::HTTP_OK, |
| 121 net::URLRequestStatus::SUCCESS); |
| 122 } |
| 123 |
| 124 void TearDownMockAccountReconcilor() { |
| 125 GaiaAuthFetcherIOS::SetShouldUseGaiaAuthFetcherIOSForTesting(true); |
| 126 delete gFakeURLFetcherFactory; |
| 127 gFakeURLFetcherFactory = nullptr; |
| 128 } |
| 129 |
| 130 bool SignOutAndClearAccounts() { |
| 131 ios::ChromeBrowserState* browser_state = GetOriginalBrowserState(); |
| 132 DCHECK(browser_state); |
| 133 |
| 134 // Sign out current user. |
| 135 AuthenticationService* authentication_service = |
| 136 AuthenticationServiceFactory::GetForBrowserState(browser_state); |
| 137 if (authentication_service->IsAuthenticated()) { |
| 138 authentication_service->SignOut(signin_metrics::SIGNOUT_TEST, nil); |
| 139 } |
| 140 |
| 141 // Clear the tracked accounts. |
| 142 AccountTrackerService* account_tracker = |
| 143 ios::AccountTrackerServiceFactory::GetForBrowserState(browser_state); |
| 144 for (const AccountInfo& info : account_tracker->GetAccounts()) { |
| 145 account_tracker->RemoveAccount(info.account_id); |
| 146 } |
| 147 |
| 148 // Clear last signed in user preference. |
| 149 browser_state->GetPrefs()->ClearPref(prefs::kGoogleServicesLastAccountId); |
| 150 browser_state->GetPrefs()->ClearPref(prefs::kGoogleServicesLastUsername); |
| 151 |
| 152 // Clear known identities. |
| 153 ios::ChromeIdentityService* identity_service = |
| 154 ios::GetChromeBrowserProvider()->GetChromeIdentityService(); |
| 155 base::scoped_nsobject<NSArray> identities( |
| 156 [identity_service->GetAllIdentities() copy]); |
| 157 for (ChromeIdentity* identity in identities.get()) { |
| 158 identity_service->ForgetIdentity(identity, nil); |
| 159 } |
| 160 |
| 161 NSDate* deadline = [NSDate dateWithTimeIntervalSinceNow:10.0]; |
| 162 while (identity_service->HasIdentities() && |
| 163 [[NSDate date] compare:deadline] != NSOrderedDescending) { |
| 164 base::test::ios::SpinRunLoopWithMaxDelay( |
| 165 base::TimeDelta::FromSecondsD(0.01)); |
| 166 } |
| 167 return !identity_service->HasIdentities(); |
| 168 } |
| 169 |
| 170 } // namespace chrome_test_util |
OLD | NEW |