account_reconcilor_unittest.cc revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
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)
5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/command_line.h"
68bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/run_loop.h"
8cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/time/time.h"
10f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "build/build_config.h"
11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/prefs/pref_service_syncable.h"
128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/account_reconcilor_factory.h"
13e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/browser/signin/chrome_signin_client_factory.h"
148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
15effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/fake_signin_manager.h"
178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/signin/signin_manager_factory.h"
19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/signin/test_signin_client_builder.h"
20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/test/base/testing_browser_process.h"
218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/test/base/testing_profile.h"
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/test/base/testing_profile_manager.h"
23c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "components/signin/core/browser/account_reconcilor.h"
24effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "components/signin/core/browser/profile_oauth2_token_service.h"
25e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "components/signin/core/browser/signin_manager.h"
26f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "components/signin/core/common/profile_management_switches.h"
27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/signin/core/common/signin_switches.h"
281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "content/public/test/test_browser_thread_bundle.h"
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "google_apis/gaia/gaia_urls.h"
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "net/url_request/test_url_fetcher_factory.h"
318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h"
328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)namespace {
358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)const char kTestEmail[] = "user@gmail.com";
371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class MockAccountReconcilor : public testing::StrictMock<AccountReconcilor> {
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
40e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  static KeyedService* Build(content::BrowserContext* context);
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
42c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  MockAccountReconcilor(ProfileOAuth2TokenService* token_service,
43c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                        SigninManagerBase* signin_manager,
44c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                        SigninClient* client);
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ~MockAccountReconcilor() {}
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MOCK_METHOD1(PerformMergeAction, void(const std::string& account_id));
48cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  MOCK_METHOD1(PerformStartRemoveAction, void(const std::string& account_id));
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  MOCK_METHOD3(
50cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      PerformFinishRemoveAction,
51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      void(const std::string& account_id,
52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)           const GoogleServiceAuthError& error,
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)           const std::vector<std::pair<std::string, bool> >& accounts));
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MOCK_METHOD2(PerformAddToChromeAction, void(const std::string& account_id,
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                              int session_index));
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MOCK_METHOD0(PerformLogoutAllAccountsAction, void());
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// static
60e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen MurdochKeyedService* MockAccountReconcilor::Build(content::BrowserContext* context) {
61e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  Profile* profile = Profile::FromBrowserContext(context);
62e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  AccountReconcilor* reconcilor = new MockAccountReconcilor(
63c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
64c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      SigninManagerFactory::GetForProfile(profile),
65c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      ChromeSigninClientFactory::GetForProfile(profile));
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  reconcilor->Initialize(false /* start_reconcile_if_tokens_available */);
67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return reconcilor;
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
70c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen MurdochMockAccountReconcilor::MockAccountReconcilor(
71c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    ProfileOAuth2TokenService* token_service,
72c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    SigninManagerBase* signin_manager,
73c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    SigninClient* client)
74c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    : testing::StrictMock<AccountReconcilor>(token_service,
75c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                             signin_manager,
76c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                             client) {}
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)class AccountReconcilorTest : public testing::Test {
818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) public:
821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  AccountReconcilorTest();
838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  virtual void SetUp() OVERRIDE;
848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
85cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  TestingProfile* profile() { return profile_; }
861e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  FakeSigninManagerForTesting* signin_manager() { return signin_manager_; }
871e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  FakeProfileOAuth2TokenService* token_service() { return token_service_; }
888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void SetFakeResponse(const std::string& url,
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       const std::string& data,
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       net::HttpStatusCode code,
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       net::URLRequestStatus::Status status) {
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    url_fetcher_factory_.SetFakeResponse(GURL(url), data, code, status);
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MockAccountReconcilor* GetMockReconcilor();
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void SimulateMergeSessionCompleted(
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      MergeSessionHelper::Observer* observer,
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::string& account_id,
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const GoogleServiceAuthError& error);
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void SimulateRefreshTokenFetched(
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilor* reconcilor,
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::string& account_id,
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::string& refresh_token);
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
108a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) private:
1091e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  content::TestBrowserThreadBundle bundle_;
110cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  TestingProfile* profile_;
1111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  FakeSigninManagerForTesting* signin_manager_;
1121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  FakeProfileOAuth2TokenService* token_service_;
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MockAccountReconcilor* mock_reconcilor_;
1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  net::FakeURLFetcherFactory url_fetcher_factory_;
115cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  scoped_ptr<TestingProfileManager> testing_profile_manager_;
1168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)};
1178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)AccountReconcilorTest::AccountReconcilorTest()
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    : signin_manager_(NULL),
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      token_service_(NULL),
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      mock_reconcilor_(NULL),
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      url_fetcher_factory_(NULL) {}
1231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void AccountReconcilorTest::SetUp() {
125cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  CommandLine::ForCurrentProcess()->AppendSwitch(
126f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      switches::kEnableNewProfileManagement);
127cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
128cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  testing_profile_manager_.reset(
129cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
130cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ASSERT_TRUE(testing_profile_manager_.get()->SetUp());
131cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
132cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  TestingProfile::TestingFactories factories;
133cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  factories.push_back(std::make_pair(ChromeSigninClientFactory::GetInstance(),
134cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      signin::BuildTestSigninClient));
135cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  factories.push_back(std::make_pair(
136cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      ProfileOAuth2TokenServiceFactory::GetInstance(),
137cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      BuildFakeProfileOAuth2TokenService));
138cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  factories.push_back(std::make_pair(SigninManagerFactory::GetInstance(),
139cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      FakeSigninManagerBase::Build));
140cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  factories.push_back(std::make_pair(AccountReconcilorFactory::GetInstance(),
141cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      MockAccountReconcilor::Build));
142cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
143cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  profile_ = testing_profile_manager_.get()->CreateTestingProfile("name",
144cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                              scoped_ptr<PrefServiceSyncable>(),
145cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                              base::UTF8ToUTF16("name"), 0, std::string(),
146cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                              factories);
1471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  signin_manager_ =
1491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      static_cast<FakeSigninManagerForTesting*>(
1501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)          SigninManagerFactory::GetForProfile(profile()));
1511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  token_service_ =
1531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      static_cast<FakeProfileOAuth2TokenService*>(
1541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)          ProfileOAuth2TokenServiceFactory::GetForProfile(profile()));
1558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)MockAccountReconcilor* AccountReconcilorTest::GetMockReconcilor() {
1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!mock_reconcilor_) {
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    mock_reconcilor_ =
1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        static_cast<MockAccountReconcilor*>(
1615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            AccountReconcilorFactory::GetForProfile(profile()));
1625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return mock_reconcilor_;
1655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void AccountReconcilorTest::SimulateMergeSessionCompleted(
1685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    MergeSessionHelper::Observer* observer,
1695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::string& account_id,
1705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const GoogleServiceAuthError& error) {
1715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  observer->MergeSessionCompleted(account_id, error);
1725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void AccountReconcilorTest::SimulateRefreshTokenFetched(
1755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    AccountReconcilor* reconcilor,
1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::string& account_id,
1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::string& refresh_token) {
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->HandleRefreshTokenFetched(account_id, refresh_token);
1795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)TEST_F(AccountReconcilorTest, Basic) {
1828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  AccountReconcilor* reconcilor =
1838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
1845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
185c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  ASSERT_EQ(token_service(), reconcilor->token_service());
1868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1871e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1881e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#if !defined(OS_CHROMEOS)
1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
190cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// This method requires the use of the |TestSigninClient| to be created from the
191cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// |ChromeSigninClientFactory| because it overrides the |GoogleSigninSucceeded|
192cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// method with an empty implementation. On MacOS, the normal implementation
193cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// causes the try_bots to time out.
1941e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST_F(AccountReconcilorTest, SigninManagerRegistration) {
1951e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  AccountReconcilor* reconcilor =
1961e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
1975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
1985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->IsRegisteredWithTokenService());
1991e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
200cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  signin_manager()->set_password("password");
2011e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  signin_manager()->OnExternalSigninCompleted(kTestEmail);
2025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
2031e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
204cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(), PerformLogoutAllAccountsAction());
205cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
2061e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  signin_manager()->SignOut();
2075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->IsRegisteredWithTokenService());
2085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
210cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// This method requires the use of the |TestSigninClient| to be created from the
211cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// |ChromeSigninClientFactory| because it overrides the |GoogleSigninSucceeded|
212cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// method with an empty implementation. On MacOS, the normal implementation
213cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// causes the try_bots to time out.
2145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, Reauth) {
2155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
216cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  signin_manager()->set_password("password");
2175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
2195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
2205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
2215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
2225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Simulate reauth.  The state of the reconcilor should not change.
2245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->OnExternalSigninCompleted(kTestEmail);
2255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
2261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif  // !defined(OS_CHROMEOS)
2291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST_F(AccountReconcilorTest, ProfileAlreadyConnected) {
2311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
2321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  AccountReconcilor* reconcilor =
2341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
2355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
2365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
2375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, GetAccountsFromCookieSuccess) {
2405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
2415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
2425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
2435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
2445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
2455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
246a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
247a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 0]]]",
2485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
2495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
2515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
2525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
2545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreGaiaAccountsSet());
255a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const std::vector<std::pair<std::string, bool> >& accounts =
2565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      reconcilor->GetGaiaAccountsForTesting();
2575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(1u, accounts.size());
258a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_EQ("user@gmail.com", accounts[0].first);
2595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, GetAccountsFromCookieFailure) {
2625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
2635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
2645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
2655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
2665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
2675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
268a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(), "",
2695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_NOT_FOUND, net::URLRequestStatus::SUCCESS);
2705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
2725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
2735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
2755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(0u, reconcilor->GetGaiaAccountsForTesting().size());
2765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, ValidateAccountsFromTokens) {
2795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
2805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
2815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
2835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
2845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
2855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->ValidateAccountsFromTokenService();
2875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
2885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2895c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  SetFakeResponse(GaiaUrls::GetInstance()->people_get_url().spec(),
2905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
2915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueTokenForAllPendingRequests("access_token",
2925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
2935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
2955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked());
2965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(1u, reconcilor->GetValidChromeAccountsForTesting().size());
2975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(0u, reconcilor->GetInvalidChromeAccountsForTesting().size());
2985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, ValidateAccountsFromTokensFailedUserInfo) {
3015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
3025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
3035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
3055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
3065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
3075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->ValidateAccountsFromTokenService();
3095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
3105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3115c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  SetFakeResponse(GaiaUrls::GetInstance()->people_get_url().spec(),
3125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "", net::HTTP_NOT_FOUND, net::URLRequestStatus::SUCCESS);
3135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueTokenForAllPendingRequests("access_token",
3145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
3155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
3175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked());
3185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(0u, reconcilor->GetValidChromeAccountsForTesting().size());
3195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(1u, reconcilor->GetInvalidChromeAccountsForTesting().size());
3205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, ValidateAccountsFromTokensFailedTokenRequest) {
3235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
3245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
3255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
3275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
3285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
3295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->ValidateAccountsFromTokenService();
3315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
3325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueErrorForAllPendingRequests(
3345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      GoogleServiceAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS));
3355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
3375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked());
3385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(0u, reconcilor->GetValidChromeAccountsForTesting().size());
3395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(1u, reconcilor->GetInvalidChromeAccountsForTesting().size());
3405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileNoop) {
3435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
3445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
3455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
3475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
3485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
3495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
350a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
351a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
3525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
3535c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  SetFakeResponse(GaiaUrls::GetInstance()->people_get_url().spec(),
3545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
3555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
3575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
3585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
3595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
3615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreGaiaAccountsSet());
3625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(1u, reconcilor->GetGaiaAccountsForTesting().size());
3635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
3645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
3665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
3675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
3695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked());
3705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
3715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
373a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// This is test is needed until chrome changes to use gaia obfuscated id.
374a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// The signin manager and token service use the gaia "email" property, which
375a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// preserves dots in usernames and preserves case. gaia::ParseListAccountsData()
376a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// however uses gaia "displayEmail" which does not preserve case, and then
377a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// passes the string through gaia::CanonicalizeEmail() which removes dots.  This
378a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// tests makes sure that an email like "Dot.S@hmail.com", as seen by the
379a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// token service, will be considered the same as "dots@gmail.com" as returned
380a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// by gaia::ParseListAccountsData().
381a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileNoopWithDots) {
382a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("Dot.S@gmail.com");
383a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  token_service()->UpdateCredentials("Dot.S@gmail.com", "refresh_token");
384a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
385a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  AccountReconcilor* reconcilor =
386a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
387a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
388a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
389a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
390a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"dot.s@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
391a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
3925c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  SetFakeResponse(GaiaUrls::GetInstance()->people_get_url().spec(),
393a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
394a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
395a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  reconcilor->StartReconcile();
396a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
397a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
398a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
399a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
400a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreGaiaAccountsSet());
401a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_EQ(1u, reconcilor->GetGaiaAccountsForTesting().size());
402a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_STREQ("dots@gmail.com",
403a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)               reconcilor->GetGaiaAccountsForTesting()[0].first.c_str());
404a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
405a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
406a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("Dot.S@gmail.com", "access_token",
407a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
408a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
409a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
410a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked());
411a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
412a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
413a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
4145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileNoopMultiple) {
4155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("user@gmail.com");
4165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
4175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
4185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
4205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
4215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
4225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
423a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
424a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
425a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)               "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
4265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
4275c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  SetFakeResponse(GaiaUrls::GetInstance()->people_get_url().spec(),
4285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
4295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
4315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
4325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
4335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
4355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreGaiaAccountsSet());
4365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
4375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(2u, reconcilor->GetGaiaAccountsForTesting().size());
4385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("other@gmail.com", "access_token",
4405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
4415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
4435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
4445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
4465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
4475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
4495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked());
4505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
4515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileAddToCookie) {
4545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("user@gmail.com");
4555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
4565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
4575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("other@gmail.com"));
4595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
460a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
461a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
4625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
4635c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  SetFakeResponse(GaiaUrls::GetInstance()->people_get_url().spec(),
4645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
4655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor = GetMockReconcilor();
4675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
4685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("other@gmail.com", "access_token",
4695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
4705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
4715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
4725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
4745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
4755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SimulateMergeSessionCompleted(reconcilor, "other@gmail.com",
4765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                GoogleServiceAuthError::AuthErrorNone());
4775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
4785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileAddToChrome) {
4815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("user@gmail.com");
4825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
4835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(),
4855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              PerformAddToChromeAction("other@gmail.com", 1));
4865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
487a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
488a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
489a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)               "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
4905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
4915c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  SetFakeResponse(GaiaUrls::GetInstance()->people_get_url().spec(),
4925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
4935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor = GetMockReconcilor();
4955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
4965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
4975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
4985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
5005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
5015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SimulateRefreshTokenFetched(reconcilor, "other@gmail.com", "");
5025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
5035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
5045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileBadPrimary) {
5065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("user@gmail.com");
5075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
5085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
5095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(), PerformLogoutAllAccountsAction());
5115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("user@gmail.com"));
5125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("other@gmail.com"));
5135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
514a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
515a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
516a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)               "[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
5175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
5185c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  SetFakeResponse(GaiaUrls::GetInstance()->people_get_url().spec(),
5195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
5205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor = GetMockReconcilor();
5225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
5235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("other@gmail.com", "access_token",
5245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
5255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
5265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
5275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
5295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
5305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SimulateMergeSessionCompleted(reconcilor, "other@gmail.com",
5315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                GoogleServiceAuthError::AuthErrorNone());
5325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
5335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SimulateMergeSessionCompleted(reconcilor, "user@gmail.com",
5345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                GoogleServiceAuthError::AuthErrorNone());
5355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
5361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
537f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
5385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileOnlyOnce) {
5395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername(kTestEmail);
5405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->UpdateCredentials(kTestEmail, "refresh_token");
5415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AccountReconcilor* reconcilor =
5435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
5445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
5455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
546a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
547a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
548a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
5495c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  SetFakeResponse(GaiaUrls::GetInstance()->people_get_url().spec(),
550a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
551a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
552a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
553a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  reconcilor->StartReconcile();
554a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
555a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
556a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
557a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
558a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
559a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
560a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
561a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
562a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
563a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TEST_F(AccountReconcilorTest, StartReconcileWithSessionInfoExpiredDefault) {
564a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  signin_manager()->SetAuthenticatedUsername("user@gmail.com");
565a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
566a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
567a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
568a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("user@gmail.com"));
569a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
570a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
571a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 0],"
572a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)               "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
5735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::HTTP_OK, net::URLRequestStatus::SUCCESS);
5745c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  SetFakeResponse(GaiaUrls::GetInstance()->people_get_url().spec(),
5755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
5765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
577a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  AccountReconcilor* reconcilor =
578a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      AccountReconcilorFactory::GetForProfile(profile());
579a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_TRUE(reconcilor);
580a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
5815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
5825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reconcilor->StartReconcile();
5835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reconcilor->is_reconcile_started_);
5845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
5865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
587a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  token_service()->IssueAllTokensForAccount("other@gmail.com", "access_token",
588a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::Time::Now() + base::TimeDelta::FromHours(1));
5895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
591a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SimulateMergeSessionCompleted(reconcilor, "user@gmail.com",
592a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                GoogleServiceAuthError::AuthErrorNone());
5935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(reconcilor->is_reconcile_started_);
5945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
595