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

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

Issue 15897006: Identity API: switch WebAuthFlow dialog to component app (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase to ToT Created 7 years, 6 months 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
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/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/component_loader.h"
9 #include "chrome/browser/extensions/extension_apitest.h" 10 #include "chrome/browser/extensions/extension_apitest.h"
10 #include "chrome/browser/extensions/extension_browsertest.h" 11 #include "chrome/browser/extensions/extension_browsertest.h"
11 #include "chrome/browser/extensions/extension_function_test_utils.h" 12 #include "chrome/browser/extensions/extension_function_test_utils.h"
12 #include "chrome/browser/extensions/extension_service.h" 13 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/ui/browser.h" 14 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_window.h" 15 #include "chrome/browser/ui/browser_window.h"
15 #include "chrome/common/chrome_notification_types.h" 16 #include "chrome/common/chrome_notification_types.h"
16 #include "chrome/common/chrome_switches.h" 17 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/extensions/api/identity/oauth2_manifest_handler.h" 18 #include "chrome/common/extensions/api/identity/oauth2_manifest_handler.h"
18 #include "chrome/test/base/in_process_browser_test.h" 19 #include "chrome/test/base/in_process_browser_test.h"
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 private: 183 private:
183 ResultType result_; 184 ResultType result_;
184 OAuth2MintTokenFlow::Delegate* delegate_; 185 OAuth2MintTokenFlow::Delegate* delegate_;
185 }; 186 };
186 187
187 BrowserContextKeyedService* IdentityAPITestFactory( 188 BrowserContextKeyedService* IdentityAPITestFactory(
188 content::BrowserContext* profile) { 189 content::BrowserContext* profile) {
189 return new IdentityAPI(static_cast<Profile*>(profile)); 190 return new IdentityAPI(static_cast<Profile*>(profile));
190 } 191 }
191 192
193 // Waits for a specific GURL to generate a NOTIFICATION_LOAD_STOP
194 // event, and closes the window embedding the webcontents.
195 class WaitForGURLAndCloseWindow : public content::WindowedNotificationObserver {
196 public:
197 explicit WaitForGURLAndCloseWindow(GURL url)
198 : WindowedNotificationObserver(
199 content::NOTIFICATION_LOAD_STOP,
200 content::NotificationService::AllSources()),
201 url_(url) {}
202
203 // NotificationObserver:
204 virtual void Observe(int type,
205 const content::NotificationSource& source,
206 const content::NotificationDetails& details) OVERRIDE {
207 content::NavigationController* web_auth_flow_controller =
208 content::Source<content::NavigationController>(source).ptr();
209 content::WebContents* web_contents =
210 web_auth_flow_controller->GetWebContents();
211
212 if (web_contents->GetURL() == url_) {
213 web_contents->GetEmbedderWebContents()->Close();
214 // Condtionally invoke parent class so that Wait will not exit
215 // until the target URL arrives.
216 content::WindowedNotificationObserver::Observe(type, source, details);
217 }
218 }
219
220 private:
221 GURL url_;
222 };
223
192 } // namespace 224 } // namespace
193 225
194 class MockGetAuthTokenFunction : public IdentityGetAuthTokenFunction { 226 class MockGetAuthTokenFunction : public IdentityGetAuthTokenFunction {
195 public: 227 public:
196 MockGetAuthTokenFunction() : login_ui_result_(true), 228 MockGetAuthTokenFunction() : login_ui_result_(true),
197 scope_ui_result_(true), 229 scope_ui_result_(true),
198 login_ui_shown_(false), 230 login_ui_shown_(false),
199 scope_ui_shown_(false) { 231 scope_ui_shown_(false) {
200 } 232 }
201 233
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after
538 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow)); 570 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
539 func->set_scope_ui_failure(GaiaWebAuthFlow::WINDOW_CLOSED); 571 func->set_scope_ui_failure(GaiaWebAuthFlow::WINDOW_CLOSED);
540 std::string error = utils::RunFunctionAndReturnError( 572 std::string error = utils::RunFunctionAndReturnError(
541 func.get(), "[{\"interactive\": true}]", browser()); 573 func.get(), "[{\"interactive\": true}]", browser());
542 EXPECT_EQ(std::string(errors::kUserRejected), error); 574 EXPECT_EQ(std::string(errors::kUserRejected), error);
543 EXPECT_FALSE(func->login_ui_shown()); 575 EXPECT_FALSE(func->login_ui_shown());
544 EXPECT_TRUE(func->scope_ui_shown()); 576 EXPECT_TRUE(func->scope_ui_shown());
545 } 577 }
546 578
547 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 579 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
580 InteractiveApprovalLoadFailed) {
581 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction());
582 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
583 EXPECT_CALL(*func.get(), HasLoginToken())
584 .WillOnce(Return(true));
585 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow(
586 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get());
587 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
588 func->set_scope_ui_failure(GaiaWebAuthFlow::LOAD_FAILED);
589 std::string error = utils::RunFunctionAndReturnError(
590 func.get(), "[{\"interactive\": true}]", browser());
591 EXPECT_EQ(std::string(errors::kPageLoadFailure), error);
592 EXPECT_FALSE(func->login_ui_shown());
593 EXPECT_TRUE(func->scope_ui_shown());
594 }
595
596 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
548 InteractiveApprovalInvalidRedirect) { 597 InteractiveApprovalInvalidRedirect) {
549 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 598 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction());
550 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); 599 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
551 EXPECT_CALL(*func.get(), HasLoginToken()) 600 EXPECT_CALL(*func.get(), HasLoginToken())
552 .WillOnce(Return(true)); 601 .WillOnce(Return(true));
553 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( 602 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow(
554 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get()); 603 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get());
555 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow)); 604 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
556 func->set_scope_ui_failure(GaiaWebAuthFlow::INVALID_REDIRECT); 605 func->set_scope_ui_failure(GaiaWebAuthFlow::INVALID_REDIRECT);
557 std::string error = utils::RunFunctionAndReturnError( 606 std::string error = utils::RunFunctionAndReturnError(
(...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after
954 "chrome/test/data/extensions/api_test/identity"))); 1003 "chrome/test/data/extensions/api_test/identity")));
955 ASSERT_TRUE(https_server.Start()); 1004 ASSERT_TRUE(https_server.Start());
956 GURL auth_url(https_server.GetURL("files/interaction_required.html")); 1005 GURL auth_url(https_server.GetURL("files/interaction_required.html"));
957 1006
958 scoped_refptr<IdentityLaunchWebAuthFlowFunction> function( 1007 scoped_refptr<IdentityLaunchWebAuthFlowFunction> function(
959 new IdentityLaunchWebAuthFlowFunction()); 1008 new IdentityLaunchWebAuthFlowFunction());
960 scoped_refptr<Extension> empty_extension( 1009 scoped_refptr<Extension> empty_extension(
961 utils::CreateEmptyExtension()); 1010 utils::CreateEmptyExtension());
962 function->set_extension(empty_extension.get()); 1011 function->set_extension(empty_extension.get());
963 1012
964 content::WindowedNotificationObserver popup_observer( 1013 WaitForGURLAndCloseWindow popup_observer(auth_url);
965 chrome::NOTIFICATION_BROWSER_WINDOW_READY,
966 content::NotificationService::AllSources());
967 1014
968 std::string args = "[{\"interactive\": true, \"url\": \"" + 1015 std::string args = "[{\"interactive\": true, \"url\": \"" +
969 auth_url.spec() + "\"}]"; 1016 auth_url.spec() + "\"}]";
970 RunFunctionAsync(function, args); 1017 RunFunctionAsync(function, args);
971 1018
972 popup_observer.Wait(); 1019 popup_observer.Wait();
973 content::Source<Browser>(popup_observer.source())->window()->Close();
974 1020
975 EXPECT_EQ(std::string(errors::kUserRejected), WaitForError(function)); 1021 EXPECT_EQ(std::string(errors::kUserRejected), WaitForError(function));
976 } 1022 }
977 1023
978 IN_PROC_BROWSER_TEST_F(LaunchWebAuthFlowFunctionTest, InteractionRequired) { 1024 IN_PROC_BROWSER_TEST_F(LaunchWebAuthFlowFunctionTest, InteractionRequired) {
979 net::SpawnedTestServer https_server( 1025 net::SpawnedTestServer https_server(
980 net::SpawnedTestServer::TYPE_HTTPS, 1026 net::SpawnedTestServer::TYPE_HTTPS,
981 net::SpawnedTestServer::kLocalhost, 1027 net::SpawnedTestServer::kLocalhost,
982 base::FilePath(FILE_PATH_LITERAL( 1028 base::FilePath(FILE_PATH_LITERAL(
983 "chrome/test/data/extensions/api_test/identity"))); 1029 "chrome/test/data/extensions/api_test/identity")));
984 ASSERT_TRUE(https_server.Start()); 1030 ASSERT_TRUE(https_server.Start());
985 GURL auth_url(https_server.GetURL("files/interaction_required.html")); 1031 GURL auth_url(https_server.GetURL("files/interaction_required.html"));
986 1032
987 scoped_refptr<IdentityLaunchWebAuthFlowFunction> function( 1033 scoped_refptr<IdentityLaunchWebAuthFlowFunction> function(
988 new IdentityLaunchWebAuthFlowFunction()); 1034 new IdentityLaunchWebAuthFlowFunction());
989 scoped_refptr<Extension> empty_extension( 1035 scoped_refptr<Extension> empty_extension(
990 utils::CreateEmptyExtension()); 1036 utils::CreateEmptyExtension());
991 function->set_extension(empty_extension.get()); 1037 function->set_extension(empty_extension.get());
992 1038
993 std::string args = "[{\"interactive\": false, \"url\": \"" + 1039 std::string args = "[{\"interactive\": false, \"url\": \"" +
994 auth_url.spec() + "\"}]"; 1040 auth_url.spec() + "\"}]";
995 std::string error = utils::RunFunctionAndReturnError(function, args, 1041 std::string error = utils::RunFunctionAndReturnError(function, args,
996 browser()); 1042 browser());
997 1043
998 EXPECT_EQ(std::string(errors::kInteractionRequired), error); 1044 EXPECT_EQ(std::string(errors::kInteractionRequired), error);
999 } 1045 }
1000 1046
1047 IN_PROC_BROWSER_TEST_F(LaunchWebAuthFlowFunctionTest, LoadFailed) {
1048 net::SpawnedTestServer https_server(
1049 net::SpawnedTestServer::TYPE_HTTPS,
1050 net::SpawnedTestServer::kLocalhost,
1051 base::FilePath(FILE_PATH_LITERAL(
1052 "chrome/test/data/extensions/api_test/identity")));
1053 ASSERT_TRUE(https_server.Start());
1054 GURL auth_url(https_server.GetURL("files/five_hundred.html"));
1055
1056 scoped_refptr<IdentityLaunchWebAuthFlowFunction> function(
1057 new IdentityLaunchWebAuthFlowFunction());
1058 scoped_refptr<Extension> empty_extension(
1059 utils::CreateEmptyExtension());
1060 function->set_extension(empty_extension.get());
1061
1062 std::string args = "[{\"interactive\": true, \"url\": \"" +
1063 auth_url.spec() + "\"}]";
1064 std::string error = utils::RunFunctionAndReturnError(function, args,
1065 browser());
1066
1067 EXPECT_EQ(std::string(errors::kPageLoadFailure), error);
1068 }
1069
1001 IN_PROC_BROWSER_TEST_F(LaunchWebAuthFlowFunctionTest, NonInteractiveSuccess) { 1070 IN_PROC_BROWSER_TEST_F(LaunchWebAuthFlowFunctionTest, NonInteractiveSuccess) {
1002 scoped_refptr<IdentityLaunchWebAuthFlowFunction> function( 1071 scoped_refptr<IdentityLaunchWebAuthFlowFunction> function(
1003 new IdentityLaunchWebAuthFlowFunction()); 1072 new IdentityLaunchWebAuthFlowFunction());
1004 scoped_refptr<Extension> empty_extension( 1073 scoped_refptr<Extension> empty_extension(
1005 utils::CreateEmptyExtension()); 1074 utils::CreateEmptyExtension());
1006 function->set_extension(empty_extension.get()); 1075 function->set_extension(empty_extension.get());
1007 1076
1008 function->InitFinalRedirectURLPrefixForTest("abcdefghij"); 1077 function->InitFinalRedirectURLPrefixForTest("abcdefghij");
1009 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( 1078 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult(
1010 function, 1079 function,
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
1061 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( 1130 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult(
1062 function, args, browser())); 1131 function, args, browser()));
1063 1132
1064 std::string url; 1133 std::string url;
1065 EXPECT_TRUE(value->GetAsString(&url)); 1134 EXPECT_TRUE(value->GetAsString(&url));
1066 EXPECT_EQ(std::string("https://abcdefghij.chromiumapp.org/callback#test"), 1135 EXPECT_EQ(std::string("https://abcdefghij.chromiumapp.org/callback#test"),
1067 url); 1136 url);
1068 } 1137 }
1069 1138
1070 } // namespace extensions 1139 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/identity/identity_api.cc ('k') | chrome/browser/extensions/api/identity/web_auth_flow.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698