account_reconcilor_unittest.cc revision e5d81f57cb97b3b6b7fccc9c5610d21eb81db09d
18bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
28bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
38bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// found in the LICENSE file.
48bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
58bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/run_loop.h"
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/time/time.h"
88bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/account_reconcilor.h"
98bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/account_reconcilor_factory.h"
10e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/browser/signin/chrome_signin_client_factory.h"
118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
12effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/fake_signin_manager.h"
148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/signin_manager_factory.h"
168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/test/base/testing_profile.h"
17effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "components/signin/core/browser/profile_oauth2_token_service.h"
18e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "components/signin/core/browser/signin_manager.h"
191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "content/public/test/test_browser_thread_bundle.h"
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "google_apis/gaia/gaia_urls.h"
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "net/url_request/test_url_fetcher_factory.h"
228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h"
238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)namespace {
268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)const char kTestEmail[] = "user@gmail.com";
281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class MockAccountReconcilor : public testing::StrictMock<AccountReconcilor> {
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
31e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  static KeyedService* Build(content::BrowserContext* context);
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
33e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  explicit MockAccountReconcilor(Profile* profile, SigninClient* client);
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ~MockAccountReconcilor() {}
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MOCK_METHOD1(PerformMergeAction, void(const std::string& account_id));
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MOCK_METHOD1(StartRemoveAction, void(const std::string& account_id));
38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  MOCK_METHOD3(
39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      FinishRemoveAction,
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      void(const std::string& account_id,
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)           const GoogleServiceAuthError& error,
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)           const std::vector<std::pair<std::string, bool> >& accounts));
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MOCK_METHOD2(PerformAddToChromeAction, void(const std::string& account_id,
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                              int session_index));
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MOCK_METHOD0(PerformLogoutAllAccountsAction, void());
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// static
49e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen MurdochKeyedService* MockAccountReconcilor::Build(content::BrowserContext* context) {
50e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  Profile* profile = Profile::FromBrowserContext(context);
51e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  AccountReconcilor* reconcilor = new MockAccountReconcilor(
52e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      profile, ChromeSigninClientFactory::GetForProfile(profile));
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  reconcilor->Initialize(false /* start_reconcile_if_tokens_available */);
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return reconcilor;
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
57e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen MurdochMockAccountReconcilor::MockAccountReconcilor(Profile* profile,
58e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch                                             SigninClient* client)
59e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    : testing::StrictMock<AccountReconcilor>(profile, client) {}
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)class AccountReconcilorTest : public testing::Test {
648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) public:
651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  AccountReconcilorTest();
668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  virtual void SetUp() OVERRIDE;
678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  virtual void TearDown() OVERRIDE;
688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  TestingProfile* profile() { return profile_.get(); }
701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  FakeSigninManagerForTesting* signin_manager() { return signin_manager_; }
711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  FakeProfileOAuth2TokenService* token_service() { return token_service_; }
728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void SetFakeResponse(const std::string& url,
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       const std::string& data,
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       net::HttpStatusCode code,
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       net::URLRequestStatus::Status status) {
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    url_fetcher_factory_.SetFakeResponse(GURL(url), data, code, status);
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MockAccountReconcilor* GetMockReconcilor();
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void SimulateMergeSessionCompleted(
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      MergeSessionHelper::Observer* observer,
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::string& account_id,
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const GoogleServiceAuthError& error);
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void SimulateRefreshTokenFetched(
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilor* reconcilor,
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::string& account_id,
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::string& refresh_token);
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
92a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) private:
931e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  content::TestBrowserThreadBundle bundle_;
948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  scoped_ptr<TestingProfile> profile_;
951e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  FakeSigninManagerForTesting* signin_manager_;
961e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  FakeProfileOAuth2TokenService* token_service_;
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MockAccountReconcilor* mock_reconcilor_;
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  net::FakeURLFetcherFactory url_fetcher_factory_;
998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)};
1008bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1011e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)AccountReconcilorTest::AccountReconcilorTest()
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    : signin_manager_(NULL),
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      token_service_(NULL),
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      mock_reconcilor_(NULL),
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      url_fetcher_factory_(NULL) {}
1061e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void AccountReconcilorTest::SetUp() {
1088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  TestingProfile::Builder builder;
1098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
110effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                            BuildFakeProfileOAuth2TokenService);
1118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  builder.AddTestingFactory(SigninManagerFactory::GetInstance(),
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                            FakeSigninManagerBase::Build);
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  builder.AddTestingFactory(AccountReconcilorFactory::GetInstance(),
1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                            MockAccountReconcilor::Build);
1158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  profile_ = builder.Build();
1161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  signin_manager_ =
1181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      static_cast<FakeSigninManagerForTesting*>(
1191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)          SigninManagerFactory::GetForProfile(profile()));
1201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  token_service_ =
1221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      static_cast<FakeProfileOAuth2TokenService*>(
1231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)          ProfileOAuth2TokenServiceFactory::GetForProfile(profile()));
1248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void AccountReconcilorTest::TearDown() {
1278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Destroy the profile before all threads are torn down.
1288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  profile_.reset();
1298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)MockAccountReconcilor* AccountReconcilorTest::GetMockReconcilor() {
1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!mock_reconcilor_) {
1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    mock_reconcilor_ =
1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        static_cast<MockAccountReconcilor*>(
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            AccountReconcilorFactory::GetForProfile(profile()));
1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return mock_reconcilor_;
1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void AccountReconcilorTest::SimulateMergeSessionCompleted(
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    MergeSessionHelper::Observer* observer,
1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::string& account_id,
1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const GoogleServiceAuthError& error) {
1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  observer->MergeSessionCompleted(account_id, error);
1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void AccountReconcilorTest::SimulateRefreshTokenFetched(
1495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    AccountReconcilor* reconcilor,
1505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::string& account_id,
1515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::string& refresh_token) {
1525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->HandleRefreshTokenFetched(account_id, refresh_token);
1535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)TEST_F(AccountReconcilorTest, Basic) {
1568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  AccountReconcilor* reconcilor =
1578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
1598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ASSERT_EQ(profile(), reconcilor->profile());
1608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#if !defined(OS_CHROMEOS)
1635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1641e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST_F(AccountReconcilorTest, SigninManagerRegistration) {
1651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  AccountReconcilor* reconcilor =
1661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
1675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
1681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ASSERT_FALSE(reconcilor->IsPeriodicReconciliationRunning());
1695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->IsRegisteredWithTokenService());
1701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  signin_manager()->OnExternalSigninCompleted(kTestEmail);
1721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsPeriodicReconciliationRunning());
1735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
1741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  signin_manager()->SignOut();
1761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ASSERT_FALSE(reconcilor->IsPeriodicReconciliationRunning());
1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->IsRegisteredWithTokenService());
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, Reauth) {
1815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
1825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
1845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
1855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsPeriodicReconciliationRunning());
1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Simulate reauth.  The state of the reconcilor should not change.
1905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->OnExternalSigninCompleted(kTestEmail);
1915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsPeriodicReconciliationRunning());
1925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
1931e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif  // !defined(OS_CHROMEOS)
1961e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1971e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST_F(AccountReconcilorTest, ProfileAlreadyConnected) {
1981e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
1991e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2001e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  AccountReconcilor* reconcilor =
2011e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
2025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
2031e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsPeriodicReconciliationRunning());
2045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
2055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, GetAccountsFromCookieSuccess) {
2085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
2095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
2105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
2115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
2125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
2135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
214a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
215a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 0]]]",
2165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
2175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
2195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
2205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
2225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreGaiaAccountsSet());
223a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const std::vector<std::pair<std::string, bool> >& accounts =
2245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      reconcilor->GetGaiaAccountsForTesting();
2255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(1u, accounts.size());
226a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_EQ("user@gmail.com", accounts[0].first);
2275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, GetAccountsFromCookieFailure) {
2305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
2315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
2325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
2335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
2345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
2355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
236a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(), "",
2375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_NOT_FOUND, net::URLRequestStatus::SUCCESS);
2385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
2405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
2415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
2435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(0u, reconcilor->GetGaiaAccountsForTesting().size());
2445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, ValidateAccountsFromTokens) {
2475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
2485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
2495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
2515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
2525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
2535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->ValidateAccountsFromTokenService();
2555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
2565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
2585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
2595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueTokenForAllPendingRequests("access_token",
2605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
2615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
2635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked());
2645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(1u, reconcilor->GetValidChromeAccountsForTesting().size());
2655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(0u, reconcilor->GetInvalidChromeAccountsForTesting().size());
2665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, ValidateAccountsFromTokensFailedUserInfo) {
2695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
2705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
2715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
2735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
2745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
2755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->ValidateAccountsFromTokenService();
2775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
2785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
2805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "", net::HTTP_NOT_FOUND, net::URLRequestStatus::SUCCESS);
2815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueTokenForAllPendingRequests("access_token",
2825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
2835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
2855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked());
2865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(0u, reconcilor->GetValidChromeAccountsForTesting().size());
2875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(1u, reconcilor->GetInvalidChromeAccountsForTesting().size());
2885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, ValidateAccountsFromTokensFailedTokenRequest) {
2915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
2925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
2935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
2955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
2965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
2975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->ValidateAccountsFromTokenService();
2995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
3005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueErrorForAllPendingRequests(
3025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      GoogleServiceAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS));
3035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
3055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked());
3065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(0u, reconcilor->GetValidChromeAccountsForTesting().size());
3075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(1u, reconcilor->GetInvalidChromeAccountsForTesting().size());
3085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileNoop) {
3115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
3125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
3135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
3155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
3165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
3175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
318a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
319a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
3205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
3215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
3225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
3235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
3255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
3265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
3275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
3295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreGaiaAccountsSet());
3305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(1u, reconcilor->GetGaiaAccountsForTesting().size());
3315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
3325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
3345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
3355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
3375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked());
3385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
3395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
341a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// This is test is needed until chrome changes to use gaia obfuscated id.
342a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// The signin manager and token service use the gaia "email" property, which
343a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// preserves dots in usernames and preserves case. gaia::ParseListAccountsData()
344a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// however uses gaia "displayEmail" which does not preserve case, and then
345a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// passes the string through gaia::CanonicalizeEmail() which removes dots.  This
346a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// tests makes sure that an email like "Dot.S@hmail.com", as seen by the
347a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// token service, will be considered the same as "dots@gmail.com" as returned
348a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// by gaia::ParseListAccountsData().
349a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileNoopWithDots) {
350a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("Dot.S@gmail.com");
351a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  token_service()->UpdateCredentials("Dot.S@gmail.com", "refresh_token");
352a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
353a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  AccountReconcilor* reconcilor =
354a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
355a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
356a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
357a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
358a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"dot.s@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
359a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
360a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
361a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
362a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
363a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  reconcilor->StartReconcile();
364a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
365a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
366a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
367a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
368a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreGaiaAccountsSet());
369a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_EQ(1u, reconcilor->GetGaiaAccountsForTesting().size());
370a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_STREQ("dots@gmail.com",
371a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)               reconcilor->GetGaiaAccountsForTesting()[0].first.c_str());
372a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
373a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
374a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("Dot.S@gmail.com", "access_token",
375a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
376a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
377a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
378a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked());
379a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
380a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
381a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
3825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileNoopMultiple) {
3835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("user@gmail.com");
3845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
3855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
3865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
3885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
3895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
3905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
391a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
392a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
393a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)               "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
3945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
3955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
3965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
3975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
3995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
4005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
4015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
4035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreGaiaAccountsSet());
4045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
4055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(2u, reconcilor->GetGaiaAccountsForTesting().size());
4065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("other@gmail.com", "access_token",
4085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
4095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
4115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
4125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
4145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
4155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
4175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked());
4185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
4195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileAddToCookie) {
4225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("user@gmail.com");
4235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
4245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
4255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("other@gmail.com"));
4275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
428a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
429a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
4305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
4315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
4325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
4335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor = GetMockReconcilor();
4355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
4365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("other@gmail.com", "access_token",
4375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
4385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
4395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
4405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
4425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
4435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SimulateMergeSessionCompleted(reconcilor, "other@gmail.com",
4445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                GoogleServiceAuthError::AuthErrorNone());
4455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
4465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileAddToChrome) {
4495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("user@gmail.com");
4505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
4515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(),
4535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              PerformAddToChromeAction("other@gmail.com", 1));
4545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
455a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
456a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
457a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)               "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
4585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
4595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
4605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
4615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor = GetMockReconcilor();
4635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
4645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
4655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
4665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
4685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
4695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SimulateRefreshTokenFetched(reconcilor, "other@gmail.com", "");
4705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
4715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileBadPrimary) {
4745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("user@gmail.com");
4755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
4765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
4775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(), PerformLogoutAllAccountsAction());
4795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("user@gmail.com"));
4805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("other@gmail.com"));
4815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
482a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
483a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
484a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)               "[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
4855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
4865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
4875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
4885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor = GetMockReconcilor();
4905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
4915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("other@gmail.com", "access_token",
4925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
4935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
4945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
4955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
4975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
4985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SimulateMergeSessionCompleted(reconcilor, "other@gmail.com",
4995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                GoogleServiceAuthError::AuthErrorNone());
5005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
5015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SimulateMergeSessionCompleted(reconcilor, "user@gmail.com",
5025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                GoogleServiceAuthError::AuthErrorNone());
5035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
5041e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
505f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
5065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileOnlyOnce) {
5075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
5085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
5095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
5115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
5125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
5135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
514a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
515a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
516a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
517a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
518a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
519a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
520a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
521a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  reconcilor->StartReconcile();
522a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
523a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
524a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
525a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
526a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
527a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
528a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
529a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
530a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
531a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileWithSessionInfoExpiredDefault) {
532a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("user@gmail.com");
533a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
534a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
535a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
536a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("user@gmail.com"));
537a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
538a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
539a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 0],"
540a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)               "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
5415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
5425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
5435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
5445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
545a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  AccountReconcilor* reconcilor =
546a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
547a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
548a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
5495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
5505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
5515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
5525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
5545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
555a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("other@gmail.com", "access_token",
556a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
5575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
559a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SimulateMergeSessionCompleted(reconcilor, "user@gmail.com",
560a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                GoogleServiceAuthError::AuthErrorNone());
5615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
5625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
563