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

Side by Side Diff: components/signin/core/browser/signin_header_helper_unittest.cc

Issue 2923733003: [signin] Add DICe flow for account consistency requests. (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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/signin/core/browser/signin_header_helper.h"
6
5 #include <memory> 7 #include <memory>
8 #include <string>
6 9
7 #include "base/command_line.h" 10 #include "base/command_line.h"
8 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "build/build_config.h"
9 #include "components/content_settings/core/browser/cookie_settings.h" 13 #include "components/content_settings/core/browser/cookie_settings.h"
10 #include "components/signin/core/browser/signin_header_helper.h"
11 #include "components/signin/core/common/profile_management_switches.h" 14 #include "components/signin/core/common/profile_management_switches.h"
12 #include "components/sync_preferences/testing_pref_service_syncable.h" 15 #include "components/sync_preferences/testing_pref_service_syncable.h"
16 #include "google_apis/gaia/gaia_urls.h"
13 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" 17 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
14 #include "net/url_request/url_request_test_util.h" 18 #include "net/url_request/url_request_test_util.h"
15 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
16 #include "url/gurl.h" 20 #include "url/gurl.h"
17 21
18 class SigninHeaderHelperTest : public testing::Test { 22 class SigninHeaderHelperTest : public testing::Test {
19 protected: 23 protected:
20 void SetUp() override { 24 void SetUp() override {
21 content_settings::CookieSettings::RegisterProfilePrefs(prefs_.registry()); 25 content_settings::CookieSettings::RegisterProfilePrefs(prefs_.registry());
22 HostContentSettingsMap::RegisterProfilePrefs(prefs_.registry()); 26 HostContentSettingsMap::RegisterProfilePrefs(prefs_.registry());
23 27
24 settings_map_ = new HostContentSettingsMap( 28 settings_map_ = new HostContentSettingsMap(
25 &prefs_, false /* incognito_profile */, false /* guest_profile */, 29 &prefs_, false /* incognito_profile */, false /* guest_profile */,
26 false /* store_last_modified */); 30 false /* store_last_modified */);
27 cookie_settings_ = 31 cookie_settings_ =
28 new content_settings::CookieSettings(settings_map_.get(), &prefs_, ""); 32 new content_settings::CookieSettings(settings_map_.get(), &prefs_, "");
29 } 33 }
30 34
31 void TearDown() override { settings_map_->ShutdownOnUIThread(); } 35 void TearDown() override { settings_map_->ShutdownOnUIThread(); }
32 36
33 void CheckMirrorCookieRequest(const GURL& url, 37 void CheckMirrorCookieRequest(const GURL& url,
34 const std::string& account_id, 38 const std::string& account_id,
35 const std::string& expected_request) { 39 const std::string& expected_request) {
36 EXPECT_EQ(signin::BuildMirrorRequestCookieIfPossible( 40 EXPECT_EQ(signin::BuildMirrorRequestCookieIfPossible(
37 url, account_id, cookie_settings_.get(), 41 url, account_id, cookie_settings_.get(),
38 signin::PROFILE_MODE_DEFAULT), 42 signin::PROFILE_MODE_DEFAULT),
39 expected_request); 43 expected_request);
40 } 44 }
41 45
42 void CheckMirrorHeaderRequest(const GURL& url, 46 void CheckAccountConsistencyHeaderRequest(
43 const std::string& account_id, 47 const GURL& url,
44 const std::string& expected_request) { 48 const std::string& account_id,
49 const char* header_name,
50 const std::string& expected_request) {
45 bool expected_result = !expected_request.empty(); 51 bool expected_result = !expected_request.empty();
46 std::unique_ptr<net::URLRequest> url_request = 52 std::unique_ptr<net::URLRequest> url_request =
47 url_request_context_.CreateRequest(url, net::DEFAULT_PRIORITY, nullptr, 53 url_request_context_.CreateRequest(url, net::DEFAULT_PRIORITY, nullptr,
48 TRAFFIC_ANNOTATION_FOR_TESTS); 54 TRAFFIC_ANNOTATION_FOR_TESTS);
49 EXPECT_EQ(signin::AppendOrRemoveAccountConsistentyRequestHeader( 55 EXPECT_EQ(signin::AppendOrRemoveAccountConsistentyRequestHeader(
50 url_request.get(), GURL(), account_id, cookie_settings_.get(), 56 url_request.get(), GURL(), account_id, cookie_settings_.get(),
51 signin::PROFILE_MODE_DEFAULT), 57 signin::PROFILE_MODE_DEFAULT),
52 expected_result); 58 expected_result);
53 std::string request; 59 std::string request;
54 EXPECT_EQ(url_request->extra_request_headers().GetHeader( 60 EXPECT_EQ(
55 signin::kChromeConnectedHeader, &request), 61 url_request->extra_request_headers().GetHeader(header_name, &request),
56 expected_result); 62 expected_result);
57 if (expected_result) { 63 if (expected_result) {
58 EXPECT_EQ(expected_request, request); 64 EXPECT_EQ(expected_request, request);
59 } 65 }
60 } 66 }
61 67
68 void CheckMirrorHeaderRequest(const GURL& url,
69 const std::string& account_id,
70 const std::string& expected_request) {
71 return CheckAccountConsistencyHeaderRequest(
72 url, account_id, signin::kChromeConnectedHeader, expected_request);
73 }
74
75 void CheckDiceHeaderRequest(const GURL& url,
76 const std::string& expected_request) {
77 return CheckAccountConsistencyHeaderRequest(
78 url, "", signin::kChromeIDConsistencyRequestHeader, expected_request);
79 }
80
62 base::MessageLoop loop_; 81 base::MessageLoop loop_;
63 82
64 sync_preferences::TestingPrefServiceSyncable prefs_; 83 sync_preferences::TestingPrefServiceSyncable prefs_;
65 net::TestURLRequestContext url_request_context_; 84 net::TestURLRequestContext url_request_context_;
66 85
67 scoped_refptr<HostContentSettingsMap> settings_map_; 86 scoped_refptr<HostContentSettingsMap> settings_map_;
68 scoped_refptr<content_settings::CookieSettings> cookie_settings_; 87 scoped_refptr<content_settings::CookieSettings> cookie_settings_;
69 }; 88 };
70 89
71 // Tests that no Mirror request is returned when the user is not signed in (no 90 // Tests that no Mirror request is returned when the user is not signed in (no
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 TEST_F(SigninHeaderHelperTest, TestMirrorRequestGoogleCom) { 130 TEST_F(SigninHeaderHelperTest, TestMirrorRequestGoogleCom) {
112 switches::EnableAccountConsistencyMirrorForTesting( 131 switches::EnableAccountConsistencyMirrorForTesting(
113 base::CommandLine::ForCurrentProcess()); 132 base::CommandLine::ForCurrentProcess());
114 CheckMirrorHeaderRequest(GURL("https://www.google.com"), "0123456789", 133 CheckMirrorHeaderRequest(GURL("https://www.google.com"), "0123456789",
115 "mode=0,enable_account_consistency=true"); 134 "mode=0,enable_account_consistency=true");
116 CheckMirrorCookieRequest( 135 CheckMirrorCookieRequest(
117 GURL("https://www.google.com"), "0123456789", 136 GURL("https://www.google.com"), "0123456789",
118 "id=0123456789:mode=0:enable_account_consistency=true"); 137 "id=0123456789:mode=0:enable_account_consistency=true");
119 } 138 }
120 139
140 // Mirror is always enabled on Android and iOS, so these tests are only relevant
141 // on Desktop.
142 #if !defined(OS_ANDROID) && !defined(OS_IOS)
143
144 // Tests that the Mirror request is returned when the target is a Gaia URL, even
145 // if account consistency is disabled.
146 TEST_F(SigninHeaderHelperTest, TestMirrorRequestGaiaURL) {
msarda 2017/06/07 13:06:30 We must not pass the mirror header when mirror is
droger 2017/06/07 13:29:57 As said in other comments, this is the current beh
msarda 2017/06/08 02:53:38 You are correct. See my comments in the previous f
147 ASSERT_FALSE(switches::IsAccountConsistencyMirrorEnabled());
148 CheckMirrorHeaderRequest(GURL("https://accounts.google.com"), "0123456789",
149 "mode=0,enable_account_consistency=false");
150 CheckMirrorCookieRequest(
151 GURL("https://accounts.google.com"), "0123456789",
152 "id=0123456789:mode=0:enable_account_consistency=false");
153 }
154
155 // Tests that no DICe request is returned when the target is not a Gaia URL.
msarda 2017/06/07 13:06:31 Let's use Dice (or dice) and not DICe when referri
156 TEST_F(SigninHeaderHelperTest, TestNoDiceRequestNoGaiaURL) {
157 switches::EnableAccountConsistencyDiceForTesting(
158 base::CommandLine::ForCurrentProcess());
159 CheckDiceHeaderRequest(GURL("https://docs.google.com"), "");
160 }
161
162 // Tests that a DICe request is returned when the target is a Gaia URL.
163 TEST_F(SigninHeaderHelperTest, TestDiceRequestGaiaURL) {
164 switches::EnableAccountConsistencyDiceForTesting(
165 base::CommandLine::ForCurrentProcess());
166 std::string client_id = GaiaUrls::GetInstance()->oauth2_chrome_client_id();
167 ASSERT_FALSE(client_id.empty());
168 CheckDiceHeaderRequest(GURL("https://accounts.google.com"),
169 "client_id=" + client_id);
170 }
171
121 // Tests that the Mirror request is returned with the GAIA Id on Drive origin, 172 // Tests that the Mirror request is returned with the GAIA Id on Drive origin,
122 // even if account consistency is disabled. 173 // even if account consistency is disabled.
123 //
124 // Account consistency if always enabled on Android and iOS, so this test is
125 // only relevant on Desktop.
126 #if !defined(OS_ANDROID) && !defined(OS_IOS)
127 TEST_F(SigninHeaderHelperTest, TestMirrorRequestDrive) { 174 TEST_F(SigninHeaderHelperTest, TestMirrorRequestDrive) {
128 DCHECK(!switches::IsAccountConsistencyMirrorEnabled()); 175 ASSERT_FALSE(switches::IsAccountConsistencyMirrorEnabled());
129 CheckMirrorHeaderRequest( 176 CheckMirrorHeaderRequest(
130 GURL("https://docs.google.com/document"), "0123456789", 177 GURL("https://docs.google.com/document"), "0123456789",
131 "id=0123456789,mode=0,enable_account_consistency=false"); 178 "id=0123456789,mode=0,enable_account_consistency=false");
132 CheckMirrorCookieRequest( 179 CheckMirrorCookieRequest(
133 GURL("https://drive.google.com/drive"), "0123456789", 180 GURL("https://drive.google.com/drive"), "0123456789",
134 "id=0123456789:mode=0:enable_account_consistency=false"); 181 "id=0123456789:mode=0:enable_account_consistency=false");
135 182
136 // Enable Account Consistency will override the disable. 183 // Enable Account Consistency will override the disable.
137 switches::EnableAccountConsistencyMirrorForTesting( 184 switches::EnableAccountConsistencyMirrorForTesting(
138 base::CommandLine::ForCurrentProcess()); 185 base::CommandLine::ForCurrentProcess());
139 CheckMirrorHeaderRequest( 186 CheckMirrorHeaderRequest(
140 GURL("https://docs.google.com/document"), "0123456789", 187 GURL("https://docs.google.com/document"), "0123456789",
141 "id=0123456789,mode=0,enable_account_consistency=true"); 188 "id=0123456789,mode=0,enable_account_consistency=true");
142 CheckMirrorCookieRequest( 189 CheckMirrorCookieRequest(
143 GURL("https://drive.google.com/drive"), "0123456789", 190 GURL("https://drive.google.com/drive"), "0123456789",
144 "id=0123456789:mode=0:enable_account_consistency=true"); 191 "id=0123456789:mode=0:enable_account_consistency=true");
145 } 192 }
146 #endif 193
194 #endif // !defined(OS_ANDROID) && !defined(OS_IOS)
147 195
148 // Tests that the Mirror header request is returned normally when the redirect 196 // Tests that the Mirror header request is returned normally when the redirect
149 // URL is eligible. 197 // URL is eligible.
150 TEST_F(SigninHeaderHelperTest, TestMirrorHeaderEligibleRedirectURL) { 198 TEST_F(SigninHeaderHelperTest, TestMirrorHeaderEligibleRedirectURL) {
151 switches::EnableAccountConsistencyMirrorForTesting( 199 switches::EnableAccountConsistencyMirrorForTesting(
152 base::CommandLine::ForCurrentProcess()); 200 base::CommandLine::ForCurrentProcess());
153 const GURL url("https://docs.google.com/document"); 201 const GURL url("https://docs.google.com/document");
154 const GURL redirect_url("https://www.google.com"); 202 const GURL redirect_url("https://www.google.com");
155 const std::string account_id = "0123456789"; 203 const std::string account_id = "0123456789";
156 std::unique_ptr<net::URLRequest> url_request = 204 std::unique_ptr<net::URLRequest> url_request =
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 url_request->SetExtraRequestHeaderByName(signin::kChromeConnectedHeader, 244 url_request->SetExtraRequestHeaderByName(signin::kChromeConnectedHeader,
197 fake_header, false); 245 fake_header, false);
198 EXPECT_FALSE(signin::AppendOrRemoveAccountConsistentyRequestHeader( 246 EXPECT_FALSE(signin::AppendOrRemoveAccountConsistentyRequestHeader(
199 url_request.get(), redirect_url, account_id, cookie_settings_.get(), 247 url_request.get(), redirect_url, account_id, cookie_settings_.get(),
200 signin::PROFILE_MODE_DEFAULT)); 248 signin::PROFILE_MODE_DEFAULT));
201 std::string header; 249 std::string header;
202 EXPECT_TRUE(url_request->extra_request_headers().GetHeader( 250 EXPECT_TRUE(url_request->extra_request_headers().GetHeader(
203 signin::kChromeConnectedHeader, &header)); 251 signin::kChromeConnectedHeader, &header));
204 EXPECT_EQ(fake_header, header); 252 EXPECT_EQ(fake_header, header);
205 } 253 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698