1 // Copyright 2013 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.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/run_loop.h"
7 #include "base/time/time.h"
8 #include "chrome/browser/signin/account_reconcilor_factory.h"
9 #include "chrome/browser/signin/chrome_signin_client_factory.h"
10 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
11 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
12 #include "chrome/browser/signin/fake_signin_manager.h"
13 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
14 #include "chrome/browser/signin/signin_manager_factory.h"
15 #include "chrome/test/base/testing_profile.h"
16 #include "components/signin/core/browser/account_reconcilor.h"
17 #include "components/signin/core/browser/profile_oauth2_token_service.h"
18 #include "components/signin/core/browser/signin_manager.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "google_apis/gaia/gaia_urls.h"
21 #include "net/url_request/test_url_fetcher_factory.h"
22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h"
27 const char kTestEmail
[] = "user@gmail.com";
29 class MockAccountReconcilor
: public testing::StrictMock
<AccountReconcilor
> {
31 static KeyedService
* Build(content::BrowserContext
* context
);
33 MockAccountReconcilor(ProfileOAuth2TokenService
* token_service
,
34 SigninManagerBase
* signin_manager
,
35 SigninClient
* client
);
36 virtual ~MockAccountReconcilor() {}
38 MOCK_METHOD1(PerformMergeAction
, void(const std::string
& account_id
));
39 MOCK_METHOD1(StartRemoveAction
, void(const std::string
& account_id
));
42 void(const std::string
& account_id
,
43 const GoogleServiceAuthError
& error
,
44 const std::vector
<std::pair
<std::string
, bool> >& accounts
));
45 MOCK_METHOD2(PerformAddToChromeAction
, void(const std::string
& account_id
,
47 MOCK_METHOD0(PerformLogoutAllAccountsAction
, void());
51 KeyedService
* MockAccountReconcilor::Build(content::BrowserContext
* context
) {
52 Profile
* profile
= Profile::FromBrowserContext(context
);
53 AccountReconcilor
* reconcilor
= new MockAccountReconcilor(
54 ProfileOAuth2TokenServiceFactory::GetForProfile(profile
),
55 SigninManagerFactory::GetForProfile(profile
),
56 ChromeSigninClientFactory::GetForProfile(profile
));
57 reconcilor
->Initialize(false /* start_reconcile_if_tokens_available */);
61 MockAccountReconcilor::MockAccountReconcilor(
62 ProfileOAuth2TokenService
* token_service
,
63 SigninManagerBase
* signin_manager
,
65 : testing::StrictMock
<AccountReconcilor
>(token_service
,
71 class AccountReconcilorTest
: public testing::Test
{
73 AccountReconcilorTest();
74 virtual void SetUp() OVERRIDE
;
75 virtual void TearDown() OVERRIDE
;
77 TestingProfile
* profile() { return profile_
.get(); }
78 FakeSigninManagerForTesting
* signin_manager() { return signin_manager_
; }
79 FakeProfileOAuth2TokenService
* token_service() { return token_service_
; }
81 void SetFakeResponse(const std::string
& url
,
82 const std::string
& data
,
83 net::HttpStatusCode code
,
84 net::URLRequestStatus::Status status
) {
85 url_fetcher_factory_
.SetFakeResponse(GURL(url
), data
, code
, status
);
88 MockAccountReconcilor
* GetMockReconcilor();
90 void SimulateMergeSessionCompleted(
91 MergeSessionHelper::Observer
* observer
,
92 const std::string
& account_id
,
93 const GoogleServiceAuthError
& error
);
95 void SimulateRefreshTokenFetched(
96 AccountReconcilor
* reconcilor
,
97 const std::string
& account_id
,
98 const std::string
& refresh_token
);
101 content::TestBrowserThreadBundle bundle_
;
102 scoped_ptr
<TestingProfile
> profile_
;
103 FakeSigninManagerForTesting
* signin_manager_
;
104 FakeProfileOAuth2TokenService
* token_service_
;
105 MockAccountReconcilor
* mock_reconcilor_
;
106 net::FakeURLFetcherFactory url_fetcher_factory_
;
109 AccountReconcilorTest::AccountReconcilorTest()
110 : signin_manager_(NULL
),
111 token_service_(NULL
),
112 mock_reconcilor_(NULL
),
113 url_fetcher_factory_(NULL
) {}
115 void AccountReconcilorTest::SetUp() {
116 TestingProfile::Builder builder
;
117 builder
.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
118 BuildFakeProfileOAuth2TokenService
);
119 builder
.AddTestingFactory(SigninManagerFactory::GetInstance(),
120 FakeSigninManagerBase::Build
);
121 builder
.AddTestingFactory(AccountReconcilorFactory::GetInstance(),
122 MockAccountReconcilor::Build
);
123 profile_
= builder
.Build();
126 static_cast<FakeSigninManagerForTesting
*>(
127 SigninManagerFactory::GetForProfile(profile()));
130 static_cast<FakeProfileOAuth2TokenService
*>(
131 ProfileOAuth2TokenServiceFactory::GetForProfile(profile()));
134 void AccountReconcilorTest::TearDown() {
135 // Destroy the profile before all threads are torn down.
139 MockAccountReconcilor
* AccountReconcilorTest::GetMockReconcilor() {
140 if (!mock_reconcilor_
) {
142 static_cast<MockAccountReconcilor
*>(
143 AccountReconcilorFactory::GetForProfile(profile()));
146 return mock_reconcilor_
;
149 void AccountReconcilorTest::SimulateMergeSessionCompleted(
150 MergeSessionHelper::Observer
* observer
,
151 const std::string
& account_id
,
152 const GoogleServiceAuthError
& error
) {
153 observer
->MergeSessionCompleted(account_id
, error
);
156 void AccountReconcilorTest::SimulateRefreshTokenFetched(
157 AccountReconcilor
* reconcilor
,
158 const std::string
& account_id
,
159 const std::string
& refresh_token
) {
160 reconcilor
->HandleRefreshTokenFetched(account_id
, refresh_token
);
163 TEST_F(AccountReconcilorTest
, Basic
) {
164 AccountReconcilor
* reconcilor
=
165 AccountReconcilorFactory::GetForProfile(profile());
166 ASSERT_TRUE(reconcilor
);
167 ASSERT_EQ(token_service(), reconcilor
->token_service());
170 #if !defined(OS_CHROMEOS)
172 TEST_F(AccountReconcilorTest
, SigninManagerRegistration
) {
173 AccountReconcilor
* reconcilor
=
174 AccountReconcilorFactory::GetForProfile(profile());
175 ASSERT_TRUE(reconcilor
);
176 ASSERT_FALSE(reconcilor
->IsRegisteredWithTokenService());
178 signin_manager()->OnExternalSigninCompleted(kTestEmail
);
179 ASSERT_TRUE(reconcilor
->IsRegisteredWithTokenService());
181 EXPECT_CALL(*GetMockReconcilor(), PerformLogoutAllAccountsAction());
183 signin_manager()->SignOut();
184 ASSERT_FALSE(reconcilor
->IsRegisteredWithTokenService());
187 TEST_F(AccountReconcilorTest
, Reauth
) {
188 signin_manager()->SetAuthenticatedUsername(kTestEmail
);
190 AccountReconcilor
* reconcilor
=
191 AccountReconcilorFactory::GetForProfile(profile());
192 ASSERT_TRUE(reconcilor
);
193 ASSERT_TRUE(reconcilor
->IsRegisteredWithTokenService());
195 // Simulate reauth. The state of the reconcilor should not change.
196 signin_manager()->OnExternalSigninCompleted(kTestEmail
);
197 ASSERT_TRUE(reconcilor
->IsRegisteredWithTokenService());
200 #endif // !defined(OS_CHROMEOS)
202 TEST_F(AccountReconcilorTest
, ProfileAlreadyConnected
) {
203 signin_manager()->SetAuthenticatedUsername(kTestEmail
);
205 AccountReconcilor
* reconcilor
=
206 AccountReconcilorFactory::GetForProfile(profile());
207 ASSERT_TRUE(reconcilor
);
208 ASSERT_TRUE(reconcilor
->IsRegisteredWithTokenService());
211 TEST_F(AccountReconcilorTest
, GetAccountsFromCookieSuccess
) {
212 signin_manager()->SetAuthenticatedUsername(kTestEmail
);
213 token_service()->UpdateCredentials(kTestEmail
, "refresh_token");
214 AccountReconcilor
* reconcilor
=
215 AccountReconcilorFactory::GetForProfile(profile());
216 ASSERT_TRUE(reconcilor
);
218 SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
219 "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 0]]]",
220 net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
222 reconcilor
->StartReconcile();
223 ASSERT_FALSE(reconcilor
->AreGaiaAccountsSet());
225 base::RunLoop().RunUntilIdle();
226 ASSERT_TRUE(reconcilor
->AreGaiaAccountsSet());
227 const std::vector
<std::pair
<std::string
, bool> >& accounts
=
228 reconcilor
->GetGaiaAccountsForTesting();
229 ASSERT_EQ(1u, accounts
.size());
230 ASSERT_EQ("user@gmail.com", accounts
[0].first
);
233 TEST_F(AccountReconcilorTest
, GetAccountsFromCookieFailure
) {
234 signin_manager()->SetAuthenticatedUsername(kTestEmail
);
235 token_service()->UpdateCredentials(kTestEmail
, "refresh_token");
236 AccountReconcilor
* reconcilor
=
237 AccountReconcilorFactory::GetForProfile(profile());
238 ASSERT_TRUE(reconcilor
);
240 SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(), "",
241 net::HTTP_NOT_FOUND
, net::URLRequestStatus::SUCCESS
);
243 reconcilor
->StartReconcile();
244 ASSERT_FALSE(reconcilor
->AreGaiaAccountsSet());
246 base::RunLoop().RunUntilIdle();
247 ASSERT_EQ(0u, reconcilor
->GetGaiaAccountsForTesting().size());
250 TEST_F(AccountReconcilorTest
, ValidateAccountsFromTokens
) {
251 signin_manager()->SetAuthenticatedUsername(kTestEmail
);
252 token_service()->UpdateCredentials(kTestEmail
, "refresh_token");
254 AccountReconcilor
* reconcilor
=
255 AccountReconcilorFactory::GetForProfile(profile());
256 ASSERT_TRUE(reconcilor
);
258 reconcilor
->ValidateAccountsFromTokenService();
259 ASSERT_FALSE(reconcilor
->AreAllRefreshTokensChecked());
261 SetFakeResponse(GaiaUrls::GetInstance()->people_get_url().spec(),
262 "{\"id\":\"foo\"}", net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
263 token_service()->IssueTokenForAllPendingRequests("access_token",
264 base::Time::Now() + base::TimeDelta::FromHours(1));
266 base::RunLoop().RunUntilIdle();
267 ASSERT_TRUE(reconcilor
->AreAllRefreshTokensChecked());
268 ASSERT_EQ(1u, reconcilor
->GetValidChromeAccountsForTesting().size());
269 ASSERT_EQ(0u, reconcilor
->GetInvalidChromeAccountsForTesting().size());
272 TEST_F(AccountReconcilorTest
, ValidateAccountsFromTokensFailedUserInfo
) {
273 signin_manager()->SetAuthenticatedUsername(kTestEmail
);
274 token_service()->UpdateCredentials(kTestEmail
, "refresh_token");
276 AccountReconcilor
* reconcilor
=
277 AccountReconcilorFactory::GetForProfile(profile());
278 ASSERT_TRUE(reconcilor
);
280 reconcilor
->ValidateAccountsFromTokenService();
281 ASSERT_FALSE(reconcilor
->AreAllRefreshTokensChecked());
283 SetFakeResponse(GaiaUrls::GetInstance()->people_get_url().spec(),
284 "", net::HTTP_NOT_FOUND
, net::URLRequestStatus::SUCCESS
);
285 token_service()->IssueTokenForAllPendingRequests("access_token",
286 base::Time::Now() + base::TimeDelta::FromHours(1));
288 base::RunLoop().RunUntilIdle();
289 ASSERT_TRUE(reconcilor
->AreAllRefreshTokensChecked());
290 ASSERT_EQ(0u, reconcilor
->GetValidChromeAccountsForTesting().size());
291 ASSERT_EQ(1u, reconcilor
->GetInvalidChromeAccountsForTesting().size());
294 TEST_F(AccountReconcilorTest
, ValidateAccountsFromTokensFailedTokenRequest
) {
295 signin_manager()->SetAuthenticatedUsername(kTestEmail
);
296 token_service()->UpdateCredentials(kTestEmail
, "refresh_token");
298 AccountReconcilor
* reconcilor
=
299 AccountReconcilorFactory::GetForProfile(profile());
300 ASSERT_TRUE(reconcilor
);
302 reconcilor
->ValidateAccountsFromTokenService();
303 ASSERT_FALSE(reconcilor
->AreAllRefreshTokensChecked());
305 token_service()->IssueErrorForAllPendingRequests(
306 GoogleServiceAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
));
308 base::RunLoop().RunUntilIdle();
309 ASSERT_TRUE(reconcilor
->AreAllRefreshTokensChecked());
310 ASSERT_EQ(0u, reconcilor
->GetValidChromeAccountsForTesting().size());
311 ASSERT_EQ(1u, reconcilor
->GetInvalidChromeAccountsForTesting().size());
314 TEST_F(AccountReconcilorTest
, StartReconcileNoop
) {
315 signin_manager()->SetAuthenticatedUsername(kTestEmail
);
316 token_service()->UpdateCredentials(kTestEmail
, "refresh_token");
318 AccountReconcilor
* reconcilor
=
319 AccountReconcilorFactory::GetForProfile(profile());
320 ASSERT_TRUE(reconcilor
);
322 SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
323 "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
324 net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
325 SetFakeResponse(GaiaUrls::GetInstance()->people_get_url().spec(),
326 "{\"id\":\"foo\"}", net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
328 reconcilor
->StartReconcile();
329 ASSERT_FALSE(reconcilor
->AreGaiaAccountsSet());
330 ASSERT_FALSE(reconcilor
->AreAllRefreshTokensChecked());
332 base::RunLoop().RunUntilIdle();
333 ASSERT_TRUE(reconcilor
->AreGaiaAccountsSet());
334 ASSERT_EQ(1u, reconcilor
->GetGaiaAccountsForTesting().size());
335 ASSERT_FALSE(reconcilor
->AreAllRefreshTokensChecked());
337 token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
338 base::Time::Now() + base::TimeDelta::FromHours(1));
340 base::RunLoop().RunUntilIdle();
341 ASSERT_TRUE(reconcilor
->AreAllRefreshTokensChecked());
342 ASSERT_FALSE(reconcilor
->is_reconcile_started_
);
345 // This is test is needed until chrome changes to use gaia obfuscated id.
346 // The signin manager and token service use the gaia "email" property, which
347 // preserves dots in usernames and preserves case. gaia::ParseListAccountsData()
348 // however uses gaia "displayEmail" which does not preserve case, and then
349 // passes the string through gaia::CanonicalizeEmail() which removes dots. This
350 // tests makes sure that an email like "Dot.S@hmail.com", as seen by the
351 // token service, will be considered the same as "dots@gmail.com" as returned
352 // by gaia::ParseListAccountsData().
353 TEST_F(AccountReconcilorTest
, StartReconcileNoopWithDots
) {
354 signin_manager()->SetAuthenticatedUsername("Dot.S@gmail.com");
355 token_service()->UpdateCredentials("Dot.S@gmail.com", "refresh_token");
357 AccountReconcilor
* reconcilor
=
358 AccountReconcilorFactory::GetForProfile(profile());
359 ASSERT_TRUE(reconcilor
);
361 SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
362 "[\"f\", [[\"b\", 0, \"n\", \"dot.s@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
363 net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
364 SetFakeResponse(GaiaUrls::GetInstance()->people_get_url().spec(),
365 "{\"id\":\"foo\"}", net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
367 reconcilor
->StartReconcile();
368 ASSERT_FALSE(reconcilor
->AreGaiaAccountsSet());
369 ASSERT_FALSE(reconcilor
->AreAllRefreshTokensChecked());
371 base::RunLoop().RunUntilIdle();
372 ASSERT_TRUE(reconcilor
->AreGaiaAccountsSet());
373 ASSERT_EQ(1u, reconcilor
->GetGaiaAccountsForTesting().size());
374 ASSERT_STREQ("dots@gmail.com",
375 reconcilor
->GetGaiaAccountsForTesting()[0].first
.c_str());
376 ASSERT_FALSE(reconcilor
->AreAllRefreshTokensChecked());
378 token_service()->IssueAllTokensForAccount("Dot.S@gmail.com", "access_token",
379 base::Time::Now() + base::TimeDelta::FromHours(1));
381 base::RunLoop().RunUntilIdle();
382 ASSERT_TRUE(reconcilor
->AreAllRefreshTokensChecked());
383 ASSERT_FALSE(reconcilor
->is_reconcile_started_
);
386 TEST_F(AccountReconcilorTest
, StartReconcileNoopMultiple
) {
387 signin_manager()->SetAuthenticatedUsername("user@gmail.com");
388 token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
389 token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
391 AccountReconcilor
* reconcilor
=
392 AccountReconcilorFactory::GetForProfile(profile());
393 ASSERT_TRUE(reconcilor
);
395 SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
396 "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
397 "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
398 net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
399 SetFakeResponse(GaiaUrls::GetInstance()->people_get_url().spec(),
400 "{\"id\":\"foo\"}", net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
402 reconcilor
->StartReconcile();
403 ASSERT_FALSE(reconcilor
->AreGaiaAccountsSet());
404 ASSERT_FALSE(reconcilor
->AreAllRefreshTokensChecked());
406 base::RunLoop().RunUntilIdle();
407 ASSERT_TRUE(reconcilor
->AreGaiaAccountsSet());
408 ASSERT_FALSE(reconcilor
->AreAllRefreshTokensChecked());
409 ASSERT_EQ(2u, reconcilor
->GetGaiaAccountsForTesting().size());
411 token_service()->IssueAllTokensForAccount("other@gmail.com", "access_token",
412 base::Time::Now() + base::TimeDelta::FromHours(1));
414 base::RunLoop().RunUntilIdle();
415 ASSERT_FALSE(reconcilor
->AreAllRefreshTokensChecked());
417 token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
418 base::Time::Now() + base::TimeDelta::FromHours(1));
420 base::RunLoop().RunUntilIdle();
421 ASSERT_TRUE(reconcilor
->AreAllRefreshTokensChecked());
422 ASSERT_FALSE(reconcilor
->is_reconcile_started_
);
425 TEST_F(AccountReconcilorTest
, StartReconcileAddToCookie
) {
426 signin_manager()->SetAuthenticatedUsername("user@gmail.com");
427 token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
428 token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
430 EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("other@gmail.com"));
432 SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
433 "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
434 net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
435 SetFakeResponse(GaiaUrls::GetInstance()->people_get_url().spec(),
436 "{\"id\":\"foo\"}", net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
438 AccountReconcilor
* reconcilor
= GetMockReconcilor();
439 reconcilor
->StartReconcile();
440 token_service()->IssueAllTokensForAccount("other@gmail.com", "access_token",
441 base::Time::Now() + base::TimeDelta::FromHours(1));
442 token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
443 base::Time::Now() + base::TimeDelta::FromHours(1));
445 base::RunLoop().RunUntilIdle();
446 ASSERT_TRUE(reconcilor
->is_reconcile_started_
);
447 SimulateMergeSessionCompleted(reconcilor
, "other@gmail.com",
448 GoogleServiceAuthError::AuthErrorNone());
449 ASSERT_FALSE(reconcilor
->is_reconcile_started_
);
452 TEST_F(AccountReconcilorTest
, StartReconcileAddToChrome
) {
453 signin_manager()->SetAuthenticatedUsername("user@gmail.com");
454 token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
456 EXPECT_CALL(*GetMockReconcilor(),
457 PerformAddToChromeAction("other@gmail.com", 1));
459 SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
460 "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
461 "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
462 net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
463 SetFakeResponse(GaiaUrls::GetInstance()->people_get_url().spec(),
464 "{\"id\":\"foo\"}", net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
466 AccountReconcilor
* reconcilor
= GetMockReconcilor();
467 reconcilor
->StartReconcile();
468 token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
469 base::Time::Now() + base::TimeDelta::FromHours(1));
471 base::RunLoop().RunUntilIdle();
472 ASSERT_TRUE(reconcilor
->is_reconcile_started_
);
473 SimulateRefreshTokenFetched(reconcilor
, "other@gmail.com", "");
474 ASSERT_FALSE(reconcilor
->is_reconcile_started_
);
477 TEST_F(AccountReconcilorTest
, StartReconcileBadPrimary
) {
478 signin_manager()->SetAuthenticatedUsername("user@gmail.com");
479 token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
480 token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
482 EXPECT_CALL(*GetMockReconcilor(), PerformLogoutAllAccountsAction());
483 EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("user@gmail.com"));
484 EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("other@gmail.com"));
486 SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
487 "[\"f\", [[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
488 "[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
489 net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
490 SetFakeResponse(GaiaUrls::GetInstance()->people_get_url().spec(),
491 "{\"id\":\"foo\"}", net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
493 AccountReconcilor
* reconcilor
= GetMockReconcilor();
494 reconcilor
->StartReconcile();
495 token_service()->IssueAllTokensForAccount("other@gmail.com", "access_token",
496 base::Time::Now() + base::TimeDelta::FromHours(1));
497 token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
498 base::Time::Now() + base::TimeDelta::FromHours(1));
500 base::RunLoop().RunUntilIdle();
501 ASSERT_TRUE(reconcilor
->is_reconcile_started_
);
502 SimulateMergeSessionCompleted(reconcilor
, "other@gmail.com",
503 GoogleServiceAuthError::AuthErrorNone());
504 ASSERT_TRUE(reconcilor
->is_reconcile_started_
);
505 SimulateMergeSessionCompleted(reconcilor
, "user@gmail.com",
506 GoogleServiceAuthError::AuthErrorNone());
507 ASSERT_FALSE(reconcilor
->is_reconcile_started_
);
510 TEST_F(AccountReconcilorTest
, StartReconcileOnlyOnce
) {
511 signin_manager()->SetAuthenticatedUsername(kTestEmail
);
512 token_service()->UpdateCredentials(kTestEmail
, "refresh_token");
514 AccountReconcilor
* reconcilor
=
515 AccountReconcilorFactory::GetForProfile(profile());
516 ASSERT_TRUE(reconcilor
);
518 SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
519 "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
520 net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
521 SetFakeResponse(GaiaUrls::GetInstance()->people_get_url().spec(),
522 "{\"id\":\"foo\"}", net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
524 ASSERT_FALSE(reconcilor
->is_reconcile_started_
);
525 reconcilor
->StartReconcile();
526 ASSERT_TRUE(reconcilor
->is_reconcile_started_
);
528 token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
529 base::Time::Now() + base::TimeDelta::FromHours(1));
531 base::RunLoop().RunUntilIdle();
532 ASSERT_FALSE(reconcilor
->is_reconcile_started_
);
535 TEST_F(AccountReconcilorTest
, StartReconcileWithSessionInfoExpiredDefault
) {
536 signin_manager()->SetAuthenticatedUsername("user@gmail.com");
537 token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
538 token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
540 EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("user@gmail.com"));
542 SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
543 "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 0],"
544 "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
545 net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
546 SetFakeResponse(GaiaUrls::GetInstance()->people_get_url().spec(),
547 "{\"id\":\"foo\"}", net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
549 AccountReconcilor
* reconcilor
=
550 AccountReconcilorFactory::GetForProfile(profile());
551 ASSERT_TRUE(reconcilor
);
553 ASSERT_FALSE(reconcilor
->is_reconcile_started_
);
554 reconcilor
->StartReconcile();
555 ASSERT_TRUE(reconcilor
->is_reconcile_started_
);
557 token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
558 base::Time::Now() + base::TimeDelta::FromHours(1));
559 token_service()->IssueAllTokensForAccount("other@gmail.com", "access_token",
560 base::Time::Now() + base::TimeDelta::FromHours(1));
562 base::RunLoop().RunUntilIdle();
563 SimulateMergeSessionCompleted(reconcilor
, "user@gmail.com",
564 GoogleServiceAuthError::AuthErrorNone());
565 ASSERT_FALSE(reconcilor
->is_reconcile_started_
);