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

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

Issue 10701041: implement sign in dialog (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 5 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 | 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/string_util.h"
6 #include "base/values.h"
5 #include "chrome/browser/extensions/api/identity/identity_api.h" 7 #include "chrome/browser/extensions/api/identity/identity_api.h"
6 #include "chrome/browser/extensions/api/identity/web_auth_flow.h" 8 #include "chrome/browser/extensions/api/identity/web_auth_flow.h"
7 #include "chrome/browser/extensions/extension_apitest.h" 9 #include "chrome/browser/extensions/extension_apitest.h"
10 #include "chrome/browser/extensions/extension_browsertest.h"
11 #include "chrome/browser/extensions/extension_function_test_utils.h"
12 #include "chrome/browser/ui/webui/signin/login_ui_service.h"
8 #include "chrome/common/net/gaia/google_service_auth_error.h" 13 #include "chrome/common/net/gaia/google_service_auth_error.h"
9 #include "chrome/common/net/gaia/oauth2_mint_token_flow.h" 14 #include "chrome/common/net/gaia/oauth2_mint_token_flow.h"
10 #include "chrome/common/chrome_switches.h" 15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/test/base/in_process_browser_test.h"
11 #include "googleurl/src/gurl.h" 17 #include "googleurl/src/gurl.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 using extensions::Extension;
22 using extensions::IdentityGetAuthTokenFunction;
23 using testing::_;
24 using testing::Return;
25 using testing::ReturnRef;
26
27 namespace errors = extensions::identity_constants;
28 namespace utils = extension_function_test_utils;
12 29
13 namespace { 30 namespace {
14 31
15 class IdentityInterceptor : public OAuth2MintTokenFlow::InterceptorForTests { 32 static const char kAccessToken[] = "auth_token";
33
34 class TestLoginUI : public LoginUIService::LoginUI {
16 public: 35 public:
17 IdentityInterceptor() : called_(false) { } 36 virtual void FocusUI() OVERRIDE {}
18 37 virtual void CloseUI() OVERRIDE {}
19 virtual bool DoIntercept(const OAuth2MintTokenFlow* flow, 38 };
20 std::string* access_token, 39
21 GoogleServiceAuthError* error) OVERRIDE { 40 class TestOAuth2MintTokenFlow : public OAuth2MintTokenFlow {
22 *access_token = "auth_token"; 41 public:
23 called_ = true; 42 enum ResultType {
24 return true; 43 ISSUE_ADVICE_SUCCESS,
25 } 44 MINT_TOKEN_SUCCESS,
26 45 MINT_TOKEN_FAILURE
27 bool called() const { return called_; } 46 };
47
48 TestOAuth2MintTokenFlow(ResultType result,
49 OAuth2MintTokenFlow::Delegate* delegate)
50 : OAuth2MintTokenFlow(NULL, delegate, OAuth2MintTokenFlow::Parameters()),
51 result_(result),
52 delegate_(delegate) {
53 }
54
55 virtual void Start() OVERRIDE {
56 switch (result_) {
57 case ISSUE_ADVICE_SUCCESS: {
58 IssueAdviceInfo info;
59 delegate_->OnIssueAdviceSuccess(info);
60 break;
61 }
62 case MINT_TOKEN_SUCCESS: {
63 delegate_->OnMintTokenSuccess(kAccessToken);
64 break;
65 }
66 case MINT_TOKEN_FAILURE: {
67 GoogleServiceAuthError error(
68 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
69 delegate_->OnMintTokenFailure(error);
70 break;
71 }
72 }
73 }
28 74
29 private: 75 private:
30 bool called_; 76 ResultType result_;
77 OAuth2MintTokenFlow::Delegate* delegate_;
31 }; 78 };
32 79
33 class WebAuthFlowInterceptor 80 } // namespace
34 : public extensions::WebAuthFlow::InterceptorForTests { 81
82 class MockGetAuthTokenFunction : public IdentityGetAuthTokenFunction {
35 public: 83 public:
36 WebAuthFlowInterceptor() : called_(false) { } 84 MockGetAuthTokenFunction() : install_ui_result_(false),
37 85 login_ui_shown_(false),
38 virtual GURL DoIntercept(const GURL& provider_url) OVERRIDE { 86 install_ui_shown_(false) {
39 called_ = true; 87 }
40 return GURL("https://abcd.chromiumapp.org/cb#access_token=tok"); 88
41 } 89 void set_install_ui_result(bool result) {
42 90 install_ui_result_ = result;
43 bool called() const { return called_; } 91 }
44 92
93 bool login_ui_shown() const {
94 return login_ui_shown_;
95 }
96
97 bool install_ui_shown() const {
98 return install_ui_shown_;
99 }
100
101 virtual void StartObservingLoginService() OVERRIDE {
102 // Do nothing in tests.
103 }
104 virtual void StopObservingLoginService() OVERRIDE {
105 // Do nothing in tests.
106 }
107
108 virtual void ShowLoginPopup() OVERRIDE {
109 login_ui_shown_ = true;
110 // Explicitly call OnLoginUIClosed.
111 TestLoginUI login_ui;;
112 OnLoginUIClosed(&login_ui);
113 }
114
115 virtual void ShowOAuthApprovalDialog(
116 const IssueAdviceInfo& issue_advice) OVERRIDE {
117 install_ui_shown_ = true;
118 // Call InstallUIProceed or InstallUIAbort based on the flag.
119 if (install_ui_result_)
120 InstallUIProceed();
121 else
122 InstallUIAbort(true);
123 }
124
125 MOCK_CONST_METHOD0(HasLoginToken, bool ());
126 MOCK_METHOD1(CreateMintTokenFlow,
127 OAuth2MintTokenFlow* (OAuth2MintTokenFlow::Mode mode));
45 private: 128 private:
46 bool called_; 129 ~MockGetAuthTokenFunction() {}
130 bool install_ui_result_;
131 bool login_ui_shown_;
132 bool install_ui_shown_;
47 }; 133 };
48 134
49 } // namespace 135 class GetAuthTokenFunctionTest : public ExtensionBrowserTest {
50 136 protected:
51 IN_PROC_BROWSER_TEST_F(PlatformAppApiTest, Identity) { 137 enum OAuth2Fields {
52 IdentityInterceptor id_interceptor; 138 NONE = 0,
53 OAuth2MintTokenFlow::SetInterceptorForTests(&id_interceptor); 139 CLIENT_ID = 1,
54 WebAuthFlowInterceptor waf_interceptor; 140 SCOPES = 2
55 extensions::WebAuthFlow::SetInterceptorForTests(&waf_interceptor); 141 };
56 ASSERT_TRUE(RunExtensionTest("identity")) << message_; 142
57 ASSERT_TRUE(id_interceptor.called()); 143 ~GetAuthTokenFunctionTest() {}
58 ASSERT_TRUE(waf_interceptor.called()); 144
145 // Helper to create an extension with specific OAuth2Info fields set.
146 // |fields_to_set| should be computed by using fields of Oauth2Fields enum.
147 const Extension* CreateExtension(int fields_to_set) {
148 const Extension* ext = LoadExtension(
149 test_data_dir_.AppendASCII("platform_apps/oauth2"));
150 Extension::OAuth2Info& oauth2_info = const_cast<Extension::OAuth2Info&>(
151 ext->oauth2_info());
152 if ((fields_to_set & CLIENT_ID) != 0)
153 oauth2_info.client_id = "client1";
154 if ((fields_to_set & SCOPES) != 0) {
155 oauth2_info.scopes.push_back("scope1");
156 oauth2_info.scopes.push_back("scope2");
157 }
158 return ext;
159 }
59 }; 160 };
161
162 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
163 NoClientId) {
164 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction());
165 func->set_extension(CreateExtension(SCOPES));
166 std::string error = utils::RunFunctionAndReturnError(
167 func.get(), "[{}]", browser());
168 EXPECT_EQ(std::string(errors::kInvalidClientId), error);
169 EXPECT_FALSE(func->login_ui_shown());
170 EXPECT_FALSE(func->install_ui_shown());
171 }
172
173 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
174 NoScopes) {
175 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction());
176 func->set_extension(CreateExtension(CLIENT_ID));
177 std::string error = utils::RunFunctionAndReturnError(
178 func.get(), "[{}]", browser());
179 EXPECT_EQ(std::string(errors::kInvalidScopes), error);
180 EXPECT_FALSE(func->login_ui_shown());
181 EXPECT_FALSE(func->install_ui_shown());
182 }
183
184 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
185 NonInteractiveNotSignedIn) {
186 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction());
187 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
188 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(false));
189 std::string error = utils::RunFunctionAndReturnError(
190 func.get(), "[{}]", browser());
191 EXPECT_EQ(std::string(errors::kUserNotSignedIn), error);
192 EXPECT_FALSE(func->login_ui_shown());
193 EXPECT_FALSE(func->install_ui_shown());
194 }
195
196 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
197 NonInteractiveMintFailure) {
198 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction());
199 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
200 EXPECT_CALL(*func.get(), HasLoginToken())
201 .WillOnce(Return(true))
202 .WillOnce(Return(true));
203 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow(
204 TestOAuth2MintTokenFlow::MINT_TOKEN_FAILURE, func.get());
205 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
206 std::string error = utils::RunFunctionAndReturnError(
207 func.get(), "[{}]", browser());
208 EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false));
209 EXPECT_FALSE(func->login_ui_shown());
210 EXPECT_FALSE(func->install_ui_shown());
211 }
212
213 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
214 NonInteractiveSuccess) {
215 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction());
216 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
217 EXPECT_CALL(*func.get(), HasLoginToken())
218 .WillOnce(Return(true))
219 .WillOnce(Return(true));
220 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow(
221 TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS, func.get());
222 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
223 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult(
224 func.get(), "[{}]", browser()));
225 std::string access_token;
226 EXPECT_TRUE(value->GetAsString(&access_token));
227 EXPECT_EQ(std::string(kAccessToken), access_token);
228 EXPECT_FALSE(func->login_ui_shown());
229 EXPECT_FALSE(func->install_ui_shown());
230 }
231
232 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
233 InteractiveLoginCanceled) {
234 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction());
235 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
236 EXPECT_CALL(*func.get(), HasLoginToken()).WillRepeatedly(Return(false));
237 std::string error = utils::RunFunctionAndReturnError(
238 func.get(), "[{\"interactive\": true}]", browser());
239 EXPECT_EQ(std::string(errors::kUserNotSignedIn), error);
240 EXPECT_TRUE(func->login_ui_shown());
241 EXPECT_FALSE(func->install_ui_shown());
242 }
243
244 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
245 InteractiveLoginSuccessMintFailure) {
246 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction());
247 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
248 EXPECT_CALL(*func.get(), HasLoginToken())
249 .WillOnce(Return(false))
250 .WillOnce(Return(true));
251 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow(
252 TestOAuth2MintTokenFlow::MINT_TOKEN_FAILURE, func.get());
253 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
254 std::string error = utils::RunFunctionAndReturnError(
255 func.get(), "[{\"interactive\": true}]", browser());
256 EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false));
257 EXPECT_TRUE(func->login_ui_shown());
258 EXPECT_FALSE(func->install_ui_shown());
259 }
260
261 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
262 InteractiveLoginSuccessMintSuccess) {
263 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction());
264 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
265 EXPECT_CALL(*func.get(), HasLoginToken())
266 .WillOnce(Return(false))
267 .WillOnce(Return(true));
268 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow(
269 TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS, func.get());
270 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
271 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult(
272 func.get(), "[{\"interactive\": true}]", browser()));
273 std::string access_token;
274 EXPECT_TRUE(value->GetAsString(&access_token));
275 EXPECT_EQ(std::string(kAccessToken), access_token);
276 EXPECT_TRUE(func->login_ui_shown());
277 EXPECT_FALSE(func->install_ui_shown());
278 }
279
280 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
281 InteractiveLoginSuccessApprovalAborted) {
282 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction());
283 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
284 EXPECT_CALL(*func.get(), HasLoginToken())
285 .WillOnce(Return(false))
286 .WillOnce(Return(true));
287 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow(
288 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get());
289 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
290 func->set_install_ui_result(false);
291 std::string error = utils::RunFunctionAndReturnError(
292 func.get(), "[{\"interactive\": true}]", browser());
293 EXPECT_EQ(std::string(errors::kUserRejected), error);
294 EXPECT_TRUE(func->login_ui_shown());
295 EXPECT_TRUE(func->install_ui_shown());
296 }
297
298 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
299 InteractiveLoginSuccessApprovalDoneMintFailure) {
300 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction());
301 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
302 EXPECT_CALL(*func.get(), HasLoginToken())
303 .WillOnce(Return(false))
304 .WillOnce(Return(true))
305 .WillOnce(Return(true));
306 TestOAuth2MintTokenFlow* flow1 = new TestOAuth2MintTokenFlow(
307 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get());
308 TestOAuth2MintTokenFlow* flow2 = new TestOAuth2MintTokenFlow(
309 TestOAuth2MintTokenFlow::MINT_TOKEN_FAILURE, func.get());
310 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_))
311 .WillOnce(Return(flow1))
312 .WillOnce(Return(flow2));
313
314 func->set_install_ui_result(true);
315 std::string error = utils::RunFunctionAndReturnError(
316 func.get(), "[{\"interactive\": true}]", browser());
317 EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false));
318 EXPECT_TRUE(func->login_ui_shown());
319 EXPECT_TRUE(func->install_ui_shown());
320 }
321
322 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
323 InteractiveLoginSuccessApprovalDoneMintSuccess) {
324 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction());
325 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
326 EXPECT_CALL(*func.get(), HasLoginToken())
327 .WillOnce(Return(false))
328 .WillOnce(Return(true))
329 .WillOnce(Return(true));
330 TestOAuth2MintTokenFlow* flow1 = new TestOAuth2MintTokenFlow(
331 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get());
332 TestOAuth2MintTokenFlow* flow2 = new TestOAuth2MintTokenFlow(
333 TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS, func.get());
334 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_))
335 .WillOnce(Return(flow1))
336 .WillOnce(Return(flow2));
337
338 func->set_install_ui_result(true);
339 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult(
340 func.get(), "[{\"interactive\": true}]", browser()));
341 std::string access_token;
342 EXPECT_TRUE(value->GetAsString(&access_token));
343 EXPECT_EQ(std::string(kAccessToken), access_token);
344 EXPECT_TRUE(func->login_ui_shown());
345 EXPECT_TRUE(func->install_ui_shown());
346 }
347
348 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
349 InteractiveApprovalAborted) {
350 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction());
351 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
352 EXPECT_CALL(*func.get(), HasLoginToken())
353 .WillOnce(Return(true))
354 .WillOnce(Return(true));
355 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow(
356 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get());
357 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
358 func->set_install_ui_result(false);
359 std::string error = utils::RunFunctionAndReturnError(
360 func.get(), "[{\"interactive\": true}]", browser());
361 EXPECT_EQ(std::string(errors::kUserRejected), error);
362 EXPECT_FALSE(func->login_ui_shown());
363 EXPECT_TRUE(func->install_ui_shown());
364 }
365
366 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
367 InteractiveApprovalDoneMintSuccess) {
368 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction());
369 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
370 EXPECT_CALL(*func.get(), HasLoginToken())
371 .WillOnce(Return(true))
372 .WillOnce(Return(true))
373 .WillOnce(Return(true));
374 TestOAuth2MintTokenFlow* flow1 = new TestOAuth2MintTokenFlow(
375 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get());
376 TestOAuth2MintTokenFlow* flow2 = new TestOAuth2MintTokenFlow(
377 TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS, func.get());
378 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_))
379 .WillOnce(Return(flow1))
380 .WillOnce(Return(flow2));
381
382 func->set_install_ui_result(true);
383 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult(
384 func.get(), "[{\"interactive\": true}]", browser()));
385 std::string access_token;
386 EXPECT_TRUE(value->GetAsString(&access_token));
387 EXPECT_EQ(std::string(kAccessToken), access_token);
388 EXPECT_FALSE(func->login_ui_shown());
389 EXPECT_TRUE(func->install_ui_shown());
390 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/identity/identity_api.cc ('k') | chrome/browser/extensions/extension_function_test_utils.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698