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

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: Created 7 years, 7 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 content::WindowedNotificationObserver::Observe(type, source, details);
miket_OOO 2013/05/24 16:37:56 Did you mean to call the inherited behavior only c
Michael Courage 2013/05/30 22:27:03 Yes, added a comment to that effect.
215 }
216 }
217
218 private:
219 GURL url_;
220 };
221
192 } // namespace 222 } // namespace
193 223
194 class MockGetAuthTokenFunction : public IdentityGetAuthTokenFunction { 224 class MockGetAuthTokenFunction : public IdentityGetAuthTokenFunction {
195 public: 225 public:
196 MockGetAuthTokenFunction() : login_ui_result_(true), 226 MockGetAuthTokenFunction() : login_ui_result_(true),
197 scope_ui_result_(true), 227 scope_ui_result_(true),
198 login_ui_shown_(false), 228 login_ui_shown_(false),
199 scope_ui_shown_(false) { 229 scope_ui_shown_(false) {
200 } 230 }
201 231
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after
538 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow)); 568 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
539 func->set_scope_ui_failure(GaiaWebAuthFlow::WINDOW_CLOSED); 569 func->set_scope_ui_failure(GaiaWebAuthFlow::WINDOW_CLOSED);
540 std::string error = utils::RunFunctionAndReturnError( 570 std::string error = utils::RunFunctionAndReturnError(
541 func.get(), "[{\"interactive\": true}]", browser()); 571 func.get(), "[{\"interactive\": true}]", browser());
542 EXPECT_EQ(std::string(errors::kUserRejected), error); 572 EXPECT_EQ(std::string(errors::kUserRejected), error);
543 EXPECT_FALSE(func->login_ui_shown()); 573 EXPECT_FALSE(func->login_ui_shown());
544 EXPECT_TRUE(func->scope_ui_shown()); 574 EXPECT_TRUE(func->scope_ui_shown());
545 } 575 }
546 576
547 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 577 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
578 InteractiveApprovalLoadFailed) {
579 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction());
580 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
581 EXPECT_CALL(*func.get(), HasLoginToken())
582 .WillOnce(Return(true));
583 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow(
584 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get());
585 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
586 func->set_scope_ui_failure(GaiaWebAuthFlow::LOAD_FAILED);
587 std::string error = utils::RunFunctionAndReturnError(
588 func.get(), "[{\"interactive\": true}]", browser());
589 EXPECT_EQ(std::string(errors::kPageLoadFailure), error);
590 EXPECT_FALSE(func->login_ui_shown());
591 EXPECT_TRUE(func->scope_ui_shown());
592 }
593
594 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
548 InteractiveApprovalInvalidRedirect) { 595 InteractiveApprovalInvalidRedirect) {
549 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 596 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction());
550 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); 597 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
551 EXPECT_CALL(*func.get(), HasLoginToken()) 598 EXPECT_CALL(*func.get(), HasLoginToken())
552 .WillOnce(Return(true)); 599 .WillOnce(Return(true));
553 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( 600 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow(
554 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get()); 601 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get());
555 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow)); 602 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
556 func->set_scope_ui_failure(GaiaWebAuthFlow::INVALID_REDIRECT); 603 func->set_scope_ui_failure(GaiaWebAuthFlow::INVALID_REDIRECT);
557 std::string error = utils::RunFunctionAndReturnError( 604 std::string error = utils::RunFunctionAndReturnError(
(...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after
933 IdentityTokenCacheValue token(kAccessToken, 980 IdentityTokenCacheValue token(kAccessToken,
934 base::TimeDelta::FromSeconds(3600)); 981 base::TimeDelta::FromSeconds(3600));
935 SetCachedToken(token); 982 SetCachedToken(token);
936 EXPECT_TRUE(InvalidateDefaultToken()); 983 EXPECT_TRUE(InvalidateDefaultToken());
937 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_NOTFOUND, 984 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_NOTFOUND,
938 GetCachedToken().status()); 985 GetCachedToken().status());
939 } 986 }
940 987
941 class LaunchWebAuthFlowFunctionTest : public AsyncExtensionBrowserTest { 988 class LaunchWebAuthFlowFunctionTest : public AsyncExtensionBrowserTest {
942 public: 989 public:
990 void LoadComponent() {
991 // Normally the ComponentLoader won't load the component in tests.
992 ExtensionService* service = extensions::ExtensionSystem::Get(
993 profile())->extension_service();
994
995 service->component_loader()->Add(
996 IDR_IDENTITY_API_SCOPE_APPROVAL_MANIFEST,
997 base::FilePath(FILE_PATH_LITERAL("identity_scope_approval_dialog")));
998 }
999
943 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 1000 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
944 // Reduce performance test variance by disabling background networking. 1001 // Reduce performance test variance by disabling background networking.
945 command_line->AppendSwitch(switches::kDisableBackgroundNetworking); 1002 command_line->AppendSwitch(switches::kDisableBackgroundNetworking);
946 } 1003 }
947 }; 1004 };
948 1005
949 IN_PROC_BROWSER_TEST_F(LaunchWebAuthFlowFunctionTest, UserCloseWindow) { 1006 IN_PROC_BROWSER_TEST_F(LaunchWebAuthFlowFunctionTest, UserCloseWindow) {
1007 LoadComponent();
950 net::SpawnedTestServer https_server( 1008 net::SpawnedTestServer https_server(
951 net::SpawnedTestServer::TYPE_HTTPS, 1009 net::SpawnedTestServer::TYPE_HTTPS,
952 net::SpawnedTestServer::kLocalhost, 1010 net::SpawnedTestServer::kLocalhost,
953 base::FilePath(FILE_PATH_LITERAL( 1011 base::FilePath(FILE_PATH_LITERAL(
954 "chrome/test/data/extensions/api_test/identity"))); 1012 "chrome/test/data/extensions/api_test/identity")));
955 ASSERT_TRUE(https_server.Start()); 1013 ASSERT_TRUE(https_server.Start());
956 GURL auth_url(https_server.GetURL("files/interaction_required.html")); 1014 GURL auth_url(https_server.GetURL("files/interaction_required.html"));
957 1015
958 scoped_refptr<IdentityLaunchWebAuthFlowFunction> function( 1016 scoped_refptr<IdentityLaunchWebAuthFlowFunction> function(
959 new IdentityLaunchWebAuthFlowFunction()); 1017 new IdentityLaunchWebAuthFlowFunction());
960 scoped_refptr<Extension> empty_extension( 1018 scoped_refptr<Extension> empty_extension(
961 utils::CreateEmptyExtension()); 1019 utils::CreateEmptyExtension());
962 function->set_extension(empty_extension.get()); 1020 function->set_extension(empty_extension.get());
963 1021
964 content::WindowedNotificationObserver popup_observer( 1022 WaitForGURLAndCloseWindow popup_observer(auth_url);
965 chrome::NOTIFICATION_BROWSER_WINDOW_READY,
966 content::NotificationService::AllSources());
967 1023
968 std::string args = "[{\"interactive\": true, \"url\": \"" + 1024 std::string args = "[{\"interactive\": true, \"url\": \"" +
969 auth_url.spec() + "\"}]"; 1025 auth_url.spec() + "\"}]";
970 RunFunctionAsync(function, args); 1026 RunFunctionAsync(function, args);
971 1027
972 popup_observer.Wait(); 1028 popup_observer.Wait();
973 content::Source<Browser>(popup_observer.source())->window()->Close();
974 1029
975 EXPECT_EQ(std::string(errors::kUserRejected), WaitForError(function)); 1030 EXPECT_EQ(std::string(errors::kUserRejected), WaitForError(function));
976 } 1031 }
977 1032
978 IN_PROC_BROWSER_TEST_F(LaunchWebAuthFlowFunctionTest, InteractionRequired) { 1033 IN_PROC_BROWSER_TEST_F(LaunchWebAuthFlowFunctionTest, InteractionRequired) {
1034 LoadComponent();
979 net::SpawnedTestServer https_server( 1035 net::SpawnedTestServer https_server(
980 net::SpawnedTestServer::TYPE_HTTPS, 1036 net::SpawnedTestServer::TYPE_HTTPS,
981 net::SpawnedTestServer::kLocalhost, 1037 net::SpawnedTestServer::kLocalhost,
982 base::FilePath(FILE_PATH_LITERAL( 1038 base::FilePath(FILE_PATH_LITERAL(
983 "chrome/test/data/extensions/api_test/identity"))); 1039 "chrome/test/data/extensions/api_test/identity")));
984 ASSERT_TRUE(https_server.Start()); 1040 ASSERT_TRUE(https_server.Start());
985 GURL auth_url(https_server.GetURL("files/interaction_required.html")); 1041 GURL auth_url(https_server.GetURL("files/interaction_required.html"));
986 1042
987 scoped_refptr<IdentityLaunchWebAuthFlowFunction> function( 1043 scoped_refptr<IdentityLaunchWebAuthFlowFunction> function(
988 new IdentityLaunchWebAuthFlowFunction()); 1044 new IdentityLaunchWebAuthFlowFunction());
989 scoped_refptr<Extension> empty_extension( 1045 scoped_refptr<Extension> empty_extension(
990 utils::CreateEmptyExtension()); 1046 utils::CreateEmptyExtension());
991 function->set_extension(empty_extension.get()); 1047 function->set_extension(empty_extension.get());
992 1048
993 std::string args = "[{\"interactive\": false, \"url\": \"" + 1049 std::string args = "[{\"interactive\": false, \"url\": \"" +
994 auth_url.spec() + "\"}]"; 1050 auth_url.spec() + "\"}]";
995 std::string error = utils::RunFunctionAndReturnError(function, args, 1051 std::string error = utils::RunFunctionAndReturnError(function, args,
996 browser()); 1052 browser());
997 1053
998 EXPECT_EQ(std::string(errors::kInteractionRequired), error); 1054 EXPECT_EQ(std::string(errors::kInteractionRequired), error);
999 } 1055 }
1000 1056
1001 IN_PROC_BROWSER_TEST_F(LaunchWebAuthFlowFunctionTest, NonInteractiveSuccess) { 1057 IN_PROC_BROWSER_TEST_F(LaunchWebAuthFlowFunctionTest, LoadFailed) {
1058 LoadComponent();
1059 net::SpawnedTestServer https_server(
1060 net::SpawnedTestServer::TYPE_HTTPS,
1061 net::SpawnedTestServer::kLocalhost,
1062 base::FilePath(FILE_PATH_LITERAL(
1063 "chrome/test/data/extensions/api_test/identity")));
1064 ASSERT_TRUE(https_server.Start());
1065 GURL auth_url(https_server.GetURL("files/five_hundred.html"));
1066
1002 scoped_refptr<IdentityLaunchWebAuthFlowFunction> function( 1067 scoped_refptr<IdentityLaunchWebAuthFlowFunction> function(
1003 new IdentityLaunchWebAuthFlowFunction()); 1068 new IdentityLaunchWebAuthFlowFunction());
1004 scoped_refptr<Extension> empty_extension( 1069 scoped_refptr<Extension> empty_extension(
1070 utils::CreateEmptyExtension());
1071 function->set_extension(empty_extension.get());
1072
1073 std::string args = "[{\"interactive\": true, \"url\": \"" +
1074 auth_url.spec() + "\"}]";
1075 std::string error = utils::RunFunctionAndReturnError(function, args,
1076 browser());
1077
1078 EXPECT_EQ(std::string(errors::kPageLoadFailure), error);
1079 }
1080
1081 IN_PROC_BROWSER_TEST_F(LaunchWebAuthFlowFunctionTest, NonInteractiveSuccess) {
1082 LoadComponent();
1083 scoped_refptr<IdentityLaunchWebAuthFlowFunction> function(
1084 new IdentityLaunchWebAuthFlowFunction());
1085 scoped_refptr<Extension> empty_extension(
1005 utils::CreateEmptyExtension()); 1086 utils::CreateEmptyExtension());
1006 function->set_extension(empty_extension.get()); 1087 function->set_extension(empty_extension.get());
1007 1088
1008 function->InitFinalRedirectURLPrefixForTest("abcdefghij"); 1089 function->InitFinalRedirectURLPrefixForTest("abcdefghij");
1009 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( 1090 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult(
1010 function, 1091 function,
1011 "[{\"interactive\": false," 1092 "[{\"interactive\": false,"
1012 "\"url\": \"https://abcdefghij.chromiumapp.org/callback#test\"}]", 1093 "\"url\": \"https://abcdefghij.chromiumapp.org/callback#test\"}]",
1013 browser())); 1094 browser()));
1014 1095
1015 std::string url; 1096 std::string url;
1016 EXPECT_TRUE(value->GetAsString(&url)); 1097 EXPECT_TRUE(value->GetAsString(&url));
1017 EXPECT_EQ(std::string("https://abcdefghij.chromiumapp.org/callback#test"), 1098 EXPECT_EQ(std::string("https://abcdefghij.chromiumapp.org/callback#test"),
1018 url); 1099 url);
1019 } 1100 }
1020 1101
1021 IN_PROC_BROWSER_TEST_F( 1102 IN_PROC_BROWSER_TEST_F(
1022 LaunchWebAuthFlowFunctionTest, InteractiveFirstNavigationSuccess) { 1103 LaunchWebAuthFlowFunctionTest, InteractiveFirstNavigationSuccess) {
1104 LoadComponent();
1023 scoped_refptr<IdentityLaunchWebAuthFlowFunction> function( 1105 scoped_refptr<IdentityLaunchWebAuthFlowFunction> function(
1024 new IdentityLaunchWebAuthFlowFunction()); 1106 new IdentityLaunchWebAuthFlowFunction());
1025 scoped_refptr<Extension> empty_extension( 1107 scoped_refptr<Extension> empty_extension(
1026 utils::CreateEmptyExtension()); 1108 utils::CreateEmptyExtension());
1027 function->set_extension(empty_extension.get()); 1109 function->set_extension(empty_extension.get());
1028 1110
1029 function->InitFinalRedirectURLPrefixForTest("abcdefghij"); 1111 function->InitFinalRedirectURLPrefixForTest("abcdefghij");
1030 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( 1112 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult(
1031 function, 1113 function,
1032 "[{\"interactive\": true," 1114 "[{\"interactive\": true,"
1033 "\"url\": \"https://abcdefghij.chromiumapp.org/callback#test\"}]", 1115 "\"url\": \"https://abcdefghij.chromiumapp.org/callback#test\"}]",
1034 browser())); 1116 browser()));
1035 1117
1036 std::string url; 1118 std::string url;
1037 EXPECT_TRUE(value->GetAsString(&url)); 1119 EXPECT_TRUE(value->GetAsString(&url));
1038 EXPECT_EQ(std::string("https://abcdefghij.chromiumapp.org/callback#test"), 1120 EXPECT_EQ(std::string("https://abcdefghij.chromiumapp.org/callback#test"),
1039 url); 1121 url);
1040 } 1122 }
1041 1123
1042 IN_PROC_BROWSER_TEST_F( 1124 IN_PROC_BROWSER_TEST_F(
1043 LaunchWebAuthFlowFunctionTest, InteractiveSecondNavigationSuccess) { 1125 LaunchWebAuthFlowFunctionTest, InteractiveSecondNavigationSuccess) {
1126 LoadComponent();
1044 net::SpawnedTestServer https_server( 1127 net::SpawnedTestServer https_server(
1045 net::SpawnedTestServer::TYPE_HTTPS, 1128 net::SpawnedTestServer::TYPE_HTTPS,
1046 net::SpawnedTestServer::kLocalhost, 1129 net::SpawnedTestServer::kLocalhost,
1047 base::FilePath(FILE_PATH_LITERAL( 1130 base::FilePath(FILE_PATH_LITERAL(
1048 "chrome/test/data/extensions/api_test/identity"))); 1131 "chrome/test/data/extensions/api_test/identity")));
1049 ASSERT_TRUE(https_server.Start()); 1132 ASSERT_TRUE(https_server.Start());
1050 GURL auth_url(https_server.GetURL("files/redirect_to_chromiumapp.html")); 1133 GURL auth_url(https_server.GetURL("files/redirect_to_chromiumapp.html"));
1051 1134
1052 scoped_refptr<IdentityLaunchWebAuthFlowFunction> function( 1135 scoped_refptr<IdentityLaunchWebAuthFlowFunction> function(
1053 new IdentityLaunchWebAuthFlowFunction()); 1136 new IdentityLaunchWebAuthFlowFunction());
1054 scoped_refptr<Extension> empty_extension( 1137 scoped_refptr<Extension> empty_extension(
1055 utils::CreateEmptyExtension()); 1138 utils::CreateEmptyExtension());
1056 function->set_extension(empty_extension.get()); 1139 function->set_extension(empty_extension.get());
1057 1140
1058 function->InitFinalRedirectURLPrefixForTest("abcdefghij"); 1141 function->InitFinalRedirectURLPrefixForTest("abcdefghij");
1059 std::string args = "[{\"interactive\": true, \"url\": \"" + 1142 std::string args = "[{\"interactive\": true, \"url\": \"" +
1060 auth_url.spec() + "\"}]"; 1143 auth_url.spec() + "\"}]";
1061 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( 1144 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult(
1062 function, args, browser())); 1145 function, args, browser()));
1063 1146
1064 std::string url; 1147 std::string url;
1065 EXPECT_TRUE(value->GetAsString(&url)); 1148 EXPECT_TRUE(value->GetAsString(&url));
1066 EXPECT_EQ(std::string("https://abcdefghij.chromiumapp.org/callback#test"), 1149 EXPECT_EQ(std::string("https://abcdefghij.chromiumapp.org/callback#test"),
1067 url); 1150 url);
1068 } 1151 }
1069 1152
1070 } // namespace extensions 1153 } // 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