| OLD | NEW | 
|---|
| (Empty) |  | 
|  | 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 
|  | 2 // Use of this source code is governed by a BSD-style license that can be | 
|  | 3 // found in the LICENSE file. | 
|  | 4 | 
|  | 5 #include "chrome/browser/signin/dice_response_handler.h" | 
|  | 6 | 
|  | 7 #include "base/command_line.h" | 
|  | 8 #include "base/memory/ref_counted.h" | 
|  | 9 #include "base/message_loop/message_loop.h" | 
|  | 10 #include "base/test/test_simple_task_runner.h" | 
|  | 11 #include "chrome/test/base/testing_profile.h" | 
|  | 12 #include "components/signin/core/browser/account_tracker_service.h" | 
|  | 13 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 
|  | 14 #include "components/signin/core/browser/signin_header_helper.h" | 
|  | 15 #include "components/signin/core/browser/test_signin_client.h" | 
|  | 16 #include "components/signin/core/common/profile_management_switches.h" | 
|  | 17 #include "components/sync_preferences/testing_pref_service_syncable.h" | 
|  | 18 #include "content/public/test/test_browser_thread_bundle.h" | 
|  | 19 #include "google_apis/gaia/fake_oauth2_token_service_delegate.h" | 
|  | 20 #include "net/url_request/url_request_test_util.h" | 
|  | 21 #include "testing/gmock/include/gmock/gmock.h" | 
|  | 22 #include "testing/gtest/include/gtest/gtest.h" | 
|  | 23 | 
|  | 24 using signin::DiceAction; | 
|  | 25 using signin::DiceResponseParams; | 
|  | 26 | 
|  | 27 namespace { | 
|  | 28 | 
|  | 29 const char kAuthorizationCode[] = "authorization_code"; | 
|  | 30 const char kEmail[] = "email"; | 
|  | 31 const char kObfuscatedGaiaID[] = "obfuscated_gaia_id"; | 
|  | 32 const int kSessionIndex = 42; | 
|  | 33 | 
|  | 34 // TestSigninClient implementation that intercepts the GaiaAuthConsumer and | 
|  | 35 // replaces it by a dummy one. | 
|  | 36 class DiceTestSigninClient : public TestSigninClient, public GaiaAuthConsumer { | 
|  | 37  public: | 
|  | 38   explicit DiceTestSigninClient(PrefService* pref_service) | 
|  | 39       : TestSigninClient(pref_service), consumer_(nullptr) {} | 
|  | 40 | 
|  | 41   ~DiceTestSigninClient() override {} | 
|  | 42 | 
|  | 43   std::unique_ptr<GaiaAuthFetcher> CreateGaiaAuthFetcher( | 
|  | 44       GaiaAuthConsumer* consumer, | 
|  | 45       const std::string& source, | 
|  | 46       net::URLRequestContextGetter* getter) override { | 
|  | 47     DCHECK(!consumer_ || (consumer_ == consumer)); | 
|  | 48     consumer_ = consumer; | 
|  | 49 | 
|  | 50     // Pass |this| as a dummy consumer to CreateGaiaAuthFetcher(). | 
|  | 51     // Since DiceTestSigninClient does not overrides any consumer method, | 
|  | 52     // everything will be dropped on the floor. | 
|  | 53     return TestSigninClient::CreateGaiaAuthFetcher(this, source, getter); | 
|  | 54   } | 
|  | 55 | 
|  | 56   GaiaAuthConsumer* consumer_; | 
|  | 57 }; | 
|  | 58 | 
|  | 59 class DiceResponseHandlerTest : public testing::Test { | 
|  | 60  protected: | 
|  | 61   DiceResponseHandlerTest() | 
|  | 62       : task_runner_(new base::TestSimpleTaskRunner()), | 
|  | 63         request_context_getter_( | 
|  | 64             new net::TestURLRequestContextGetter(task_runner_)), | 
|  | 65         signin_client_(&pref_service_), | 
|  | 66         token_service_(base::MakeUnique<FakeOAuth2TokenServiceDelegate>( | 
|  | 67             request_context_getter_.get())), | 
|  | 68         dice_response_handler_(&signin_client_, | 
|  | 69                                &token_service_, | 
|  | 70                                &account_tracker_service_) { | 
|  | 71     switches::EnableAccountConsistencyDiceForTesting( | 
|  | 72         base::CommandLine::ForCurrentProcess()); | 
|  | 73     signin_client_.SetURLRequestContext(request_context_getter_.get()); | 
|  | 74     AccountTrackerService::RegisterPrefs(pref_service_.registry()); | 
|  | 75     account_tracker_service_.Initialize(&signin_client_); | 
|  | 76   } | 
|  | 77 | 
|  | 78   ~DiceResponseHandlerTest() override { task_runner_->ClearPendingTasks(); } | 
|  | 79 | 
|  | 80   DiceResponseParams MakeDiceParams(DiceAction action) { | 
|  | 81     DiceResponseParams dice_params; | 
|  | 82     dice_params.user_intention = action; | 
|  | 83     dice_params.obfuscated_gaia_id = kObfuscatedGaiaID; | 
|  | 84     dice_params.email = kEmail; | 
|  | 85     dice_params.session_index = kSessionIndex; | 
|  | 86     dice_params.authorization_code = kAuthorizationCode; | 
|  | 87     return dice_params; | 
|  | 88   } | 
|  | 89 | 
|  | 90   base::MessageLoop loop_; | 
|  | 91   scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 
|  | 92   scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; | 
|  | 93   sync_preferences::TestingPrefServiceSyncable pref_service_; | 
|  | 94   DiceTestSigninClient signin_client_; | 
|  | 95   ProfileOAuth2TokenService token_service_; | 
|  | 96   AccountTrackerService account_tracker_service_; | 
|  | 97   DiceResponseHandler dice_response_handler_; | 
|  | 98 }; | 
|  | 99 | 
|  | 100 // Checks that a SIGNIN action triggers a token exchange request. | 
|  | 101 TEST_F(DiceResponseHandlerTest, Signin) { | 
|  | 102   DiceResponseParams dice_params = MakeDiceParams(DiceAction::SIGNIN); | 
|  | 103   ASSERT_FALSE( | 
|  | 104       token_service_.RefreshTokenIsAvailable(dice_params.obfuscated_gaia_id)); | 
|  | 105   dice_response_handler_.ProcessDiceHeader(dice_params); | 
|  | 106   // Check that a GaiaAuthFetcher has been created. | 
|  | 107   ASSERT_TRUE(signin_client_.consumer_); | 
|  | 108   // Simulate GaiaAuthFetcher success. | 
|  | 109   signin_client_.consumer_->OnClientOAuthSuccess( | 
|  | 110       GaiaAuthConsumer::ClientOAuthResult("refresh_token", "access_token", 10)); | 
|  | 111   // Check that the token has been inserted in the token service. | 
|  | 112   EXPECT_TRUE( | 
|  | 113       token_service_.RefreshTokenIsAvailable(dice_params.obfuscated_gaia_id)); | 
|  | 114 } | 
|  | 115 | 
|  | 116 // Tests that the DiceResponseHandler is created for a normal profile but not | 
|  | 117 // for an incognito profile. | 
|  | 118 TEST(DiceResponseHandlerFactoryTest, NotInIncognito) { | 
|  | 119   content::TestBrowserThreadBundle thread_bundle; | 
|  | 120   TestingProfile profile; | 
|  | 121   EXPECT_THAT(DiceResponseHandler::GetForProfile(&profile), testing::NotNull()); | 
|  | 122   EXPECT_THAT( | 
|  | 123       DiceResponseHandler::GetForProfile(profile.GetOffTheRecordProfile()), | 
|  | 124       testing::IsNull()); | 
|  | 125 } | 
|  | 126 | 
|  | 127 }  // namespace | 
| OLD | NEW | 
|---|