| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/signin/core/browser/mutable_profile_oauth2_token_service.h" | 5 #include "chrome/browser/signin/mutable_profile_oauth2_token_service_delegate.h" |
| 6 | 6 |
| 7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 8 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
| 9 #include "components/signin/core/browser/signin_error_controller.h" | 9 #include "components/signin/core/browser/signin_error_controller.h" |
| 10 #include "components/signin/core/browser/test_signin_client.h" | 10 #include "components/signin/core/browser/test_signin_client.h" |
| 11 #include "components/signin/core/browser/webdata/token_web_data.h" | 11 #include "components/signin/core/browser/webdata/token_web_data.h" |
| 12 #include "google_apis/gaia/gaia_constants.h" | 12 #include "google_apis/gaia/gaia_constants.h" |
| 13 #include "google_apis/gaia/gaia_urls.h" | 13 #include "google_apis/gaia/gaia_urls.h" |
| 14 #include "google_apis/gaia/google_service_auth_error.h" | 14 #include "google_apis/gaia/google_service_auth_error.h" |
| 15 #include "google_apis/gaia/oauth2_access_token_consumer.h" | 15 #include "google_apis/gaia/oauth2_access_token_consumer.h" |
| 16 #include "google_apis/gaia/oauth2_token_service_test_util.h" | 16 #include "google_apis/gaia/oauth2_token_service_test_util.h" |
| 17 #include "net/http/http_status_code.h" | 17 #include "net/http/http_status_code.h" |
| 18 #include "net/url_request/test_url_fetcher_factory.h" | 18 #include "net/url_request/test_url_fetcher_factory.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 20 |
| 21 #if defined(OS_MACOSX) | 21 #if defined(OS_MACOSX) |
| 22 #include "components/os_crypt/os_crypt.h" | 22 #include "components/os_crypt/os_crypt.h" |
| 23 #endif | 23 #endif |
| 24 | 24 |
| 25 // Defining constant here to handle backward compatiblity tests, but this | 25 // Defining constant here to handle backward compatiblity tests, but this |
| 26 // constant is no longer used in current versions of chrome. | 26 // constant is no longer used in current versions of chrome. |
| 27 static const char kLSOService[] = "lso"; | 27 static const char kLSOService[] = "lso"; |
| 28 static const char kEmail[] = "user@gmail.com"; | 28 static const char kEmail[] = "user@gmail.com"; |
| 29 | 29 |
| 30 class MutableProfileOAuth2TokenServiceTest | 30 class MutableProfileOAuth2TokenServiceDelegateTest |
| 31 : public testing::Test, | 31 : public testing::Test, |
| 32 public OAuth2AccessTokenConsumer, | 32 public OAuth2AccessTokenConsumer, |
| 33 public OAuth2TokenService::Observer { | 33 public OAuth2TokenService::Observer { |
| 34 public: | 34 public: |
| 35 MutableProfileOAuth2TokenServiceTest() | 35 MutableProfileOAuth2TokenServiceDelegateTest() |
| 36 : factory_(NULL), | 36 : factory_(NULL), |
| 37 access_token_success_count_(0), | 37 access_token_success_count_(0), |
| 38 access_token_failure_count_(0), | 38 access_token_failure_count_(0), |
| 39 access_token_failure_(GoogleServiceAuthError::NONE), | 39 access_token_failure_(GoogleServiceAuthError::NONE), |
| 40 token_available_count_(0), | 40 token_available_count_(0), |
| 41 token_revoked_count_(0), | 41 token_revoked_count_(0), |
| 42 tokens_loaded_count_(0), | 42 tokens_loaded_count_(0), |
| 43 start_batch_changes_(0), | 43 start_batch_changes_(0), |
| 44 end_batch_changes_(0) {} | 44 end_batch_changes_(0) {} |
| 45 | 45 |
| 46 void SetUp() override { | 46 void SetUp() override { |
| 47 #if defined(OS_MACOSX) | 47 #if defined(OS_MACOSX) |
| 48 OSCrypt::UseMockKeychain(true); | 48 OSCrypt::UseMockKeychain(true); |
| 49 #endif | 49 #endif |
| 50 | 50 |
| 51 factory_.SetFakeResponse(GaiaUrls::GetInstance()->oauth2_revoke_url(), | 51 factory_.SetFakeResponse(GaiaUrls::GetInstance()->oauth2_revoke_url(), "", |
| 52 "", | 52 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
| 53 net::HTTP_OK, | 53 oauth2_service_delegate_.reset(new MutableProfileOAuth2TokenServiceDelegate( |
| 54 net::URLRequestStatus::SUCCESS); | 54 &client_, &signin_error_controller_)); |
| 55 oauth2_service_.Initialize(&client_, &signin_error_controller_); | |
| 56 // Make sure PO2TS has a chance to load itself before continuing. | 55 // Make sure PO2TS has a chance to load itself before continuing. |
| 57 base::RunLoop().RunUntilIdle(); | 56 base::RunLoop().RunUntilIdle(); |
| 58 oauth2_service_.AddObserver(this); | 57 oauth2_service_delegate_->AddObserver(this); |
| 59 } | 58 } |
| 60 | 59 |
| 61 void TearDown() override { | 60 void TearDown() override { |
| 62 oauth2_service_.RemoveObserver(this); | 61 oauth2_service_delegate_->RemoveObserver(this); |
| 63 oauth2_service_.Shutdown(); | 62 oauth2_service_delegate_->Shutdown(); |
| 64 } | 63 } |
| 65 | 64 |
| 66 void AddAuthTokenManually(const std::string& service, | 65 void AddAuthTokenManually(const std::string& service, |
| 67 const std::string& value) { | 66 const std::string& value) { |
| 68 scoped_refptr<TokenWebData> token_web_data = client_.GetDatabase(); | 67 scoped_refptr<TokenWebData> token_web_data = client_.GetDatabase(); |
| 69 if (token_web_data.get()) | 68 if (token_web_data.get()) |
| 70 token_web_data->SetTokenForService(service, value); | 69 token_web_data->SetTokenForService(service, value); |
| 71 } | 70 } |
| 72 | 71 |
| 73 // OAuth2AccessTokenConusmer implementation | 72 // OAuth2AccessTokenConusmer implementation |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 EXPECT_EQ(0, token_available_count_); | 126 EXPECT_EQ(0, token_available_count_); |
| 128 EXPECT_EQ(0, token_revoked_count_); | 127 EXPECT_EQ(0, token_revoked_count_); |
| 129 EXPECT_EQ(1, tokens_loaded_count_); | 128 EXPECT_EQ(1, tokens_loaded_count_); |
| 130 ResetObserverCounts(); | 129 ResetObserverCounts(); |
| 131 } | 130 } |
| 132 | 131 |
| 133 protected: | 132 protected: |
| 134 base::MessageLoop message_loop_; | 133 base::MessageLoop message_loop_; |
| 135 net::FakeURLFetcherFactory factory_; | 134 net::FakeURLFetcherFactory factory_; |
| 136 TestSigninClient client_; | 135 TestSigninClient client_; |
| 137 MutableProfileOAuth2TokenService oauth2_service_; | 136 scoped_ptr<MutableProfileOAuth2TokenServiceDelegate> oauth2_service_delegate_; |
| 138 TestingOAuth2TokenServiceConsumer consumer_; | 137 TestingOAuth2TokenServiceConsumer consumer_; |
| 139 SigninErrorController signin_error_controller_; | 138 SigninErrorController signin_error_controller_; |
| 140 int access_token_success_count_; | 139 int access_token_success_count_; |
| 141 int access_token_failure_count_; | 140 int access_token_failure_count_; |
| 142 GoogleServiceAuthError access_token_failure_; | 141 GoogleServiceAuthError access_token_failure_; |
| 143 int token_available_count_; | 142 int token_available_count_; |
| 144 int token_revoked_count_; | 143 int token_revoked_count_; |
| 145 int tokens_loaded_count_; | 144 int tokens_loaded_count_; |
| 146 int start_batch_changes_; | 145 int start_batch_changes_; |
| 147 int end_batch_changes_; | 146 int end_batch_changes_; |
| 148 }; | 147 }; |
| 149 | 148 |
| 150 TEST_F(MutableProfileOAuth2TokenServiceTest, PersistenceDBUpgrade) { | 149 TEST_F(MutableProfileOAuth2TokenServiceDelegateTest, PersistenceDBUpgrade) { |
| 151 std::string main_account_id(kEmail); | 150 std::string main_account_id(kEmail); |
| 152 std::string main_refresh_token("old_refresh_token"); | 151 std::string main_refresh_token("old_refresh_token"); |
| 153 | 152 |
| 154 // Populate DB with legacy tokens. | 153 // Populate DB with legacy tokens. |
| 155 AddAuthTokenManually(GaiaConstants::kSyncService, "syncServiceToken"); | 154 AddAuthTokenManually(GaiaConstants::kSyncService, "syncServiceToken"); |
| 156 AddAuthTokenManually(kLSOService, "lsoToken"); | 155 AddAuthTokenManually(kLSOService, "lsoToken"); |
| 157 AddAuthTokenManually(GaiaConstants::kGaiaOAuth2LoginRefreshToken, | 156 AddAuthTokenManually(GaiaConstants::kGaiaOAuth2LoginRefreshToken, |
| 158 main_refresh_token); | 157 main_refresh_token); |
| 159 | 158 |
| 160 // Force LoadCredentials. | 159 // Force LoadCredentials. |
| 161 oauth2_service_.LoadCredentials(main_account_id); | 160 oauth2_service_delegate_->LoadCredentials(main_account_id); |
| 162 base::RunLoop().RunUntilIdle(); | 161 base::RunLoop().RunUntilIdle(); |
| 163 | 162 |
| 164 // Legacy tokens get discarded, but the old refresh token is kept. | 163 // Legacy tokens get discarded, but the old refresh token is kept. |
| 165 EXPECT_EQ(1, tokens_loaded_count_); | 164 EXPECT_EQ(1, tokens_loaded_count_); |
| 166 EXPECT_EQ(1, token_available_count_); | 165 EXPECT_EQ(1, token_available_count_); |
| 167 EXPECT_EQ(1, start_batch_changes_); | 166 EXPECT_EQ(1, start_batch_changes_); |
| 168 EXPECT_EQ(1, end_batch_changes_); | 167 EXPECT_EQ(1, end_batch_changes_); |
| 169 EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable(main_account_id)); | 168 EXPECT_TRUE( |
| 170 EXPECT_EQ(1U, oauth2_service_.refresh_tokens().size()); | 169 oauth2_service_delegate_->RefreshTokenIsAvailable(main_account_id)); |
| 170 EXPECT_EQ(1U, oauth2_service_delegate_->refresh_tokens_.size()); |
| 171 EXPECT_EQ(main_refresh_token, | 171 EXPECT_EQ(main_refresh_token, |
| 172 oauth2_service_.refresh_tokens()[main_account_id]->refresh_token()); | 172 oauth2_service_delegate_->refresh_tokens_[main_account_id] |
| 173 ->refresh_token()); |
| 173 | 174 |
| 174 // Add an old legacy token to the DB, to ensure it will not overwrite existing | 175 // Add an old legacy token to the DB, to ensure it will not overwrite existing |
| 175 // credentials for main account. | 176 // credentials for main account. |
| 176 AddAuthTokenManually(GaiaConstants::kGaiaOAuth2LoginRefreshToken, | 177 AddAuthTokenManually(GaiaConstants::kGaiaOAuth2LoginRefreshToken, |
| 177 "secondOldRefreshToken"); | 178 "secondOldRefreshToken"); |
| 178 // Add some other legacy token. (Expected to get discarded). | 179 // Add some other legacy token. (Expected to get discarded). |
| 179 AddAuthTokenManually(kLSOService, "lsoToken"); | 180 AddAuthTokenManually(kLSOService, "lsoToken"); |
| 180 // Also add a token using PO2TS.UpdateCredentials and make sure upgrade does | 181 // Also add a token using PO2TS.UpdateCredentials and make sure upgrade does |
| 181 // not wipe it. | 182 // not wipe it. |
| 182 std::string other_account_id("other_account_id"); | 183 std::string other_account_id("other_account_id"); |
| 183 std::string other_refresh_token("other_refresh_token"); | 184 std::string other_refresh_token("other_refresh_token"); |
| 184 oauth2_service_.UpdateCredentials(other_account_id, other_refresh_token); | 185 oauth2_service_delegate_->UpdateCredentials(other_account_id, |
| 186 other_refresh_token); |
| 185 ResetObserverCounts(); | 187 ResetObserverCounts(); |
| 186 | 188 |
| 187 // Force LoadCredentials. | 189 // Force LoadCredentials. |
| 188 oauth2_service_.LoadCredentials(main_account_id); | 190 oauth2_service_delegate_->LoadCredentials(main_account_id); |
| 189 base::RunLoop().RunUntilIdle(); | 191 base::RunLoop().RunUntilIdle(); |
| 190 | 192 |
| 191 // Again legacy tokens get discarded, but since the main porfile account | 193 // Again legacy tokens get discarded, but since the main porfile account |
| 192 // token is present it is not overwritten. | 194 // token is present it is not overwritten. |
| 193 EXPECT_EQ(2, token_available_count_); | 195 EXPECT_EQ(2, token_available_count_); |
| 194 EXPECT_EQ(1, tokens_loaded_count_); | 196 EXPECT_EQ(1, tokens_loaded_count_); |
| 195 EXPECT_EQ(1, start_batch_changes_); | 197 EXPECT_EQ(1, start_batch_changes_); |
| 196 EXPECT_EQ(1, end_batch_changes_); | 198 EXPECT_EQ(1, end_batch_changes_); |
| 197 EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable(main_account_id)); | 199 EXPECT_EQ(main_refresh_token, |
| 200 oauth2_service_delegate_->GetRefreshToken(main_account_id)); |
| 201 EXPECT_TRUE( |
| 202 oauth2_service_delegate_->RefreshTokenIsAvailable(main_account_id)); |
| 198 // TODO(fgorski): cover both using RefreshTokenIsAvailable() and then get the | 203 // TODO(fgorski): cover both using RefreshTokenIsAvailable() and then get the |
| 199 // tokens using GetRefreshToken() | 204 // tokens using GetRefreshToken() |
| 200 EXPECT_EQ(2U, oauth2_service_.refresh_tokens().size()); | 205 EXPECT_EQ(2U, oauth2_service_delegate_->refresh_tokens_.size()); |
| 201 EXPECT_EQ(main_refresh_token, | 206 EXPECT_EQ(main_refresh_token, |
| 202 oauth2_service_.refresh_tokens()[main_account_id]->refresh_token()); | 207 oauth2_service_delegate_->refresh_tokens_[main_account_id] |
| 203 EXPECT_EQ( | 208 ->refresh_token()); |
| 204 other_refresh_token, | 209 EXPECT_EQ(other_refresh_token, |
| 205 oauth2_service_.refresh_tokens()[other_account_id]->refresh_token()); | 210 oauth2_service_delegate_->refresh_tokens_[other_account_id] |
| 211 ->refresh_token()); |
| 206 | 212 |
| 207 oauth2_service_.RevokeAllCredentials(); | 213 oauth2_service_delegate_->RevokeAllCredentials(); |
| 208 EXPECT_EQ(2, start_batch_changes_); | 214 EXPECT_EQ(2, start_batch_changes_); |
| 209 EXPECT_EQ(2, end_batch_changes_); | 215 EXPECT_EQ(2, end_batch_changes_); |
| 210 } | 216 } |
| 211 | 217 |
| 212 TEST_F(MutableProfileOAuth2TokenServiceTest, PersistenceRevokeCredentials) { | 218 TEST_F(MutableProfileOAuth2TokenServiceDelegateTest, |
| 219 PersistenceRevokeCredentials) { |
| 213 std::string account_id_1 = "account_id_1"; | 220 std::string account_id_1 = "account_id_1"; |
| 214 std::string refresh_token_1 = "refresh_token_1"; | 221 std::string refresh_token_1 = "refresh_token_1"; |
| 215 std::string account_id_2 = "account_id_2"; | 222 std::string account_id_2 = "account_id_2"; |
| 216 std::string refresh_token_2 = "refresh_token_2"; | 223 std::string refresh_token_2 = "refresh_token_2"; |
| 217 | 224 |
| 218 // TODO(fgorski): Enable below when implemented: | 225 // TODO(fgorski): Enable below when implemented: |
| 219 // EXPECT_FALSE(oauth2_servive_->RefreshTokenIsAvailable(account_id_1)); | 226 // EXPECT_FALSE(oauth2_servive_->RefreshTokenIsAvailable(account_id_1)); |
| 220 // EXPECT_FALSE(oauth2_servive_->RefreshTokenIsAvailable(account_id_2)); | 227 // EXPECT_FALSE(oauth2_servive_->RefreshTokenIsAvailable(account_id_2)); |
| 221 | 228 |
| 222 oauth2_service_.UpdateCredentials(account_id_1, refresh_token_1); | 229 oauth2_service_delegate_->UpdateCredentials(account_id_1, refresh_token_1); |
| 223 oauth2_service_.UpdateCredentials(account_id_2, refresh_token_2); | 230 oauth2_service_delegate_->UpdateCredentials(account_id_2, refresh_token_2); |
| 224 EXPECT_EQ(2, start_batch_changes_); | 231 EXPECT_EQ(2, start_batch_changes_); |
| 225 EXPECT_EQ(2, end_batch_changes_); | 232 EXPECT_EQ(2, end_batch_changes_); |
| 226 | 233 |
| 227 // TODO(fgorski): Enable below when implemented: | 234 // TODO(fgorski): Enable below when implemented: |
| 228 // EXPECT_TRUE(oauth2_servive_->RefreshTokenIsAvailable(account_id_1)); | 235 // EXPECT_TRUE(oauth2_servive_->RefreshTokenIsAvailable(account_id_1)); |
| 229 // EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable(account_id_2)); | 236 EXPECT_TRUE(oauth2_service_delegate_->RefreshTokenIsAvailable(account_id_2)); |
| 230 | 237 |
| 231 ResetObserverCounts(); | 238 ResetObserverCounts(); |
| 232 oauth2_service_.RevokeCredentials(account_id_1); | 239 oauth2_service_delegate_->RevokeCredentials(account_id_1); |
| 233 EXPECT_EQ(1, start_batch_changes_); | 240 EXPECT_EQ(1, start_batch_changes_); |
| 234 EXPECT_EQ(1, end_batch_changes_); | 241 EXPECT_EQ(1, end_batch_changes_); |
| 235 ExpectOneTokenRevokedNotification(); | 242 ExpectOneTokenRevokedNotification(); |
| 236 | 243 |
| 237 // TODO(fgorski): Enable below when implemented: | 244 // TODO(fgorski): Enable below when implemented: |
| 238 // EXPECT_FALSE(oauth2_servive_->RefreshTokenIsAvailable(account_id_1)); | 245 // EXPECT_FALSE(oauth2_servive_->RefreshTokenIsAvailable(account_id_1)); |
| 239 // EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable(account_id_2)); | 246 EXPECT_TRUE(oauth2_service_delegate_->RefreshTokenIsAvailable(account_id_2)); |
| 240 | 247 |
| 241 oauth2_service_.RevokeAllCredentials(); | 248 oauth2_service_delegate_->RevokeAllCredentials(); |
| 242 EXPECT_EQ(0, token_available_count_); | 249 EXPECT_EQ(0, token_available_count_); |
| 243 EXPECT_EQ(1, token_revoked_count_); | 250 EXPECT_EQ(1, token_revoked_count_); |
| 244 EXPECT_EQ(0, tokens_loaded_count_); | 251 EXPECT_EQ(0, tokens_loaded_count_); |
| 245 EXPECT_EQ(1, start_batch_changes_); | 252 EXPECT_EQ(1, start_batch_changes_); |
| 246 EXPECT_EQ(1, end_batch_changes_); | 253 EXPECT_EQ(1, end_batch_changes_); |
| 247 ResetObserverCounts(); | 254 ResetObserverCounts(); |
| 248 } | 255 } |
| 249 | 256 |
| 250 TEST_F(MutableProfileOAuth2TokenServiceTest, PersistenceLoadCredentials) { | 257 TEST_F(MutableProfileOAuth2TokenServiceDelegateTest, |
| 258 PersistenceLoadCredentials) { |
| 251 // Ensure DB is clean. | 259 // Ensure DB is clean. |
| 252 oauth2_service_.RevokeAllCredentials(); | 260 oauth2_service_delegate_->RevokeAllCredentials(); |
| 253 ResetObserverCounts(); | 261 ResetObserverCounts(); |
| 254 // Perform a load from an empty DB. | 262 // Perform a load from an empty DB. |
| 255 oauth2_service_.LoadCredentials("account_id"); | 263 oauth2_service_delegate_->LoadCredentials("account_id"); |
| 256 base::RunLoop().RunUntilIdle(); | 264 base::RunLoop().RunUntilIdle(); |
| 257 EXPECT_EQ(1, start_batch_changes_); | 265 EXPECT_EQ(1, start_batch_changes_); |
| 258 EXPECT_EQ(1, end_batch_changes_); | 266 EXPECT_EQ(1, end_batch_changes_); |
| 259 ExpectOneTokensLoadedNotification(); | 267 ExpectOneTokensLoadedNotification(); |
| 260 // LoadCredentials() guarantees that the account given to it as argument | 268 // LoadCredentials() guarantees that the account given to it as argument |
| 261 // is in the refresh_token map. | 269 // is in the refresh_token map. |
| 262 EXPECT_EQ(1U, oauth2_service_.refresh_tokens().size()); | 270 EXPECT_EQ(1U, oauth2_service_delegate_->refresh_tokens_.size()); |
| 263 EXPECT_TRUE( | 271 EXPECT_TRUE(oauth2_service_delegate_->refresh_tokens_["account_id"] |
| 264 oauth2_service_.refresh_tokens()["account_id"]->refresh_token().empty()); | 272 ->refresh_token() |
| 273 .empty()); |
| 265 // Setup a DB with tokens that don't require upgrade and clear memory. | 274 // Setup a DB with tokens that don't require upgrade and clear memory. |
| 266 oauth2_service_.UpdateCredentials("account_id", "refresh_token"); | 275 oauth2_service_delegate_->UpdateCredentials("account_id", "refresh_token"); |
| 267 oauth2_service_.UpdateCredentials("account_id2", "refresh_token2"); | 276 oauth2_service_delegate_->UpdateCredentials("account_id2", "refresh_token2"); |
| 268 oauth2_service_.refresh_tokens().clear(); | 277 oauth2_service_delegate_->refresh_tokens_.clear(); |
| 269 EXPECT_EQ(2, start_batch_changes_); | 278 EXPECT_EQ(2, start_batch_changes_); |
| 270 EXPECT_EQ(2, end_batch_changes_); | 279 EXPECT_EQ(2, end_batch_changes_); |
| 271 ResetObserverCounts(); | 280 ResetObserverCounts(); |
| 272 | 281 |
| 273 oauth2_service_.LoadCredentials("account_id"); | 282 oauth2_service_delegate_->LoadCredentials("account_id"); |
| 274 base::RunLoop().RunUntilIdle(); | 283 base::RunLoop().RunUntilIdle(); |
| 275 EXPECT_EQ(2, token_available_count_); | 284 EXPECT_EQ(2, token_available_count_); |
| 276 EXPECT_EQ(0, token_revoked_count_); | 285 EXPECT_EQ(0, token_revoked_count_); |
| 277 EXPECT_EQ(1, tokens_loaded_count_); | 286 EXPECT_EQ(1, tokens_loaded_count_); |
| 278 EXPECT_EQ(1, start_batch_changes_); | 287 EXPECT_EQ(1, start_batch_changes_); |
| 279 EXPECT_EQ(1, end_batch_changes_); | 288 EXPECT_EQ(1, end_batch_changes_); |
| 280 ResetObserverCounts(); | 289 ResetObserverCounts(); |
| 281 | 290 |
| 282 // TODO(fgorski): Enable below when implemented: | 291 // TODO(fgorski): Enable below when implemented: |
| 283 // EXPECT_TRUE(oauth2_servive_->RefreshTokenIsAvailable("account_id")); | 292 // EXPECT_TRUE(oauth2_servive_->RefreshTokenIsAvailable("account_id")); |
| 284 // EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("account_id2")); | 293 EXPECT_TRUE(oauth2_service_delegate_->RefreshTokenIsAvailable("account_id2")); |
| 285 | 294 |
| 286 oauth2_service_.RevokeAllCredentials(); | 295 oauth2_service_delegate_->RevokeAllCredentials(); |
| 287 EXPECT_EQ(0, token_available_count_); | 296 EXPECT_EQ(0, token_available_count_); |
| 288 EXPECT_EQ(2, token_revoked_count_); | 297 EXPECT_EQ(2, token_revoked_count_); |
| 289 EXPECT_EQ(0, tokens_loaded_count_); | 298 EXPECT_EQ(0, tokens_loaded_count_); |
| 290 EXPECT_EQ(1, start_batch_changes_); | 299 EXPECT_EQ(1, start_batch_changes_); |
| 291 EXPECT_EQ(1, end_batch_changes_); | 300 EXPECT_EQ(1, end_batch_changes_); |
| 292 ResetObserverCounts(); | 301 ResetObserverCounts(); |
| 293 } | 302 } |
| 294 | 303 |
| 295 TEST_F(MutableProfileOAuth2TokenServiceTest, PersistanceNotifications) { | 304 TEST_F(MutableProfileOAuth2TokenServiceDelegateTest, PersistanceNotifications) { |
| 296 EXPECT_EQ(0, oauth2_service_.cache_size_for_testing()); | 305 oauth2_service_delegate_->UpdateCredentials("account_id", "refresh_token"); |
| 297 oauth2_service_.UpdateCredentials("account_id", "refresh_token"); | |
| 298 ExpectOneTokenAvailableNotification(); | 306 ExpectOneTokenAvailableNotification(); |
| 299 | 307 |
| 300 oauth2_service_.UpdateCredentials("account_id", "refresh_token"); | 308 oauth2_service_delegate_->UpdateCredentials("account_id", "refresh_token"); |
| 301 ExpectNoNotifications(); | 309 ExpectNoNotifications(); |
| 302 | 310 |
| 303 oauth2_service_.UpdateCredentials("account_id", "refresh_token2"); | 311 oauth2_service_delegate_->UpdateCredentials("account_id", "refresh_token2"); |
| 304 ExpectOneTokenAvailableNotification(); | 312 ExpectOneTokenAvailableNotification(); |
| 305 | 313 |
| 306 oauth2_service_.RevokeCredentials("account_id"); | 314 oauth2_service_delegate_->RevokeCredentials("account_id"); |
| 307 ExpectOneTokenRevokedNotification(); | 315 ExpectOneTokenRevokedNotification(); |
| 308 | 316 |
| 309 oauth2_service_.UpdateCredentials("account_id", "refresh_token2"); | 317 oauth2_service_delegate_->UpdateCredentials("account_id", "refresh_token2"); |
| 310 ExpectOneTokenAvailableNotification(); | 318 ExpectOneTokenAvailableNotification(); |
| 311 | 319 |
| 312 oauth2_service_.RevokeAllCredentials(); | 320 oauth2_service_delegate_->RevokeAllCredentials(); |
| 313 ResetObserverCounts(); | 321 ResetObserverCounts(); |
| 314 } | 322 } |
| 315 | 323 |
| 316 TEST_F(MutableProfileOAuth2TokenServiceTest, GetAccounts) { | 324 TEST_F(MutableProfileOAuth2TokenServiceDelegateTest, GetAccounts) { |
| 317 EXPECT_TRUE(oauth2_service_.GetAccounts().empty()); | 325 EXPECT_TRUE(oauth2_service_delegate_->GetAccounts().empty()); |
| 318 oauth2_service_.UpdateCredentials("account_id1", "refresh_token1"); | 326 oauth2_service_delegate_->UpdateCredentials("account_id1", "refresh_token1"); |
| 319 oauth2_service_.UpdateCredentials("account_id2", "refresh_token2"); | 327 oauth2_service_delegate_->UpdateCredentials("account_id2", "refresh_token2"); |
| 320 std::vector<std::string> accounts = oauth2_service_.GetAccounts(); | 328 std::vector<std::string> accounts = oauth2_service_delegate_->GetAccounts(); |
| 321 EXPECT_EQ(2u, accounts.size()); | 329 EXPECT_EQ(2u, accounts.size()); |
| 322 EXPECT_EQ(1, count(accounts.begin(), accounts.end(), "account_id1")); | 330 EXPECT_EQ(1, count(accounts.begin(), accounts.end(), "account_id1")); |
| 323 EXPECT_EQ(1, count(accounts.begin(), accounts.end(), "account_id2")); | 331 EXPECT_EQ(1, count(accounts.begin(), accounts.end(), "account_id2")); |
| 324 oauth2_service_.RevokeCredentials("account_id2"); | 332 oauth2_service_delegate_->RevokeCredentials("account_id2"); |
| 325 accounts = oauth2_service_.GetAccounts(); | 333 accounts = oauth2_service_delegate_->GetAccounts(); |
| 326 EXPECT_EQ(1u, oauth2_service_.GetAccounts().size()); | 334 EXPECT_EQ(1u, oauth2_service_delegate_->GetAccounts().size()); |
| 327 EXPECT_EQ(1, count(accounts.begin(), accounts.end(), "account_id1")); | 335 EXPECT_EQ(1, count(accounts.begin(), accounts.end(), "account_id1")); |
| 328 } | 336 } |
| 329 | 337 |
| 330 TEST_F(MutableProfileOAuth2TokenServiceTest, TokenServiceUpdateClearsCache) { | 338 TEST_F(MutableProfileOAuth2TokenServiceDelegateTest, FetchPersistentError) { |
| 331 EXPECT_EQ(0, oauth2_service_.cache_size_for_testing()); | 339 oauth2_service_delegate_->UpdateCredentials(kEmail, "refreshToken"); |
| 332 std::set<std::string> scope_list; | |
| 333 scope_list.insert("scope"); | |
| 334 oauth2_service_.UpdateCredentials(kEmail, "refreshToken"); | |
| 335 ExpectOneTokenAvailableNotification(); | |
| 336 factory_.SetFakeResponse(GaiaUrls::GetInstance()->oauth2_token_url(), | |
| 337 GetValidTokenResponse("token", 3600), | |
| 338 net::HTTP_OK, | |
| 339 net::URLRequestStatus::SUCCESS); | |
| 340 | |
| 341 scoped_ptr<OAuth2TokenService::Request> request( | |
| 342 oauth2_service_.StartRequest(kEmail, scope_list, &consumer_)); | |
| 343 base::RunLoop().RunUntilIdle(); | |
| 344 EXPECT_EQ(1, consumer_.number_of_successful_tokens_); | |
| 345 EXPECT_EQ(0, consumer_.number_of_errors_); | |
| 346 EXPECT_EQ("token", consumer_.last_token_); | |
| 347 EXPECT_EQ(1, oauth2_service_.cache_size_for_testing()); | |
| 348 | |
| 349 // Signs out and signs in | |
| 350 oauth2_service_.RevokeCredentials(kEmail); | |
| 351 ExpectOneTokenRevokedNotification(); | |
| 352 | |
| 353 EXPECT_EQ(0, oauth2_service_.cache_size_for_testing()); | |
| 354 oauth2_service_.UpdateCredentials(kEmail, "refreshToken"); | |
| 355 ExpectOneTokenAvailableNotification(); | |
| 356 factory_.SetFakeResponse(GaiaUrls::GetInstance()->oauth2_token_url(), | |
| 357 GetValidTokenResponse("another token", 3600), | |
| 358 net::HTTP_OK, | |
| 359 net::URLRequestStatus::SUCCESS); | |
| 360 | |
| 361 request = oauth2_service_.StartRequest(kEmail, scope_list, &consumer_); | |
| 362 base::RunLoop().RunUntilIdle(); | |
| 363 EXPECT_EQ(2, consumer_.number_of_successful_tokens_); | |
| 364 EXPECT_EQ(0, consumer_.number_of_errors_); | |
| 365 EXPECT_EQ("another token", consumer_.last_token_); | |
| 366 EXPECT_EQ(1, oauth2_service_.cache_size_for_testing()); | |
| 367 } | |
| 368 | |
| 369 TEST_F(MutableProfileOAuth2TokenServiceTest, FetchTransientError) { | |
| 370 factory_.SetFakeResponse(GaiaUrls::GetInstance()->oauth2_token_url(), | |
| 371 "", | |
| 372 net::HTTP_FORBIDDEN, | |
| 373 net::URLRequestStatus::FAILED); | |
| 374 | |
| 375 EXPECT_EQ(0, oauth2_service_.cache_size_for_testing()); | |
| 376 std::set<std::string> scope_list; | |
| 377 scope_list.insert("scope"); | |
| 378 oauth2_service_.set_max_authorization_token_fetch_retries_for_testing(0); | |
| 379 oauth2_service_.UpdateCredentials(kEmail, "refreshToken"); | |
| 380 ExpectOneTokenAvailableNotification(); | |
| 381 | |
| 382 scoped_ptr<OAuth2TokenService::Request> request( | |
| 383 oauth2_service_.StartRequest(kEmail, scope_list, &consumer_)); | |
| 384 base::RunLoop().RunUntilIdle(); | |
| 385 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), | |
| 386 signin_error_controller_.auth_error()); | |
| 387 } | |
| 388 | |
| 389 TEST_F(MutableProfileOAuth2TokenServiceTest, FetchPersistentError) { | |
| 390 oauth2_service_.UpdateCredentials(kEmail, "refreshToken"); | |
| 391 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), | 340 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), |
| 392 signin_error_controller_.auth_error()); | 341 signin_error_controller_.auth_error()); |
| 393 | 342 |
| 394 GoogleServiceAuthError authfail(GoogleServiceAuthError::ACCOUNT_DELETED); | 343 GoogleServiceAuthError authfail(GoogleServiceAuthError::ACCOUNT_DELETED); |
| 395 oauth2_service_.UpdateAuthError(kEmail, authfail); | 344 oauth2_service_delegate_->UpdateAuthError(kEmail, authfail); |
| 396 EXPECT_NE(GoogleServiceAuthError::AuthErrorNone(), | 345 EXPECT_NE(GoogleServiceAuthError::AuthErrorNone(), |
| 397 signin_error_controller_.auth_error()); | 346 signin_error_controller_.auth_error()); |
| 398 | 347 |
| 399 // Create a "success" fetch we don't expect to get called. | 348 // Create a "success" fetch we don't expect to get called. |
| 400 factory_.SetFakeResponse(GaiaUrls::GetInstance()->oauth2_token_url(), | 349 factory_.SetFakeResponse(GaiaUrls::GetInstance()->oauth2_token_url(), |
| 401 GetValidTokenResponse("token", 3600), | 350 GetValidTokenResponse("token", 3600), net::HTTP_OK, |
| 402 net::HTTP_OK, | |
| 403 net::URLRequestStatus::SUCCESS); | 351 net::URLRequestStatus::SUCCESS); |
| 404 | 352 |
| 405 EXPECT_EQ(0, access_token_success_count_); | 353 EXPECT_EQ(0, access_token_success_count_); |
| 406 EXPECT_EQ(0, access_token_failure_count_); | 354 EXPECT_EQ(0, access_token_failure_count_); |
| 407 std::vector<std::string> scope_list; | 355 std::vector<std::string> scope_list; |
| 408 scope_list.push_back("scope"); | 356 scope_list.push_back("scope"); |
| 409 scoped_ptr<OAuth2AccessTokenFetcher> fetcher( | 357 scoped_ptr<OAuth2AccessTokenFetcher> fetcher( |
| 410 oauth2_service_.CreateAccessTokenFetcher( | 358 oauth2_service_delegate_->CreateAccessTokenFetcher( |
| 411 kEmail, oauth2_service_.GetRequestContext(), this)); | 359 kEmail, oauth2_service_delegate_->GetRequestContext(), this)); |
| 412 fetcher->Start("foo", "bar", scope_list); | 360 fetcher->Start("foo", "bar", scope_list); |
| 413 base::RunLoop().RunUntilIdle(); | 361 base::RunLoop().RunUntilIdle(); |
| 414 EXPECT_EQ(0, access_token_success_count_); | 362 EXPECT_EQ(0, access_token_success_count_); |
| 415 EXPECT_EQ(1, access_token_failure_count_); | 363 EXPECT_EQ(1, access_token_failure_count_); |
| 416 } | 364 } |
| 417 | 365 |
| 418 TEST_F(MutableProfileOAuth2TokenServiceTest, RetryBackoff) { | 366 TEST_F(MutableProfileOAuth2TokenServiceDelegateTest, RetryBackoff) { |
| 419 oauth2_service_.UpdateCredentials(kEmail, "refreshToken"); | 367 oauth2_service_delegate_->UpdateCredentials(kEmail, "refreshToken"); |
| 420 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), | 368 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), |
| 421 signin_error_controller_.auth_error()); | 369 signin_error_controller_.auth_error()); |
| 422 | 370 |
| 423 GoogleServiceAuthError authfail(GoogleServiceAuthError::SERVICE_UNAVAILABLE); | 371 GoogleServiceAuthError authfail(GoogleServiceAuthError::SERVICE_UNAVAILABLE); |
| 424 oauth2_service_.UpdateAuthError(kEmail, authfail); | 372 oauth2_service_delegate_->UpdateAuthError(kEmail, authfail); |
| 425 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), | 373 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), |
| 426 signin_error_controller_.auth_error()); | 374 signin_error_controller_.auth_error()); |
| 427 | 375 |
| 428 // Create a "success" fetch we don't expect to get called just yet. | 376 // Create a "success" fetch we don't expect to get called just yet. |
| 429 factory_.SetFakeResponse(GaiaUrls::GetInstance()->oauth2_token_url(), | 377 factory_.SetFakeResponse(GaiaUrls::GetInstance()->oauth2_token_url(), |
| 430 GetValidTokenResponse("token", 3600), | 378 GetValidTokenResponse("token", 3600), net::HTTP_OK, |
| 431 net::HTTP_OK, | |
| 432 net::URLRequestStatus::SUCCESS); | 379 net::URLRequestStatus::SUCCESS); |
| 433 | 380 |
| 434 // Transient error will repeat until backoff period expires. | 381 // Transient error will repeat until backoff period expires. |
| 435 EXPECT_EQ(0, access_token_success_count_); | 382 EXPECT_EQ(0, access_token_success_count_); |
| 436 EXPECT_EQ(0, access_token_failure_count_); | 383 EXPECT_EQ(0, access_token_failure_count_); |
| 437 std::vector<std::string> scope_list; | 384 std::vector<std::string> scope_list; |
| 438 scope_list.push_back("scope"); | 385 scope_list.push_back("scope"); |
| 439 scoped_ptr<OAuth2AccessTokenFetcher> fetcher1( | 386 scoped_ptr<OAuth2AccessTokenFetcher> fetcher1( |
| 440 oauth2_service_.CreateAccessTokenFetcher( | 387 oauth2_service_delegate_->CreateAccessTokenFetcher( |
| 441 kEmail, oauth2_service_.GetRequestContext(), this)); | 388 kEmail, oauth2_service_delegate_->GetRequestContext(), this)); |
| 442 fetcher1->Start("foo", "bar", scope_list); | 389 fetcher1->Start("foo", "bar", scope_list); |
| 443 base::RunLoop().RunUntilIdle(); | 390 base::RunLoop().RunUntilIdle(); |
| 444 EXPECT_EQ(0, access_token_success_count_); | 391 EXPECT_EQ(0, access_token_success_count_); |
| 445 EXPECT_EQ(1, access_token_failure_count_); | 392 EXPECT_EQ(1, access_token_failure_count_); |
| 446 | 393 |
| 447 // Pretend that backoff has expired and try again. | 394 // Pretend that backoff has expired and try again. |
| 448 oauth2_service_.backoff_entry_.SetCustomReleaseTime(base::TimeTicks()); | 395 oauth2_service_delegate_->backoff_entry_.SetCustomReleaseTime( |
| 396 base::TimeTicks()); |
| 449 scoped_ptr<OAuth2AccessTokenFetcher> fetcher2( | 397 scoped_ptr<OAuth2AccessTokenFetcher> fetcher2( |
| 450 oauth2_service_.CreateAccessTokenFetcher( | 398 oauth2_service_delegate_->CreateAccessTokenFetcher( |
| 451 kEmail, oauth2_service_.GetRequestContext(), this)); | 399 kEmail, oauth2_service_delegate_->GetRequestContext(), this)); |
| 452 fetcher2->Start("foo", "bar", scope_list); | 400 fetcher2->Start("foo", "bar", scope_list); |
| 453 base::RunLoop().RunUntilIdle(); | 401 base::RunLoop().RunUntilIdle(); |
| 454 EXPECT_EQ(1, access_token_success_count_); | 402 EXPECT_EQ(1, access_token_success_count_); |
| 455 EXPECT_EQ(1, access_token_failure_count_); | 403 EXPECT_EQ(1, access_token_failure_count_); |
| 456 } | 404 } |
| 457 | 405 |
| 458 TEST_F(MutableProfileOAuth2TokenServiceTest, CanonicalizeAccountId) { | 406 TEST_F(MutableProfileOAuth2TokenServiceDelegateTest, CanonicalizeAccountId) { |
| 459 std::map<std::string, std::string> tokens; | 407 std::map<std::string, std::string> tokens; |
| 460 tokens["AccountId-user@gmail.com"] = "refresh_token"; | 408 tokens["AccountId-user@gmail.com"] = "refresh_token"; |
| 461 tokens["AccountId-Foo.Bar@gmail.com"] = "refresh_token"; | 409 tokens["AccountId-Foo.Bar@gmail.com"] = "refresh_token"; |
| 462 tokens["AccountId-12345"] = "refresh_token"; | 410 tokens["AccountId-12345"] = "refresh_token"; |
| 463 | 411 |
| 464 oauth2_service_.LoadAllCredentialsIntoMemory(tokens); | 412 oauth2_service_delegate_->LoadAllCredentialsIntoMemory(tokens); |
| 465 | 413 |
| 466 EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("user@gmail.com")); | 414 EXPECT_TRUE( |
| 467 EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("foobar@gmail.com")); | 415 oauth2_service_delegate_->RefreshTokenIsAvailable("user@gmail.com")); |
| 468 EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("12345")); | 416 EXPECT_TRUE( |
| 417 oauth2_service_delegate_->RefreshTokenIsAvailable("foobar@gmail.com")); |
| 418 EXPECT_TRUE(oauth2_service_delegate_->RefreshTokenIsAvailable("12345")); |
| 469 } | 419 } |
| 470 | 420 |
| 471 TEST_F(MutableProfileOAuth2TokenServiceTest, CanonAndNonCanonAccountId) { | 421 TEST_F(MutableProfileOAuth2TokenServiceDelegateTest, |
| 422 CanonAndNonCanonAccountId) { |
| 472 std::map<std::string, std::string> tokens; | 423 std::map<std::string, std::string> tokens; |
| 473 tokens["AccountId-Foo.Bar@gmail.com"] = "bad_token"; | 424 tokens["AccountId-Foo.Bar@gmail.com"] = "bad_token"; |
| 474 tokens["AccountId-foobar@gmail.com"] = "good_token"; | 425 tokens["AccountId-foobar@gmail.com"] = "good_token"; |
| 475 | 426 |
| 476 oauth2_service_.LoadAllCredentialsIntoMemory(tokens); | 427 oauth2_service_delegate_->LoadAllCredentialsIntoMemory(tokens); |
| 477 | 428 |
| 478 EXPECT_EQ(1u, oauth2_service_.GetAccounts().size()); | 429 EXPECT_EQ(1u, oauth2_service_delegate_->GetAccounts().size()); |
| 479 EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("foobar@gmail.com")); | 430 EXPECT_TRUE( |
| 480 EXPECT_STREQ("good_token", | 431 oauth2_service_delegate_->RefreshTokenIsAvailable("foobar@gmail.com")); |
| 481 oauth2_service_.GetRefreshToken("foobar@gmail.com").c_str()); | 432 EXPECT_STREQ( |
| 433 "good_token", |
| 434 oauth2_service_delegate_->GetRefreshToken("foobar@gmail.com").c_str()); |
| 482 } | 435 } |
| 436 |
| 437 TEST_F(MutableProfileOAuth2TokenServiceDelegateTest, ShutdownService) { |
| 438 EXPECT_TRUE(oauth2_service_delegate_->GetAccounts().empty()); |
| 439 oauth2_service_delegate_->UpdateCredentials("account_id1", "refresh_token1"); |
| 440 oauth2_service_delegate_->UpdateCredentials("account_id2", "refresh_token2"); |
| 441 std::vector<std::string> accounts = oauth2_service_delegate_->GetAccounts(); |
| 442 EXPECT_EQ(2u, accounts.size()); |
| 443 EXPECT_EQ(1, count(accounts.begin(), accounts.end(), "account_id1")); |
| 444 EXPECT_EQ(1, count(accounts.begin(), accounts.end(), "account_id2")); |
| 445 oauth2_service_delegate_->LoadCredentials("account_id1"); |
| 446 oauth2_service_delegate_->UpdateCredentials("account_id1", "refresh_token3"); |
| 447 oauth2_service_delegate_->Shutdown(); |
| 448 EXPECT_TRUE(oauth2_service_delegate_->server_revokes_.empty()); |
| 449 EXPECT_TRUE(oauth2_service_delegate_->refresh_tokens_.empty()); |
| 450 EXPECT_EQ(0, oauth2_service_delegate_->web_data_service_request_); |
| 451 } |
| OLD | NEW |