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

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: Add test for Dice disabled 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" 14 #include "components/signin/core/browser/chrome_connected_header_helper.h"
11 #include "components/signin/core/common/profile_management_switches.h" 15 #include "components/signin/core/common/profile_management_switches.h"
12 #include "components/sync_preferences/testing_pref_service_syncable.h" 16 #include "components/sync_preferences/testing_pref_service_syncable.h"
17 #include "google_apis/gaia/gaia_urls.h"
13 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" 18 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
14 #include "net/url_request/url_request_test_util.h" 19 #include "net/url_request/url_request_test_util.h"
15 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
16 #include "url/gurl.h" 21 #include "url/gurl.h"
17 22
23 #if !defined(OS_IOS) && !defined(OS_ANDROID)
24 #include "components/signin/core/browser/dice_header_helper.h"
25 #endif
26
18 class SigninHeaderHelperTest : public testing::Test { 27 class SigninHeaderHelperTest : public testing::Test {
19 protected: 28 protected:
20 void SetUp() override { 29 void SetUp() override {
21 content_settings::CookieSettings::RegisterProfilePrefs(prefs_.registry()); 30 content_settings::CookieSettings::RegisterProfilePrefs(prefs_.registry());
22 HostContentSettingsMap::RegisterProfilePrefs(prefs_.registry()); 31 HostContentSettingsMap::RegisterProfilePrefs(prefs_.registry());
23 32
24 settings_map_ = new HostContentSettingsMap( 33 settings_map_ = new HostContentSettingsMap(
25 &prefs_, false /* incognito_profile */, false /* guest_profile */, 34 &prefs_, false /* incognito_profile */, false /* guest_profile */,
26 false /* store_last_modified */); 35 false /* store_last_modified */);
27 cookie_settings_ = 36 cookie_settings_ =
28 new content_settings::CookieSettings(settings_map_.get(), &prefs_, ""); 37 new content_settings::CookieSettings(settings_map_.get(), &prefs_, "");
29 } 38 }
30 39
31 void TearDown() override { settings_map_->ShutdownOnUIThread(); } 40 void TearDown() override { settings_map_->ShutdownOnUIThread(); }
32 41
33 void CheckMirrorCookieRequest(const GURL& url, 42 void CheckMirrorCookieRequest(const GURL& url,
34 const std::string& account_id, 43 const std::string& account_id,
35 const std::string& expected_request) { 44 const std::string& expected_request) {
36 EXPECT_EQ(signin::BuildMirrorRequestCookieIfPossible( 45 EXPECT_EQ(signin::ChromeConnectedHeaderHelper::BuildRequestCookieIfPossible(
msarda 2017/06/08 23:43:05 I'd like to avoid this call from the test and just
droger 2017/06/09 09:52:24 Done.
37 url, account_id, cookie_settings_.get(), 46 url, account_id, cookie_settings_.get(),
38 signin::PROFILE_MODE_DEFAULT), 47 signin::PROFILE_MODE_DEFAULT),
39 expected_request); 48 expected_request);
40 } 49 }
41 50
42 void CheckMirrorHeaderRequest(const GURL& url, 51 void CheckAccountConsistencyHeaderRequest(
43 const std::string& account_id, 52 const GURL& url,
44 const std::string& expected_request) { 53 const std::string& account_id,
54 const char* header_name,
55 const std::string& expected_request) {
45 bool expected_result = !expected_request.empty(); 56 bool expected_result = !expected_request.empty();
46 std::unique_ptr<net::URLRequest> url_request = 57 std::unique_ptr<net::URLRequest> url_request =
47 url_request_context_.CreateRequest(url, net::DEFAULT_PRIORITY, nullptr, 58 url_request_context_.CreateRequest(url, net::DEFAULT_PRIORITY, nullptr,
48 TRAFFIC_ANNOTATION_FOR_TESTS); 59 TRAFFIC_ANNOTATION_FOR_TESTS);
49 EXPECT_EQ(signin::AppendOrRemoveAccountConsistentyRequestHeader( 60 signin::AppendOrRemoveAccountConsistentyRequestHeader(
50 url_request.get(), GURL(), account_id, cookie_settings_.get(), 61 url_request.get(), GURL(), account_id, cookie_settings_.get(),
51 signin::PROFILE_MODE_DEFAULT), 62 signin::PROFILE_MODE_DEFAULT);
52 expected_result);
53 std::string request; 63 std::string request;
54 EXPECT_EQ(url_request->extra_request_headers().GetHeader( 64 EXPECT_EQ(
55 signin::kChromeConnectedHeader, &request), 65 url_request->extra_request_headers().GetHeader(header_name, &request),
56 expected_result); 66 expected_result);
57 if (expected_result) { 67 if (expected_result) {
58 EXPECT_EQ(expected_request, request); 68 EXPECT_EQ(expected_request, request);
59 } 69 }
60 } 70 }
61 71
72 void CheckMirrorHeaderRequest(const GURL& url,
73 const std::string& account_id,
74 const std::string& expected_request) {
75 return CheckAccountConsistencyHeaderRequest(
76 url, account_id, signin::kChromeConnectedHeader, expected_request);
77 }
78
79 #if !defined(OS_IOS) && !defined(OS_ANDROID)
80 void CheckDiceHeaderRequest(const GURL& url,
81 const std::string& expected_request) {
82 return CheckAccountConsistencyHeaderRequest(
83 url, "", signin::kDiceRequestHeader, expected_request);
84 }
85 #endif
86
62 base::MessageLoop loop_; 87 base::MessageLoop loop_;
63 88
64 sync_preferences::TestingPrefServiceSyncable prefs_; 89 sync_preferences::TestingPrefServiceSyncable prefs_;
65 net::TestURLRequestContext url_request_context_; 90 net::TestURLRequestContext url_request_context_;
66 91
67 scoped_refptr<HostContentSettingsMap> settings_map_; 92 scoped_refptr<HostContentSettingsMap> settings_map_;
68 scoped_refptr<content_settings::CookieSettings> cookie_settings_; 93 scoped_refptr<content_settings::CookieSettings> cookie_settings_;
69 }; 94 };
70 95
71 // Tests that no Mirror request is returned when the user is not signed in (no 96 // 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) { 136 TEST_F(SigninHeaderHelperTest, TestMirrorRequestGoogleCom) {
112 switches::EnableAccountConsistencyMirrorForTesting( 137 switches::EnableAccountConsistencyMirrorForTesting(
113 base::CommandLine::ForCurrentProcess()); 138 base::CommandLine::ForCurrentProcess());
114 CheckMirrorHeaderRequest(GURL("https://www.google.com"), "0123456789", 139 CheckMirrorHeaderRequest(GURL("https://www.google.com"), "0123456789",
115 "mode=0,enable_account_consistency=true"); 140 "mode=0,enable_account_consistency=true");
116 CheckMirrorCookieRequest( 141 CheckMirrorCookieRequest(
117 GURL("https://www.google.com"), "0123456789", 142 GURL("https://www.google.com"), "0123456789",
118 "id=0123456789:mode=0:enable_account_consistency=true"); 143 "id=0123456789:mode=0:enable_account_consistency=true");
119 } 144 }
120 145
146 // Mirror is always enabled on Android and iOS, so these tests are only relevant
147 // on Desktop.
148 #if !defined(OS_ANDROID) && !defined(OS_IOS)
149
150 // Tests that the Mirror request is returned when the target is a Gaia URL, even
151 // if account consistency is disabled.
152 TEST_F(SigninHeaderHelperTest, TestMirrorRequestGaiaURL) {
153 ASSERT_FALSE(switches::IsAccountConsistencyMirrorEnabled());
154 CheckMirrorHeaderRequest(GURL("https://accounts.google.com"), "0123456789",
msarda 2017/06/08 23:43:05 This actually end up calling |signin::AppendOrRemo
droger 2017/06/09 09:52:24 Good point, added a test.
155 "mode=0,enable_account_consistency=false");
156 CheckMirrorCookieRequest(
157 GURL("https://accounts.google.com"), "0123456789",
158 "id=0123456789:mode=0:enable_account_consistency=false");
159 }
160
161 // Tests that no Dice request is returned when the target is not a Gaia URL.
162 TEST_F(SigninHeaderHelperTest, TestNoDiceRequestNoGaiaURL) {
163 switches::EnableAccountConsistencyDiceForTesting(
164 base::CommandLine::ForCurrentProcess());
165 CheckDiceHeaderRequest(GURL("https://docs.google.com"), "");
166 }
167
168 // Tests that a Dice request is returned when the target is a Gaia URL.
169 TEST_F(SigninHeaderHelperTest, TestDiceRequestGaiaURL) {
170 switches::EnableAccountConsistencyDiceForTesting(
171 base::CommandLine::ForCurrentProcess());
172 std::string client_id = GaiaUrls::GetInstance()->oauth2_chrome_client_id();
173 ASSERT_FALSE(client_id.empty());
174 CheckDiceHeaderRequest(GURL("https://accounts.google.com"),
175 "client_id=" + client_id);
176 }
177
178 // Tests that no Dice request is returned when Dice is not enabled.
179 TEST_F(SigninHeaderHelperTest, TestNoDiceRequestWhenDisabled) {
180 switches::EnableAccountConsistencyMirrorForTesting(
181 base::CommandLine::ForCurrentProcess());
182 CheckDiceHeaderRequest(GURL("https://accounts.google.com"), "");
183 }
184
121 // Tests that the Mirror request is returned with the GAIA Id on Drive origin, 185 // Tests that the Mirror request is returned with the GAIA Id on Drive origin,
122 // even if account consistency is disabled. 186 // 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) { 187 TEST_F(SigninHeaderHelperTest, TestMirrorRequestDrive) {
128 DCHECK(!switches::IsAccountConsistencyMirrorEnabled()); 188 ASSERT_FALSE(switches::IsAccountConsistencyMirrorEnabled());
129 CheckMirrorHeaderRequest( 189 CheckMirrorHeaderRequest(
130 GURL("https://docs.google.com/document"), "0123456789", 190 GURL("https://docs.google.com/document"), "0123456789",
131 "id=0123456789,mode=0,enable_account_consistency=false"); 191 "id=0123456789,mode=0,enable_account_consistency=false");
132 CheckMirrorCookieRequest( 192 CheckMirrorCookieRequest(
133 GURL("https://drive.google.com/drive"), "0123456789", 193 GURL("https://drive.google.com/drive"), "0123456789",
134 "id=0123456789:mode=0:enable_account_consistency=false"); 194 "id=0123456789:mode=0:enable_account_consistency=false");
135 195
136 // Enable Account Consistency will override the disable. 196 // Enable Account Consistency will override the disable.
137 switches::EnableAccountConsistencyMirrorForTesting( 197 switches::EnableAccountConsistencyMirrorForTesting(
138 base::CommandLine::ForCurrentProcess()); 198 base::CommandLine::ForCurrentProcess());
139 CheckMirrorHeaderRequest( 199 CheckMirrorHeaderRequest(
140 GURL("https://docs.google.com/document"), "0123456789", 200 GURL("https://docs.google.com/document"), "0123456789",
141 "id=0123456789,mode=0,enable_account_consistency=true"); 201 "id=0123456789,mode=0,enable_account_consistency=true");
142 CheckMirrorCookieRequest( 202 CheckMirrorCookieRequest(
143 GURL("https://drive.google.com/drive"), "0123456789", 203 GURL("https://drive.google.com/drive"), "0123456789",
144 "id=0123456789:mode=0:enable_account_consistency=true"); 204 "id=0123456789:mode=0:enable_account_consistency=true");
145 } 205 }
146 #endif 206
207 #endif // !defined(OS_ANDROID) && !defined(OS_IOS)
147 208
148 // Tests that the Mirror header request is returned normally when the redirect 209 // Tests that the Mirror header request is returned normally when the redirect
149 // URL is eligible. 210 // URL is eligible.
150 TEST_F(SigninHeaderHelperTest, TestMirrorHeaderEligibleRedirectURL) { 211 TEST_F(SigninHeaderHelperTest, TestMirrorHeaderEligibleRedirectURL) {
151 switches::EnableAccountConsistencyMirrorForTesting( 212 switches::EnableAccountConsistencyMirrorForTesting(
152 base::CommandLine::ForCurrentProcess()); 213 base::CommandLine::ForCurrentProcess());
153 const GURL url("https://docs.google.com/document"); 214 const GURL url("https://docs.google.com/document");
154 const GURL redirect_url("https://www.google.com"); 215 const GURL redirect_url("https://www.google.com");
155 const std::string account_id = "0123456789"; 216 const std::string account_id = "0123456789";
156 std::unique_ptr<net::URLRequest> url_request = 217 std::unique_ptr<net::URLRequest> url_request =
157 url_request_context_.CreateRequest(url, net::DEFAULT_PRIORITY, nullptr, 218 url_request_context_.CreateRequest(url, net::DEFAULT_PRIORITY, nullptr,
158 TRAFFIC_ANNOTATION_FOR_TESTS); 219 TRAFFIC_ANNOTATION_FOR_TESTS);
159 EXPECT_TRUE(signin::AppendOrRemoveAccountConsistentyRequestHeader( 220 signin::AppendOrRemoveAccountConsistentyRequestHeader(
160 url_request.get(), redirect_url, account_id, cookie_settings_.get(), 221 url_request.get(), redirect_url, account_id, cookie_settings_.get(),
161 signin::PROFILE_MODE_DEFAULT)); 222 signin::PROFILE_MODE_DEFAULT);
162 EXPECT_TRUE(url_request->extra_request_headers().HasHeader( 223 EXPECT_TRUE(url_request->extra_request_headers().HasHeader(
163 signin::kChromeConnectedHeader)); 224 signin::kChromeConnectedHeader));
164 } 225 }
165 226
166 // Tests that the Mirror header request is stripped when the redirect URL is not 227 // Tests that the Mirror header request is stripped when the redirect URL is not
167 // eligible. 228 // eligible.
168 TEST_F(SigninHeaderHelperTest, TestMirrorHeaderNonEligibleRedirectURL) { 229 TEST_F(SigninHeaderHelperTest, TestMirrorHeaderNonEligibleRedirectURL) {
169 switches::EnableAccountConsistencyMirrorForTesting( 230 switches::EnableAccountConsistencyMirrorForTesting(
170 base::CommandLine::ForCurrentProcess()); 231 base::CommandLine::ForCurrentProcess());
171 const GURL url("https://docs.google.com/document"); 232 const GURL url("https://docs.google.com/document");
172 const GURL redirect_url("http://www.foo.com"); 233 const GURL redirect_url("http://www.foo.com");
173 const std::string account_id = "0123456789"; 234 const std::string account_id = "0123456789";
174 std::unique_ptr<net::URLRequest> url_request = 235 std::unique_ptr<net::URLRequest> url_request =
175 url_request_context_.CreateRequest(url, net::DEFAULT_PRIORITY, nullptr, 236 url_request_context_.CreateRequest(url, net::DEFAULT_PRIORITY, nullptr,
176 TRAFFIC_ANNOTATION_FOR_TESTS); 237 TRAFFIC_ANNOTATION_FOR_TESTS);
177 EXPECT_FALSE(signin::AppendOrRemoveAccountConsistentyRequestHeader( 238 signin::AppendOrRemoveAccountConsistentyRequestHeader(
178 url_request.get(), redirect_url, account_id, cookie_settings_.get(), 239 url_request.get(), redirect_url, account_id, cookie_settings_.get(),
179 signin::PROFILE_MODE_DEFAULT)); 240 signin::PROFILE_MODE_DEFAULT);
180 EXPECT_FALSE(url_request->extra_request_headers().HasHeader( 241 EXPECT_FALSE(url_request->extra_request_headers().HasHeader(
181 signin::kChromeConnectedHeader)); 242 signin::kChromeConnectedHeader));
182 } 243 }
183 244
184 // Tests that the Mirror header, whatever its value is, is untouched when both 245 // Tests that the Mirror header, whatever its value is, is untouched when both
185 // the current and the redirect URL are non-eligible. 246 // the current and the redirect URL are non-eligible.
186 TEST_F(SigninHeaderHelperTest, TestIgnoreMirrorHeaderNonEligibleURLs) { 247 TEST_F(SigninHeaderHelperTest, TestIgnoreMirrorHeaderNonEligibleURLs) {
187 switches::EnableAccountConsistencyMirrorForTesting( 248 switches::EnableAccountConsistencyMirrorForTesting(
188 base::CommandLine::ForCurrentProcess()); 249 base::CommandLine::ForCurrentProcess());
189 const GURL url("https://www.bar.com"); 250 const GURL url("https://www.bar.com");
190 const GURL redirect_url("http://www.foo.com"); 251 const GURL redirect_url("http://www.foo.com");
191 const std::string account_id = "0123456789"; 252 const std::string account_id = "0123456789";
192 const std::string fake_header = "foo,bar"; 253 const std::string fake_header = "foo,bar";
193 std::unique_ptr<net::URLRequest> url_request = 254 std::unique_ptr<net::URLRequest> url_request =
194 url_request_context_.CreateRequest(url, net::DEFAULT_PRIORITY, nullptr, 255 url_request_context_.CreateRequest(url, net::DEFAULT_PRIORITY, nullptr,
195 TRAFFIC_ANNOTATION_FOR_TESTS); 256 TRAFFIC_ANNOTATION_FOR_TESTS);
196 url_request->SetExtraRequestHeaderByName(signin::kChromeConnectedHeader, 257 url_request->SetExtraRequestHeaderByName(signin::kChromeConnectedHeader,
197 fake_header, false); 258 fake_header, false);
198 EXPECT_FALSE(signin::AppendOrRemoveAccountConsistentyRequestHeader( 259 signin::AppendOrRemoveAccountConsistentyRequestHeader(
199 url_request.get(), redirect_url, account_id, cookie_settings_.get(), 260 url_request.get(), redirect_url, account_id, cookie_settings_.get(),
200 signin::PROFILE_MODE_DEFAULT)); 261 signin::PROFILE_MODE_DEFAULT);
201 std::string header; 262 std::string header;
202 EXPECT_TRUE(url_request->extra_request_headers().GetHeader( 263 EXPECT_TRUE(url_request->extra_request_headers().GetHeader(
203 signin::kChromeConnectedHeader, &header)); 264 signin::kChromeConnectedHeader, &header));
204 EXPECT_EQ(fake_header, header); 265 EXPECT_EQ(fake_header, header);
205 } 266 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698