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

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

Issue 2360733002: Add unit tests for Signin Header Helpers. (Closed)
Patch Set: Better BUILD.gn Created 4 years, 2 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 | « components/signin/core/browser/BUILD.gn ('k') | no next file » | 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 2016 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 <memory>
6
7 #include "base/command_line.h"
8 #include "base/message_loop/message_loop.h"
9 #include "components/content_settings/core/browser/cookie_settings.h"
10 #include "components/pref_registry/testing_pref_service_syncable.h"
11 #include "components/signin/core/browser/signin_header_helper.h"
12 #include "components/signin/core/common/signin_switches.h"
13 #include "net/url_request/url_request_test_util.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "url/gurl.h"
16
17 class SigninHeaderHelperTest : public testing::Test {
18 protected:
19 void SetUp() override {
20 content_settings::CookieSettings::RegisterProfilePrefs(prefs_.registry());
21 HostContentSettingsMap::RegisterProfilePrefs(prefs_.registry());
22
23 settings_map_ = new HostContentSettingsMap(
24 &prefs_, false /* incognito_profile */, false /* guest_profile */);
25 cookie_settings_ =
26 new content_settings::CookieSettings(settings_map_.get(), &prefs_, "");
27 }
28
29 void TearDown() override { settings_map_->ShutdownOnUIThread(); }
30
31 void CheckMirrorCookieRequest(const GURL& url,
32 const std::string& account_id,
33 const std::string& expected_request) {
34 EXPECT_EQ(signin::BuildMirrorRequestCookieIfPossible(
35 url, account_id, cookie_settings_.get(),
36 signin::PROFILE_MODE_DEFAULT),
37 expected_request);
38 }
39
40 void CheckMirrorHeaderRequest(const GURL& url,
41 const std::string& account_id,
42 const std::string& expected_request) {
43 bool expected_result = !expected_request.empty();
44 std::unique_ptr<net::URLRequest> url_request =
45 url_request_context_.CreateRequest(url, net::DEFAULT_PRIORITY, nullptr);
46 EXPECT_EQ(signin::AppendOrRemoveMirrorRequestHeaderIfPossible(
47 url_request.get(), GURL(), account_id, cookie_settings_.get(),
48 signin::PROFILE_MODE_DEFAULT),
49 expected_result);
50 std::string request;
51 EXPECT_EQ(url_request->extra_request_headers().GetHeader(
52 signin::kChromeConnectedHeader, &request),
53 expected_result);
54 if (expected_result) {
55 EXPECT_EQ(expected_request, request);
56 }
57 }
58
59 base::MessageLoop loop_;
60
61 user_prefs::TestingPrefServiceSyncable prefs_;
62 net::TestURLRequestContext url_request_context_;
63
64 scoped_refptr<HostContentSettingsMap> settings_map_;
65 scoped_refptr<content_settings::CookieSettings> cookie_settings_;
66 };
67
68 // Tests that no Mirror request is returned when the user is not signed in (no
69 // account id).
70 TEST_F(SigninHeaderHelperTest, TestNoMirrorRequestNoAccountId) {
71 base::CommandLine::ForCurrentProcess()->AppendSwitch(
72 switches::kEnableAccountConsistency);
73 CheckMirrorHeaderRequest(GURL("https://docs.google.com"), "", "");
74 CheckMirrorCookieRequest(GURL("https://docs.google.com"), "", "");
75 }
76
77 // Tests that no Mirror request is returned when the cookies aren't allowed to
78 // be set.
79 TEST_F(SigninHeaderHelperTest, TestNoMirrorRequestCookieSettingBlocked) {
80 base::CommandLine::ForCurrentProcess()->AppendSwitch(
81 switches::kEnableAccountConsistency);
82 cookie_settings_->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK);
83 CheckMirrorHeaderRequest(GURL("https://docs.google.com"), "0123456789", "");
84 CheckMirrorCookieRequest(GURL("https://docs.google.com"), "0123456789", "");
85 }
86
87 // Tests that no Mirror request is returned when the target is a non-Google URL.
88 TEST_F(SigninHeaderHelperTest, TestNoMirrorRequestExternalURL) {
89 base::CommandLine::ForCurrentProcess()->AppendSwitch(
90 switches::kEnableAccountConsistency);
91 CheckMirrorHeaderRequest(GURL("https://foo.com"), "0123456789", "");
92 CheckMirrorCookieRequest(GURL("https://foo.com"), "0123456789", "");
93 }
94
95 // Tests that the Mirror request is returned without the GAIA Id when the target
96 // is a google TLD domain.
97 TEST_F(SigninHeaderHelperTest, TestMirrorRequestGoogleTLD) {
98 base::CommandLine::ForCurrentProcess()->AppendSwitch(
99 switches::kEnableAccountConsistency);
100 CheckMirrorHeaderRequest(GURL("https://google.fr"), "0123456789",
101 "mode=0,enable_account_consistency=true");
102 CheckMirrorCookieRequest(GURL("https://google.de"), "0123456789",
103 "mode=0:enable_account_consistency=true");
104 }
105
106 // Tests that the Mirror request is returned when the target is the domain
107 // google.com, and that the GAIA Id is only attached for the cookie.
108 TEST_F(SigninHeaderHelperTest, TestMirrorRequestGoogleCom) {
109 base::CommandLine::ForCurrentProcess()->AppendSwitch(
110 switches::kEnableAccountConsistency);
111 CheckMirrorHeaderRequest(GURL("https://www.google.com"), "0123456789",
112 "mode=0,enable_account_consistency=true");
113 CheckMirrorCookieRequest(
114 GURL("https://www.google.com"), "0123456789",
115 "id=0123456789:mode=0:enable_account_consistency=true");
116 }
117
118 // Tests that the Mirror request is returned with the GAIA Id on Drive origin,
119 // even if account consistency is disabled.
120 TEST_F(SigninHeaderHelperTest, TestMirrorRequestDrive) {
Roger Tawa OOO till Jul 10th 2016/09/22 19:19:52 Maybe DCHECK that consistency is off at start of t
bzanotti 2016/09/23 14:18:10 I think the CommandLine is reset before each tests
121 base::CommandLine::ForCurrentProcess()->AppendSwitch(
122 switches::kDisableAccountConsistency);
123 CheckMirrorHeaderRequest(
124 GURL("https://docs.google.com/document"), "0123456789",
125 "id=0123456789,mode=0,enable_account_consistency=false");
126 CheckMirrorCookieRequest(
127 GURL("https://drive.google.com/drive"), "0123456789",
128 "id=0123456789:mode=0:enable_account_consistency=false");
129
130 // Enable Account Consistency will override the disable.
131 base::CommandLine::ForCurrentProcess()->AppendSwitch(
132 switches::kEnableAccountConsistency);
133
134 CheckMirrorHeaderRequest(
135 GURL("https://docs.google.com/document"), "0123456789",
136 "id=0123456789,mode=0,enable_account_consistency=true");
137 CheckMirrorCookieRequest(
138 GURL("https://drive.google.com/drive"), "0123456789",
139 "id=0123456789:mode=0:enable_account_consistency=true");
140 }
141
142 // Tests that the Mirror header request is returned normally when the redirect
143 // URL is eligible.
144 TEST_F(SigninHeaderHelperTest, TestMirrorHeaderEligibleRedirectURL) {
145 base::CommandLine::ForCurrentProcess()->AppendSwitch(
146 switches::kEnableAccountConsistency);
147
148 const GURL url("https://docs.google.com/document");
149 const GURL redirect_url("https://www.google.com");
150 const std::string account_id = "0123456789";
151 std::unique_ptr<net::URLRequest> url_request =
152 url_request_context_.CreateRequest(url, net::DEFAULT_PRIORITY, nullptr);
153 EXPECT_TRUE(signin::AppendOrRemoveMirrorRequestHeaderIfPossible(
154 url_request.get(), redirect_url, account_id, cookie_settings_.get(),
155 signin::PROFILE_MODE_DEFAULT));
156 EXPECT_TRUE(url_request->extra_request_headers().HasHeader(
157 signin::kChromeConnectedHeader));
158 }
159
160 // Tests that the Mirror header request is stripped when the redirect URL is not
161 // eligible.
162 TEST_F(SigninHeaderHelperTest, TestMirrorHeaderNonEligibleRedirectURL) {
163 base::CommandLine::ForCurrentProcess()->AppendSwitch(
164 switches::kEnableAccountConsistency);
165
166 const GURL url("https://docs.google.com/document");
167 const GURL redirect_url("http://www.foo.com");
168 const std::string account_id = "0123456789";
169 std::unique_ptr<net::URLRequest> url_request =
170 url_request_context_.CreateRequest(url, net::DEFAULT_PRIORITY, nullptr);
171 EXPECT_FALSE(signin::AppendOrRemoveMirrorRequestHeaderIfPossible(
172 url_request.get(), redirect_url, account_id, cookie_settings_.get(),
173 signin::PROFILE_MODE_DEFAULT));
174 EXPECT_FALSE(url_request->extra_request_headers().HasHeader(
175 signin::kChromeConnectedHeader));
176 }
Roger Tawa OOO till Jul 10th 2016/09/22 19:19:52 According to Dominic, if both the original URL and
bzanotti 2016/09/23 14:18:10 Done.
OLDNEW
« no previous file with comments | « components/signin/core/browser/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698