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

Side by Side Diff: chrome/browser/extensions/api/identity/experimental_identity_apitest.cc

Issue 99173004: Identity API: add multi-account support to token cache and request queues (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/strings/string_util.h" 5 #include "base/strings/string_util.h"
6 #include "base/strings/stringprintf.h" 6 #include "base/strings/stringprintf.h"
7 #include "base/values.h" 7 #include "base/values.h"
8 #include "chrome/browser/chrome_notification_types.h" 8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/extensions/api/identity/experimental_identity_api.h" 9 #include "chrome/browser/extensions/api/identity/experimental_identity_api.h"
10 #include "chrome/browser/extensions/api/identity/identity_api.h" 10 #include "chrome/browser/extensions/api/identity/identity_api.h"
11 #include "chrome/browser/extensions/api/identity/web_auth_flow.h" 11 #include "chrome/browser/extensions/api/identity/web_auth_flow.h"
12 #include "chrome/browser/extensions/extension_apitest.h" 12 #include "chrome/browser/extensions/extension_apitest.h"
13 #include "chrome/browser/extensions/extension_browsertest.h" 13 #include "chrome/browser/extensions/extension_browsertest.h"
14 #include "chrome/browser/extensions/extension_function_test_utils.h" 14 #include "chrome/browser/extensions/extension_function_test_utils.h"
15 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/signin/profile_oauth2_token_service.h"
17 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
16 #include "chrome/browser/ui/browser.h" 18 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/browser_window.h" 19 #include "chrome/browser/ui/browser_window.h"
18 #include "chrome/common/extensions/api/identity/oauth2_manifest_handler.h" 20 #include "chrome/common/extensions/api/identity/oauth2_manifest_handler.h"
19 #include "chrome/test/base/in_process_browser_test.h" 21 #include "chrome/test/base/in_process_browser_test.h"
20 #include "content/public/browser/notification_service.h" 22 #include "content/public/browser/notification_service.h"
21 #include "content/public/browser/notification_source.h" 23 #include "content/public/browser/notification_source.h"
22 #include "content/public/test/test_utils.h" 24 #include "content/public/test/test_utils.h"
23 #include "extensions/common/id_util.h" 25 #include "extensions/common/id_util.h"
24 #include "google_apis/gaia/google_service_auth_error.h" 26 #include "google_apis/gaia/google_service_auth_error.h"
25 #include "google_apis/gaia/oauth2_mint_token_flow.h" 27 #include "google_apis/gaia/oauth2_mint_token_flow.h"
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 const Extension* ext = 262 const Extension* ext =
261 LoadExtension(test_data_dir_.AppendASCII("platform_apps/oauth2")); 263 LoadExtension(test_data_dir_.AppendASCII("platform_apps/oauth2"));
262 OAuth2Info& oauth2_info = 264 OAuth2Info& oauth2_info =
263 const_cast<OAuth2Info&>(OAuth2Info::GetOAuth2Info(ext)); 265 const_cast<OAuth2Info&>(OAuth2Info::GetOAuth2Info(ext));
264 if ((fields_to_set & CLIENT_ID) != 0) 266 if ((fields_to_set & CLIENT_ID) != 0)
265 oauth2_info.client_id = "client1"; 267 oauth2_info.client_id = "client1";
266 if ((fields_to_set & SCOPES) != 0) { 268 if ((fields_to_set & SCOPES) != 0) {
267 oauth2_info.scopes.push_back("scope1"); 269 oauth2_info.scopes.push_back("scope1");
268 oauth2_info.scopes.push_back("scope2"); 270 oauth2_info.scopes.push_back("scope2");
269 } 271 }
272
273 extension_id_ = ext->id();
274 oauth_scopes_ = std::set<std::string>(oauth2_info.scopes.begin(),
275 oauth2_info.scopes.end());
270 return ext; 276 return ext;
271 } 277 }
272 278
273 IdentityAPI* id_api() { 279 IdentityAPI* id_api() {
274 return IdentityAPI::GetFactoryInstance()->GetForProfile( 280 return IdentityAPI::GetFactoryInstance()->GetForProfile(
275 browser()->profile()); 281 browser()->profile());
276 } 282 }
283
284 const IdentityTokenCacheValue& GetCachedToken() {
285 ProfileOAuth2TokenService* token_service =
286 ProfileOAuth2TokenServiceFactory::GetForProfile(browser()->profile());
287 ExtensionTokenKey key(
288 extension_id_, token_service->GetPrimaryAccountId(), oauth_scopes_);
289 return id_api()->GetCachedToken(key);
290 }
291
292 private:
293 std::string extension_id_;
294 std::set<std::string> oauth_scopes_;
277 }; 295 };
278 296
279 IN_PROC_BROWSER_TEST_F(ExperimentalGetAuthTokenFunctionTest, NoClientId) { 297 IN_PROC_BROWSER_TEST_F(ExperimentalGetAuthTokenFunctionTest, NoClientId) {
280 scoped_refptr<ExperimentalMockGetAuthTokenFunction> func( 298 scoped_refptr<ExperimentalMockGetAuthTokenFunction> func(
281 new ExperimentalMockGetAuthTokenFunction()); 299 new ExperimentalMockGetAuthTokenFunction());
282 func->set_extension(CreateExtension(SCOPES)); 300 func->set_extension(CreateExtension(SCOPES));
283 std::string error = 301 std::string error =
284 utils::RunFunctionAndReturnError(func.get(), "[{}]", browser()); 302 utils::RunFunctionAndReturnError(func.get(), "[{}]", browser());
285 EXPECT_EQ(std::string(errors::kInvalidClientId), error); 303 EXPECT_EQ(std::string(errors::kInvalidClientId), error);
286 EXPECT_FALSE(func->login_ui_shown()); 304 EXPECT_FALSE(func->login_ui_shown());
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(true)); 366 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(true));
349 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( 367 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow(
350 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get()); 368 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get());
351 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow)); 369 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
352 std::string error = 370 std::string error =
353 utils::RunFunctionAndReturnError(func.get(), "[{}]", browser()); 371 utils::RunFunctionAndReturnError(func.get(), "[{}]", browser());
354 EXPECT_EQ(std::string(errors::kNoGrant), error); 372 EXPECT_EQ(std::string(errors::kNoGrant), error);
355 EXPECT_FALSE(func->login_ui_shown()); 373 EXPECT_FALSE(func->login_ui_shown());
356 EXPECT_FALSE(func->install_ui_shown()); 374 EXPECT_FALSE(func->install_ui_shown());
357 375
358 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(extension.get()); 376 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_NOTFOUND,
359 EXPECT_EQ( 377 GetCachedToken().status());
360 IdentityTokenCacheValue::CACHE_STATUS_NOTFOUND,
361 id_api()->GetCachedToken(extension->id(), oauth2_info.scopes).status());
362 } 378 }
363 379
364 IN_PROC_BROWSER_TEST_F(ExperimentalGetAuthTokenFunctionTest, 380 IN_PROC_BROWSER_TEST_F(ExperimentalGetAuthTokenFunctionTest,
365 NonInteractiveMintBadCredentials) { 381 NonInteractiveMintBadCredentials) {
366 scoped_refptr<ExperimentalMockGetAuthTokenFunction> func( 382 scoped_refptr<ExperimentalMockGetAuthTokenFunction> func(
367 new ExperimentalMockGetAuthTokenFunction()); 383 new ExperimentalMockGetAuthTokenFunction());
368 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); 384 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
369 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(true)); 385 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(true));
370 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( 386 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow(
371 TestOAuth2MintTokenFlow::MINT_TOKEN_BAD_CREDENTIALS, func.get()); 387 TestOAuth2MintTokenFlow::MINT_TOKEN_BAD_CREDENTIALS, func.get());
(...skipping 10 matching lines...) Expand all
382 #define MAYBE_NonInteractiveSuccess DISABLED_NonInteractiveSuccess 398 #define MAYBE_NonInteractiveSuccess DISABLED_NonInteractiveSuccess
383 #else 399 #else
384 #define MAYBE_NonInteractiveSuccess NonInteractiveSuccess 400 #define MAYBE_NonInteractiveSuccess NonInteractiveSuccess
385 #endif 401 #endif
386 IN_PROC_BROWSER_TEST_F(ExperimentalGetAuthTokenFunctionTest, 402 IN_PROC_BROWSER_TEST_F(ExperimentalGetAuthTokenFunctionTest,
387 MAYBE_NonInteractiveSuccess) { 403 MAYBE_NonInteractiveSuccess) {
388 scoped_refptr<ExperimentalMockGetAuthTokenFunction> func( 404 scoped_refptr<ExperimentalMockGetAuthTokenFunction> func(
389 new ExperimentalMockGetAuthTokenFunction()); 405 new ExperimentalMockGetAuthTokenFunction());
390 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); 406 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES));
391 func->set_extension(extension.get()); 407 func->set_extension(extension.get());
392 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(extension.get());
393 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(true)); 408 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(true));
394 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( 409 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow(
395 TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS, func.get()); 410 TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS, func.get());
396 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow)); 411 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
397 scoped_ptr<base::Value> value( 412 scoped_ptr<base::Value> value(
398 utils::RunFunctionAndReturnSingleResult(func.get(), "[{}]", browser())); 413 utils::RunFunctionAndReturnSingleResult(func.get(), "[{}]", browser()));
399 std::string access_token; 414 std::string access_token;
400 EXPECT_TRUE(value->GetAsString(&access_token)); 415 EXPECT_TRUE(value->GetAsString(&access_token));
401 EXPECT_EQ(std::string(kAccessToken), access_token); 416 EXPECT_EQ(std::string(kAccessToken), access_token);
402 EXPECT_FALSE(func->login_ui_shown()); 417 EXPECT_FALSE(func->login_ui_shown());
403 EXPECT_FALSE(func->install_ui_shown()); 418 EXPECT_FALSE(func->install_ui_shown());
404 EXPECT_EQ( 419 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_NOTFOUND,
405 IdentityTokenCacheValue::CACHE_STATUS_NOTFOUND, 420 GetCachedToken().status());
406 id_api()->GetCachedToken(extension->id(), oauth2_info.scopes).status());
407 } 421 }
408 422
409 IN_PROC_BROWSER_TEST_F(ExperimentalGetAuthTokenFunctionTest, 423 IN_PROC_BROWSER_TEST_F(ExperimentalGetAuthTokenFunctionTest,
410 InteractiveLoginCanceled) { 424 InteractiveLoginCanceled) {
411 scoped_refptr<ExperimentalMockGetAuthTokenFunction> func( 425 scoped_refptr<ExperimentalMockGetAuthTokenFunction> func(
412 new ExperimentalMockGetAuthTokenFunction()); 426 new ExperimentalMockGetAuthTokenFunction());
413 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); 427 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
414 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(false)); 428 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(false));
415 func->set_login_ui_result(false); 429 func->set_login_ui_result(false);
416 std::string error = utils::RunFunctionAndReturnError( 430 std::string error = utils::RunFunctionAndReturnError(
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
539 EXPECT_TRUE(func->login_ui_shown()); 553 EXPECT_TRUE(func->login_ui_shown());
540 EXPECT_TRUE(func->install_ui_shown()); 554 EXPECT_TRUE(func->install_ui_shown());
541 } 555 }
542 556
543 IN_PROC_BROWSER_TEST_F(ExperimentalGetAuthTokenFunctionTest, 557 IN_PROC_BROWSER_TEST_F(ExperimentalGetAuthTokenFunctionTest,
544 InteractiveLoginSuccessApprovalDoneMintSuccess) { 558 InteractiveLoginSuccessApprovalDoneMintSuccess) {
545 scoped_refptr<ExperimentalMockGetAuthTokenFunction> func( 559 scoped_refptr<ExperimentalMockGetAuthTokenFunction> func(
546 new ExperimentalMockGetAuthTokenFunction()); 560 new ExperimentalMockGetAuthTokenFunction());
547 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); 561 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES));
548 func->set_extension(extension.get()); 562 func->set_extension(extension.get());
549 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(extension.get());
550 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(false)); 563 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(false));
551 func->set_login_ui_result(true); 564 func->set_login_ui_result(true);
552 TestOAuth2MintTokenFlow* flow1 = new TestOAuth2MintTokenFlow( 565 TestOAuth2MintTokenFlow* flow1 = new TestOAuth2MintTokenFlow(
553 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get()); 566 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get());
554 TestOAuth2MintTokenFlow* flow2 = new TestOAuth2MintTokenFlow( 567 TestOAuth2MintTokenFlow* flow2 = new TestOAuth2MintTokenFlow(
555 TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS, func.get()); 568 TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS, func.get());
556 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow1)) 569 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow1))
557 .WillOnce(Return(flow2)); 570 .WillOnce(Return(flow2));
558 571
559 func->set_install_ui_result(true); 572 func->set_install_ui_result(true);
560 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( 573 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult(
561 func.get(), "[{\"interactive\": true}]", browser())); 574 func.get(), "[{\"interactive\": true}]", browser()));
562 std::string access_token; 575 std::string access_token;
563 EXPECT_TRUE(value->GetAsString(&access_token)); 576 EXPECT_TRUE(value->GetAsString(&access_token));
564 EXPECT_EQ(std::string(kAccessToken), access_token); 577 EXPECT_EQ(std::string(kAccessToken), access_token);
565 EXPECT_TRUE(func->login_ui_shown()); 578 EXPECT_TRUE(func->login_ui_shown());
566 EXPECT_TRUE(func->install_ui_shown()); 579 EXPECT_TRUE(func->install_ui_shown());
567 EXPECT_EQ( 580 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_NOTFOUND,
568 IdentityTokenCacheValue::CACHE_STATUS_NOTFOUND, 581 GetCachedToken().status());
569 id_api()->GetCachedToken(extension->id(), oauth2_info.scopes).status());
570 } 582 }
571 583
572 IN_PROC_BROWSER_TEST_F(ExperimentalGetAuthTokenFunctionTest, 584 IN_PROC_BROWSER_TEST_F(ExperimentalGetAuthTokenFunctionTest,
573 InteractiveApprovalAborted) { 585 InteractiveApprovalAborted) {
574 scoped_refptr<ExperimentalMockGetAuthTokenFunction> func( 586 scoped_refptr<ExperimentalMockGetAuthTokenFunction> func(
575 new ExperimentalMockGetAuthTokenFunction()); 587 new ExperimentalMockGetAuthTokenFunction());
576 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); 588 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
577 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(true)); 589 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(true));
578 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( 590 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow(
579 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get()); 591 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get());
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
770 "https://abcdefghij.chromiumapp.org/callback#test')</script>\"}]", 782 "https://abcdefghij.chromiumapp.org/callback#test')</script>\"}]",
771 browser())); 783 browser()));
772 784
773 std::string url; 785 std::string url;
774 EXPECT_TRUE(value->GetAsString(&url)); 786 EXPECT_TRUE(value->GetAsString(&url));
775 EXPECT_EQ(std::string("https://abcdefghij.chromiumapp.org/callback#test"), 787 EXPECT_EQ(std::string("https://abcdefghij.chromiumapp.org/callback#test"),
776 url); 788 url);
777 } 789 }
778 790
779 } // namespace extensions 791 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698