OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "chrome/browser/signin/dice_response_handler.h" | 5 #include "chrome/browser/signin/dice_response_handler.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/test/test_simple_task_runner.h" | 10 #include "base/test/test_mock_time_task_runner.h" |
| 11 #include "base/threading/thread_task_runner_handle.h" |
| 12 #include "base/time/time.h" |
11 #include "chrome/test/base/testing_profile.h" | 13 #include "chrome/test/base/testing_profile.h" |
12 #include "components/signin/core/browser/account_tracker_service.h" | 14 #include "components/signin/core/browser/account_tracker_service.h" |
13 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 15 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
14 #include "components/signin/core/browser/signin_header_helper.h" | 16 #include "components/signin/core/browser/signin_header_helper.h" |
15 #include "components/signin/core/browser/test_signin_client.h" | 17 #include "components/signin/core/browser/test_signin_client.h" |
16 #include "components/signin/core/common/profile_management_switches.h" | 18 #include "components/signin/core/common/profile_management_switches.h" |
17 #include "components/sync_preferences/testing_pref_service_syncable.h" | 19 #include "components/sync_preferences/testing_pref_service_syncable.h" |
18 #include "content/public/test/test_browser_thread_bundle.h" | 20 #include "content/public/test/test_browser_thread_bundle.h" |
19 #include "google_apis/gaia/fake_oauth2_token_service_delegate.h" | 21 #include "google_apis/gaia/fake_oauth2_token_service_delegate.h" |
20 #include "net/url_request/url_request_test_util.h" | 22 #include "net/url_request/url_request_test_util.h" |
(...skipping 27 matching lines...) Expand all Loading... |
48 // Pass |this| as a dummy consumer. Drop everything on the floor. | 50 // Pass |this| as a dummy consumer. Drop everything on the floor. |
49 return TestSigninClient::CreateGaiaAuthFetcher(this, source, getter); | 51 return TestSigninClient::CreateGaiaAuthFetcher(this, source, getter); |
50 } | 52 } |
51 | 53 |
52 GaiaAuthConsumer* consumer_; | 54 GaiaAuthConsumer* consumer_; |
53 }; | 55 }; |
54 | 56 |
55 class DiceResponseHandlerTest : public testing::Test { | 57 class DiceResponseHandlerTest : public testing::Test { |
56 protected: | 58 protected: |
57 DiceResponseHandlerTest() | 59 DiceResponseHandlerTest() |
58 : task_runner_(new base::TestSimpleTaskRunner()), | 60 : loop_(base::MessageLoop::TYPE_IO), // URLRequestContext requires IO. |
| 61 task_runner_(new base::TestMockTimeTaskRunner()), |
59 request_context_getter_( | 62 request_context_getter_( |
60 new net::TestURLRequestContextGetter(task_runner_)), | 63 new net::TestURLRequestContextGetter(task_runner_)), |
61 signin_client_(&pref_service_), | 64 signin_client_(&pref_service_), |
62 token_service_(base::MakeUnique<FakeOAuth2TokenServiceDelegate>( | 65 token_service_(base::MakeUnique<FakeOAuth2TokenServiceDelegate>( |
63 request_context_getter_.get())), | 66 request_context_getter_.get())), |
64 dice_response_handler_(&signin_client_, | 67 dice_response_handler_(&signin_client_, |
65 &token_service_, | 68 &token_service_, |
66 &account_tracker_service_) { | 69 &account_tracker_service_) { |
| 70 loop_.SetTaskRunner(task_runner_); |
| 71 DCHECK_EQ(task_runner_, base::ThreadTaskRunnerHandle::Get()); |
67 switches::EnableAccountConsistencyDiceForTesting( | 72 switches::EnableAccountConsistencyDiceForTesting( |
68 base::CommandLine::ForCurrentProcess()); | 73 base::CommandLine::ForCurrentProcess()); |
69 signin_client_.SetURLRequestContext(request_context_getter_.get()); | 74 signin_client_.SetURLRequestContext(request_context_getter_.get()); |
70 AccountTrackerService::RegisterPrefs(pref_service_.registry()); | 75 AccountTrackerService::RegisterPrefs(pref_service_.registry()); |
71 account_tracker_service_.Initialize(&signin_client_); | 76 account_tracker_service_.Initialize(&signin_client_); |
72 } | 77 } |
73 | 78 |
74 ~DiceResponseHandlerTest() override {} | 79 ~DiceResponseHandlerTest() override {} |
75 | 80 |
76 DiceResponseParams MakeDiceParams(DiceAction action) { | 81 DiceResponseParams MakeDiceParams(DiceAction action) { |
77 DiceResponseParams dice_params; | 82 DiceResponseParams dice_params; |
78 dice_params.user_intention = action; | 83 dice_params.user_intention = action; |
79 dice_params.obfuscated_gaia_id = kObfuscatedGaiaID; | 84 dice_params.obfuscated_gaia_id = kObfuscatedGaiaID; |
80 dice_params.email = kEmail; | 85 dice_params.email = kEmail; |
81 dice_params.session_index = kSessionIndex; | 86 dice_params.session_index = kSessionIndex; |
82 dice_params.authorization_code = kAuthorizationCode; | 87 dice_params.authorization_code = kAuthorizationCode; |
83 return dice_params; | 88 return dice_params; |
84 } | 89 } |
85 | 90 |
86 base::MessageLoop loop_; | 91 base::MessageLoop loop_; |
87 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 92 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; |
88 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; | 93 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; |
89 sync_preferences::TestingPrefServiceSyncable pref_service_; | 94 sync_preferences::TestingPrefServiceSyncable pref_service_; |
90 DiceTestSigninClient signin_client_; | 95 DiceTestSigninClient signin_client_; |
91 ProfileOAuth2TokenService token_service_; | 96 ProfileOAuth2TokenService token_service_; |
92 AccountTrackerService account_tracker_service_; | 97 AccountTrackerService account_tracker_service_; |
93 DiceResponseHandler dice_response_handler_; | 98 DiceResponseHandler dice_response_handler_; |
94 }; | 99 }; |
95 | 100 |
96 // Checks that a SIGNIN action triggers a token exchange request. | 101 // Checks that a SIGNIN action triggers a token exchange request. |
97 TEST_F(DiceResponseHandlerTest, Signin) { | 102 TEST_F(DiceResponseHandlerTest, Signin) { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 EXPECT_TRUE( | 164 EXPECT_TRUE( |
160 token_service_.RefreshTokenIsAvailable(dice_params_1.obfuscated_gaia_id)); | 165 token_service_.RefreshTokenIsAvailable(dice_params_1.obfuscated_gaia_id)); |
161 // Simulate GaiaAuthFetcher success for the second request. | 166 // Simulate GaiaAuthFetcher success for the second request. |
162 consumer_2->OnClientOAuthSuccess( | 167 consumer_2->OnClientOAuthSuccess( |
163 GaiaAuthConsumer::ClientOAuthResult("refresh_token", "access_token", 10)); | 168 GaiaAuthConsumer::ClientOAuthResult("refresh_token", "access_token", 10)); |
164 // Check that the token has been inserted in the token service. | 169 // Check that the token has been inserted in the token service. |
165 EXPECT_TRUE( | 170 EXPECT_TRUE( |
166 token_service_.RefreshTokenIsAvailable(dice_params_2.obfuscated_gaia_id)); | 171 token_service_.RefreshTokenIsAvailable(dice_params_2.obfuscated_gaia_id)); |
167 } | 172 } |
168 | 173 |
| 174 TEST_F(DiceResponseHandlerTest, Timeout) { |
| 175 DiceResponseParams dice_params = MakeDiceParams(DiceAction::SIGNIN); |
| 176 ASSERT_FALSE( |
| 177 token_service_.RefreshTokenIsAvailable(dice_params.obfuscated_gaia_id)); |
| 178 dice_response_handler_.ProcessDiceHeader(dice_params); |
| 179 // Check that a GaiaAuthFetcher has been created. |
| 180 ASSERT_THAT(signin_client_.consumer_, testing::NotNull()); |
| 181 EXPECT_EQ( |
| 182 1u, dice_response_handler_.GetPendingDiceTokenFetchersCountForTesting()); |
| 183 // Force a timeout. |
| 184 task_runner_->FastForwardBy( |
| 185 base::TimeDelta::FromSeconds(kDiceTokenFetchTimeoutSeconds + 1)); |
| 186 EXPECT_EQ( |
| 187 0u, dice_response_handler_.GetPendingDiceTokenFetchersCountForTesting()); |
| 188 } |
| 189 |
169 // Tests that the DiceResponseHandler is created for a normal profile but not | 190 // Tests that the DiceResponseHandler is created for a normal profile but not |
170 // for an incognito profile. | 191 // for an incognito profile. |
171 TEST(DiceResponseHandlerFactoryTest, NotInIncognito) { | 192 TEST(DiceResponseHandlerFactoryTest, NotInIncognito) { |
172 content::TestBrowserThreadBundle thread_bundle; | 193 content::TestBrowserThreadBundle thread_bundle; |
173 TestingProfile profile; | 194 TestingProfile profile; |
174 EXPECT_THAT(DiceResponseHandler::GetForProfile(&profile), testing::NotNull()); | 195 EXPECT_THAT(DiceResponseHandler::GetForProfile(&profile), testing::NotNull()); |
175 EXPECT_THAT( | 196 EXPECT_THAT( |
176 DiceResponseHandler::GetForProfile(profile.GetOffTheRecordProfile()), | 197 DiceResponseHandler::GetForProfile(profile.GetOffTheRecordProfile()), |
177 testing::IsNull()); | 198 testing::IsNull()); |
178 } | 199 } |
179 | 200 |
180 } // namespace | 201 } // namespace |
OLD | NEW |