Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(269)

Side by Side Diff: chrome/browser/signin/mutable_profile_oauth2_token_service_delegate_unittest.cc

Issue 1143323005: Refactor AO2TS to make it easier to componentize. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address roger's comments Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_.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_->AddObserver(this);
59 } 58 }
60 59
61 void TearDown() override { 60 void TearDown() override {
62 oauth2_service_.RemoveObserver(this); 61 oauth2_service_->RemoveObserver(this);
63 oauth2_service_.Shutdown(); 62 oauth2_service_->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
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_;
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_->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(oauth2_service_->RefreshTokenIsAvailable(main_account_id));
170 EXPECT_EQ(1U, oauth2_service_.refresh_tokens().size()); 169 EXPECT_EQ(1U, oauth2_service_->refresh_tokens_.size());
171 EXPECT_EQ(main_refresh_token, 170 EXPECT_EQ(main_refresh_token,
172 oauth2_service_.refresh_tokens()[main_account_id]->refresh_token()); 171 oauth2_service_->refresh_tokens_[main_account_id]->refresh_token());
173 172
174 // Add an old legacy token to the DB, to ensure it will not overwrite existing 173 // Add an old legacy token to the DB, to ensure it will not overwrite existing
175 // credentials for main account. 174 // credentials for main account.
176 AddAuthTokenManually(GaiaConstants::kGaiaOAuth2LoginRefreshToken, 175 AddAuthTokenManually(GaiaConstants::kGaiaOAuth2LoginRefreshToken,
177 "secondOldRefreshToken"); 176 "secondOldRefreshToken");
178 // Add some other legacy token. (Expected to get discarded). 177 // Add some other legacy token. (Expected to get discarded).
179 AddAuthTokenManually(kLSOService, "lsoToken"); 178 AddAuthTokenManually(kLSOService, "lsoToken");
180 // Also add a token using PO2TS.UpdateCredentials and make sure upgrade does 179 // Also add a token using PO2TS.UpdateCredentials and make sure upgrade does
181 // not wipe it. 180 // not wipe it.
182 std::string other_account_id("other_account_id"); 181 std::string other_account_id("other_account_id");
183 std::string other_refresh_token("other_refresh_token"); 182 std::string other_refresh_token("other_refresh_token");
184 oauth2_service_.UpdateCredentials(other_account_id, other_refresh_token); 183 oauth2_service_->UpdateCredentials(other_account_id, other_refresh_token);
185 ResetObserverCounts(); 184 ResetObserverCounts();
186 185
187 // Force LoadCredentials. 186 // Force LoadCredentials.
188 oauth2_service_.LoadCredentials(main_account_id); 187 oauth2_service_->LoadCredentials(main_account_id);
189 base::RunLoop().RunUntilIdle(); 188 base::RunLoop().RunUntilIdle();
190 189
191 // Again legacy tokens get discarded, but since the main porfile account 190 // Again legacy tokens get discarded, but since the main porfile account
192 // token is present it is not overwritten. 191 // token is present it is not overwritten.
193 EXPECT_EQ(2, token_available_count_); 192 EXPECT_EQ(2, token_available_count_);
194 EXPECT_EQ(1, tokens_loaded_count_); 193 EXPECT_EQ(1, tokens_loaded_count_);
195 EXPECT_EQ(1, start_batch_changes_); 194 EXPECT_EQ(1, start_batch_changes_);
196 EXPECT_EQ(1, end_batch_changes_); 195 EXPECT_EQ(1, end_batch_changes_);
197 EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable(main_account_id)); 196 EXPECT_EQ(main_refresh_token,
197 oauth2_service_->GetRefreshToken(main_account_id));
198 EXPECT_TRUE(oauth2_service_->RefreshTokenIsAvailable(main_account_id));
198 // TODO(fgorski): cover both using RefreshTokenIsAvailable() and then get the 199 // TODO(fgorski): cover both using RefreshTokenIsAvailable() and then get the
199 // tokens using GetRefreshToken() 200 // tokens using GetRefreshToken()
200 EXPECT_EQ(2U, oauth2_service_.refresh_tokens().size()); 201 EXPECT_EQ(2U, oauth2_service_->refresh_tokens_.size());
201 EXPECT_EQ(main_refresh_token, 202 EXPECT_EQ(main_refresh_token,
202 oauth2_service_.refresh_tokens()[main_account_id]->refresh_token()); 203 oauth2_service_->refresh_tokens_[main_account_id]->refresh_token());
203 EXPECT_EQ( 204 EXPECT_EQ(
204 other_refresh_token, 205 other_refresh_token,
205 oauth2_service_.refresh_tokens()[other_account_id]->refresh_token()); 206 oauth2_service_->refresh_tokens_[other_account_id]->refresh_token());
206 207
207 oauth2_service_.RevokeAllCredentials(); 208 oauth2_service_->RevokeAllCredentials();
208 EXPECT_EQ(2, start_batch_changes_); 209 EXPECT_EQ(2, start_batch_changes_);
209 EXPECT_EQ(2, end_batch_changes_); 210 EXPECT_EQ(2, end_batch_changes_);
210 } 211 }
211 212
212 TEST_F(MutableProfileOAuth2TokenServiceTest, PersistenceRevokeCredentials) { 213 TEST_F(MutableProfileOAuth2TokenServiceDelegateTest,
214 PersistenceRevokeCredentials) {
213 std::string account_id_1 = "account_id_1"; 215 std::string account_id_1 = "account_id_1";
214 std::string refresh_token_1 = "refresh_token_1"; 216 std::string refresh_token_1 = "refresh_token_1";
215 std::string account_id_2 = "account_id_2"; 217 std::string account_id_2 = "account_id_2";
216 std::string refresh_token_2 = "refresh_token_2"; 218 std::string refresh_token_2 = "refresh_token_2";
217 219
218 // TODO(fgorski): Enable below when implemented: 220 // TODO(fgorski): Enable below when implemented:
219 // EXPECT_FALSE(oauth2_servive_->RefreshTokenIsAvailable(account_id_1)); 221 // EXPECT_FALSE(oauth2_servive_->RefreshTokenIsAvailable(account_id_1));
220 // EXPECT_FALSE(oauth2_servive_->RefreshTokenIsAvailable(account_id_2)); 222 // EXPECT_FALSE(oauth2_servive_->RefreshTokenIsAvailable(account_id_2));
221 223
222 oauth2_service_.UpdateCredentials(account_id_1, refresh_token_1); 224 oauth2_service_->UpdateCredentials(account_id_1, refresh_token_1);
223 oauth2_service_.UpdateCredentials(account_id_2, refresh_token_2); 225 oauth2_service_->UpdateCredentials(account_id_2, refresh_token_2);
224 EXPECT_EQ(2, start_batch_changes_); 226 EXPECT_EQ(2, start_batch_changes_);
225 EXPECT_EQ(2, end_batch_changes_); 227 EXPECT_EQ(2, end_batch_changes_);
226 228
227 // TODO(fgorski): Enable below when implemented: 229 // TODO(fgorski): Enable below when implemented:
228 // EXPECT_TRUE(oauth2_servive_->RefreshTokenIsAvailable(account_id_1)); 230 // EXPECT_TRUE(oauth2_servive_->RefreshTokenIsAvailable(account_id_1));
229 // EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable(account_id_2)); 231 EXPECT_TRUE(oauth2_service_->RefreshTokenIsAvailable(account_id_2));
230 232
231 ResetObserverCounts(); 233 ResetObserverCounts();
232 oauth2_service_.RevokeCredentials(account_id_1); 234 oauth2_service_->RevokeCredentials(account_id_1);
233 EXPECT_EQ(1, start_batch_changes_); 235 EXPECT_EQ(1, start_batch_changes_);
234 EXPECT_EQ(1, end_batch_changes_); 236 EXPECT_EQ(1, end_batch_changes_);
235 ExpectOneTokenRevokedNotification(); 237 ExpectOneTokenRevokedNotification();
236 238
237 // TODO(fgorski): Enable below when implemented: 239 // TODO(fgorski): Enable below when implemented:
238 // EXPECT_FALSE(oauth2_servive_->RefreshTokenIsAvailable(account_id_1)); 240 // EXPECT_FALSE(oauth2_servive_->RefreshTokenIsAvailable(account_id_1));
239 // EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable(account_id_2)); 241 EXPECT_TRUE(oauth2_service_->RefreshTokenIsAvailable(account_id_2));
240 242
241 oauth2_service_.RevokeAllCredentials(); 243 oauth2_service_->RevokeAllCredentials();
242 EXPECT_EQ(0, token_available_count_); 244 EXPECT_EQ(0, token_available_count_);
243 EXPECT_EQ(1, token_revoked_count_); 245 EXPECT_EQ(1, token_revoked_count_);
244 EXPECT_EQ(0, tokens_loaded_count_); 246 EXPECT_EQ(0, tokens_loaded_count_);
245 EXPECT_EQ(1, start_batch_changes_); 247 EXPECT_EQ(1, start_batch_changes_);
246 EXPECT_EQ(1, end_batch_changes_); 248 EXPECT_EQ(1, end_batch_changes_);
247 ResetObserverCounts(); 249 ResetObserverCounts();
248 } 250 }
249 251
250 TEST_F(MutableProfileOAuth2TokenServiceTest, PersistenceLoadCredentials) { 252 TEST_F(MutableProfileOAuth2TokenServiceDelegateTest,
253 PersistenceLoadCredentials) {
251 // Ensure DB is clean. 254 // Ensure DB is clean.
252 oauth2_service_.RevokeAllCredentials(); 255 oauth2_service_->RevokeAllCredentials();
253 ResetObserverCounts(); 256 ResetObserverCounts();
254 // Perform a load from an empty DB. 257 // Perform a load from an empty DB.
255 oauth2_service_.LoadCredentials("account_id"); 258 oauth2_service_->LoadCredentials("account_id");
256 base::RunLoop().RunUntilIdle(); 259 base::RunLoop().RunUntilIdle();
257 EXPECT_EQ(1, start_batch_changes_); 260 EXPECT_EQ(1, start_batch_changes_);
258 EXPECT_EQ(1, end_batch_changes_); 261 EXPECT_EQ(1, end_batch_changes_);
259 ExpectOneTokensLoadedNotification(); 262 ExpectOneTokensLoadedNotification();
260 // LoadCredentials() guarantees that the account given to it as argument 263 // LoadCredentials() guarantees that the account given to it as argument
261 // is in the refresh_token map. 264 // is in the refresh_token map.
262 EXPECT_EQ(1U, oauth2_service_.refresh_tokens().size()); 265 EXPECT_EQ(1U, oauth2_service_->refresh_tokens_.size());
263 EXPECT_TRUE( 266 EXPECT_TRUE(
264 oauth2_service_.refresh_tokens()["account_id"]->refresh_token().empty()); 267 oauth2_service_->refresh_tokens_["account_id"]->refresh_token().empty());
265 // Setup a DB with tokens that don't require upgrade and clear memory. 268 // Setup a DB with tokens that don't require upgrade and clear memory.
266 oauth2_service_.UpdateCredentials("account_id", "refresh_token"); 269 oauth2_service_->UpdateCredentials("account_id", "refresh_token");
267 oauth2_service_.UpdateCredentials("account_id2", "refresh_token2"); 270 oauth2_service_->UpdateCredentials("account_id2", "refresh_token2");
268 oauth2_service_.refresh_tokens().clear(); 271 oauth2_service_->refresh_tokens_.clear();
269 EXPECT_EQ(2, start_batch_changes_); 272 EXPECT_EQ(2, start_batch_changes_);
270 EXPECT_EQ(2, end_batch_changes_); 273 EXPECT_EQ(2, end_batch_changes_);
271 ResetObserverCounts(); 274 ResetObserverCounts();
272 275
273 oauth2_service_.LoadCredentials("account_id"); 276 oauth2_service_->LoadCredentials("account_id");
274 base::RunLoop().RunUntilIdle(); 277 base::RunLoop().RunUntilIdle();
275 EXPECT_EQ(2, token_available_count_); 278 EXPECT_EQ(2, token_available_count_);
276 EXPECT_EQ(0, token_revoked_count_); 279 EXPECT_EQ(0, token_revoked_count_);
277 EXPECT_EQ(1, tokens_loaded_count_); 280 EXPECT_EQ(1, tokens_loaded_count_);
278 EXPECT_EQ(1, start_batch_changes_); 281 EXPECT_EQ(1, start_batch_changes_);
279 EXPECT_EQ(1, end_batch_changes_); 282 EXPECT_EQ(1, end_batch_changes_);
280 ResetObserverCounts(); 283 ResetObserverCounts();
281 284
282 // TODO(fgorski): Enable below when implemented: 285 // TODO(fgorski): Enable below when implemented:
283 // EXPECT_TRUE(oauth2_servive_->RefreshTokenIsAvailable("account_id")); 286 // EXPECT_TRUE(oauth2_servive_->RefreshTokenIsAvailable("account_id"));
284 // EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("account_id2")); 287 EXPECT_TRUE(oauth2_service_->RefreshTokenIsAvailable("account_id2"));
285 288
286 oauth2_service_.RevokeAllCredentials(); 289 oauth2_service_->RevokeAllCredentials();
287 EXPECT_EQ(0, token_available_count_); 290 EXPECT_EQ(0, token_available_count_);
288 EXPECT_EQ(2, token_revoked_count_); 291 EXPECT_EQ(2, token_revoked_count_);
289 EXPECT_EQ(0, tokens_loaded_count_); 292 EXPECT_EQ(0, tokens_loaded_count_);
290 EXPECT_EQ(1, start_batch_changes_); 293 EXPECT_EQ(1, start_batch_changes_);
291 EXPECT_EQ(1, end_batch_changes_); 294 EXPECT_EQ(1, end_batch_changes_);
292 ResetObserverCounts(); 295 ResetObserverCounts();
293 } 296 }
294 297
295 TEST_F(MutableProfileOAuth2TokenServiceTest, PersistanceNotifications) { 298 TEST_F(MutableProfileOAuth2TokenServiceDelegateTest, PersistanceNotifications) {
296 EXPECT_EQ(0, oauth2_service_.cache_size_for_testing()); 299 oauth2_service_->UpdateCredentials("account_id", "refresh_token");
297 oauth2_service_.UpdateCredentials("account_id", "refresh_token");
298 ExpectOneTokenAvailableNotification(); 300 ExpectOneTokenAvailableNotification();
299 301
300 oauth2_service_.UpdateCredentials("account_id", "refresh_token"); 302 oauth2_service_->UpdateCredentials("account_id", "refresh_token");
301 ExpectNoNotifications(); 303 ExpectNoNotifications();
302 304
303 oauth2_service_.UpdateCredentials("account_id", "refresh_token2"); 305 oauth2_service_->UpdateCredentials("account_id", "refresh_token2");
304 ExpectOneTokenAvailableNotification(); 306 ExpectOneTokenAvailableNotification();
305 307
306 oauth2_service_.RevokeCredentials("account_id"); 308 oauth2_service_->RevokeCredentials("account_id");
307 ExpectOneTokenRevokedNotification(); 309 ExpectOneTokenRevokedNotification();
308 310
309 oauth2_service_.UpdateCredentials("account_id", "refresh_token2"); 311 oauth2_service_->UpdateCredentials("account_id", "refresh_token2");
310 ExpectOneTokenAvailableNotification(); 312 ExpectOneTokenAvailableNotification();
311 313
312 oauth2_service_.RevokeAllCredentials(); 314 oauth2_service_->RevokeAllCredentials();
313 ResetObserverCounts(); 315 ResetObserverCounts();
314 } 316 }
315 317
316 TEST_F(MutableProfileOAuth2TokenServiceTest, GetAccounts) { 318 TEST_F(MutableProfileOAuth2TokenServiceDelegateTest, GetAccounts) {
317 EXPECT_TRUE(oauth2_service_.GetAccounts().empty()); 319 EXPECT_TRUE(oauth2_service_->GetAccounts().empty());
318 oauth2_service_.UpdateCredentials("account_id1", "refresh_token1"); 320 oauth2_service_->UpdateCredentials("account_id1", "refresh_token1");
319 oauth2_service_.UpdateCredentials("account_id2", "refresh_token2"); 321 oauth2_service_->UpdateCredentials("account_id2", "refresh_token2");
320 std::vector<std::string> accounts = oauth2_service_.GetAccounts(); 322 std::vector<std::string> accounts = oauth2_service_->GetAccounts();
321 EXPECT_EQ(2u, accounts.size()); 323 EXPECT_EQ(2u, accounts.size());
322 EXPECT_EQ(1, count(accounts.begin(), accounts.end(), "account_id1")); 324 EXPECT_EQ(1, count(accounts.begin(), accounts.end(), "account_id1"));
323 EXPECT_EQ(1, count(accounts.begin(), accounts.end(), "account_id2")); 325 EXPECT_EQ(1, count(accounts.begin(), accounts.end(), "account_id2"));
324 oauth2_service_.RevokeCredentials("account_id2"); 326 oauth2_service_->RevokeCredentials("account_id2");
325 accounts = oauth2_service_.GetAccounts(); 327 accounts = oauth2_service_->GetAccounts();
326 EXPECT_EQ(1u, oauth2_service_.GetAccounts().size()); 328 EXPECT_EQ(1u, oauth2_service_->GetAccounts().size());
327 EXPECT_EQ(1, count(accounts.begin(), accounts.end(), "account_id1")); 329 EXPECT_EQ(1, count(accounts.begin(), accounts.end(), "account_id1"));
328 } 330 }
329 331
330 TEST_F(MutableProfileOAuth2TokenServiceTest, TokenServiceUpdateClearsCache) { 332 TEST_F(MutableProfileOAuth2TokenServiceDelegateTest, FetchPersistentError) {
331 EXPECT_EQ(0, oauth2_service_.cache_size_for_testing()); 333 oauth2_service_->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 }
Roger Tawa OOO till Jul 10th 2015/06/04 18:19:55 Why are the two tests above removed?
gogerald1 2015/06/25 14:06:18 The test "TokenServiceUpdateClearsCache" test the
388
389 TEST_F(MutableProfileOAuth2TokenServiceTest, FetchPersistentError) {
390 oauth2_service_.UpdateCredentials(kEmail, "refreshToken");
391 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), 334 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(),
392 signin_error_controller_.auth_error()); 335 signin_error_controller_.auth_error());
393 336
394 GoogleServiceAuthError authfail(GoogleServiceAuthError::ACCOUNT_DELETED); 337 GoogleServiceAuthError authfail(GoogleServiceAuthError::ACCOUNT_DELETED);
395 oauth2_service_.UpdateAuthError(kEmail, authfail); 338 oauth2_service_->UpdateAuthError(kEmail, authfail);
396 EXPECT_NE(GoogleServiceAuthError::AuthErrorNone(), 339 EXPECT_NE(GoogleServiceAuthError::AuthErrorNone(),
397 signin_error_controller_.auth_error()); 340 signin_error_controller_.auth_error());
398 341
399 // Create a "success" fetch we don't expect to get called. 342 // Create a "success" fetch we don't expect to get called.
400 factory_.SetFakeResponse(GaiaUrls::GetInstance()->oauth2_token_url(), 343 factory_.SetFakeResponse(GaiaUrls::GetInstance()->oauth2_token_url(),
401 GetValidTokenResponse("token", 3600), 344 GetValidTokenResponse("token", 3600), net::HTTP_OK,
402 net::HTTP_OK,
403 net::URLRequestStatus::SUCCESS); 345 net::URLRequestStatus::SUCCESS);
404 346
405 EXPECT_EQ(0, access_token_success_count_); 347 EXPECT_EQ(0, access_token_success_count_);
406 EXPECT_EQ(0, access_token_failure_count_); 348 EXPECT_EQ(0, access_token_failure_count_);
407 std::vector<std::string> scope_list; 349 std::vector<std::string> scope_list;
408 scope_list.push_back("scope"); 350 scope_list.push_back("scope");
409 scoped_ptr<OAuth2AccessTokenFetcher> fetcher( 351 scoped_ptr<OAuth2AccessTokenFetcher> fetcher(
410 oauth2_service_.CreateAccessTokenFetcher( 352 oauth2_service_->CreateAccessTokenFetcher(
411 kEmail, oauth2_service_.GetRequestContext(), this)); 353 kEmail, oauth2_service_->GetRequestContext(), this));
412 fetcher->Start("foo", "bar", scope_list); 354 fetcher->Start("foo", "bar", scope_list);
413 base::RunLoop().RunUntilIdle(); 355 base::RunLoop().RunUntilIdle();
414 EXPECT_EQ(0, access_token_success_count_); 356 EXPECT_EQ(0, access_token_success_count_);
415 EXPECT_EQ(1, access_token_failure_count_); 357 EXPECT_EQ(1, access_token_failure_count_);
416 } 358 }
417 359
418 TEST_F(MutableProfileOAuth2TokenServiceTest, RetryBackoff) { 360 TEST_F(MutableProfileOAuth2TokenServiceDelegateTest, RetryBackoff) {
419 oauth2_service_.UpdateCredentials(kEmail, "refreshToken"); 361 oauth2_service_->UpdateCredentials(kEmail, "refreshToken");
420 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), 362 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(),
421 signin_error_controller_.auth_error()); 363 signin_error_controller_.auth_error());
422 364
423 GoogleServiceAuthError authfail(GoogleServiceAuthError::SERVICE_UNAVAILABLE); 365 GoogleServiceAuthError authfail(GoogleServiceAuthError::SERVICE_UNAVAILABLE);
424 oauth2_service_.UpdateAuthError(kEmail, authfail); 366 oauth2_service_->UpdateAuthError(kEmail, authfail);
425 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), 367 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(),
426 signin_error_controller_.auth_error()); 368 signin_error_controller_.auth_error());
427 369
428 // Create a "success" fetch we don't expect to get called just yet. 370 // Create a "success" fetch we don't expect to get called just yet.
429 factory_.SetFakeResponse(GaiaUrls::GetInstance()->oauth2_token_url(), 371 factory_.SetFakeResponse(GaiaUrls::GetInstance()->oauth2_token_url(),
430 GetValidTokenResponse("token", 3600), 372 GetValidTokenResponse("token", 3600), net::HTTP_OK,
431 net::HTTP_OK,
432 net::URLRequestStatus::SUCCESS); 373 net::URLRequestStatus::SUCCESS);
433 374
434 // Transient error will repeat until backoff period expires. 375 // Transient error will repeat until backoff period expires.
435 EXPECT_EQ(0, access_token_success_count_); 376 EXPECT_EQ(0, access_token_success_count_);
436 EXPECT_EQ(0, access_token_failure_count_); 377 EXPECT_EQ(0, access_token_failure_count_);
437 std::vector<std::string> scope_list; 378 std::vector<std::string> scope_list;
438 scope_list.push_back("scope"); 379 scope_list.push_back("scope");
439 scoped_ptr<OAuth2AccessTokenFetcher> fetcher1( 380 scoped_ptr<OAuth2AccessTokenFetcher> fetcher1(
440 oauth2_service_.CreateAccessTokenFetcher( 381 oauth2_service_->CreateAccessTokenFetcher(
441 kEmail, oauth2_service_.GetRequestContext(), this)); 382 kEmail, oauth2_service_->GetRequestContext(), this));
442 fetcher1->Start("foo", "bar", scope_list); 383 fetcher1->Start("foo", "bar", scope_list);
443 base::RunLoop().RunUntilIdle(); 384 base::RunLoop().RunUntilIdle();
444 EXPECT_EQ(0, access_token_success_count_); 385 EXPECT_EQ(0, access_token_success_count_);
445 EXPECT_EQ(1, access_token_failure_count_); 386 EXPECT_EQ(1, access_token_failure_count_);
446 387
447 // Pretend that backoff has expired and try again. 388 // Pretend that backoff has expired and try again.
448 oauth2_service_.backoff_entry_.SetCustomReleaseTime(base::TimeTicks()); 389 oauth2_service_->backoff_entry_.SetCustomReleaseTime(base::TimeTicks());
449 scoped_ptr<OAuth2AccessTokenFetcher> fetcher2( 390 scoped_ptr<OAuth2AccessTokenFetcher> fetcher2(
450 oauth2_service_.CreateAccessTokenFetcher( 391 oauth2_service_->CreateAccessTokenFetcher(
451 kEmail, oauth2_service_.GetRequestContext(), this)); 392 kEmail, oauth2_service_->GetRequestContext(), this));
452 fetcher2->Start("foo", "bar", scope_list); 393 fetcher2->Start("foo", "bar", scope_list);
453 base::RunLoop().RunUntilIdle(); 394 base::RunLoop().RunUntilIdle();
454 EXPECT_EQ(1, access_token_success_count_); 395 EXPECT_EQ(1, access_token_success_count_);
455 EXPECT_EQ(1, access_token_failure_count_); 396 EXPECT_EQ(1, access_token_failure_count_);
456 } 397 }
457 398
458 TEST_F(MutableProfileOAuth2TokenServiceTest, CanonicalizeAccountId) { 399 TEST_F(MutableProfileOAuth2TokenServiceDelegateTest, CanonicalizeAccountId) {
459 std::map<std::string, std::string> tokens; 400 std::map<std::string, std::string> tokens;
460 tokens["AccountId-user@gmail.com"] = "refresh_token"; 401 tokens["AccountId-user@gmail.com"] = "refresh_token";
461 tokens["AccountId-Foo.Bar@gmail.com"] = "refresh_token"; 402 tokens["AccountId-Foo.Bar@gmail.com"] = "refresh_token";
462 tokens["AccountId-12345"] = "refresh_token"; 403 tokens["AccountId-12345"] = "refresh_token";
463 404
464 oauth2_service_.LoadAllCredentialsIntoMemory(tokens); 405 oauth2_service_->LoadAllCredentialsIntoMemory(tokens);
465 406
466 EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("user@gmail.com")); 407 EXPECT_TRUE(!oauth2_service_->GetRefreshToken("user@gmail.com").empty());
467 EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("foobar@gmail.com")); 408 EXPECT_TRUE(!oauth2_service_->GetRefreshToken("foobar@gmail.com").empty());
468 EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("12345")); 409 EXPECT_TRUE(!oauth2_service_->GetRefreshToken("12345").empty());
469 } 410 }
411
412 TEST_F(MutableProfileOAuth2TokenServiceDelegateTest, ShutdownService) {
413 EXPECT_TRUE(oauth2_service_->GetAccounts().empty());
414 oauth2_service_->UpdateCredentials("account_id1", "refresh_token1");
415 oauth2_service_->UpdateCredentials("account_id2", "refresh_token2");
416 std::vector<std::string> accounts = oauth2_service_->GetAccounts();
417 EXPECT_EQ(2u, accounts.size());
418 EXPECT_EQ(1, count(accounts.begin(), accounts.end(), "account_id1"));
419 EXPECT_EQ(1, count(accounts.begin(), accounts.end(), "account_id2"));
420 oauth2_service_->LoadCredentials("account_id1");
421 oauth2_service_->UpdateCredentials("account_id1", "refresh_token3");
422 oauth2_service_->Shutdown();
423 EXPECT_TRUE(oauth2_service_->server_revokes_.empty());
424 EXPECT_TRUE(oauth2_service_->refresh_tokens_.empty());
425 EXPECT_EQ(0, oauth2_service_->web_data_service_request_);
426 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698