Support policy registration using a preobtained access token.
[chromium-blink-merge.git] / google_apis / gaia / oauth2_token_service_unittest.cc
blobb79e361c44c7f6c9cec6f0cac4553dab4924d125
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/gaia_constants.h"
10 #include "google_apis/gaia/google_service_auth_error.h"
11 #include "google_apis/gaia/oauth2_access_token_consumer.h"
12 #include "google_apis/gaia/oauth2_access_token_fetcher_impl.h"
13 #include "google_apis/gaia/oauth2_token_service.h"
14 #include "google_apis/gaia/oauth2_token_service_test_util.h"
15 #include "net/http/http_status_code.h"
16 #include "net/url_request/test_url_fetcher_factory.h"
17 #include "net/url_request/url_fetcher_delegate.h"
18 #include "net/url_request/url_request_test_util.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 // A testing consumer that retries on error.
22 class RetryingTestingOAuth2TokenServiceConsumer
23 : public TestingOAuth2TokenServiceConsumer {
24 public:
25 RetryingTestingOAuth2TokenServiceConsumer(
26 OAuth2TokenService* oauth2_service,
27 const std::string& account_id)
28 : oauth2_service_(oauth2_service),
29 account_id_(account_id) {}
30 virtual ~RetryingTestingOAuth2TokenServiceConsumer() {}
32 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request,
33 const GoogleServiceAuthError& error) OVERRIDE {
34 TestingOAuth2TokenServiceConsumer::OnGetTokenFailure(request, error);
35 request_.reset(oauth2_service_->StartRequest(
36 account_id_, OAuth2TokenService::ScopeSet(), this).release());
39 OAuth2TokenService* oauth2_service_;
40 std::string account_id_;
41 scoped_ptr<OAuth2TokenService::Request> request_;
44 class TestOAuth2TokenService : public OAuth2TokenService {
45 public:
46 explicit TestOAuth2TokenService(net::TestURLRequestContextGetter* getter)
47 : request_context_getter_(getter) {
50 void CancelAllRequestsForTest() { CancelAllRequests(); }
52 void CancelRequestsForAccountForTest(const std::string& account_id) {
53 CancelRequestsForAccount(account_id);
56 // For testing: set the refresh token to be used.
57 void set_refresh_token(const std::string& account_id,
58 const std::string& refresh_token) {
59 if (refresh_token.empty())
60 refresh_tokens_.erase(account_id);
61 else
62 refresh_tokens_[account_id] = refresh_token;
65 virtual bool RefreshTokenIsAvailable(const std::string& account_id) const
66 OVERRIDE {
67 std::map<std::string, std::string>::const_iterator it =
68 refresh_tokens_.find(account_id);
70 return it != refresh_tokens_.end();
73 private:
74 // OAuth2TokenService implementation.
75 virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE {
76 return request_context_getter_.get();
79 virtual OAuth2AccessTokenFetcher* CreateAccessTokenFetcher(
80 const std::string& account_id,
81 net::URLRequestContextGetter* getter,
82 OAuth2AccessTokenConsumer* consumer) OVERRIDE {
83 std::map<std::string, std::string>::const_iterator it =
84 refresh_tokens_.find(account_id);
85 DCHECK(it != refresh_tokens_.end());
86 std::string refresh_token(it->second);
87 return new OAuth2AccessTokenFetcherImpl(consumer, getter, refresh_token);
90 std::map<std::string, std::string> refresh_tokens_;
91 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
94 class OAuth2TokenServiceTest : public testing::Test {
95 public:
96 virtual void SetUp() OVERRIDE {
97 oauth2_service_.reset(
98 new TestOAuth2TokenService(new net::TestURLRequestContextGetter(
99 message_loop_.message_loop_proxy())));
100 account_id_ = "test_user@gmail.com";
103 virtual void TearDown() OVERRIDE {
104 // Makes sure that all the clean up tasks are run.
105 base::RunLoop().RunUntilIdle();
108 protected:
109 base::MessageLoopForIO message_loop_; // net:: stuff needs IO message loop.
110 net::TestURLFetcherFactory factory_;
111 scoped_ptr<TestOAuth2TokenService> oauth2_service_;
112 std::string account_id_;
113 TestingOAuth2TokenServiceConsumer consumer_;
116 TEST_F(OAuth2TokenServiceTest, NoOAuth2RefreshToken) {
117 scoped_ptr<OAuth2TokenService::Request> request(
118 oauth2_service_->StartRequest(account_id_, OAuth2TokenService::ScopeSet(),
119 &consumer_));
120 base::RunLoop().RunUntilIdle();
122 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
123 EXPECT_EQ(1, consumer_.number_of_errors_);
126 TEST_F(OAuth2TokenServiceTest, FailureShouldNotRetry) {
127 oauth2_service_->set_refresh_token(account_id_, "refreshToken");
128 scoped_ptr<OAuth2TokenService::Request> request(
129 oauth2_service_->StartRequest(account_id_,
130 OAuth2TokenService::ScopeSet(),
131 &consumer_));
132 base::RunLoop().RunUntilIdle();
134 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
135 EXPECT_EQ(0, consumer_.number_of_errors_);
136 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
137 ASSERT_TRUE(fetcher);
138 fetcher->set_response_code(net::HTTP_UNAUTHORIZED);
139 fetcher->SetResponseString(std::string());
140 fetcher->delegate()->OnURLFetchComplete(fetcher);
141 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
142 EXPECT_EQ(1, consumer_.number_of_errors_);
143 EXPECT_EQ(fetcher, factory_.GetFetcherByID(0));
146 TEST_F(OAuth2TokenServiceTest, SuccessWithoutCaching) {
147 oauth2_service_->set_refresh_token(account_id_, "refreshToken");
148 scoped_ptr<OAuth2TokenService::Request> request(
149 oauth2_service_->StartRequest(account_id_,
150 OAuth2TokenService::ScopeSet(),
151 &consumer_));
152 base::RunLoop().RunUntilIdle();
154 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
155 EXPECT_EQ(0, consumer_.number_of_errors_);
156 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
157 ASSERT_TRUE(fetcher);
158 fetcher->set_response_code(net::HTTP_OK);
159 fetcher->SetResponseString(GetValidTokenResponse("token", 3600));
160 fetcher->delegate()->OnURLFetchComplete(fetcher);
161 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
162 EXPECT_EQ(0, consumer_.number_of_errors_);
163 EXPECT_EQ("token", consumer_.last_token_);
166 TEST_F(OAuth2TokenServiceTest, SuccessWithCaching) {
167 OAuth2TokenService::ScopeSet scopes1;
168 scopes1.insert("s1");
169 scopes1.insert("s2");
170 OAuth2TokenService::ScopeSet scopes1_same;
171 scopes1_same.insert("s2");
172 scopes1_same.insert("s1");
173 OAuth2TokenService::ScopeSet scopes2;
174 scopes2.insert("s3");
176 oauth2_service_->set_refresh_token(account_id_, "refreshToken");
178 // First request.
179 scoped_ptr<OAuth2TokenService::Request> request(
180 oauth2_service_->StartRequest(account_id_, scopes1, &consumer_));
181 base::RunLoop().RunUntilIdle();
183 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
184 EXPECT_EQ(0, consumer_.number_of_errors_);
185 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
186 ASSERT_TRUE(fetcher);
187 fetcher->set_response_code(net::HTTP_OK);
188 fetcher->SetResponseString(GetValidTokenResponse("token", 3600));
189 fetcher->delegate()->OnURLFetchComplete(fetcher);
190 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
191 EXPECT_EQ(0, consumer_.number_of_errors_);
192 EXPECT_EQ("token", consumer_.last_token_);
194 // Second request to the same set of scopes, should return the same token
195 // without needing a network request.
196 scoped_ptr<OAuth2TokenService::Request> request2(
197 oauth2_service_->StartRequest(account_id_, scopes1_same, &consumer_));
198 base::RunLoop().RunUntilIdle();
200 // No new network fetcher.
201 EXPECT_EQ(2, consumer_.number_of_successful_tokens_);
202 EXPECT_EQ(0, consumer_.number_of_errors_);
203 EXPECT_EQ("token", consumer_.last_token_);
205 // Third request to a new set of scopes, should return another token.
206 scoped_ptr<OAuth2TokenService::Request> request3(
207 oauth2_service_->StartRequest(account_id_, scopes2, &consumer_));
208 base::RunLoop().RunUntilIdle();
209 EXPECT_EQ(2, consumer_.number_of_successful_tokens_);
210 EXPECT_EQ(0, consumer_.number_of_errors_);
211 fetcher = factory_.GetFetcherByID(0);
212 ASSERT_TRUE(fetcher);
213 fetcher->set_response_code(net::HTTP_OK);
214 fetcher->SetResponseString(GetValidTokenResponse("token2", 3600));
215 fetcher->delegate()->OnURLFetchComplete(fetcher);
216 EXPECT_EQ(3, consumer_.number_of_successful_tokens_);
217 EXPECT_EQ(0, consumer_.number_of_errors_);
218 EXPECT_EQ("token2", consumer_.last_token_);
221 TEST_F(OAuth2TokenServiceTest, SuccessAndExpirationAndFailure) {
222 oauth2_service_->set_refresh_token(account_id_, "refreshToken");
224 // First request.
225 scoped_ptr<OAuth2TokenService::Request> request(
226 oauth2_service_->StartRequest(account_id_,
227 OAuth2TokenService::ScopeSet(),
228 &consumer_));
229 base::RunLoop().RunUntilIdle();
230 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
231 EXPECT_EQ(0, consumer_.number_of_errors_);
232 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
233 ASSERT_TRUE(fetcher);
234 fetcher->set_response_code(net::HTTP_OK);
235 fetcher->SetResponseString(GetValidTokenResponse("token", 0));
236 fetcher->delegate()->OnURLFetchComplete(fetcher);
237 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
238 EXPECT_EQ(0, consumer_.number_of_errors_);
239 EXPECT_EQ("token", consumer_.last_token_);
241 // Second request must try to access the network as the token has expired.
242 scoped_ptr<OAuth2TokenService::Request> request2(
243 oauth2_service_->StartRequest(account_id_,
244 OAuth2TokenService::ScopeSet(),
245 &consumer_));
246 base::RunLoop().RunUntilIdle();
247 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
248 EXPECT_EQ(0, consumer_.number_of_errors_);
250 // Network failure.
251 fetcher = factory_.GetFetcherByID(0);
252 ASSERT_TRUE(fetcher);
253 fetcher->set_response_code(net::HTTP_UNAUTHORIZED);
254 fetcher->SetResponseString(std::string());
255 fetcher->delegate()->OnURLFetchComplete(fetcher);
256 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
257 EXPECT_EQ(1, consumer_.number_of_errors_);
260 TEST_F(OAuth2TokenServiceTest, SuccessAndExpirationAndSuccess) {
261 oauth2_service_->set_refresh_token(account_id_, "refreshToken");
263 // First request.
264 scoped_ptr<OAuth2TokenService::Request> request(
265 oauth2_service_->StartRequest(account_id_,
266 OAuth2TokenService::ScopeSet(),
267 &consumer_));
268 base::RunLoop().RunUntilIdle();
269 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
270 EXPECT_EQ(0, consumer_.number_of_errors_);
271 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
272 ASSERT_TRUE(fetcher);
273 fetcher->set_response_code(net::HTTP_OK);
274 fetcher->SetResponseString(GetValidTokenResponse("token", 0));
275 fetcher->delegate()->OnURLFetchComplete(fetcher);
276 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
277 EXPECT_EQ(0, consumer_.number_of_errors_);
278 EXPECT_EQ("token", consumer_.last_token_);
280 // Second request must try to access the network as the token has expired.
281 scoped_ptr<OAuth2TokenService::Request> request2(
282 oauth2_service_->StartRequest(account_id_,
283 OAuth2TokenService::ScopeSet(),
284 &consumer_));
285 base::RunLoop().RunUntilIdle();
286 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
287 EXPECT_EQ(0, consumer_.number_of_errors_);
289 fetcher = factory_.GetFetcherByID(0);
290 ASSERT_TRUE(fetcher);
291 fetcher->set_response_code(net::HTTP_OK);
292 fetcher->SetResponseString(GetValidTokenResponse("another token", 0));
293 fetcher->delegate()->OnURLFetchComplete(fetcher);
294 EXPECT_EQ(2, consumer_.number_of_successful_tokens_);
295 EXPECT_EQ(0, consumer_.number_of_errors_);
296 EXPECT_EQ("another token", consumer_.last_token_);
299 TEST_F(OAuth2TokenServiceTest, RequestDeletedBeforeCompletion) {
300 oauth2_service_->set_refresh_token(account_id_, "refreshToken");
302 scoped_ptr<OAuth2TokenService::Request> request(
303 oauth2_service_->StartRequest(account_id_,
304 OAuth2TokenService::ScopeSet(),
305 &consumer_));
306 base::RunLoop().RunUntilIdle();
307 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
308 EXPECT_EQ(0, consumer_.number_of_errors_);
309 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
310 ASSERT_TRUE(fetcher);
312 request.reset();
314 fetcher->set_response_code(net::HTTP_OK);
315 fetcher->SetResponseString(GetValidTokenResponse("token", 3600));
316 fetcher->delegate()->OnURLFetchComplete(fetcher);
317 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
318 EXPECT_EQ(0, consumer_.number_of_errors_);
321 TEST_F(OAuth2TokenServiceTest, RequestDeletedAfterCompletion) {
322 oauth2_service_->set_refresh_token(account_id_, "refreshToken");
324 scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest(
325 account_id_, OAuth2TokenService::ScopeSet(), &consumer_));
326 base::RunLoop().RunUntilIdle();
327 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
328 ASSERT_TRUE(fetcher);
329 fetcher->set_response_code(net::HTTP_OK);
330 fetcher->SetResponseString(GetValidTokenResponse("token", 3600));
331 fetcher->delegate()->OnURLFetchComplete(fetcher);
332 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
333 EXPECT_EQ(0, consumer_.number_of_errors_);
334 EXPECT_EQ("token", consumer_.last_token_);
336 request.reset();
338 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
339 EXPECT_EQ(0, consumer_.number_of_errors_);
340 EXPECT_EQ("token", consumer_.last_token_);
343 TEST_F(OAuth2TokenServiceTest, MultipleRequestsForTheSameScopesWithOneDeleted) {
344 oauth2_service_->set_refresh_token(account_id_, "refreshToken");
346 scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest(
347 account_id_, OAuth2TokenService::ScopeSet(), &consumer_));
348 base::RunLoop().RunUntilIdle();
349 scoped_ptr<OAuth2TokenService::Request> request2(
350 oauth2_service_->StartRequest(account_id_, OAuth2TokenService::ScopeSet(),
351 &consumer_));
352 base::RunLoop().RunUntilIdle();
354 request.reset();
356 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
357 ASSERT_TRUE(fetcher);
358 fetcher->set_response_code(net::HTTP_OK);
359 fetcher->SetResponseString(GetValidTokenResponse("token", 3600));
360 fetcher->delegate()->OnURLFetchComplete(fetcher);
361 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
362 EXPECT_EQ(0, consumer_.number_of_errors_);
365 TEST_F(OAuth2TokenServiceTest, ClearedRefreshTokenFailsSubsequentRequests) {
366 // We have a valid refresh token; the first request is successful.
367 oauth2_service_->set_refresh_token(account_id_, "refreshToken");
368 scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest(
369 account_id_, OAuth2TokenService::ScopeSet(), &consumer_));
370 base::RunLoop().RunUntilIdle();
371 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
372 ASSERT_TRUE(fetcher);
373 fetcher->set_response_code(net::HTTP_OK);
374 fetcher->SetResponseString(GetValidTokenResponse("token", 3600));
375 fetcher->delegate()->OnURLFetchComplete(fetcher);
376 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
377 EXPECT_EQ(0, consumer_.number_of_errors_);
378 EXPECT_EQ("token", consumer_.last_token_);
380 // The refresh token is no longer available; subsequent requests fail.
381 oauth2_service_->set_refresh_token(account_id_, "");
382 request = oauth2_service_->StartRequest(account_id_,
383 OAuth2TokenService::ScopeSet(), &consumer_);
384 base::RunLoop().RunUntilIdle();
385 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
386 EXPECT_EQ(1, consumer_.number_of_errors_);
389 TEST_F(OAuth2TokenServiceTest,
390 ChangedRefreshTokenDoesNotAffectInFlightRequests) {
391 oauth2_service_->set_refresh_token(account_id_, "first refreshToken");
392 OAuth2TokenService::ScopeSet scopes;
393 scopes.insert("s1");
394 scopes.insert("s2");
395 OAuth2TokenService::ScopeSet scopes1;
396 scopes.insert("s3");
397 scopes.insert("s4");
399 scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest(
400 account_id_, scopes, &consumer_));
401 base::RunLoop().RunUntilIdle();
402 net::TestURLFetcher* fetcher1 = factory_.GetFetcherByID(0);
403 ASSERT_TRUE(fetcher1);
405 // Note |request| is still pending when the refresh token changes.
406 oauth2_service_->set_refresh_token(account_id_, "second refreshToken");
408 // A 2nd request (using the new refresh token) that occurs and completes
409 // while the 1st request is in flight is successful.
410 TestingOAuth2TokenServiceConsumer consumer2;
411 scoped_ptr<OAuth2TokenService::Request> request2(
412 oauth2_service_->StartRequest(account_id_, scopes1, &consumer2));
413 base::RunLoop().RunUntilIdle();
415 net::TestURLFetcher* fetcher2 = factory_.GetFetcherByID(0);
416 fetcher2->set_response_code(net::HTTP_OK);
417 fetcher2->SetResponseString(GetValidTokenResponse("second token", 3600));
418 fetcher2->delegate()->OnURLFetchComplete(fetcher2);
419 EXPECT_EQ(1, consumer2.number_of_successful_tokens_);
420 EXPECT_EQ(0, consumer2.number_of_errors_);
421 EXPECT_EQ("second token", consumer2.last_token_);
423 fetcher1->set_response_code(net::HTTP_OK);
424 fetcher1->SetResponseString(GetValidTokenResponse("first token", 3600));
425 fetcher1->delegate()->OnURLFetchComplete(fetcher1);
426 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
427 EXPECT_EQ(0, consumer_.number_of_errors_);
428 EXPECT_EQ("first token", consumer_.last_token_);
431 TEST_F(OAuth2TokenServiceTest, ServiceShutDownBeforeFetchComplete) {
432 oauth2_service_->set_refresh_token(account_id_, "refreshToken");
433 scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest(
434 account_id_, OAuth2TokenService::ScopeSet(), &consumer_));
435 base::RunLoop().RunUntilIdle();
436 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
437 EXPECT_EQ(0, consumer_.number_of_errors_);
439 // The destructor should cancel all in-flight fetchers.
440 oauth2_service_.reset(NULL);
442 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
443 EXPECT_EQ(1, consumer_.number_of_errors_);
446 TEST_F(OAuth2TokenServiceTest, RetryingConsumer) {
447 oauth2_service_->set_refresh_token(account_id_, "refreshToken");
448 RetryingTestingOAuth2TokenServiceConsumer consumer(oauth2_service_.get(),
449 account_id_);
450 scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest(
451 account_id_, OAuth2TokenService::ScopeSet(), &consumer));
452 base::RunLoop().RunUntilIdle();
453 EXPECT_EQ(0, consumer.number_of_successful_tokens_);
454 EXPECT_EQ(0, consumer.number_of_errors_);
456 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
457 ASSERT_TRUE(fetcher);
458 fetcher->set_response_code(net::HTTP_UNAUTHORIZED);
459 fetcher->SetResponseString(std::string());
460 fetcher->delegate()->OnURLFetchComplete(fetcher);
461 EXPECT_EQ(0, consumer.number_of_successful_tokens_);
462 EXPECT_EQ(1, consumer.number_of_errors_);
464 fetcher = factory_.GetFetcherByID(0);
465 ASSERT_TRUE(fetcher);
466 fetcher->set_response_code(net::HTTP_UNAUTHORIZED);
467 fetcher->SetResponseString(std::string());
468 fetcher->delegate()->OnURLFetchComplete(fetcher);
469 EXPECT_EQ(0, consumer.number_of_successful_tokens_);
470 EXPECT_EQ(2, consumer.number_of_errors_);
473 TEST_F(OAuth2TokenServiceTest, InvalidateToken) {
474 OAuth2TokenService::ScopeSet scopes;
475 oauth2_service_->set_refresh_token(account_id_, "refreshToken");
477 // First request.
478 scoped_ptr<OAuth2TokenService::Request> request(
479 oauth2_service_->StartRequest(account_id_, scopes, &consumer_));
480 base::RunLoop().RunUntilIdle();
482 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
483 EXPECT_EQ(0, consumer_.number_of_errors_);
484 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
485 ASSERT_TRUE(fetcher);
486 fetcher->set_response_code(net::HTTP_OK);
487 fetcher->SetResponseString(GetValidTokenResponse("token", 3600));
488 fetcher->delegate()->OnURLFetchComplete(fetcher);
489 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
490 EXPECT_EQ(0, consumer_.number_of_errors_);
491 EXPECT_EQ("token", consumer_.last_token_);
493 // Second request, should return the same token without needing a network
494 // request.
495 scoped_ptr<OAuth2TokenService::Request> request2(
496 oauth2_service_->StartRequest(account_id_, scopes, &consumer_));
497 base::RunLoop().RunUntilIdle();
499 // No new network fetcher.
500 EXPECT_EQ(2, consumer_.number_of_successful_tokens_);
501 EXPECT_EQ(0, consumer_.number_of_errors_);
502 EXPECT_EQ("token", consumer_.last_token_);
504 // Invalidating the token should return a new token on the next request.
505 oauth2_service_->InvalidateToken(account_id_, scopes, consumer_.last_token_);
506 scoped_ptr<OAuth2TokenService::Request> request3(
507 oauth2_service_->StartRequest(account_id_, scopes, &consumer_));
508 base::RunLoop().RunUntilIdle();
509 EXPECT_EQ(2, consumer_.number_of_successful_tokens_);
510 EXPECT_EQ(0, consumer_.number_of_errors_);
511 fetcher = factory_.GetFetcherByID(0);
512 ASSERT_TRUE(fetcher);
513 fetcher->set_response_code(net::HTTP_OK);
514 fetcher->SetResponseString(GetValidTokenResponse("token2", 3600));
515 fetcher->delegate()->OnURLFetchComplete(fetcher);
516 EXPECT_EQ(3, consumer_.number_of_successful_tokens_);
517 EXPECT_EQ(0, consumer_.number_of_errors_);
518 EXPECT_EQ("token2", consumer_.last_token_);
521 TEST_F(OAuth2TokenServiceTest, CancelAllRequests) {
522 oauth2_service_->set_refresh_token(account_id_, "refreshToken");
523 scoped_ptr<OAuth2TokenService::Request> request(
524 oauth2_service_->StartRequest(account_id_, OAuth2TokenService::ScopeSet(),
525 &consumer_));
527 oauth2_service_->set_refresh_token("account_id_2", "refreshToken2");
528 scoped_ptr<OAuth2TokenService::Request> request2(
529 oauth2_service_->StartRequest(account_id_, OAuth2TokenService::ScopeSet(),
530 &consumer_));
532 base::RunLoop().RunUntilIdle();
533 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
534 EXPECT_EQ(0, consumer_.number_of_errors_);
536 oauth2_service_->CancelAllRequestsForTest();
538 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
539 EXPECT_EQ(2, consumer_.number_of_errors_);
542 TEST_F(OAuth2TokenServiceTest, CancelRequestsForAccount) {
543 OAuth2TokenService::ScopeSet scope_set_1;
544 scope_set_1.insert("scope1");
545 scope_set_1.insert("scope2");
546 OAuth2TokenService::ScopeSet scope_set_2(scope_set_1.begin(),
547 scope_set_1.end());
548 scope_set_2.insert("scope3");
550 oauth2_service_->set_refresh_token(account_id_, "refreshToken");
551 scoped_ptr<OAuth2TokenService::Request> request1(
552 oauth2_service_->StartRequest(account_id_, scope_set_1, &consumer_));
553 scoped_ptr<OAuth2TokenService::Request> request2(
554 oauth2_service_->StartRequest(account_id_, scope_set_2, &consumer_));
556 std::string account_id_2("account_id_2");
557 oauth2_service_->set_refresh_token(account_id_2, "refreshToken2");
558 scoped_ptr<OAuth2TokenService::Request> request3(
559 oauth2_service_->StartRequest(account_id_2, scope_set_1, &consumer_));
561 base::RunLoop().RunUntilIdle();
562 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
563 EXPECT_EQ(0, consumer_.number_of_errors_);
565 oauth2_service_->CancelRequestsForAccountForTest(account_id_);
567 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
568 EXPECT_EQ(2, consumer_.number_of_errors_);
570 oauth2_service_->CancelRequestsForAccountForTest(account_id_2);
572 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
573 EXPECT_EQ(3, consumer_.number_of_errors_);
576 TEST_F(OAuth2TokenServiceTest, SameScopesRequestedForDifferentClients) {
577 std::string client_id_1("client1");
578 std::string client_secret_1("secret1");
579 std::string client_id_2("client2");
580 std::string client_secret_2("secret2");
581 std::set<std::string> scope_set;
582 scope_set.insert("scope1");
583 scope_set.insert("scope2");
585 std::string refresh_token("refreshToken");
586 oauth2_service_->set_refresh_token(account_id_, refresh_token);
588 scoped_ptr<OAuth2TokenService::Request> request1(
589 oauth2_service_->StartRequestForClient(account_id_,
590 client_id_1,
591 client_secret_1,
592 scope_set,
593 &consumer_));
594 scoped_ptr<OAuth2TokenService::Request> request2(
595 oauth2_service_->StartRequestForClient(account_id_,
596 client_id_2,
597 client_secret_2,
598 scope_set,
599 &consumer_));
600 // Start a request that should be duplicate of |request1|.
601 scoped_ptr<OAuth2TokenService::Request> request3(
602 oauth2_service_->StartRequestForClient(account_id_,
603 client_id_1,
604 client_secret_1,
605 scope_set,
606 &consumer_));
607 base::RunLoop().RunUntilIdle();
609 ASSERT_EQ(2U,
610 oauth2_service_->GetNumPendingRequestsForTesting(
611 client_id_1,
612 account_id_,
613 scope_set));
614 ASSERT_EQ(1U,
615 oauth2_service_->GetNumPendingRequestsForTesting(
616 client_id_2,
617 account_id_,
618 scope_set));
621 TEST_F(OAuth2TokenServiceTest, RequestParametersOrderTest) {
622 OAuth2TokenService::ScopeSet set_0;
623 OAuth2TokenService::ScopeSet set_1;
624 set_1.insert("1");
626 OAuth2TokenService::RequestParameters params[] = {
627 OAuth2TokenService::RequestParameters("0", "0", set_0),
628 OAuth2TokenService::RequestParameters("0", "0", set_1),
629 OAuth2TokenService::RequestParameters("0", "1", set_0),
630 OAuth2TokenService::RequestParameters("0", "1", set_1),
631 OAuth2TokenService::RequestParameters("1", "0", set_0),
632 OAuth2TokenService::RequestParameters("1", "0", set_1),
633 OAuth2TokenService::RequestParameters("1", "1", set_0),
634 OAuth2TokenService::RequestParameters("1", "1", set_1),
637 for (size_t i = 0; i < arraysize(params); i++) {
638 for (size_t j = 0; j < arraysize(params); j++) {
639 if (i == j) {
640 EXPECT_FALSE(params[i] < params[j]) << " i=" << i << ", j=" << j;
641 EXPECT_FALSE(params[j] < params[i]) << " i=" << i << ", j=" << j;
642 } else if (i < j) {
643 EXPECT_TRUE(params[i] < params[j]) << " i=" << i << ", j=" << j;
644 EXPECT_FALSE(params[j] < params[i]) << " i=" << i << ", j=" << j;
645 } else {
646 EXPECT_TRUE(params[j] < params[i]) << " i=" << i << ", j=" << j;
647 EXPECT_FALSE(params[i] < params[j]) << " i=" << i << ", j=" << j;