Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(347)

Side by Side Diff: chrome/browser/signin/dice_response_handler_unittest.cc

Issue 2947853002: [signin] Unit test for DiceResponseHandler (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/test/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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";
msarda 2017/06/22 09:29:48 I think we can drop the obfuscated from the Gaia I
droger 2017/06/22 13:21:20 SGTM. I'm doing this in another CL: https://coder
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 std::unique_ptr<GaiaAuthFetcher> CreateGaiaAuthFetcher(
msarda 2017/06/22 09:29:48 Should this be override?
droger 2017/06/22 13:21:20 Yes, it is override. Did you miss it on line 44?
42 GaiaAuthConsumer* consumer,
43 const std::string& source,
44 net::URLRequestContextGetter* getter) override {
45 DCHECK(!consumer_ || (consumer_ == consumer));
46 consumer_ = consumer;
47
48 // Pass |this| as a dummy consumer. Drop everything on the floor.
msarda 2017/06/22 09:29:48 I do not understand this comment - this method is
droger 2017/06/22 13:21:20 I am passing |this| to CreateGaiaAuthFetcher inste
49 return TestSigninClient::CreateGaiaAuthFetcher(this, source, getter);
50 }
51
52 GaiaAuthConsumer* consumer_;
53 };
54
55 class DiceResponseHandlerTest : public testing::Test {
56 protected:
57 DiceResponseHandlerTest()
msarda 2017/06/22 09:29:47 I think in general we prefer to do the initializat
droger 2017/06/22 13:21:19 This is the other way around. Initialization in Se
58 : task_runner_(new base::TestSimpleTaskRunner()),
59 request_context_getter_(
60 new net::TestURLRequestContextGetter(task_runner_)),
61 signin_client_(&pref_service_),
62 token_service_(base::MakeUnique<FakeOAuth2TokenServiceDelegate>(
63 request_context_getter_.get())),
64 dice_response_handler_(&signin_client_,
65 &token_service_,
66 &account_tracker_service_) {
67 switches::EnableAccountConsistencyDiceForTesting(
68 base::CommandLine::ForCurrentProcess());
69 signin_client_.SetURLRequestContext(request_context_getter_.get());
70 AccountTrackerService::RegisterPrefs(pref_service_.registry());
71 account_tracker_service_.Initialize(&signin_client_);
72 }
73
74 ~DiceResponseHandlerTest() override {}
75
76 DiceResponseParams MakeDiceParams(DiceAction action) {
77 DiceResponseParams dice_params;
78 dice_params.user_intention = action;
79 dice_params.obfuscated_gaia_id = kObfuscatedGaiaID;
msarda 2017/06/22 09:29:47 Same remark as above - the Gaia ID in Chrome is al
80 dice_params.email = kEmail;
81 dice_params.session_index = kSessionIndex;
82 dice_params.authorization_code = kAuthorizationCode;
83 return dice_params;
84 }
85
86 base::MessageLoop loop_;
msarda 2017/06/22 09:29:48 Is the message loop needed?
droger 2017/06/22 13:21:20 Yes, GaiaAuthFetcher requires it. Removing it giv
87 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
88 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
89 sync_preferences::TestingPrefServiceSyncable pref_service_;
90 DiceTestSigninClient signin_client_;
91 ProfileOAuth2TokenService token_service_;
92 AccountTrackerService account_tracker_service_;
93 DiceResponseHandler dice_response_handler_;
94 };
95
96 // Checks that a SIGNIN action triggers a token exchange request.
97 TEST_F(DiceResponseHandlerTest, Signin) {
98 DiceResponseParams dice_params = MakeDiceParams(DiceAction::SIGNIN);
99 ASSERT_FALSE(
100 token_service_.RefreshTokenIsAvailable(dice_params.obfuscated_gaia_id));
101 dice_response_handler_.ProcessDiceHeader(dice_params);
102 // Check that a GaiaAuthFetcher has been created.
103 ASSERT_TRUE(signin_client_.consumer_);
104 // Simulate GaiaAuthFetcher success.
105 signin_client_.consumer_->OnClientOAuthSuccess(
msarda 2017/06/22 09:29:47 Please also add a test for OnClientOAuthFailure.
droger 2017/06/22 13:21:20 Doing this in another CL because then I can use th
106 GaiaAuthConsumer::ClientOAuthResult("refresh_token", "access_token", 10));
107 // Check that the token has been inserted in the token service.
108 EXPECT_TRUE(
109 token_service_.RefreshTokenIsAvailable(dice_params.obfuscated_gaia_id));
110 }
111
112 // Tests that the DiceResponseHandler is created for a normal profile but not
113 // for an incognito profile.
114 TEST(DiceResponseHandlerFactoryTest, NotInIncognito) {
115 content::TestBrowserThreadBundle thread_bundle;
116 TestingProfile profile;
117 EXPECT_THAT(DiceResponseHandler::GetForProfile(&profile), testing::NotNull());
118 EXPECT_THAT(
119 DiceResponseHandler::GetForProfile(profile.GetOffTheRecordProfile()),
120 testing::IsNull());
121 }
122
123 } // namespace
OLDNEW
« no previous file with comments | « no previous file | chrome/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698