OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/string_util.h" | 5 #include "base/string_util.h" |
6 #include "base/stringprintf.h" | 6 #include "base/stringprintf.h" |
7 #include "base/values.h" | 7 #include "base/values.h" |
8 #include "chrome/browser/extensions/api/identity/identity_api.h" | 8 #include "chrome/browser/extensions/api/identity/identity_api.h" |
9 #include "chrome/browser/extensions/api/identity/web_auth_flow.h" | 9 #include "chrome/browser/extensions/api/identity/web_auth_flow.h" |
10 #include "chrome/browser/extensions/extension_apitest.h" | 10 #include "chrome/browser/extensions/extension_apitest.h" |
11 #include "chrome/browser/extensions/extension_browsertest.h" | 11 #include "chrome/browser/extensions/extension_browsertest.h" |
12 #include "chrome/browser/extensions/extension_function_test_utils.h" | 12 #include "chrome/browser/extensions/extension_function_test_utils.h" |
13 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
14 #include "chrome/browser/ui/browser_window.h" | 14 #include "chrome/browser/ui/browser_window.h" |
15 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
16 #include "chrome/common/chrome_notification_types.h" | 16 #include "chrome/common/chrome_notification_types.h" |
17 #include "chrome/common/extensions/api/identity/oauth2_manifest_handler.h" | 17 #include "chrome/common/extensions/api/identity/oauth2_manifest_handler.h" |
18 #include "chrome/test/base/in_process_browser_test.h" | 18 #include "chrome/test/base/in_process_browser_test.h" |
19 #include "content/public/browser/notification_service.h" | 19 #include "content/public/browser/notification_service.h" |
20 #include "content/public/browser/notification_source.h" | 20 #include "content/public/browser/notification_source.h" |
21 #include "content/public/test/test_utils.h" | 21 #include "content/public/test/test_utils.h" |
| 22 #include "extensions/common/id_util.h" |
22 #include "google_apis/gaia/google_service_auth_error.h" | 23 #include "google_apis/gaia/google_service_auth_error.h" |
23 #include "google_apis/gaia/oauth2_mint_token_flow.h" | 24 #include "google_apis/gaia/oauth2_mint_token_flow.h" |
24 #include "googleurl/src/gurl.h" | 25 #include "googleurl/src/gurl.h" |
25 #include "testing/gmock/include/gmock/gmock.h" | 26 #include "testing/gmock/include/gmock/gmock.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
27 | 28 |
28 using testing::_; | 29 using testing::_; |
29 using testing::Return; | 30 using testing::Return; |
30 using testing::ReturnRef; | 31 using testing::ReturnRef; |
31 | 32 |
32 namespace extensions { | 33 namespace extensions { |
33 | 34 |
34 namespace { | 35 namespace { |
35 | 36 |
36 namespace errors = identity_constants; | 37 namespace errors = identity_constants; |
37 namespace utils = extension_function_test_utils; | 38 namespace utils = extension_function_test_utils; |
38 | 39 |
39 static const char kAccessToken[] = "auth_token"; | 40 static const char kAccessToken[] = "auth_token"; |
| 41 static const char kExtensionId[] = "ext_id"; |
40 | 42 |
41 // This helps us be able to wait until an AsyncExtensionFunction calls | 43 // This helps us be able to wait until an AsyncExtensionFunction calls |
42 // SendResponse. | 44 // SendResponse. |
43 class SendResponseDelegate | 45 class SendResponseDelegate |
44 : public UIThreadExtensionFunction::DelegateForTests { | 46 : public UIThreadExtensionFunction::DelegateForTests { |
45 public: | 47 public: |
46 SendResponseDelegate() : should_post_quit_(false) {} | 48 SendResponseDelegate() : should_post_quit_(false) {} |
47 | 49 |
48 virtual ~SendResponseDelegate() {} | 50 virtual ~SendResponseDelegate() {} |
49 | 51 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 } | 153 } |
152 | 154 |
153 virtual void Start() OVERRIDE { | 155 virtual void Start() OVERRIDE { |
154 switch (result_) { | 156 switch (result_) { |
155 case ISSUE_ADVICE_SUCCESS: { | 157 case ISSUE_ADVICE_SUCCESS: { |
156 IssueAdviceInfo info; | 158 IssueAdviceInfo info; |
157 delegate_->OnIssueAdviceSuccess(info); | 159 delegate_->OnIssueAdviceSuccess(info); |
158 break; | 160 break; |
159 } | 161 } |
160 case MINT_TOKEN_SUCCESS: { | 162 case MINT_TOKEN_SUCCESS: { |
161 delegate_->OnMintTokenSuccess(kAccessToken); | 163 delegate_->OnMintTokenSuccess(kAccessToken, 3600); |
162 break; | 164 break; |
163 } | 165 } |
164 case MINT_TOKEN_FAILURE: { | 166 case MINT_TOKEN_FAILURE: { |
165 GoogleServiceAuthError error(GoogleServiceAuthError::CONNECTION_FAILED); | 167 GoogleServiceAuthError error(GoogleServiceAuthError::CONNECTION_FAILED); |
166 delegate_->OnMintTokenFailure(error); | 168 delegate_->OnMintTokenFailure(error); |
167 break; | 169 break; |
168 } | 170 } |
169 case MINT_TOKEN_BAD_CREDENTIALS: { | 171 case MINT_TOKEN_BAD_CREDENTIALS: { |
170 GoogleServiceAuthError error( | 172 GoogleServiceAuthError error( |
171 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); | 173 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 OAuth2Info::GetOAuth2Info(ext)); | 268 OAuth2Info::GetOAuth2Info(ext)); |
267 if ((fields_to_set & CLIENT_ID) != 0) | 269 if ((fields_to_set & CLIENT_ID) != 0) |
268 oauth2_info.client_id = "client1"; | 270 oauth2_info.client_id = "client1"; |
269 if ((fields_to_set & SCOPES) != 0) { | 271 if ((fields_to_set & SCOPES) != 0) { |
270 oauth2_info.scopes.push_back("scope1"); | 272 oauth2_info.scopes.push_back("scope1"); |
271 oauth2_info.scopes.push_back("scope2"); | 273 oauth2_info.scopes.push_back("scope2"); |
272 } | 274 } |
273 return ext; | 275 return ext; |
274 } | 276 } |
275 | 277 |
276 void InitializeTestAPIFactory() { | 278 IdentityAPI* id_api() { |
277 IdentityAPI::GetFactoryInstance()->SetTestingFactory( | 279 return IdentityAPI::GetFactoryInstance()->GetForProfile( |
278 browser()->profile(), &IdentityAPITestFactory); | 280 browser()->profile()); |
279 } | 281 } |
280 }; | 282 }; |
281 | 283 |
282 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, | 284 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, |
283 NoClientId) { | 285 NoClientId) { |
284 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); | 286 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); |
285 func->set_extension(CreateExtension(SCOPES)); | 287 func->set_extension(CreateExtension(SCOPES)); |
286 std::string error = utils::RunFunctionAndReturnError( | 288 std::string error = utils::RunFunctionAndReturnError( |
287 func.get(), "[{}]", browser()); | 289 func.get(), "[{}]", browser()); |
288 EXPECT_EQ(std::string(errors::kInvalidClientId), error); | 290 EXPECT_EQ(std::string(errors::kInvalidClientId), error); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 TestOAuth2MintTokenFlow::MINT_TOKEN_FAILURE, func.get()); | 325 TestOAuth2MintTokenFlow::MINT_TOKEN_FAILURE, func.get()); |
324 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow)); | 326 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow)); |
325 std::string error = utils::RunFunctionAndReturnError( | 327 std::string error = utils::RunFunctionAndReturnError( |
326 func.get(), "[{}]", browser()); | 328 func.get(), "[{}]", browser()); |
327 EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false)); | 329 EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false)); |
328 EXPECT_FALSE(func->login_ui_shown()); | 330 EXPECT_FALSE(func->login_ui_shown()); |
329 EXPECT_FALSE(func->install_ui_shown()); | 331 EXPECT_FALSE(func->install_ui_shown()); |
330 } | 332 } |
331 | 333 |
332 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, | 334 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, |
| 335 NonInteractiveMintAdviceSuccess) { |
| 336 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); |
| 337 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); |
| 338 func->set_extension(extension); |
| 339 EXPECT_CALL(*func.get(), HasLoginToken()) |
| 340 .WillOnce(Return(true)); |
| 341 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( |
| 342 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get()); |
| 343 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow)); |
| 344 std::string error = utils::RunFunctionAndReturnError( |
| 345 func.get(), "[{}]", browser()); |
| 346 EXPECT_TRUE(StartsWithASCII(error, errors::kNoGrant, false)); |
| 347 EXPECT_FALSE(func->login_ui_shown()); |
| 348 EXPECT_FALSE(func->install_ui_shown()); |
| 349 |
| 350 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(extension); |
| 351 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_ADVICE, |
| 352 id_api()->GetCachedToken(extension->id(), |
| 353 oauth2_info.scopes).status()); |
| 354 } |
| 355 |
| 356 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, |
333 NonInteractiveMintBadCredentials) { | 357 NonInteractiveMintBadCredentials) { |
334 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); | 358 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); |
335 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); | 359 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); |
336 EXPECT_CALL(*func.get(), HasLoginToken()) | 360 EXPECT_CALL(*func.get(), HasLoginToken()) |
337 .WillOnce(Return(true)); | 361 .WillOnce(Return(true)); |
338 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( | 362 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( |
339 TestOAuth2MintTokenFlow::MINT_TOKEN_BAD_CREDENTIALS, func.get()); | 363 TestOAuth2MintTokenFlow::MINT_TOKEN_BAD_CREDENTIALS, func.get()); |
340 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow)); | 364 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow)); |
341 std::string error = utils::RunFunctionAndReturnError( | 365 std::string error = utils::RunFunctionAndReturnError( |
342 func.get(), "[{}]", browser()); | 366 func.get(), "[{}]", browser()); |
343 EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false)); | 367 EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false)); |
344 EXPECT_FALSE(func->login_ui_shown()); | 368 EXPECT_FALSE(func->login_ui_shown()); |
345 EXPECT_FALSE(func->install_ui_shown()); | 369 EXPECT_FALSE(func->install_ui_shown()); |
346 } | 370 } |
347 | 371 |
348 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, | 372 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, |
349 NonInteractiveSuccess) { | 373 NonInteractiveSuccess) { |
350 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); | 374 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); |
351 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); | 375 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); |
| 376 func->set_extension(extension); |
| 377 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(extension); |
352 EXPECT_CALL(*func.get(), HasLoginToken()) | 378 EXPECT_CALL(*func.get(), HasLoginToken()) |
353 .WillOnce(Return(true)); | 379 .WillOnce(Return(true)); |
354 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( | 380 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( |
355 TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS, func.get()); | 381 TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS, func.get()); |
356 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow)); | 382 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow)); |
357 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( | 383 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( |
358 func.get(), "[{}]", browser())); | 384 func.get(), "[{}]", browser())); |
359 std::string access_token; | 385 std::string access_token; |
360 EXPECT_TRUE(value->GetAsString(&access_token)); | 386 EXPECT_TRUE(value->GetAsString(&access_token)); |
361 EXPECT_EQ(std::string(kAccessToken), access_token); | 387 EXPECT_EQ(std::string(kAccessToken), access_token); |
362 EXPECT_FALSE(func->login_ui_shown()); | 388 EXPECT_FALSE(func->login_ui_shown()); |
363 EXPECT_FALSE(func->install_ui_shown()); | 389 EXPECT_FALSE(func->install_ui_shown()); |
| 390 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN, |
| 391 id_api()->GetCachedToken(extension->id(), |
| 392 oauth2_info.scopes).status()); |
364 } | 393 } |
365 | 394 |
366 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, | 395 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, |
367 InteractiveLoginCanceled) { | 396 InteractiveLoginCanceled) { |
368 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); | 397 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); |
369 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); | 398 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); |
370 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(false)); | 399 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(false)); |
371 func->set_login_ui_result(false); | 400 func->set_login_ui_result(false); |
372 std::string error = utils::RunFunctionAndReturnError( | 401 std::string error = utils::RunFunctionAndReturnError( |
373 func.get(), "[{\"interactive\": true}]", browser()); | 402 func.get(), "[{\"interactive\": true}]", browser()); |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
479 std::string error = utils::RunFunctionAndReturnError( | 508 std::string error = utils::RunFunctionAndReturnError( |
480 func.get(), "[{\"interactive\": true}]", browser()); | 509 func.get(), "[{\"interactive\": true}]", browser()); |
481 EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false)); | 510 EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false)); |
482 EXPECT_TRUE(func->login_ui_shown()); | 511 EXPECT_TRUE(func->login_ui_shown()); |
483 EXPECT_TRUE(func->install_ui_shown()); | 512 EXPECT_TRUE(func->install_ui_shown()); |
484 } | 513 } |
485 | 514 |
486 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, | 515 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, |
487 InteractiveLoginSuccessApprovalDoneMintSuccess) { | 516 InteractiveLoginSuccessApprovalDoneMintSuccess) { |
488 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); | 517 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); |
489 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); | 518 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); |
| 519 func->set_extension(extension); |
| 520 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(extension); |
490 EXPECT_CALL(*func.get(), HasLoginToken()) | 521 EXPECT_CALL(*func.get(), HasLoginToken()) |
491 .WillOnce(Return(false)); | 522 .WillOnce(Return(false)); |
492 func->set_login_ui_result(true); | 523 func->set_login_ui_result(true); |
493 TestOAuth2MintTokenFlow* flow1 = new TestOAuth2MintTokenFlow( | 524 TestOAuth2MintTokenFlow* flow1 = new TestOAuth2MintTokenFlow( |
494 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get()); | 525 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get()); |
495 TestOAuth2MintTokenFlow* flow2 = new TestOAuth2MintTokenFlow( | 526 TestOAuth2MintTokenFlow* flow2 = new TestOAuth2MintTokenFlow( |
496 TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS, func.get()); | 527 TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS, func.get()); |
497 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)) | 528 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)) |
498 .WillOnce(Return(flow1)) | 529 .WillOnce(Return(flow1)) |
499 .WillOnce(Return(flow2)); | 530 .WillOnce(Return(flow2)); |
500 | 531 |
501 func->set_install_ui_result(true); | 532 func->set_install_ui_result(true); |
502 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( | 533 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( |
503 func.get(), "[{\"interactive\": true}]", browser())); | 534 func.get(), "[{\"interactive\": true}]", browser())); |
504 std::string access_token; | 535 std::string access_token; |
505 EXPECT_TRUE(value->GetAsString(&access_token)); | 536 EXPECT_TRUE(value->GetAsString(&access_token)); |
506 EXPECT_EQ(std::string(kAccessToken), access_token); | 537 EXPECT_EQ(std::string(kAccessToken), access_token); |
507 EXPECT_TRUE(func->login_ui_shown()); | 538 EXPECT_TRUE(func->login_ui_shown()); |
508 EXPECT_TRUE(func->install_ui_shown()); | 539 EXPECT_TRUE(func->install_ui_shown()); |
| 540 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN, |
| 541 id_api()->GetCachedToken(extension->id(), |
| 542 oauth2_info.scopes).status()); |
509 } | 543 } |
510 | 544 |
511 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, | 545 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, |
512 InteractiveApprovalAborted) { | 546 InteractiveApprovalAborted) { |
513 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); | 547 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); |
514 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); | 548 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); |
515 EXPECT_CALL(*func.get(), HasLoginToken()) | 549 EXPECT_CALL(*func.get(), HasLoginToken()) |
516 .WillOnce(Return(true)); | 550 .WillOnce(Return(true)); |
517 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( | 551 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( |
518 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get()); | 552 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get()); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
565 | 599 |
566 func->set_install_ui_result(true); | 600 func->set_install_ui_result(true); |
567 std::string error = utils::RunFunctionAndReturnError( | 601 std::string error = utils::RunFunctionAndReturnError( |
568 func.get(), "[{\"interactive\": true}]", browser()); | 602 func.get(), "[{\"interactive\": true}]", browser()); |
569 EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false)); | 603 EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false)); |
570 EXPECT_FALSE(func->login_ui_shown()); | 604 EXPECT_FALSE(func->login_ui_shown()); |
571 EXPECT_TRUE(func->install_ui_shown()); | 605 EXPECT_TRUE(func->install_ui_shown()); |
572 } | 606 } |
573 | 607 |
574 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, NoninteractiveQueue) { | 608 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, NoninteractiveQueue) { |
575 InitializeTestAPIFactory(); | |
576 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); | 609 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); |
577 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); | 610 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); |
578 func->set_extension(extension); | 611 func->set_extension(extension); |
579 | 612 |
580 // Create a fake request to block the queue. | 613 // Create a fake request to block the queue. |
581 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(extension); | 614 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(extension); |
582 std::set<std::string> scopes(oauth2_info.scopes.begin(), | 615 std::set<std::string> scopes(oauth2_info.scopes.begin(), |
583 oauth2_info.scopes.end()); | 616 oauth2_info.scopes.end()); |
584 IdentityAPI* id_api = | 617 IdentityAPI* id_api = |
585 extensions::IdentityAPI::GetFactoryInstance()->GetForProfile( | 618 extensions::IdentityAPI::GetFactoryInstance()->GetForProfile( |
(...skipping 22 matching lines...) Expand all Loading... |
608 | 641 |
609 scoped_ptr<base::Value> value(WaitForSingleResult(func)); | 642 scoped_ptr<base::Value> value(WaitForSingleResult(func)); |
610 std::string access_token; | 643 std::string access_token; |
611 EXPECT_TRUE(value->GetAsString(&access_token)); | 644 EXPECT_TRUE(value->GetAsString(&access_token)); |
612 EXPECT_EQ(std::string(kAccessToken), access_token); | 645 EXPECT_EQ(std::string(kAccessToken), access_token); |
613 EXPECT_FALSE(func->login_ui_shown()); | 646 EXPECT_FALSE(func->login_ui_shown()); |
614 EXPECT_FALSE(func->install_ui_shown()); | 647 EXPECT_FALSE(func->install_ui_shown()); |
615 } | 648 } |
616 | 649 |
617 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, InteractiveQueue) { | 650 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, InteractiveQueue) { |
618 InitializeTestAPIFactory(); | |
619 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); | 651 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); |
620 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); | 652 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); |
621 func->set_extension(extension); | 653 func->set_extension(extension); |
622 | 654 |
623 // Create a fake request to block the queue. | 655 // Create a fake request to block the queue. |
624 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(extension); | 656 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(extension); |
625 std::set<std::string> scopes(oauth2_info.scopes.begin(), | 657 std::set<std::string> scopes(oauth2_info.scopes.begin(), |
626 oauth2_info.scopes.end()); | 658 oauth2_info.scopes.end()); |
627 IdentityAPI* id_api = | 659 IdentityAPI* id_api = |
628 extensions::IdentityAPI::GetFactoryInstance()->GetForProfile( | 660 extensions::IdentityAPI::GetFactoryInstance()->GetForProfile( |
(...skipping 29 matching lines...) Expand all Loading... |
658 scoped_ptr<base::Value> value(WaitForSingleResult(func)); | 690 scoped_ptr<base::Value> value(WaitForSingleResult(func)); |
659 std::string access_token; | 691 std::string access_token; |
660 EXPECT_TRUE(value->GetAsString(&access_token)); | 692 EXPECT_TRUE(value->GetAsString(&access_token)); |
661 EXPECT_EQ(std::string(kAccessToken), access_token); | 693 EXPECT_EQ(std::string(kAccessToken), access_token); |
662 EXPECT_FALSE(func->login_ui_shown()); | 694 EXPECT_FALSE(func->login_ui_shown()); |
663 EXPECT_TRUE(func->install_ui_shown()); | 695 EXPECT_TRUE(func->install_ui_shown()); |
664 } | 696 } |
665 | 697 |
666 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, | 698 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, |
667 InteractiveQueuedNoninteractiveFails) { | 699 InteractiveQueuedNoninteractiveFails) { |
668 InitializeTestAPIFactory(); | |
669 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); | 700 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); |
670 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); | 701 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); |
671 func->set_extension(extension); | 702 func->set_extension(extension); |
672 | 703 |
673 // Create a fake request to block the interactive queue. | 704 // Create a fake request to block the interactive queue. |
674 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(extension); | 705 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(extension); |
675 std::set<std::string> scopes(oauth2_info.scopes.begin(), | 706 std::set<std::string> scopes(oauth2_info.scopes.begin(), |
676 oauth2_info.scopes.end()); | 707 oauth2_info.scopes.end()); |
677 IdentityAPI* id_api = | 708 IdentityAPI* id_api = |
678 extensions::IdentityAPI::GetFactoryInstance()->GetForProfile( | 709 extensions::IdentityAPI::GetFactoryInstance()->GetForProfile( |
(...skipping 11 matching lines...) Expand all Loading... |
690 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(true)); | 721 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(true)); |
691 std::string error = utils::RunFunctionAndReturnError( | 722 std::string error = utils::RunFunctionAndReturnError( |
692 func.get(), "[{}]", browser()); | 723 func.get(), "[{}]", browser()); |
693 EXPECT_EQ(std::string(errors::kNoGrant), error); | 724 EXPECT_EQ(std::string(errors::kNoGrant), error); |
694 EXPECT_FALSE(func->login_ui_shown()); | 725 EXPECT_FALSE(func->login_ui_shown()); |
695 EXPECT_FALSE(func->install_ui_shown()); | 726 EXPECT_FALSE(func->install_ui_shown()); |
696 | 727 |
697 queue->RequestComplete(type, extension->id(), scopes, &queued_request); | 728 queue->RequestComplete(type, extension->id(), scopes, &queued_request); |
698 } | 729 } |
699 | 730 |
| 731 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, |
| 732 NonInteractiveCacheHit) { |
| 733 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); |
| 734 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); |
| 735 func->set_extension(extension); |
| 736 |
| 737 // pre-populate the cache with a token |
| 738 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(extension); |
| 739 IdentityTokenCacheValue token(kAccessToken, |
| 740 base::TimeDelta::FromSeconds(3600)); |
| 741 id_api()->SetCachedToken(extension->id(), oauth2_info.scopes, token); |
| 742 |
| 743 // Get a token. Should not require a GAIA request. |
| 744 EXPECT_CALL(*func.get(), HasLoginToken()) |
| 745 .WillOnce(Return(true)); |
| 746 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( |
| 747 func.get(), "[{}]", browser())); |
| 748 std::string access_token; |
| 749 EXPECT_TRUE(value->GetAsString(&access_token)); |
| 750 EXPECT_EQ(std::string(kAccessToken), access_token); |
| 751 EXPECT_FALSE(func->login_ui_shown()); |
| 752 EXPECT_FALSE(func->install_ui_shown()); |
| 753 } |
| 754 |
| 755 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, |
| 756 NonInteractiveIssueAdviceCacheHit) { |
| 757 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); |
| 758 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); |
| 759 func->set_extension(extension); |
| 760 |
| 761 // pre-populate the cache with advice |
| 762 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(extension); |
| 763 IssueAdviceInfo info; |
| 764 IdentityTokenCacheValue token(info); |
| 765 id_api()->SetCachedToken(extension->id(), oauth2_info.scopes, token); |
| 766 |
| 767 // Should return an error without a GAIA request. |
| 768 EXPECT_CALL(*func.get(), HasLoginToken()) |
| 769 .WillOnce(Return(true)); |
| 770 std::string error = utils::RunFunctionAndReturnError( |
| 771 func.get(), "[{}]", browser()); |
| 772 EXPECT_EQ(std::string(errors::kNoGrant), error); |
| 773 EXPECT_FALSE(func->login_ui_shown()); |
| 774 EXPECT_FALSE(func->install_ui_shown()); |
| 775 } |
| 776 |
| 777 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, |
| 778 InteractiveCacheHit) { |
| 779 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); |
| 780 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); |
| 781 func->set_extension(extension); |
| 782 |
| 783 // Create a fake request to block the queue. |
| 784 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(extension); |
| 785 std::set<std::string> scopes(oauth2_info.scopes.begin(), |
| 786 oauth2_info.scopes.end()); |
| 787 IdentityMintRequestQueue* queue = id_api()->mint_queue(); |
| 788 MockQueuedMintRequest queued_request; |
| 789 IdentityMintRequestQueue::MintType type = |
| 790 IdentityMintRequestQueue::MINT_TYPE_INTERACTIVE; |
| 791 |
| 792 EXPECT_CALL(queued_request, StartMintToken(type)).Times(1); |
| 793 queue->RequestStart(type, extension->id(), scopes, &queued_request); |
| 794 |
| 795 // The real request will start processing, but wait in the queue behind |
| 796 // the blocker. |
| 797 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(true)); |
| 798 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( |
| 799 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get()); |
| 800 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow)); |
| 801 RunFunctionAsync(func, "[{\"interactive\": true}]"); |
| 802 |
| 803 // Populate the cache with a token while the request is blocked. |
| 804 IdentityTokenCacheValue token(kAccessToken, |
| 805 base::TimeDelta::FromSeconds(3600)); |
| 806 id_api()->SetCachedToken(extension->id(), oauth2_info.scopes, token); |
| 807 |
| 808 // When we wake up the request, it returns the cached token without |
| 809 // displaying a UI, or hitting GAIA. |
| 810 |
| 811 queue->RequestComplete(type, extension->id(), scopes, &queued_request); |
| 812 |
| 813 scoped_ptr<base::Value> value(WaitForSingleResult(func)); |
| 814 std::string access_token; |
| 815 EXPECT_TRUE(value->GetAsString(&access_token)); |
| 816 EXPECT_EQ(std::string(kAccessToken), access_token); |
| 817 EXPECT_FALSE(func->login_ui_shown()); |
| 818 EXPECT_FALSE(func->install_ui_shown()); |
| 819 } |
| 820 |
| 821 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, |
| 822 LoginInvalidatesTokenCache) { |
| 823 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); |
| 824 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); |
| 825 func->set_extension(extension); |
| 826 |
| 827 // pre-populate the cache with a token |
| 828 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(extension); |
| 829 IdentityTokenCacheValue token(kAccessToken, |
| 830 base::TimeDelta::FromSeconds(3600)); |
| 831 id_api()->SetCachedToken(extension->id(), oauth2_info.scopes, token); |
| 832 |
| 833 // Because the user is not signed in, the token will be removed, |
| 834 // and we'll hit GAIA for new tokens. |
| 835 EXPECT_CALL(*func.get(), HasLoginToken()) |
| 836 .WillOnce(Return(false)); |
| 837 func->set_login_ui_result(true); |
| 838 TestOAuth2MintTokenFlow* flow1 = new TestOAuth2MintTokenFlow( |
| 839 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get()); |
| 840 TestOAuth2MintTokenFlow* flow2 = new TestOAuth2MintTokenFlow( |
| 841 TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS, func.get()); |
| 842 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)) |
| 843 .WillOnce(Return(flow1)) |
| 844 .WillOnce(Return(flow2)); |
| 845 |
| 846 func->set_install_ui_result(true); |
| 847 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( |
| 848 func.get(), "[{\"interactive\": true}]", browser())); |
| 849 std::string access_token; |
| 850 EXPECT_TRUE(value->GetAsString(&access_token)); |
| 851 EXPECT_EQ(std::string(kAccessToken), access_token); |
| 852 EXPECT_TRUE(func->login_ui_shown()); |
| 853 EXPECT_TRUE(func->install_ui_shown()); |
| 854 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN, |
| 855 id_api()->GetCachedToken(extension->id(), |
| 856 oauth2_info.scopes).status()); |
| 857 } |
| 858 |
| 859 class RemoveCachedAuthTokenFunctionTest : public ExtensionBrowserTest { |
| 860 protected: |
| 861 bool InvalidateDefaultToken() { |
| 862 scoped_refptr<IdentityRemoveCachedAuthTokenFunction> func( |
| 863 new IdentityRemoveCachedAuthTokenFunction); |
| 864 func->set_extension(utils::CreateEmptyExtension(kExtensionId)); |
| 865 return utils::RunFunction( |
| 866 func, std::string("[{\"token\": \"") + kAccessToken + "\"}]", browser(), |
| 867 extension_function_test_utils::NONE); |
| 868 } |
| 869 |
| 870 IdentityAPI* id_api() { |
| 871 return IdentityAPI::GetFactoryInstance()->GetForProfile( |
| 872 browser()->profile()); |
| 873 } |
| 874 |
| 875 void SetCachedToken(IdentityTokenCacheValue& token_data) { |
| 876 id_api()->SetCachedToken(extensions::id_util::GenerateId(kExtensionId), |
| 877 std::vector<std::string>(), token_data); |
| 878 } |
| 879 |
| 880 const IdentityTokenCacheValue& GetCachedToken() { |
| 881 return id_api()->GetCachedToken( |
| 882 extensions::id_util::GenerateId(kExtensionId), |
| 883 std::vector<std::string>()); |
| 884 } |
| 885 }; |
| 886 |
| 887 IN_PROC_BROWSER_TEST_F(RemoveCachedAuthTokenFunctionTest, NotFound) { |
| 888 EXPECT_TRUE(InvalidateDefaultToken()); |
| 889 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_NOTFOUND, |
| 890 GetCachedToken().status()); |
| 891 } |
| 892 |
| 893 IN_PROC_BROWSER_TEST_F(RemoveCachedAuthTokenFunctionTest, Advice) { |
| 894 IssueAdviceInfo info; |
| 895 IdentityTokenCacheValue advice(info); |
| 896 SetCachedToken(advice); |
| 897 EXPECT_TRUE(InvalidateDefaultToken()); |
| 898 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_ADVICE, |
| 899 GetCachedToken().status()); |
| 900 } |
| 901 |
| 902 IN_PROC_BROWSER_TEST_F(RemoveCachedAuthTokenFunctionTest, NonMatchingToken) { |
| 903 IdentityTokenCacheValue token("non_matching_token", |
| 904 base::TimeDelta::FromSeconds(3600)); |
| 905 SetCachedToken(token); |
| 906 EXPECT_TRUE(InvalidateDefaultToken()); |
| 907 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN, |
| 908 GetCachedToken().status()); |
| 909 EXPECT_EQ("non_matching_token", GetCachedToken().token()); |
| 910 } |
| 911 |
| 912 IN_PROC_BROWSER_TEST_F(RemoveCachedAuthTokenFunctionTest, MatchingToken) { |
| 913 IdentityTokenCacheValue token(kAccessToken, |
| 914 base::TimeDelta::FromSeconds(3600)); |
| 915 SetCachedToken(token); |
| 916 EXPECT_TRUE(InvalidateDefaultToken()); |
| 917 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_NOTFOUND, |
| 918 GetCachedToken().status()); |
| 919 } |
| 920 |
700 class LaunchWebAuthFlowFunctionTest : public AsyncExtensionBrowserTest { | 921 class LaunchWebAuthFlowFunctionTest : public AsyncExtensionBrowserTest { |
701 protected: | 922 protected: |
702 void RunAndCheckBounds( | 923 void RunAndCheckBounds( |
703 const std::string& extra_params, | 924 const std::string& extra_params, |
704 int expected_x, | 925 int expected_x, |
705 int expected_y, | 926 int expected_y, |
706 int expected_width, | 927 int expected_width, |
707 int expected_height) { | 928 int expected_height) { |
708 content::WindowedNotificationObserver observer( | 929 content::WindowedNotificationObserver observer( |
709 chrome::NOTIFICATION_BROWSER_WINDOW_READY, | 930 chrome::NOTIFICATION_BROWSER_WINDOW_READY, |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
861 "https://abcdefghij.chromiumapp.org/callback#test')</script>\"}]", | 1082 "https://abcdefghij.chromiumapp.org/callback#test')</script>\"}]", |
862 browser())); | 1083 browser())); |
863 | 1084 |
864 std::string url; | 1085 std::string url; |
865 EXPECT_TRUE(value->GetAsString(&url)); | 1086 EXPECT_TRUE(value->GetAsString(&url)); |
866 EXPECT_EQ(std::string("https://abcdefghij.chromiumapp.org/callback#test"), | 1087 EXPECT_EQ(std::string("https://abcdefghij.chromiumapp.org/callback#test"), |
867 url); | 1088 url); |
868 } | 1089 } |
869 | 1090 |
870 } // namespace extensions | 1091 } // namespace extensions |
OLD | NEW |