Commit 2290d784 authored by droger's avatar droger Committed by Commit Bot

[signin] Unit test for DiceResponseHandler

BUG=730589

Review-Url: https://codereview.chromium.org/2947853002
Cr-Commit-Position: refs/heads/master@{#481864}
parent 4e46f03a
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/signin/dice_response_handler.h"
#include "base/command_line.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop/message_loop.h"
#include "base/test/test_simple_task_runner.h"
#include "chrome/test/base/testing_profile.h"
#include "components/signin/core/browser/account_tracker_service.h"
#include "components/signin/core/browser/profile_oauth2_token_service.h"
#include "components/signin/core/browser/signin_header_helper.h"
#include "components/signin/core/browser/test_signin_client.h"
#include "components/signin/core/common/profile_management_switches.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "google_apis/gaia/fake_oauth2_token_service_delegate.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using signin::DiceAction;
using signin::DiceResponseParams;
namespace {
const char kAuthorizationCode[] = "authorization_code";
const char kEmail[] = "email";
const char kObfuscatedGaiaID[] = "obfuscated_gaia_id";
const int kSessionIndex = 42;
// TestSigninClient implementation that intercepts the GaiaAuthConsumer and
// replaces it by a dummy one.
class DiceTestSigninClient : public TestSigninClient, public GaiaAuthConsumer {
public:
explicit DiceTestSigninClient(PrefService* pref_service)
: TestSigninClient(pref_service), consumer_(nullptr) {}
~DiceTestSigninClient() override {}
std::unique_ptr<GaiaAuthFetcher> CreateGaiaAuthFetcher(
GaiaAuthConsumer* consumer,
const std::string& source,
net::URLRequestContextGetter* getter) override {
DCHECK(!consumer_ || (consumer_ == consumer));
consumer_ = consumer;
// Pass |this| as a dummy consumer to CreateGaiaAuthFetcher().
// Since DiceTestSigninClient does not overrides any consumer method,
// everything will be dropped on the floor.
return TestSigninClient::CreateGaiaAuthFetcher(this, source, getter);
}
GaiaAuthConsumer* consumer_;
};
class DiceResponseHandlerTest : public testing::Test {
protected:
DiceResponseHandlerTest()
: task_runner_(new base::TestSimpleTaskRunner()),
request_context_getter_(
new net::TestURLRequestContextGetter(task_runner_)),
signin_client_(&pref_service_),
token_service_(base::MakeUnique<FakeOAuth2TokenServiceDelegate>(
request_context_getter_.get())),
dice_response_handler_(&signin_client_,
&token_service_,
&account_tracker_service_) {
switches::EnableAccountConsistencyDiceForTesting(
base::CommandLine::ForCurrentProcess());
signin_client_.SetURLRequestContext(request_context_getter_.get());
AccountTrackerService::RegisterPrefs(pref_service_.registry());
account_tracker_service_.Initialize(&signin_client_);
}
~DiceResponseHandlerTest() override { task_runner_->ClearPendingTasks(); }
DiceResponseParams MakeDiceParams(DiceAction action) {
DiceResponseParams dice_params;
dice_params.user_intention = action;
dice_params.obfuscated_gaia_id = kObfuscatedGaiaID;
dice_params.email = kEmail;
dice_params.session_index = kSessionIndex;
dice_params.authorization_code = kAuthorizationCode;
return dice_params;
}
base::MessageLoop loop_;
scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
sync_preferences::TestingPrefServiceSyncable pref_service_;
DiceTestSigninClient signin_client_;
ProfileOAuth2TokenService token_service_;
AccountTrackerService account_tracker_service_;
DiceResponseHandler dice_response_handler_;
};
// Checks that a SIGNIN action triggers a token exchange request.
TEST_F(DiceResponseHandlerTest, Signin) {
DiceResponseParams dice_params = MakeDiceParams(DiceAction::SIGNIN);
ASSERT_FALSE(
token_service_.RefreshTokenIsAvailable(dice_params.obfuscated_gaia_id));
dice_response_handler_.ProcessDiceHeader(dice_params);
// Check that a GaiaAuthFetcher has been created.
ASSERT_TRUE(signin_client_.consumer_);
// Simulate GaiaAuthFetcher success.
signin_client_.consumer_->OnClientOAuthSuccess(
GaiaAuthConsumer::ClientOAuthResult("refresh_token", "access_token", 10));
// Check that the token has been inserted in the token service.
EXPECT_TRUE(
token_service_.RefreshTokenIsAvailable(dice_params.obfuscated_gaia_id));
}
// Tests that the DiceResponseHandler is created for a normal profile but not
// for an incognito profile.
TEST(DiceResponseHandlerFactoryTest, NotInIncognito) {
content::TestBrowserThreadBundle thread_bundle;
TestingProfile profile;
EXPECT_THAT(DiceResponseHandler::GetForProfile(&profile), testing::NotNull());
EXPECT_THAT(
DiceResponseHandler::GetForProfile(profile.GetOffTheRecordProfile()),
testing::IsNull());
}
} // namespace
...@@ -11,6 +11,7 @@ import("//chrome/common/features.gni") ...@@ -11,6 +11,7 @@ import("//chrome/common/features.gni")
import("//chrome/chrome_repack_locales.gni") import("//chrome/chrome_repack_locales.gni")
import("//components/offline_pages/features/features.gni") import("//components/offline_pages/features/features.gni")
import("//components/os_crypt/features.gni") import("//components/os_crypt/features.gni")
import("//components/signin/features.gni")
import("//components/spellcheck/spellcheck_build_features.gni") import("//components/spellcheck/spellcheck_build_features.gni")
import("//extensions/features/features.gni") import("//extensions/features/features.gni")
import("//media/media_options.gni") import("//media/media_options.gni")
...@@ -3491,6 +3492,10 @@ test("unit_tests") { ...@@ -3491,6 +3492,10 @@ test("unit_tests") {
data_deps += [ "//chrome:chrome_framework" ] data_deps += [ "//chrome:chrome_framework" ]
} }
if (enable_dice_support) {
sources += [ "../browser/signin/dice_response_handler_unittest.cc" ]
}
if (enable_offline_pages) { if (enable_offline_pages) {
sources += [ sources += [
"../browser/android/offline_pages/offline_page_mhtml_archiver_unittest.cc", "../browser/android/offline_pages/offline_page_mhtml_archiver_unittest.cc",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment