OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "components/signin/ios/browser/profile_oauth2_token_service_ios_delegat
e.h" |
| 6 |
| 7 #include "base/prefs/pref_registry_simple.h" |
| 8 #include "base/prefs/testing_pref_service.h" |
| 9 #include "base/run_loop.h" |
| 10 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
| 11 #include "components/signin/core/browser/test_signin_client.h" |
| 12 #include "components/signin/core/common/signin_pref_names.h" |
| 13 #include "google_apis/gaia/gaia_urls.h" |
| 14 #include "google_apis/gaia/oauth2_access_token_consumer.h" |
| 15 #include "google_apis/gaia/oauth2_token_service_test_util.h" |
| 16 #include "ios/public/test/fake_profile_oauth2_token_service_ios_provider.h" |
| 17 #include "net/url_request/test_url_fetcher_factory.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 |
| 20 class ProfileOAuth2TokenServiceIOSDelegateTest |
| 21 : public testing::Test, |
| 22 public OAuth2AccessTokenConsumer, |
| 23 public OAuth2TokenService::Observer { |
| 24 public: |
| 25 ProfileOAuth2TokenServiceIOSDelegateTest() |
| 26 : factory_(NULL), |
| 27 client_(&prefs_), |
| 28 token_available_count_(0), |
| 29 token_revoked_count_(0), |
| 30 tokens_loaded_count_(0), |
| 31 access_token_success_(0), |
| 32 access_token_failure_(0), |
| 33 last_access_token_error_(GoogleServiceAuthError::NONE) {} |
| 34 |
| 35 void SetUp() override { |
| 36 prefs_.registry()->RegisterBooleanPref( |
| 37 prefs::kTokenServiceExcludeAllSecondaryAccounts, false); |
| 38 prefs_.registry()->RegisterListPref( |
| 39 prefs::kTokenServiceExcludedSecondaryAccounts); |
| 40 |
| 41 factory_.SetFakeResponse(GaiaUrls::GetInstance()->oauth2_revoke_url(), "", |
| 42 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
| 43 fake_provider_ = client_.GetIOSProviderAsFake(); |
| 44 oauth2_service_delegate_.reset(new ProfileOAuth2TokenServiceIOSDelegate( |
| 45 &client_, &signin_error_controller_)); |
| 46 oauth2_service_delegate_->AddObserver(this); |
| 47 } |
| 48 |
| 49 void TearDown() override { |
| 50 oauth2_service_delegate_->RemoveObserver(this); |
| 51 oauth2_service_delegate_->Shutdown(); |
| 52 } |
| 53 |
| 54 // OAuth2AccessTokenConsumer implementation. |
| 55 void OnGetTokenSuccess(const std::string& access_token, |
| 56 const base::Time& expiration_time) override { |
| 57 ++access_token_success_; |
| 58 } |
| 59 |
| 60 void OnGetTokenFailure(const GoogleServiceAuthError& error) override { |
| 61 ++access_token_failure_; |
| 62 last_access_token_error_ = error; |
| 63 }; |
| 64 |
| 65 // OAuth2TokenService::Observer implementation. |
| 66 void OnRefreshTokenAvailable(const std::string& account_id) override { |
| 67 ++token_available_count_; |
| 68 } |
| 69 void OnRefreshTokenRevoked(const std::string& account_id) override { |
| 70 ++token_revoked_count_; |
| 71 } |
| 72 void OnRefreshTokensLoaded() override { ++tokens_loaded_count_; } |
| 73 |
| 74 void ResetObserverCounts() { |
| 75 token_available_count_ = 0; |
| 76 token_revoked_count_ = 0; |
| 77 tokens_loaded_count_ = 0; |
| 78 token_available_count_ = 0; |
| 79 access_token_failure_ = 0; |
| 80 } |
| 81 |
| 82 protected: |
| 83 base::MessageLoop message_loop_; |
| 84 net::FakeURLFetcherFactory factory_; |
| 85 TestingPrefServiceSimple prefs_; |
| 86 TestSigninClient client_; |
| 87 SigninErrorController signin_error_controller_; |
| 88 ios::FakeProfileOAuth2TokenServiceIOSProvider* fake_provider_; |
| 89 scoped_ptr<ProfileOAuth2TokenServiceIOSDelegate> oauth2_service_delegate_; |
| 90 TestingOAuth2TokenServiceConsumer consumer_; |
| 91 int token_available_count_; |
| 92 int token_revoked_count_; |
| 93 int tokens_loaded_count_; |
| 94 int access_token_success_; |
| 95 int access_token_failure_; |
| 96 GoogleServiceAuthError last_access_token_error_; |
| 97 }; |
| 98 |
| 99 TEST_F(ProfileOAuth2TokenServiceIOSDelegateTest, |
| 100 LoadRevokeCredentialsOneAccount) { |
| 101 fake_provider_->AddAccount("account_id"); |
| 102 oauth2_service_delegate_->LoadCredentials("account_id"); |
| 103 base::RunLoop().RunUntilIdle(); |
| 104 EXPECT_EQ(1, token_available_count_); |
| 105 EXPECT_EQ(1, tokens_loaded_count_); |
| 106 EXPECT_EQ(0, token_revoked_count_); |
| 107 EXPECT_EQ(1U, oauth2_service_delegate_->GetAccounts().size()); |
| 108 EXPECT_TRUE(oauth2_service_delegate_->RefreshTokenIsAvailable("account_id")); |
| 109 |
| 110 ResetObserverCounts(); |
| 111 oauth2_service_delegate_->RevokeAllCredentials(); |
| 112 EXPECT_EQ(0, token_available_count_); |
| 113 EXPECT_EQ(0, tokens_loaded_count_); |
| 114 EXPECT_EQ(1, token_revoked_count_); |
| 115 EXPECT_EQ(0U, oauth2_service_delegate_->GetAccounts().size()); |
| 116 EXPECT_FALSE( |
| 117 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_1")); |
| 118 } |
| 119 |
| 120 TEST_F(ProfileOAuth2TokenServiceIOSDelegateTest, |
| 121 LoadRevokeCredentialsMultipleAccounts) { |
| 122 fake_provider_->AddAccount("account_id_1"); |
| 123 fake_provider_->AddAccount("account_id_2"); |
| 124 fake_provider_->AddAccount("account_id_3"); |
| 125 oauth2_service_delegate_->LoadCredentials("account_id_1"); |
| 126 base::RunLoop().RunUntilIdle(); |
| 127 EXPECT_EQ(3, token_available_count_); |
| 128 EXPECT_EQ(1, tokens_loaded_count_); |
| 129 EXPECT_EQ(0, token_revoked_count_); |
| 130 EXPECT_EQ(3U, oauth2_service_delegate_->GetAccounts().size()); |
| 131 EXPECT_TRUE( |
| 132 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_1")); |
| 133 EXPECT_TRUE( |
| 134 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_2")); |
| 135 EXPECT_TRUE( |
| 136 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_3")); |
| 137 |
| 138 ResetObserverCounts(); |
| 139 oauth2_service_delegate_->RevokeAllCredentials(); |
| 140 EXPECT_EQ(0, token_available_count_); |
| 141 EXPECT_EQ(0, tokens_loaded_count_); |
| 142 EXPECT_EQ(3, token_revoked_count_); |
| 143 EXPECT_EQ(0U, oauth2_service_delegate_->GetAccounts().size()); |
| 144 EXPECT_FALSE( |
| 145 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_1")); |
| 146 EXPECT_FALSE( |
| 147 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_2")); |
| 148 EXPECT_FALSE( |
| 149 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_3")); |
| 150 } |
| 151 |
| 152 TEST_F(ProfileOAuth2TokenServiceIOSDelegateTest, ReloadCredentials) { |
| 153 fake_provider_->AddAccount("account_id_1"); |
| 154 fake_provider_->AddAccount("account_id_2"); |
| 155 fake_provider_->AddAccount("account_id_3"); |
| 156 oauth2_service_delegate_->LoadCredentials("account_id_1"); |
| 157 base::RunLoop().RunUntilIdle(); |
| 158 |
| 159 // Change the accounts. |
| 160 ResetObserverCounts(); |
| 161 fake_provider_->ClearAccounts(); |
| 162 fake_provider_->AddAccount("account_id_1"); |
| 163 fake_provider_->AddAccount("account_id_4"); |
| 164 oauth2_service_delegate_->ReloadCredentials(); |
| 165 |
| 166 EXPECT_EQ(1, token_available_count_); |
| 167 EXPECT_EQ(0, tokens_loaded_count_); |
| 168 EXPECT_EQ(2, token_revoked_count_); |
| 169 EXPECT_EQ(2U, oauth2_service_delegate_->GetAccounts().size()); |
| 170 EXPECT_TRUE( |
| 171 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_1")); |
| 172 EXPECT_FALSE( |
| 173 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_2")); |
| 174 EXPECT_FALSE( |
| 175 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_3")); |
| 176 EXPECT_TRUE( |
| 177 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_4")); |
| 178 } |
| 179 |
| 180 TEST_F(ProfileOAuth2TokenServiceIOSDelegateTest, |
| 181 ReloadCredentialsIgnoredIfNoPrimaryAccountId) { |
| 182 // Change the accounts. |
| 183 ResetObserverCounts(); |
| 184 fake_provider_->AddAccount("account_id_1"); |
| 185 fake_provider_->AddAccount("account_id_2"); |
| 186 base::RunLoop().RunUntilIdle(); |
| 187 |
| 188 oauth2_service_delegate_->ReloadCredentials(); |
| 189 |
| 190 EXPECT_EQ(0, token_available_count_); |
| 191 EXPECT_EQ(0, tokens_loaded_count_); |
| 192 EXPECT_EQ(0, token_revoked_count_); |
| 193 EXPECT_EQ(0U, oauth2_service_delegate_->GetAccounts().size()); |
| 194 EXPECT_FALSE( |
| 195 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_1")); |
| 196 EXPECT_FALSE( |
| 197 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_2")); |
| 198 } |
| 199 |
| 200 TEST_F(ProfileOAuth2TokenServiceIOSDelegateTest, |
| 201 ReloadCredentialsWithPrimaryAccountId) { |
| 202 // Change the accounts. |
| 203 ResetObserverCounts(); |
| 204 fake_provider_->AddAccount("account_id_1"); |
| 205 fake_provider_->AddAccount("account_id_2"); |
| 206 base::RunLoop().RunUntilIdle(); |
| 207 |
| 208 oauth2_service_delegate_->ReloadCredentials("account_id_1"); |
| 209 EXPECT_EQ(2, token_available_count_); |
| 210 EXPECT_EQ(0, tokens_loaded_count_); |
| 211 EXPECT_EQ(0, token_revoked_count_); |
| 212 EXPECT_EQ(2U, oauth2_service_delegate_->GetAccounts().size()); |
| 213 EXPECT_TRUE( |
| 214 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_1")); |
| 215 EXPECT_TRUE( |
| 216 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_2")); |
| 217 } |
| 218 |
| 219 TEST_F(ProfileOAuth2TokenServiceIOSDelegateTest, ExcludeAllSecondaryAccounts) { |
| 220 // Change the accounts. |
| 221 ResetObserverCounts(); |
| 222 fake_provider_->AddAccount("account_id_1"); |
| 223 fake_provider_->AddAccount("account_id_2"); |
| 224 base::RunLoop().RunUntilIdle(); |
| 225 |
| 226 oauth2_service_delegate_->ExcludeAllSecondaryAccounts(); |
| 227 oauth2_service_delegate_->ReloadCredentials("account_id_1"); |
| 228 EXPECT_EQ(1, token_available_count_); |
| 229 EXPECT_EQ(0, tokens_loaded_count_); |
| 230 EXPECT_EQ(0, token_revoked_count_); |
| 231 EXPECT_EQ(1U, oauth2_service_delegate_->GetAccounts().size()); |
| 232 EXPECT_TRUE( |
| 233 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_1")); |
| 234 EXPECT_FALSE( |
| 235 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_2")); |
| 236 } |
| 237 |
| 238 TEST_F(ProfileOAuth2TokenServiceIOSDelegateTest, StartRequestSuccess) { |
| 239 fake_provider_->AddAccount("account_id_1"); |
| 240 oauth2_service_delegate_->LoadCredentials("account_id_1"); |
| 241 base::RunLoop().RunUntilIdle(); |
| 242 |
| 243 // Fetch access tokens. |
| 244 ResetObserverCounts(); |
| 245 std::vector<std::string> scopes; |
| 246 scopes.push_back("scope"); |
| 247 scoped_ptr<OAuth2AccessTokenFetcher> fetcher1( |
| 248 oauth2_service_delegate_->CreateAccessTokenFetcher( |
| 249 "account_id_1", oauth2_service_delegate_->GetRequestContext(), this)); |
| 250 fetcher1->Start("foo", "bar", scopes); |
| 251 EXPECT_EQ(0, access_token_success_); |
| 252 EXPECT_EQ(0, access_token_failure_); |
| 253 |
| 254 ResetObserverCounts(); |
| 255 fake_provider_->IssueAccessTokenForAllRequests(); |
| 256 base::RunLoop().RunUntilIdle(); |
| 257 EXPECT_EQ(1, access_token_success_); |
| 258 EXPECT_EQ(0, access_token_failure_); |
| 259 } |
| 260 |
| 261 TEST_F(ProfileOAuth2TokenServiceIOSDelegateTest, StartRequestFailure) { |
| 262 fake_provider_->AddAccount("account_id_1"); |
| 263 oauth2_service_delegate_->LoadCredentials("account_id_1"); |
| 264 base::RunLoop().RunUntilIdle(); |
| 265 |
| 266 // Fetch access tokens. |
| 267 ResetObserverCounts(); |
| 268 std::vector<std::string> scopes; |
| 269 scopes.push_back("scope"); |
| 270 scoped_ptr<OAuth2AccessTokenFetcher> fetcher1( |
| 271 oauth2_service_delegate_->CreateAccessTokenFetcher( |
| 272 "account_id_1", oauth2_service_delegate_->GetRequestContext(), this)); |
| 273 fetcher1->Start("foo", "bar", scopes); |
| 274 EXPECT_EQ(0, access_token_success_); |
| 275 EXPECT_EQ(0, access_token_failure_); |
| 276 |
| 277 ResetObserverCounts(); |
| 278 fake_provider_->IssueAccessTokenErrorForAllRequests(); |
| 279 base::RunLoop().RunUntilIdle(); |
| 280 EXPECT_EQ(0, access_token_success_); |
| 281 EXPECT_EQ(1, access_token_failure_); |
| 282 } |
| 283 |
| 284 TEST_F(ProfileOAuth2TokenServiceIOSDelegateTest, ExcludeSecondaryAccounts) { |
| 285 fake_provider_->AddAccount("account_id_1"); |
| 286 fake_provider_->AddAccount("account_id_2"); |
| 287 fake_provider_->AddAccount("account_id_3"); |
| 288 oauth2_service_delegate_->LoadCredentials("account_id_1"); |
| 289 base::RunLoop().RunUntilIdle(); |
| 290 |
| 291 // Ignore one account should remove it from the list of accounts. |
| 292 ResetObserverCounts(); |
| 293 oauth2_service_delegate_->ExcludeSecondaryAccount("account_id_2"); |
| 294 oauth2_service_delegate_->ReloadCredentials(); |
| 295 |
| 296 EXPECT_EQ(0, token_available_count_); |
| 297 EXPECT_EQ(0, tokens_loaded_count_); |
| 298 EXPECT_EQ(1, token_revoked_count_); |
| 299 EXPECT_EQ(2U, oauth2_service_delegate_->GetAccounts().size()); |
| 300 EXPECT_TRUE( |
| 301 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_1")); |
| 302 EXPECT_FALSE( |
| 303 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_2")); |
| 304 EXPECT_TRUE( |
| 305 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_3")); |
| 306 |
| 307 // Clear ignored account and the refresh token should be available again. |
| 308 ResetObserverCounts(); |
| 309 oauth2_service_delegate_->IncludeSecondaryAccount("account_id_2"); |
| 310 oauth2_service_delegate_->ReloadCredentials(); |
| 311 |
| 312 EXPECT_EQ(1, token_available_count_); |
| 313 EXPECT_EQ(0, tokens_loaded_count_); |
| 314 EXPECT_EQ(0, token_revoked_count_); |
| 315 EXPECT_EQ(3U, oauth2_service_delegate_->GetAccounts().size()); |
| 316 EXPECT_TRUE( |
| 317 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_1")); |
| 318 EXPECT_TRUE( |
| 319 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_2")); |
| 320 EXPECT_TRUE( |
| 321 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_3")); |
| 322 } |
| 323 |
| 324 // Unit test for for http://crbug.com/453470 . |
| 325 TEST_F(ProfileOAuth2TokenServiceIOSDelegateTest, ExcludeSecondaryAccountTwice) { |
| 326 fake_provider_->AddAccount("account_id_1"); |
| 327 fake_provider_->AddAccount("account_id_2"); |
| 328 oauth2_service_delegate_->LoadCredentials("account_id_1"); |
| 329 base::RunLoop().RunUntilIdle(); |
| 330 EXPECT_TRUE( |
| 331 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_1")); |
| 332 EXPECT_TRUE( |
| 333 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_2")); |
| 334 |
| 335 // Ignore |account_id_2| twice. |
| 336 oauth2_service_delegate_->ExcludeSecondaryAccount("account_id_2"); |
| 337 oauth2_service_delegate_->ExcludeSecondaryAccount("account_id_2"); |
| 338 oauth2_service_delegate_->ReloadCredentials(); |
| 339 |
| 340 // Include |account_id_2| once should add the account back. |
| 341 ResetObserverCounts(); |
| 342 oauth2_service_delegate_->IncludeSecondaryAccount("account_id_2"); |
| 343 oauth2_service_delegate_->ReloadCredentials(); |
| 344 |
| 345 EXPECT_EQ(1, token_available_count_); |
| 346 EXPECT_EQ(0, tokens_loaded_count_); |
| 347 EXPECT_EQ(0, token_revoked_count_); |
| 348 EXPECT_EQ(2U, oauth2_service_delegate_->GetAccounts().size()); |
| 349 EXPECT_TRUE( |
| 350 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_1")); |
| 351 EXPECT_TRUE( |
| 352 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_2")); |
| 353 } |
| 354 |
| 355 TEST_F(ProfileOAuth2TokenServiceIOSDelegateTest, |
| 356 LoadRevokeCredentialsClearsExcludedAccounts) { |
| 357 fake_provider_->AddAccount("account_id_1"); |
| 358 fake_provider_->AddAccount("account_id_2"); |
| 359 fake_provider_->AddAccount("account_id_3"); |
| 360 |
| 361 std::vector<std::string> excluded_accounts; |
| 362 excluded_accounts.push_back("account_id_2"); |
| 363 oauth2_service_delegate_->ExcludeSecondaryAccounts(excluded_accounts); |
| 364 oauth2_service_delegate_->ReloadCredentials("account_id_1"); |
| 365 base::RunLoop().RunUntilIdle(); |
| 366 EXPECT_EQ(2, token_available_count_); |
| 367 EXPECT_EQ(0, tokens_loaded_count_); |
| 368 EXPECT_EQ(0, token_revoked_count_); |
| 369 EXPECT_EQ(2U, oauth2_service_delegate_->GetAccounts().size()); |
| 370 EXPECT_TRUE( |
| 371 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_1")); |
| 372 EXPECT_FALSE( |
| 373 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_2")); |
| 374 EXPECT_TRUE( |
| 375 oauth2_service_delegate_->RefreshTokenIsAvailable("account_id_3")); |
| 376 |
| 377 ResetObserverCounts(); |
| 378 oauth2_service_delegate_->RevokeAllCredentials(); |
| 379 EXPECT_TRUE(oauth2_service_delegate_->GetExcludedSecondaryAccounts().empty()); |
| 380 } |
OLD | NEW |