Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / google_apis / gaia / oauth2_token_service_unittest.cc
blob924ae4cd0ee0a5d4f7195b1fefa241c7fad130e6
1 // Copyright 2013 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.
5 #include <string>
7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h"
9 #include "google_apis/gaia/fake_oauth2_token_service_delegate.h"
10 #include "google_apis/gaia/gaia_constants.h"
11 #include "google_apis/gaia/google_service_auth_error.h"
12 #include "google_apis/gaia/oauth2_access_token_consumer.h"
13 #include "google_apis/gaia/oauth2_access_token_fetcher_impl.h"
14 #include "google_apis/gaia/oauth2_token_service.h"
15 #include "google_apis/gaia/oauth2_token_service_test_util.h"
16 #include "net/http/http_status_code.h"
17 #include "net/url_request/test_url_fetcher_factory.h"
18 #include "net/url_request/url_fetcher_delegate.h"
19 #include "net/url_request/url_request_test_util.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 // A testing consumer that retries on error.
23 class RetryingTestingOAuth2TokenServiceConsumer
24 : public TestingOAuth2TokenServiceConsumer {
25 public:
26 RetryingTestingOAuth2TokenServiceConsumer(
27 OAuth2TokenService* oauth2_service,
28 const std::string& account_id)
29 : oauth2_service_(oauth2_service),
30 account_id_(account_id) {}
31 ~RetryingTestingOAuth2TokenServiceConsumer() override {}
33 void OnGetTokenFailure(const OAuth2TokenService::Request* request,
34 const GoogleServiceAuthError& error) override {
35 TestingOAuth2TokenServiceConsumer::OnGetTokenFailure(request, error);
36 request_.reset(oauth2_service_->StartRequest(
37 account_id_, OAuth2TokenService::ScopeSet(), this).release());
40 OAuth2TokenService* oauth2_service_;
41 std::string account_id_;
42 scoped_ptr<OAuth2TokenService::Request> request_;
45 class TestOAuth2TokenService : public OAuth2TokenService {
46 public:
47 explicit TestOAuth2TokenService(FakeOAuth2TokenServiceDelegate* delegate)
48 : OAuth2TokenService(delegate) {}
50 void CancelAllRequestsForTest() { CancelAllRequests(); }
52 void CancelRequestsForAccountForTest(const std::string& account_id) {
53 CancelRequestsForAccount(account_id);
56 FakeOAuth2TokenServiceDelegate* GetFakeOAuth2TokenServiceDelegate() {
57 return static_cast<FakeOAuth2TokenServiceDelegate*>(GetDelegate());
61 class OAuth2TokenServiceTest : public testing::Test {
62 public:
63 void SetUp() override {
64 oauth2_service_.reset(new TestOAuth2TokenService(
65 new FakeOAuth2TokenServiceDelegate(new net::TestURLRequestContextGetter(
66 message_loop_.task_runner()))));
67 account_id_ = "test_user@gmail.com";
70 void TearDown() override {
71 // Makes sure that all the clean up tasks are run.
72 base::RunLoop().RunUntilIdle();
75 protected:
76 base::MessageLoopForIO message_loop_; // net:: stuff needs IO message loop.
77 net::TestURLFetcherFactory factory_;
78 scoped_ptr<TestOAuth2TokenService> oauth2_service_;
79 std::string account_id_;
80 TestingOAuth2TokenServiceConsumer consumer_;
83 TEST_F(OAuth2TokenServiceTest, NoOAuth2RefreshToken) {
84 scoped_ptr<OAuth2TokenService::Request> request(
85 oauth2_service_->StartRequest(account_id_, OAuth2TokenService::ScopeSet(),
86 &consumer_));
87 base::RunLoop().RunUntilIdle();
89 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
90 EXPECT_EQ(1, consumer_.number_of_errors_);
93 TEST_F(OAuth2TokenServiceTest, FailureShouldNotRetry) {
94 oauth2_service_->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials(
95 account_id_, "refreshToken");
96 scoped_ptr<OAuth2TokenService::Request> request(
97 oauth2_service_->StartRequest(account_id_,
98 OAuth2TokenService::ScopeSet(),
99 &consumer_));
100 base::RunLoop().RunUntilIdle();
102 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
103 EXPECT_EQ(0, consumer_.number_of_errors_);
104 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
105 ASSERT_TRUE(fetcher);
106 fetcher->set_response_code(net::HTTP_UNAUTHORIZED);
107 fetcher->SetResponseString(std::string());
108 fetcher->delegate()->OnURLFetchComplete(fetcher);
109 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
110 EXPECT_EQ(1, consumer_.number_of_errors_);
111 EXPECT_EQ(fetcher, factory_.GetFetcherByID(0));
114 TEST_F(OAuth2TokenServiceTest, SuccessWithoutCaching) {
115 oauth2_service_->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials(
116 account_id_, "refreshToken");
117 scoped_ptr<OAuth2TokenService::Request> request(
118 oauth2_service_->StartRequest(account_id_,
119 OAuth2TokenService::ScopeSet(),
120 &consumer_));
121 base::RunLoop().RunUntilIdle();
123 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
124 EXPECT_EQ(0, consumer_.number_of_errors_);
125 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
126 ASSERT_TRUE(fetcher);
127 fetcher->set_response_code(net::HTTP_OK);
128 fetcher->SetResponseString(GetValidTokenResponse("token", 3600));
129 fetcher->delegate()->OnURLFetchComplete(fetcher);
130 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
131 EXPECT_EQ(0, consumer_.number_of_errors_);
132 EXPECT_EQ("token", consumer_.last_token_);
135 TEST_F(OAuth2TokenServiceTest, SuccessWithCaching) {
136 OAuth2TokenService::ScopeSet scopes1;
137 scopes1.insert("s1");
138 scopes1.insert("s2");
139 OAuth2TokenService::ScopeSet scopes1_same;
140 scopes1_same.insert("s2");
141 scopes1_same.insert("s1");
142 OAuth2TokenService::ScopeSet scopes2;
143 scopes2.insert("s3");
145 oauth2_service_->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials(
146 account_id_, "refreshToken");
148 // First request.
149 scoped_ptr<OAuth2TokenService::Request> request(
150 oauth2_service_->StartRequest(account_id_, scopes1, &consumer_));
151 base::RunLoop().RunUntilIdle();
153 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
154 EXPECT_EQ(0, consumer_.number_of_errors_);
155 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
156 ASSERT_TRUE(fetcher);
157 fetcher->set_response_code(net::HTTP_OK);
158 fetcher->SetResponseString(GetValidTokenResponse("token", 3600));
159 fetcher->delegate()->OnURLFetchComplete(fetcher);
160 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
161 EXPECT_EQ(0, consumer_.number_of_errors_);
162 EXPECT_EQ("token", consumer_.last_token_);
164 // Second request to the same set of scopes, should return the same token
165 // without needing a network request.
166 scoped_ptr<OAuth2TokenService::Request> request2(
167 oauth2_service_->StartRequest(account_id_, scopes1_same, &consumer_));
168 base::RunLoop().RunUntilIdle();
170 // No new network fetcher.
171 EXPECT_EQ(2, consumer_.number_of_successful_tokens_);
172 EXPECT_EQ(0, consumer_.number_of_errors_);
173 EXPECT_EQ("token", consumer_.last_token_);
175 // Third request to a new set of scopes, should return another token.
176 scoped_ptr<OAuth2TokenService::Request> request3(
177 oauth2_service_->StartRequest(account_id_, scopes2, &consumer_));
178 base::RunLoop().RunUntilIdle();
179 EXPECT_EQ(2, consumer_.number_of_successful_tokens_);
180 EXPECT_EQ(0, consumer_.number_of_errors_);
181 fetcher = factory_.GetFetcherByID(0);
182 ASSERT_TRUE(fetcher);
183 fetcher->set_response_code(net::HTTP_OK);
184 fetcher->SetResponseString(GetValidTokenResponse("token2", 3600));
185 fetcher->delegate()->OnURLFetchComplete(fetcher);
186 EXPECT_EQ(3, consumer_.number_of_successful_tokens_);
187 EXPECT_EQ(0, consumer_.number_of_errors_);
188 EXPECT_EQ("token2", consumer_.last_token_);
191 TEST_F(OAuth2TokenServiceTest, SuccessAndExpirationAndFailure) {
192 oauth2_service_->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials(
193 account_id_, "refreshToken");
195 // First request.
196 scoped_ptr<OAuth2TokenService::Request> request(
197 oauth2_service_->StartRequest(account_id_,
198 OAuth2TokenService::ScopeSet(),
199 &consumer_));
200 base::RunLoop().RunUntilIdle();
201 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
202 EXPECT_EQ(0, consumer_.number_of_errors_);
203 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
204 ASSERT_TRUE(fetcher);
205 fetcher->set_response_code(net::HTTP_OK);
206 fetcher->SetResponseString(GetValidTokenResponse("token", 0));
207 fetcher->delegate()->OnURLFetchComplete(fetcher);
208 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
209 EXPECT_EQ(0, consumer_.number_of_errors_);
210 EXPECT_EQ("token", consumer_.last_token_);
212 // Second request must try to access the network as the token has expired.
213 scoped_ptr<OAuth2TokenService::Request> request2(
214 oauth2_service_->StartRequest(account_id_,
215 OAuth2TokenService::ScopeSet(),
216 &consumer_));
217 base::RunLoop().RunUntilIdle();
218 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
219 EXPECT_EQ(0, consumer_.number_of_errors_);
221 // Network failure.
222 fetcher = factory_.GetFetcherByID(0);
223 ASSERT_TRUE(fetcher);
224 fetcher->set_response_code(net::HTTP_UNAUTHORIZED);
225 fetcher->SetResponseString(std::string());
226 fetcher->delegate()->OnURLFetchComplete(fetcher);
227 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
228 EXPECT_EQ(1, consumer_.number_of_errors_);
231 TEST_F(OAuth2TokenServiceTest, SuccessAndExpirationAndSuccess) {
232 oauth2_service_->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials(
233 account_id_, "refreshToken");
235 // First request.
236 scoped_ptr<OAuth2TokenService::Request> request(
237 oauth2_service_->StartRequest(account_id_,
238 OAuth2TokenService::ScopeSet(),
239 &consumer_));
240 base::RunLoop().RunUntilIdle();
241 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
242 EXPECT_EQ(0, consumer_.number_of_errors_);
243 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
244 ASSERT_TRUE(fetcher);
245 fetcher->set_response_code(net::HTTP_OK);
246 fetcher->SetResponseString(GetValidTokenResponse("token", 0));
247 fetcher->delegate()->OnURLFetchComplete(fetcher);
248 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
249 EXPECT_EQ(0, consumer_.number_of_errors_);
250 EXPECT_EQ("token", consumer_.last_token_);
252 // Second request must try to access the network as the token has expired.
253 scoped_ptr<OAuth2TokenService::Request> request2(
254 oauth2_service_->StartRequest(account_id_,
255 OAuth2TokenService::ScopeSet(),
256 &consumer_));
257 base::RunLoop().RunUntilIdle();
258 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
259 EXPECT_EQ(0, consumer_.number_of_errors_);
261 fetcher = factory_.GetFetcherByID(0);
262 ASSERT_TRUE(fetcher);
263 fetcher->set_response_code(net::HTTP_OK);
264 fetcher->SetResponseString(GetValidTokenResponse("another token", 0));
265 fetcher->delegate()->OnURLFetchComplete(fetcher);
266 EXPECT_EQ(2, consumer_.number_of_successful_tokens_);
267 EXPECT_EQ(0, consumer_.number_of_errors_);
268 EXPECT_EQ("another token", consumer_.last_token_);
271 TEST_F(OAuth2TokenServiceTest, RequestDeletedBeforeCompletion) {
272 oauth2_service_->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials(
273 account_id_, "refreshToken");
275 scoped_ptr<OAuth2TokenService::Request> request(
276 oauth2_service_->StartRequest(account_id_,
277 OAuth2TokenService::ScopeSet(),
278 &consumer_));
279 base::RunLoop().RunUntilIdle();
280 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
281 EXPECT_EQ(0, consumer_.number_of_errors_);
282 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
283 ASSERT_TRUE(fetcher);
285 request.reset();
287 fetcher->set_response_code(net::HTTP_OK);
288 fetcher->SetResponseString(GetValidTokenResponse("token", 3600));
289 fetcher->delegate()->OnURLFetchComplete(fetcher);
290 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
291 EXPECT_EQ(0, consumer_.number_of_errors_);
294 TEST_F(OAuth2TokenServiceTest, RequestDeletedAfterCompletion) {
295 oauth2_service_->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials(
296 account_id_, "refreshToken");
298 scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest(
299 account_id_, OAuth2TokenService::ScopeSet(), &consumer_));
300 base::RunLoop().RunUntilIdle();
301 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
302 ASSERT_TRUE(fetcher);
303 fetcher->set_response_code(net::HTTP_OK);
304 fetcher->SetResponseString(GetValidTokenResponse("token", 3600));
305 fetcher->delegate()->OnURLFetchComplete(fetcher);
306 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
307 EXPECT_EQ(0, consumer_.number_of_errors_);
308 EXPECT_EQ("token", consumer_.last_token_);
310 request.reset();
312 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
313 EXPECT_EQ(0, consumer_.number_of_errors_);
314 EXPECT_EQ("token", consumer_.last_token_);
317 TEST_F(OAuth2TokenServiceTest, MultipleRequestsForTheSameScopesWithOneDeleted) {
318 oauth2_service_->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials(
319 account_id_, "refreshToken");
321 scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest(
322 account_id_, OAuth2TokenService::ScopeSet(), &consumer_));
323 base::RunLoop().RunUntilIdle();
324 scoped_ptr<OAuth2TokenService::Request> request2(
325 oauth2_service_->StartRequest(account_id_, OAuth2TokenService::ScopeSet(),
326 &consumer_));
327 base::RunLoop().RunUntilIdle();
329 request.reset();
331 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
332 ASSERT_TRUE(fetcher);
333 fetcher->set_response_code(net::HTTP_OK);
334 fetcher->SetResponseString(GetValidTokenResponse("token", 3600));
335 fetcher->delegate()->OnURLFetchComplete(fetcher);
336 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
337 EXPECT_EQ(0, consumer_.number_of_errors_);
340 TEST_F(OAuth2TokenServiceTest, ClearedRefreshTokenFailsSubsequentRequests) {
341 // We have a valid refresh token; the first request is successful.
342 oauth2_service_->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials(
343 account_id_, "refreshToken");
344 scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest(
345 account_id_, OAuth2TokenService::ScopeSet(), &consumer_));
346 base::RunLoop().RunUntilIdle();
347 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
348 ASSERT_TRUE(fetcher);
349 fetcher->set_response_code(net::HTTP_OK);
350 fetcher->SetResponseString(GetValidTokenResponse("token", 3600));
351 fetcher->delegate()->OnURLFetchComplete(fetcher);
352 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
353 EXPECT_EQ(0, consumer_.number_of_errors_);
354 EXPECT_EQ("token", consumer_.last_token_);
356 // The refresh token is no longer available; subsequent requests fail.
357 oauth2_service_->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials(
358 account_id_, "");
359 request = oauth2_service_->StartRequest(account_id_,
360 OAuth2TokenService::ScopeSet(), &consumer_);
361 base::RunLoop().RunUntilIdle();
362 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
363 EXPECT_EQ(1, consumer_.number_of_errors_);
366 TEST_F(OAuth2TokenServiceTest,
367 ChangedRefreshTokenDoesNotAffectInFlightRequests) {
368 oauth2_service_->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials(
369 account_id_, "first refreshToken");
370 OAuth2TokenService::ScopeSet scopes;
371 scopes.insert("s1");
372 scopes.insert("s2");
373 OAuth2TokenService::ScopeSet scopes1;
374 scopes.insert("s3");
375 scopes.insert("s4");
377 scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest(
378 account_id_, scopes, &consumer_));
379 base::RunLoop().RunUntilIdle();
380 net::TestURLFetcher* fetcher1 = factory_.GetFetcherByID(0);
381 ASSERT_TRUE(fetcher1);
383 // Note |request| is still pending when the refresh token changes.
384 oauth2_service_->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials(
385 account_id_, "second refreshToken");
387 // A 2nd request (using the new refresh token) that occurs and completes
388 // while the 1st request is in flight is successful.
389 TestingOAuth2TokenServiceConsumer consumer2;
390 scoped_ptr<OAuth2TokenService::Request> request2(
391 oauth2_service_->StartRequest(account_id_, scopes1, &consumer2));
392 base::RunLoop().RunUntilIdle();
394 net::TestURLFetcher* fetcher2 = factory_.GetFetcherByID(0);
395 fetcher2->set_response_code(net::HTTP_OK);
396 fetcher2->SetResponseString(GetValidTokenResponse("second token", 3600));
397 fetcher2->delegate()->OnURLFetchComplete(fetcher2);
398 EXPECT_EQ(1, consumer2.number_of_successful_tokens_);
399 EXPECT_EQ(0, consumer2.number_of_errors_);
400 EXPECT_EQ("second token", consumer2.last_token_);
402 fetcher1->set_response_code(net::HTTP_OK);
403 fetcher1->SetResponseString(GetValidTokenResponse("first token", 3600));
404 fetcher1->delegate()->OnURLFetchComplete(fetcher1);
405 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
406 EXPECT_EQ(0, consumer_.number_of_errors_);
407 EXPECT_EQ("first token", consumer_.last_token_);
410 TEST_F(OAuth2TokenServiceTest, ServiceShutDownBeforeFetchComplete) {
411 oauth2_service_->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials(
412 account_id_, "refreshToken");
413 scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest(
414 account_id_, OAuth2TokenService::ScopeSet(), &consumer_));
415 base::RunLoop().RunUntilIdle();
416 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
417 EXPECT_EQ(0, consumer_.number_of_errors_);
419 // The destructor should cancel all in-flight fetchers.
420 oauth2_service_.reset(NULL);
422 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
423 EXPECT_EQ(1, consumer_.number_of_errors_);
426 TEST_F(OAuth2TokenServiceTest, RetryingConsumer) {
427 oauth2_service_->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials(
428 account_id_, "refreshToken");
429 RetryingTestingOAuth2TokenServiceConsumer consumer(oauth2_service_.get(),
430 account_id_);
431 scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest(
432 account_id_, OAuth2TokenService::ScopeSet(), &consumer));
433 base::RunLoop().RunUntilIdle();
434 EXPECT_EQ(0, consumer.number_of_successful_tokens_);
435 EXPECT_EQ(0, consumer.number_of_errors_);
437 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
438 ASSERT_TRUE(fetcher);
439 fetcher->set_response_code(net::HTTP_UNAUTHORIZED);
440 fetcher->SetResponseString(std::string());
441 fetcher->delegate()->OnURLFetchComplete(fetcher);
442 EXPECT_EQ(0, consumer.number_of_successful_tokens_);
443 EXPECT_EQ(1, consumer.number_of_errors_);
445 fetcher = factory_.GetFetcherByID(0);
446 ASSERT_TRUE(fetcher);
447 fetcher->set_response_code(net::HTTP_UNAUTHORIZED);
448 fetcher->SetResponseString(std::string());
449 fetcher->delegate()->OnURLFetchComplete(fetcher);
450 EXPECT_EQ(0, consumer.number_of_successful_tokens_);
451 EXPECT_EQ(2, consumer.number_of_errors_);
454 TEST_F(OAuth2TokenServiceTest, InvalidateToken) {
455 OAuth2TokenService::ScopeSet scopes;
456 oauth2_service_->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials(
457 account_id_, "refreshToken");
459 // First request.
460 scoped_ptr<OAuth2TokenService::Request> request(
461 oauth2_service_->StartRequest(account_id_, scopes, &consumer_));
462 base::RunLoop().RunUntilIdle();
464 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
465 EXPECT_EQ(0, consumer_.number_of_errors_);
466 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
467 ASSERT_TRUE(fetcher);
468 fetcher->set_response_code(net::HTTP_OK);
469 fetcher->SetResponseString(GetValidTokenResponse("token", 3600));
470 fetcher->delegate()->OnURLFetchComplete(fetcher);
471 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
472 EXPECT_EQ(0, consumer_.number_of_errors_);
473 EXPECT_EQ("token", consumer_.last_token_);
475 // Second request, should return the same token without needing a network
476 // request.
477 scoped_ptr<OAuth2TokenService::Request> request2(
478 oauth2_service_->StartRequest(account_id_, scopes, &consumer_));
479 base::RunLoop().RunUntilIdle();
481 // No new network fetcher.
482 EXPECT_EQ(2, consumer_.number_of_successful_tokens_);
483 EXPECT_EQ(0, consumer_.number_of_errors_);
484 EXPECT_EQ("token", consumer_.last_token_);
486 // Invalidating the token should return a new token on the next request.
487 oauth2_service_->InvalidateAccessToken(account_id_, scopes,
488 consumer_.last_token_);
489 scoped_ptr<OAuth2TokenService::Request> request3(
490 oauth2_service_->StartRequest(account_id_, scopes, &consumer_));
491 base::RunLoop().RunUntilIdle();
492 EXPECT_EQ(2, consumer_.number_of_successful_tokens_);
493 EXPECT_EQ(0, consumer_.number_of_errors_);
494 fetcher = factory_.GetFetcherByID(0);
495 ASSERT_TRUE(fetcher);
496 fetcher->set_response_code(net::HTTP_OK);
497 fetcher->SetResponseString(GetValidTokenResponse("token2", 3600));
498 fetcher->delegate()->OnURLFetchComplete(fetcher);
499 EXPECT_EQ(3, consumer_.number_of_successful_tokens_);
500 EXPECT_EQ(0, consumer_.number_of_errors_);
501 EXPECT_EQ("token2", consumer_.last_token_);
504 TEST_F(OAuth2TokenServiceTest, CancelAllRequests) {
505 oauth2_service_->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials(
506 account_id_, "refreshToken");
507 scoped_ptr<OAuth2TokenService::Request> request(
508 oauth2_service_->StartRequest(account_id_, OAuth2TokenService::ScopeSet(),
509 &consumer_));
511 oauth2_service_->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials(
512 "account_id_2", "refreshToken2");
513 scoped_ptr<OAuth2TokenService::Request> request2(
514 oauth2_service_->StartRequest(account_id_, OAuth2TokenService::ScopeSet(),
515 &consumer_));
517 base::RunLoop().RunUntilIdle();
518 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
519 EXPECT_EQ(0, consumer_.number_of_errors_);
521 oauth2_service_->CancelAllRequestsForTest();
523 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
524 EXPECT_EQ(2, consumer_.number_of_errors_);
527 TEST_F(OAuth2TokenServiceTest, CancelRequestsForAccount) {
528 OAuth2TokenService::ScopeSet scope_set_1;
529 scope_set_1.insert("scope1");
530 scope_set_1.insert("scope2");
531 OAuth2TokenService::ScopeSet scope_set_2(scope_set_1.begin(),
532 scope_set_1.end());
533 scope_set_2.insert("scope3");
535 oauth2_service_->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials(
536 account_id_, "refreshToken");
537 scoped_ptr<OAuth2TokenService::Request> request1(
538 oauth2_service_->StartRequest(account_id_, scope_set_1, &consumer_));
539 scoped_ptr<OAuth2TokenService::Request> request2(
540 oauth2_service_->StartRequest(account_id_, scope_set_2, &consumer_));
542 std::string account_id_2("account_id_2");
543 oauth2_service_->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials(
544 account_id_2, "refreshToken2");
545 scoped_ptr<OAuth2TokenService::Request> request3(
546 oauth2_service_->StartRequest(account_id_2, scope_set_1, &consumer_));
548 base::RunLoop().RunUntilIdle();
549 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
550 EXPECT_EQ(0, consumer_.number_of_errors_);
552 oauth2_service_->CancelRequestsForAccountForTest(account_id_);
554 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
555 EXPECT_EQ(2, consumer_.number_of_errors_);
557 oauth2_service_->CancelRequestsForAccountForTest(account_id_2);
559 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
560 EXPECT_EQ(3, consumer_.number_of_errors_);
563 TEST_F(OAuth2TokenServiceTest, SameScopesRequestedForDifferentClients) {
564 std::string client_id_1("client1");
565 std::string client_secret_1("secret1");
566 std::string client_id_2("client2");
567 std::string client_secret_2("secret2");
568 std::set<std::string> scope_set;
569 scope_set.insert("scope1");
570 scope_set.insert("scope2");
572 std::string refresh_token("refreshToken");
573 oauth2_service_->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials(
574 account_id_, refresh_token);
576 scoped_ptr<OAuth2TokenService::Request> request1(
577 oauth2_service_->StartRequestForClient(account_id_,
578 client_id_1,
579 client_secret_1,
580 scope_set,
581 &consumer_));
582 scoped_ptr<OAuth2TokenService::Request> request2(
583 oauth2_service_->StartRequestForClient(account_id_,
584 client_id_2,
585 client_secret_2,
586 scope_set,
587 &consumer_));
588 // Start a request that should be duplicate of |request1|.
589 scoped_ptr<OAuth2TokenService::Request> request3(
590 oauth2_service_->StartRequestForClient(account_id_,
591 client_id_1,
592 client_secret_1,
593 scope_set,
594 &consumer_));
595 base::RunLoop().RunUntilIdle();
597 ASSERT_EQ(2U,
598 oauth2_service_->GetNumPendingRequestsForTesting(
599 client_id_1,
600 account_id_,
601 scope_set));
602 ASSERT_EQ(1U,
603 oauth2_service_->GetNumPendingRequestsForTesting(
604 client_id_2,
605 account_id_,
606 scope_set));
609 TEST_F(OAuth2TokenServiceTest, RequestParametersOrderTest) {
610 OAuth2TokenService::ScopeSet set_0;
611 OAuth2TokenService::ScopeSet set_1;
612 set_1.insert("1");
614 OAuth2TokenService::RequestParameters params[] = {
615 OAuth2TokenService::RequestParameters("0", "0", set_0),
616 OAuth2TokenService::RequestParameters("0", "0", set_1),
617 OAuth2TokenService::RequestParameters("0", "1", set_0),
618 OAuth2TokenService::RequestParameters("0", "1", set_1),
619 OAuth2TokenService::RequestParameters("1", "0", set_0),
620 OAuth2TokenService::RequestParameters("1", "0", set_1),
621 OAuth2TokenService::RequestParameters("1", "1", set_0),
622 OAuth2TokenService::RequestParameters("1", "1", set_1),
625 for (size_t i = 0; i < arraysize(params); i++) {
626 for (size_t j = 0; j < arraysize(params); j++) {
627 if (i == j) {
628 EXPECT_FALSE(params[i] < params[j]) << " i=" << i << ", j=" << j;
629 EXPECT_FALSE(params[j] < params[i]) << " i=" << i << ", j=" << j;
630 } else if (i < j) {
631 EXPECT_TRUE(params[i] < params[j]) << " i=" << i << ", j=" << j;
632 EXPECT_FALSE(params[j] < params[i]) << " i=" << i << ", j=" << j;
633 } else {
634 EXPECT_TRUE(params[j] < params[i]) << " i=" << i << ", j=" << j;
635 EXPECT_FALSE(params[i] < params[j]) << " i=" << i << ", j=" << j;
641 TEST_F(OAuth2TokenServiceTest, UpdateClearsCache) {
642 std::string kEmail = "test@gmail.com";
643 std::set<std::string> scope_list;
644 scope_list.insert("scope");
645 oauth2_service_->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials(
646 kEmail, "refreshToken");
647 scoped_ptr<OAuth2TokenService::Request> request(
648 oauth2_service_->StartRequest(kEmail, scope_list, &consumer_));
649 base::RunLoop().RunUntilIdle();
650 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
651 ASSERT_TRUE(fetcher);
652 fetcher->set_response_code(net::HTTP_OK);
653 fetcher->SetResponseString(GetValidTokenResponse("token", 3600));
654 fetcher->delegate()->OnURLFetchComplete(fetcher);
655 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
656 EXPECT_EQ(0, consumer_.number_of_errors_);
657 EXPECT_EQ("token", consumer_.last_token_);
658 EXPECT_EQ(1, (int)oauth2_service_->token_cache_.size());
660 // Signs out and signs in
661 oauth2_service_->RevokeAllCredentials();
663 EXPECT_EQ(0, (int)oauth2_service_->token_cache_.size());
664 oauth2_service_->GetFakeOAuth2TokenServiceDelegate()->UpdateCredentials(
665 kEmail, "refreshToken");
666 request = oauth2_service_->StartRequest(kEmail, scope_list, &consumer_);
667 base::RunLoop().RunUntilIdle();
668 fetcher = factory_.GetFetcherByID(0);
669 // ASSERT_TRUE(fetcher);
670 fetcher->set_response_code(net::HTTP_OK);
671 fetcher->SetResponseString(GetValidTokenResponse("another token", 3600));
672 fetcher->delegate()->OnURLFetchComplete(fetcher);
673 EXPECT_EQ(2, consumer_.number_of_successful_tokens_);
674 EXPECT_EQ(0, consumer_.number_of_errors_);
675 EXPECT_EQ("another token", consumer_.last_token_);
676 EXPECT_EQ(1, (int)oauth2_service_->token_cache_.size());