[NaCl SDK] nacl_io: Fix bad test added in def60c00.
[chromium-blink-merge.git] / google_apis / gaia / oauth2_token_service_unittest.cc
blob0cf0c6586ddd53a08b61f5c86ebf031addc116d5
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 ~RetryingTestingOAuth2TokenServiceConsumer() override {}
32 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 bool RefreshTokenIsAvailable(const std::string& account_id) const override {
66 std::map<std::string, std::string>::const_iterator it =
67 refresh_tokens_.find(account_id);
69 return it != refresh_tokens_.end();
72 private:
73 // OAuth2TokenService implementation.
74 net::URLRequestContextGetter* GetRequestContext() override {
75 return request_context_getter_.get();
78 OAuth2AccessTokenFetcher* CreateAccessTokenFetcher(
79 const std::string& account_id,
80 net::URLRequestContextGetter* getter,
81 OAuth2AccessTokenConsumer* consumer) override {
82 std::map<std::string, std::string>::const_iterator it =
83 refresh_tokens_.find(account_id);
84 DCHECK(it != refresh_tokens_.end());
85 std::string refresh_token(it->second);
86 return new OAuth2AccessTokenFetcherImpl(consumer, getter, refresh_token);
89 std::map<std::string, std::string> refresh_tokens_;
90 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
93 class OAuth2TokenServiceTest : public testing::Test {
94 public:
95 void SetUp() override {
96 oauth2_service_.reset(
97 new TestOAuth2TokenService(new net::TestURLRequestContextGetter(
98 message_loop_.message_loop_proxy())));
99 account_id_ = "test_user@gmail.com";
102 void TearDown() override {
103 // Makes sure that all the clean up tasks are run.
104 base::RunLoop().RunUntilIdle();
107 protected:
108 base::MessageLoopForIO message_loop_; // net:: stuff needs IO message loop.
109 net::TestURLFetcherFactory factory_;
110 scoped_ptr<TestOAuth2TokenService> oauth2_service_;
111 std::string account_id_;
112 TestingOAuth2TokenServiceConsumer consumer_;
115 TEST_F(OAuth2TokenServiceTest, NoOAuth2RefreshToken) {
116 scoped_ptr<OAuth2TokenService::Request> request(
117 oauth2_service_->StartRequest(account_id_, OAuth2TokenService::ScopeSet(),
118 &consumer_));
119 base::RunLoop().RunUntilIdle();
121 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
122 EXPECT_EQ(1, consumer_.number_of_errors_);
125 TEST_F(OAuth2TokenServiceTest, FailureShouldNotRetry) {
126 oauth2_service_->set_refresh_token(account_id_, "refreshToken");
127 scoped_ptr<OAuth2TokenService::Request> request(
128 oauth2_service_->StartRequest(account_id_,
129 OAuth2TokenService::ScopeSet(),
130 &consumer_));
131 base::RunLoop().RunUntilIdle();
133 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
134 EXPECT_EQ(0, consumer_.number_of_errors_);
135 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
136 ASSERT_TRUE(fetcher);
137 fetcher->set_response_code(net::HTTP_UNAUTHORIZED);
138 fetcher->SetResponseString(std::string());
139 fetcher->delegate()->OnURLFetchComplete(fetcher);
140 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
141 EXPECT_EQ(1, consumer_.number_of_errors_);
142 EXPECT_EQ(fetcher, factory_.GetFetcherByID(0));
145 TEST_F(OAuth2TokenServiceTest, SuccessWithoutCaching) {
146 oauth2_service_->set_refresh_token(account_id_, "refreshToken");
147 scoped_ptr<OAuth2TokenService::Request> request(
148 oauth2_service_->StartRequest(account_id_,
149 OAuth2TokenService::ScopeSet(),
150 &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_);
165 TEST_F(OAuth2TokenServiceTest, SuccessWithCaching) {
166 OAuth2TokenService::ScopeSet scopes1;
167 scopes1.insert("s1");
168 scopes1.insert("s2");
169 OAuth2TokenService::ScopeSet scopes1_same;
170 scopes1_same.insert("s2");
171 scopes1_same.insert("s1");
172 OAuth2TokenService::ScopeSet scopes2;
173 scopes2.insert("s3");
175 oauth2_service_->set_refresh_token(account_id_, "refreshToken");
177 // First request.
178 scoped_ptr<OAuth2TokenService::Request> request(
179 oauth2_service_->StartRequest(account_id_, scopes1, &consumer_));
180 base::RunLoop().RunUntilIdle();
182 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
183 EXPECT_EQ(0, consumer_.number_of_errors_);
184 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
185 ASSERT_TRUE(fetcher);
186 fetcher->set_response_code(net::HTTP_OK);
187 fetcher->SetResponseString(GetValidTokenResponse("token", 3600));
188 fetcher->delegate()->OnURLFetchComplete(fetcher);
189 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
190 EXPECT_EQ(0, consumer_.number_of_errors_);
191 EXPECT_EQ("token", consumer_.last_token_);
193 // Second request to the same set of scopes, should return the same token
194 // without needing a network request.
195 scoped_ptr<OAuth2TokenService::Request> request2(
196 oauth2_service_->StartRequest(account_id_, scopes1_same, &consumer_));
197 base::RunLoop().RunUntilIdle();
199 // No new network fetcher.
200 EXPECT_EQ(2, consumer_.number_of_successful_tokens_);
201 EXPECT_EQ(0, consumer_.number_of_errors_);
202 EXPECT_EQ("token", consumer_.last_token_);
204 // Third request to a new set of scopes, should return another token.
205 scoped_ptr<OAuth2TokenService::Request> request3(
206 oauth2_service_->StartRequest(account_id_, scopes2, &consumer_));
207 base::RunLoop().RunUntilIdle();
208 EXPECT_EQ(2, consumer_.number_of_successful_tokens_);
209 EXPECT_EQ(0, consumer_.number_of_errors_);
210 fetcher = factory_.GetFetcherByID(0);
211 ASSERT_TRUE(fetcher);
212 fetcher->set_response_code(net::HTTP_OK);
213 fetcher->SetResponseString(GetValidTokenResponse("token2", 3600));
214 fetcher->delegate()->OnURLFetchComplete(fetcher);
215 EXPECT_EQ(3, consumer_.number_of_successful_tokens_);
216 EXPECT_EQ(0, consumer_.number_of_errors_);
217 EXPECT_EQ("token2", consumer_.last_token_);
220 TEST_F(OAuth2TokenServiceTest, SuccessAndExpirationAndFailure) {
221 oauth2_service_->set_refresh_token(account_id_, "refreshToken");
223 // First request.
224 scoped_ptr<OAuth2TokenService::Request> request(
225 oauth2_service_->StartRequest(account_id_,
226 OAuth2TokenService::ScopeSet(),
227 &consumer_));
228 base::RunLoop().RunUntilIdle();
229 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
230 EXPECT_EQ(0, consumer_.number_of_errors_);
231 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
232 ASSERT_TRUE(fetcher);
233 fetcher->set_response_code(net::HTTP_OK);
234 fetcher->SetResponseString(GetValidTokenResponse("token", 0));
235 fetcher->delegate()->OnURLFetchComplete(fetcher);
236 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
237 EXPECT_EQ(0, consumer_.number_of_errors_);
238 EXPECT_EQ("token", consumer_.last_token_);
240 // Second request must try to access the network as the token has expired.
241 scoped_ptr<OAuth2TokenService::Request> request2(
242 oauth2_service_->StartRequest(account_id_,
243 OAuth2TokenService::ScopeSet(),
244 &consumer_));
245 base::RunLoop().RunUntilIdle();
246 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
247 EXPECT_EQ(0, consumer_.number_of_errors_);
249 // Network failure.
250 fetcher = factory_.GetFetcherByID(0);
251 ASSERT_TRUE(fetcher);
252 fetcher->set_response_code(net::HTTP_UNAUTHORIZED);
253 fetcher->SetResponseString(std::string());
254 fetcher->delegate()->OnURLFetchComplete(fetcher);
255 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
256 EXPECT_EQ(1, consumer_.number_of_errors_);
259 TEST_F(OAuth2TokenServiceTest, SuccessAndExpirationAndSuccess) {
260 oauth2_service_->set_refresh_token(account_id_, "refreshToken");
262 // First request.
263 scoped_ptr<OAuth2TokenService::Request> request(
264 oauth2_service_->StartRequest(account_id_,
265 OAuth2TokenService::ScopeSet(),
266 &consumer_));
267 base::RunLoop().RunUntilIdle();
268 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
269 EXPECT_EQ(0, consumer_.number_of_errors_);
270 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
271 ASSERT_TRUE(fetcher);
272 fetcher->set_response_code(net::HTTP_OK);
273 fetcher->SetResponseString(GetValidTokenResponse("token", 0));
274 fetcher->delegate()->OnURLFetchComplete(fetcher);
275 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
276 EXPECT_EQ(0, consumer_.number_of_errors_);
277 EXPECT_EQ("token", consumer_.last_token_);
279 // Second request must try to access the network as the token has expired.
280 scoped_ptr<OAuth2TokenService::Request> request2(
281 oauth2_service_->StartRequest(account_id_,
282 OAuth2TokenService::ScopeSet(),
283 &consumer_));
284 base::RunLoop().RunUntilIdle();
285 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
286 EXPECT_EQ(0, consumer_.number_of_errors_);
288 fetcher = factory_.GetFetcherByID(0);
289 ASSERT_TRUE(fetcher);
290 fetcher->set_response_code(net::HTTP_OK);
291 fetcher->SetResponseString(GetValidTokenResponse("another token", 0));
292 fetcher->delegate()->OnURLFetchComplete(fetcher);
293 EXPECT_EQ(2, consumer_.number_of_successful_tokens_);
294 EXPECT_EQ(0, consumer_.number_of_errors_);
295 EXPECT_EQ("another token", consumer_.last_token_);
298 TEST_F(OAuth2TokenServiceTest, RequestDeletedBeforeCompletion) {
299 oauth2_service_->set_refresh_token(account_id_, "refreshToken");
301 scoped_ptr<OAuth2TokenService::Request> request(
302 oauth2_service_->StartRequest(account_id_,
303 OAuth2TokenService::ScopeSet(),
304 &consumer_));
305 base::RunLoop().RunUntilIdle();
306 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
307 EXPECT_EQ(0, consumer_.number_of_errors_);
308 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
309 ASSERT_TRUE(fetcher);
311 request.reset();
313 fetcher->set_response_code(net::HTTP_OK);
314 fetcher->SetResponseString(GetValidTokenResponse("token", 3600));
315 fetcher->delegate()->OnURLFetchComplete(fetcher);
316 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
317 EXPECT_EQ(0, consumer_.number_of_errors_);
320 TEST_F(OAuth2TokenServiceTest, RequestDeletedAfterCompletion) {
321 oauth2_service_->set_refresh_token(account_id_, "refreshToken");
323 scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest(
324 account_id_, OAuth2TokenService::ScopeSet(), &consumer_));
325 base::RunLoop().RunUntilIdle();
326 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
327 ASSERT_TRUE(fetcher);
328 fetcher->set_response_code(net::HTTP_OK);
329 fetcher->SetResponseString(GetValidTokenResponse("token", 3600));
330 fetcher->delegate()->OnURLFetchComplete(fetcher);
331 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
332 EXPECT_EQ(0, consumer_.number_of_errors_);
333 EXPECT_EQ("token", consumer_.last_token_);
335 request.reset();
337 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
338 EXPECT_EQ(0, consumer_.number_of_errors_);
339 EXPECT_EQ("token", consumer_.last_token_);
342 TEST_F(OAuth2TokenServiceTest, MultipleRequestsForTheSameScopesWithOneDeleted) {
343 oauth2_service_->set_refresh_token(account_id_, "refreshToken");
345 scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest(
346 account_id_, OAuth2TokenService::ScopeSet(), &consumer_));
347 base::RunLoop().RunUntilIdle();
348 scoped_ptr<OAuth2TokenService::Request> request2(
349 oauth2_service_->StartRequest(account_id_, OAuth2TokenService::ScopeSet(),
350 &consumer_));
351 base::RunLoop().RunUntilIdle();
353 request.reset();
355 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
356 ASSERT_TRUE(fetcher);
357 fetcher->set_response_code(net::HTTP_OK);
358 fetcher->SetResponseString(GetValidTokenResponse("token", 3600));
359 fetcher->delegate()->OnURLFetchComplete(fetcher);
360 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
361 EXPECT_EQ(0, consumer_.number_of_errors_);
364 TEST_F(OAuth2TokenServiceTest, ClearedRefreshTokenFailsSubsequentRequests) {
365 // We have a valid refresh token; the first request is successful.
366 oauth2_service_->set_refresh_token(account_id_, "refreshToken");
367 scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest(
368 account_id_, OAuth2TokenService::ScopeSet(), &consumer_));
369 base::RunLoop().RunUntilIdle();
370 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
371 ASSERT_TRUE(fetcher);
372 fetcher->set_response_code(net::HTTP_OK);
373 fetcher->SetResponseString(GetValidTokenResponse("token", 3600));
374 fetcher->delegate()->OnURLFetchComplete(fetcher);
375 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
376 EXPECT_EQ(0, consumer_.number_of_errors_);
377 EXPECT_EQ("token", consumer_.last_token_);
379 // The refresh token is no longer available; subsequent requests fail.
380 oauth2_service_->set_refresh_token(account_id_, "");
381 request = oauth2_service_->StartRequest(account_id_,
382 OAuth2TokenService::ScopeSet(), &consumer_);
383 base::RunLoop().RunUntilIdle();
384 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
385 EXPECT_EQ(1, consumer_.number_of_errors_);
388 TEST_F(OAuth2TokenServiceTest,
389 ChangedRefreshTokenDoesNotAffectInFlightRequests) {
390 oauth2_service_->set_refresh_token(account_id_, "first refreshToken");
391 OAuth2TokenService::ScopeSet scopes;
392 scopes.insert("s1");
393 scopes.insert("s2");
394 OAuth2TokenService::ScopeSet scopes1;
395 scopes.insert("s3");
396 scopes.insert("s4");
398 scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest(
399 account_id_, scopes, &consumer_));
400 base::RunLoop().RunUntilIdle();
401 net::TestURLFetcher* fetcher1 = factory_.GetFetcherByID(0);
402 ASSERT_TRUE(fetcher1);
404 // Note |request| is still pending when the refresh token changes.
405 oauth2_service_->set_refresh_token(account_id_, "second refreshToken");
407 // A 2nd request (using the new refresh token) that occurs and completes
408 // while the 1st request is in flight is successful.
409 TestingOAuth2TokenServiceConsumer consumer2;
410 scoped_ptr<OAuth2TokenService::Request> request2(
411 oauth2_service_->StartRequest(account_id_, scopes1, &consumer2));
412 base::RunLoop().RunUntilIdle();
414 net::TestURLFetcher* fetcher2 = factory_.GetFetcherByID(0);
415 fetcher2->set_response_code(net::HTTP_OK);
416 fetcher2->SetResponseString(GetValidTokenResponse("second token", 3600));
417 fetcher2->delegate()->OnURLFetchComplete(fetcher2);
418 EXPECT_EQ(1, consumer2.number_of_successful_tokens_);
419 EXPECT_EQ(0, consumer2.number_of_errors_);
420 EXPECT_EQ("second token", consumer2.last_token_);
422 fetcher1->set_response_code(net::HTTP_OK);
423 fetcher1->SetResponseString(GetValidTokenResponse("first token", 3600));
424 fetcher1->delegate()->OnURLFetchComplete(fetcher1);
425 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
426 EXPECT_EQ(0, consumer_.number_of_errors_);
427 EXPECT_EQ("first token", consumer_.last_token_);
430 TEST_F(OAuth2TokenServiceTest, ServiceShutDownBeforeFetchComplete) {
431 oauth2_service_->set_refresh_token(account_id_, "refreshToken");
432 scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest(
433 account_id_, OAuth2TokenService::ScopeSet(), &consumer_));
434 base::RunLoop().RunUntilIdle();
435 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
436 EXPECT_EQ(0, consumer_.number_of_errors_);
438 // The destructor should cancel all in-flight fetchers.
439 oauth2_service_.reset(NULL);
441 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
442 EXPECT_EQ(1, consumer_.number_of_errors_);
445 TEST_F(OAuth2TokenServiceTest, RetryingConsumer) {
446 oauth2_service_->set_refresh_token(account_id_, "refreshToken");
447 RetryingTestingOAuth2TokenServiceConsumer consumer(oauth2_service_.get(),
448 account_id_);
449 scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest(
450 account_id_, OAuth2TokenService::ScopeSet(), &consumer));
451 base::RunLoop().RunUntilIdle();
452 EXPECT_EQ(0, consumer.number_of_successful_tokens_);
453 EXPECT_EQ(0, consumer.number_of_errors_);
455 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
456 ASSERT_TRUE(fetcher);
457 fetcher->set_response_code(net::HTTP_UNAUTHORIZED);
458 fetcher->SetResponseString(std::string());
459 fetcher->delegate()->OnURLFetchComplete(fetcher);
460 EXPECT_EQ(0, consumer.number_of_successful_tokens_);
461 EXPECT_EQ(1, consumer.number_of_errors_);
463 fetcher = factory_.GetFetcherByID(0);
464 ASSERT_TRUE(fetcher);
465 fetcher->set_response_code(net::HTTP_UNAUTHORIZED);
466 fetcher->SetResponseString(std::string());
467 fetcher->delegate()->OnURLFetchComplete(fetcher);
468 EXPECT_EQ(0, consumer.number_of_successful_tokens_);
469 EXPECT_EQ(2, consumer.number_of_errors_);
472 TEST_F(OAuth2TokenServiceTest, InvalidateToken) {
473 OAuth2TokenService::ScopeSet scopes;
474 oauth2_service_->set_refresh_token(account_id_, "refreshToken");
476 // First request.
477 scoped_ptr<OAuth2TokenService::Request> request(
478 oauth2_service_->StartRequest(account_id_, scopes, &consumer_));
479 base::RunLoop().RunUntilIdle();
481 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
482 EXPECT_EQ(0, consumer_.number_of_errors_);
483 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
484 ASSERT_TRUE(fetcher);
485 fetcher->set_response_code(net::HTTP_OK);
486 fetcher->SetResponseString(GetValidTokenResponse("token", 3600));
487 fetcher->delegate()->OnURLFetchComplete(fetcher);
488 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
489 EXPECT_EQ(0, consumer_.number_of_errors_);
490 EXPECT_EQ("token", consumer_.last_token_);
492 // Second request, should return the same token without needing a network
493 // request.
494 scoped_ptr<OAuth2TokenService::Request> request2(
495 oauth2_service_->StartRequest(account_id_, scopes, &consumer_));
496 base::RunLoop().RunUntilIdle();
498 // No new network fetcher.
499 EXPECT_EQ(2, consumer_.number_of_successful_tokens_);
500 EXPECT_EQ(0, consumer_.number_of_errors_);
501 EXPECT_EQ("token", consumer_.last_token_);
503 // Invalidating the token should return a new token on the next request.
504 oauth2_service_->InvalidateToken(account_id_, scopes, consumer_.last_token_);
505 scoped_ptr<OAuth2TokenService::Request> request3(
506 oauth2_service_->StartRequest(account_id_, scopes, &consumer_));
507 base::RunLoop().RunUntilIdle();
508 EXPECT_EQ(2, consumer_.number_of_successful_tokens_);
509 EXPECT_EQ(0, consumer_.number_of_errors_);
510 fetcher = factory_.GetFetcherByID(0);
511 ASSERT_TRUE(fetcher);
512 fetcher->set_response_code(net::HTTP_OK);
513 fetcher->SetResponseString(GetValidTokenResponse("token2", 3600));
514 fetcher->delegate()->OnURLFetchComplete(fetcher);
515 EXPECT_EQ(3, consumer_.number_of_successful_tokens_);
516 EXPECT_EQ(0, consumer_.number_of_errors_);
517 EXPECT_EQ("token2", consumer_.last_token_);
520 TEST_F(OAuth2TokenServiceTest, CancelAllRequests) {
521 oauth2_service_->set_refresh_token(account_id_, "refreshToken");
522 scoped_ptr<OAuth2TokenService::Request> request(
523 oauth2_service_->StartRequest(account_id_, OAuth2TokenService::ScopeSet(),
524 &consumer_));
526 oauth2_service_->set_refresh_token("account_id_2", "refreshToken2");
527 scoped_ptr<OAuth2TokenService::Request> request2(
528 oauth2_service_->StartRequest(account_id_, OAuth2TokenService::ScopeSet(),
529 &consumer_));
531 base::RunLoop().RunUntilIdle();
532 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
533 EXPECT_EQ(0, consumer_.number_of_errors_);
535 oauth2_service_->CancelAllRequestsForTest();
537 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
538 EXPECT_EQ(2, consumer_.number_of_errors_);
541 TEST_F(OAuth2TokenServiceTest, CancelRequestsForAccount) {
542 OAuth2TokenService::ScopeSet scope_set_1;
543 scope_set_1.insert("scope1");
544 scope_set_1.insert("scope2");
545 OAuth2TokenService::ScopeSet scope_set_2(scope_set_1.begin(),
546 scope_set_1.end());
547 scope_set_2.insert("scope3");
549 oauth2_service_->set_refresh_token(account_id_, "refreshToken");
550 scoped_ptr<OAuth2TokenService::Request> request1(
551 oauth2_service_->StartRequest(account_id_, scope_set_1, &consumer_));
552 scoped_ptr<OAuth2TokenService::Request> request2(
553 oauth2_service_->StartRequest(account_id_, scope_set_2, &consumer_));
555 std::string account_id_2("account_id_2");
556 oauth2_service_->set_refresh_token(account_id_2, "refreshToken2");
557 scoped_ptr<OAuth2TokenService::Request> request3(
558 oauth2_service_->StartRequest(account_id_2, scope_set_1, &consumer_));
560 base::RunLoop().RunUntilIdle();
561 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
562 EXPECT_EQ(0, consumer_.number_of_errors_);
564 oauth2_service_->CancelRequestsForAccountForTest(account_id_);
566 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
567 EXPECT_EQ(2, consumer_.number_of_errors_);
569 oauth2_service_->CancelRequestsForAccountForTest(account_id_2);
571 EXPECT_EQ(0, consumer_.number_of_successful_tokens_);
572 EXPECT_EQ(3, consumer_.number_of_errors_);
575 TEST_F(OAuth2TokenServiceTest, SameScopesRequestedForDifferentClients) {
576 std::string client_id_1("client1");
577 std::string client_secret_1("secret1");
578 std::string client_id_2("client2");
579 std::string client_secret_2("secret2");
580 std::set<std::string> scope_set;
581 scope_set.insert("scope1");
582 scope_set.insert("scope2");
584 std::string refresh_token("refreshToken");
585 oauth2_service_->set_refresh_token(account_id_, refresh_token);
587 scoped_ptr<OAuth2TokenService::Request> request1(
588 oauth2_service_->StartRequestForClient(account_id_,
589 client_id_1,
590 client_secret_1,
591 scope_set,
592 &consumer_));
593 scoped_ptr<OAuth2TokenService::Request> request2(
594 oauth2_service_->StartRequestForClient(account_id_,
595 client_id_2,
596 client_secret_2,
597 scope_set,
598 &consumer_));
599 // Start a request that should be duplicate of |request1|.
600 scoped_ptr<OAuth2TokenService::Request> request3(
601 oauth2_service_->StartRequestForClient(account_id_,
602 client_id_1,
603 client_secret_1,
604 scope_set,
605 &consumer_));
606 base::RunLoop().RunUntilIdle();
608 ASSERT_EQ(2U,
609 oauth2_service_->GetNumPendingRequestsForTesting(
610 client_id_1,
611 account_id_,
612 scope_set));
613 ASSERT_EQ(1U,
614 oauth2_service_->GetNumPendingRequestsForTesting(
615 client_id_2,
616 account_id_,
617 scope_set));
620 TEST_F(OAuth2TokenServiceTest, RequestParametersOrderTest) {
621 OAuth2TokenService::ScopeSet set_0;
622 OAuth2TokenService::ScopeSet set_1;
623 set_1.insert("1");
625 OAuth2TokenService::RequestParameters params[] = {
626 OAuth2TokenService::RequestParameters("0", "0", set_0),
627 OAuth2TokenService::RequestParameters("0", "0", set_1),
628 OAuth2TokenService::RequestParameters("0", "1", set_0),
629 OAuth2TokenService::RequestParameters("0", "1", set_1),
630 OAuth2TokenService::RequestParameters("1", "0", set_0),
631 OAuth2TokenService::RequestParameters("1", "0", set_1),
632 OAuth2TokenService::RequestParameters("1", "1", set_0),
633 OAuth2TokenService::RequestParameters("1", "1", set_1),
636 for (size_t i = 0; i < arraysize(params); i++) {
637 for (size_t j = 0; j < arraysize(params); j++) {
638 if (i == j) {
639 EXPECT_FALSE(params[i] < params[j]) << " i=" << i << ", j=" << j;
640 EXPECT_FALSE(params[j] < params[i]) << " i=" << i << ", j=" << j;
641 } else if (i < j) {
642 EXPECT_TRUE(params[i] < params[j]) << " i=" << i << ", j=" << j;
643 EXPECT_FALSE(params[j] < params[i]) << " i=" << i << ", j=" << j;
644 } else {
645 EXPECT_TRUE(params[j] < params[i]) << " i=" << i << ", j=" << j;
646 EXPECT_FALSE(params[i] < params[j]) << " i=" << i << ", j=" << j;