1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 #include "base/location.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/run_loop.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/test/null_task_runner.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "chromeos/login/auth/auth_attempt_state.h"
16 #include "chromeos/login/auth/mock_auth_attempt_state_resolver.h"
17 #include "chromeos/login/auth/mock_url_fetchers.h"
18 #include "chromeos/login/auth/online_attempt.h"
19 #include "chromeos/login/auth/test_attempt_state.h"
20 #include "chromeos/login/auth/user_context.h"
21 #include "content/public/test/test_browser_thread_bundle.h"
22 #include "google_apis/gaia/gaia_auth_consumer.h"
23 #include "google_apis/gaia/mock_url_fetcher_factory.h"
24 #include "net/url_request/url_request_context.h"
25 #include "net/url_request/url_request_context_getter.h"
26 #include "net/url_request/url_request_test_util.h"
27 #include "testing/gmock/include/gmock/gmock.h"
28 #include "testing/gtest/include/gtest/gtest.h"
31 using ::testing::AnyNumber
;
32 using ::testing::Invoke
;
33 using ::testing::Return
;
38 class TestContextURLRequestContextGetter
: public net::URLRequestContextGetter
{
40 TestContextURLRequestContextGetter()
41 : null_task_runner_(new base::NullTaskRunner
) {}
43 net::URLRequestContext
* GetURLRequestContext() override
{ return &context_
; }
45 scoped_refptr
<base::SingleThreadTaskRunner
> GetNetworkTaskRunner()
47 return null_task_runner_
;
51 ~TestContextURLRequestContextGetter() override
{}
53 net::TestURLRequestContext context_
;
54 scoped_refptr
<base::SingleThreadTaskRunner
> null_task_runner_
;
61 class OnlineAttemptTest
: public testing::Test
{
64 : state_(UserContext(), false),
65 attempt_(new OnlineAttempt(&state_
, &resolver_
)) {}
67 void SetUp() override
{
68 task_runner_
= base::ThreadTaskRunnerHandle::Get();
69 request_context_
= new TestContextURLRequestContextGetter();
72 void RunFailureTest(const GoogleServiceAuthError
& error
) {
73 EXPECT_CALL(resolver_
, Resolve()).Times(1).RetiresOnSaturation();
75 task_runner_
->PostTask(
76 FROM_HERE
, base::Bind(&OnlineAttempt::OnClientLoginFailure
,
77 attempt_
->weak_factory_
.GetWeakPtr(), error
));
78 // Force UI thread to finish tasks so I can verify |state_|.
79 base::RunLoop().RunUntilIdle();
80 EXPECT_TRUE(error
== state_
.online_outcome().error());
83 void CancelLogin(OnlineAttempt
* auth
) {
84 task_runner_
->PostTask(FROM_HERE
,
85 base::Bind(&OnlineAttempt::CancelClientLogin
,
86 auth
->weak_factory_
.GetWeakPtr()));
89 content::TestBrowserThreadBundle thread_bundle_
;
90 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
91 scoped_refptr
<net::URLRequestContextGetter
> request_context_
;
92 TestAttemptState state_
;
93 MockAuthAttemptStateResolver resolver_
;
94 scoped_ptr
<OnlineAttempt
> attempt_
;
97 TEST_F(OnlineAttemptTest
, LoginSuccess
) {
98 EXPECT_CALL(resolver_
, Resolve()).Times(1).RetiresOnSaturation();
100 task_runner_
->PostTask(FROM_HERE
,
101 base::Bind(&OnlineAttempt::OnClientLoginSuccess
,
102 attempt_
->weak_factory_
.GetWeakPtr(),
103 GaiaAuthConsumer::ClientLoginResult()));
104 // Force UI thread to finish tasks so I can verify |state_|.
105 base::RunLoop().RunUntilIdle();
108 TEST_F(OnlineAttemptTest
, LoginCancelRetry
) {
109 GoogleServiceAuthError
error(GoogleServiceAuthError::REQUEST_CANCELED
);
110 TestingProfile profile
;
112 base::RunLoop run_loop
;
113 EXPECT_CALL(resolver_
, Resolve())
114 .WillOnce(Invoke(&run_loop
, &base::RunLoop::Quit
))
115 .RetiresOnSaturation();
117 // This is how we inject fake URLFetcher objects, with a factory.
118 // This factory creates fake URLFetchers that Start() a fake fetch attempt
119 // and then come back on the UI thread saying they've been canceled.
120 MockURLFetcherFactory
<GotCanceledFetcher
> factory
;
122 attempt_
->Initiate(request_context_
.get());
126 EXPECT_TRUE(error
== state_
.online_outcome().error());
127 EXPECT_EQ(AuthFailure::NETWORK_AUTH_FAILED
, state_
.online_outcome().reason());
130 TEST_F(OnlineAttemptTest
, LoginTimeout
) {
131 AuthFailure
error(AuthFailure::LOGIN_TIMED_OUT
);
132 TestingProfile profile
;
134 base::RunLoop run_loop
;
135 EXPECT_CALL(resolver_
, Resolve())
136 .WillOnce(Invoke(&run_loop
, &base::RunLoop::Quit
))
137 .RetiresOnSaturation();
139 // This is how we inject fake URLFetcher objects, with a factory.
140 // This factory creates fake URLFetchers that Start() a fake fetch attempt
141 // and then come back on the UI thread saying they've been canceled.
142 MockURLFetcherFactory
<ExpectCanceledFetcher
> factory
;
144 attempt_
->Initiate(request_context_
.get());
146 // Post a task to cancel the login attempt.
147 CancelLogin(attempt_
.get());
151 EXPECT_EQ(AuthFailure::LOGIN_TIMED_OUT
, state_
.online_outcome().reason());
154 TEST_F(OnlineAttemptTest
, HostedLoginRejected
) {
155 AuthFailure
error(AuthFailure::FromNetworkAuthFailure(
156 GoogleServiceAuthError(GoogleServiceAuthError::HOSTED_NOT_ALLOWED
)));
157 TestingProfile profile
;
159 base::RunLoop run_loop
;
160 EXPECT_CALL(resolver_
, Resolve())
161 .WillOnce(Invoke(&run_loop
, &base::RunLoop::Quit
))
162 .RetiresOnSaturation();
164 // This is how we inject fake URLFetcher objects, with a factory.
165 MockURLFetcherFactory
<HostedFetcher
> factory
;
167 TestAttemptState
local_state(UserContext(), true);
168 attempt_
.reset(new OnlineAttempt(&local_state
, &resolver_
));
169 attempt_
->Initiate(request_context_
.get());
173 EXPECT_EQ(error
, local_state
.online_outcome());
174 EXPECT_EQ(AuthFailure::NETWORK_AUTH_FAILED
,
175 local_state
.online_outcome().reason());
178 TEST_F(OnlineAttemptTest
, FullLogin
) {
179 TestingProfile profile
;
181 base::RunLoop run_loop
;
182 EXPECT_CALL(resolver_
, Resolve())
183 .WillOnce(Invoke(&run_loop
, &base::RunLoop::Quit
))
184 .RetiresOnSaturation();
186 // This is how we inject fake URLFetcher objects, with a factory.
187 MockURLFetcherFactory
<SuccessFetcher
> factory
;
189 TestAttemptState
local_state(UserContext(), true);
190 attempt_
.reset(new OnlineAttempt(&local_state
, &resolver_
));
191 attempt_
->Initiate(request_context_
.get());
195 EXPECT_EQ(AuthFailure::AuthFailureNone(), local_state
.online_outcome());
198 TEST_F(OnlineAttemptTest
, LoginNetFailure
) {
200 GoogleServiceAuthError::FromConnectionError(net::ERR_CONNECTION_RESET
));
203 TEST_F(OnlineAttemptTest
, LoginDenied
) {
205 GoogleServiceAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
));
208 TEST_F(OnlineAttemptTest
, LoginAccountDisabled
) {
210 GoogleServiceAuthError(GoogleServiceAuthError::ACCOUNT_DISABLED
));
213 TEST_F(OnlineAttemptTest
, LoginAccountDeleted
) {
215 GoogleServiceAuthError(GoogleServiceAuthError::ACCOUNT_DELETED
));
218 TEST_F(OnlineAttemptTest
, LoginServiceUnavailable
) {
220 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE
));
223 TEST_F(OnlineAttemptTest
, CaptchaErrorOutputted
) {
224 GoogleServiceAuthError auth_error
=
225 GoogleServiceAuthError::FromClientLoginCaptchaChallenge(
227 GURL("http://accounts.google.com/Captcha?ctoken=CCTOKEN"),
228 GURL("http://www.google.com/login/captcha"));
229 RunFailureTest(auth_error
);
232 TEST_F(OnlineAttemptTest
, TwoFactorSuccess
) {
233 EXPECT_CALL(resolver_
, Resolve()).Times(1).RetiresOnSaturation();
234 GoogleServiceAuthError
error(GoogleServiceAuthError::TWO_FACTOR
);
235 task_runner_
->PostTask(
236 FROM_HERE
, base::Bind(&OnlineAttempt::OnClientLoginFailure
,
237 attempt_
->weak_factory_
.GetWeakPtr(), error
));
239 // Force UI thread to finish tasks so I can verify |state_|.
240 base::RunLoop().RunUntilIdle();
241 EXPECT_TRUE(GoogleServiceAuthError::AuthErrorNone() ==
242 state_
.online_outcome().error());
245 } // namespace chromeos