1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
ash / webui / graduation / webview_auth_handler_unittest.cc [blame]
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/webui/graduation/webview_auth_handler.h"
#include <string>
#include "ash/public/cpp/graduation/graduation_manager.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "components/signin/public/base/consent_level.h"
#include "components/signin/public/base/test_signin_client.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "components/signin/public/identity_manager/set_accounts_in_cookie_result.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/storage_partition_config.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_storage_partition.h"
#include "google_apis/gaia/gaia_constants.h"
#include "google_apis/gaia/gaia_urls.h"
#include "services/network/public/mojom/cookie_manager.mojom.h"
#include "services/network/test/test_cookie_manager.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace ash::graduation {
namespace {
constexpr char kTestAccountEmail[] = "user@test.com";
constexpr char kWebviewHostName[] = "graduation";
constexpr char kCookieResponseSuccess[] = R"(
{ "status": "OK",
"cookies":[
{
"name":"CookieName",
"value":"CookieValue",
"domain":".google.com",
"path":"/"
}
]
})";
constexpr char kCookieResponseRetry[] = R"(
{ "status": "RETRY",
"cookies":[]
})";
constexpr char kCookieResponseError[] = R"(
{ "status": "ERROR",
"cookies":[]
})";
// Returns HTTP request response that will lead to the given `result`.
std::string GetResponseFromResult(signin::SetAccountsInCookieResult result) {
switch (result) {
case signin::SetAccountsInCookieResult::kSuccess:
return kCookieResponseSuccess;
case signin::SetAccountsInCookieResult::kTransientError:
return kCookieResponseRetry;
case signin::SetAccountsInCookieResult::kPersistentError:
return kCookieResponseError;
};
}
} // namespace
// GraduationManger for tests that allows to inject identity manager and url
// loader factory.
class TestGraduationManager : public GraduationManager {
public:
explicit TestGraduationManager(
signin::IdentityManager* identity_manager,
network::TestURLLoaderFactory* url_loader_factory)
: test_identity_manager_(identity_manager) {
test_storage_partition_.set_cookie_manager_for_browser_process(
&test_cookie_manager_);
test_storage_partition_.set_url_loader_factory_for_browser_process(
url_loader_factory);
}
~TestGraduationManager() override = default;
// GraduationManagerImpl:
signin::IdentityManager* GetIdentityManager(
content::BrowserContext* context) override {
return test_identity_manager_;
}
content::StoragePartition* GetStoragePartition(
content::BrowserContext* context,
const content::StoragePartitionConfig& storage_partition_config)
override {
test_storage_partition_.set_config(storage_partition_config);
return &test_storage_partition_;
}
std::string GetLanguageCode() const override { return "en"; }
void AddObserver(GraduationManagerObserver* observer) override {}
void RemoveObserver(GraduationManagerObserver* observer) override {}
void SetClocksForTesting(const base::Clock* clock,
const base::TickClock* tick_clock) override {}
void ResumeTimerForTesting() override {}
private:
content::TestStoragePartition test_storage_partition_;
network::TestCookieManager test_cookie_manager_;
const raw_ptr<signin::IdentityManager> test_identity_manager_;
};
class WebviewAuthHandlerTest : public testing::Test {
public:
WebviewAuthHandlerTest() = default;
WebviewAuthHandlerTest(const WebviewAuthHandlerTest&) = delete;
WebviewAuthHandlerTest& operator=(const WebviewAuthHandlerTest&) = delete;
~WebviewAuthHandlerTest() override {}
protected:
WebviewAuthHandler& auth_handler() { return auth_handler_; }
void QueueResponseForResult(signin::SetAccountsInCookieResult result) {
test_signin_client_.GetTestURLLoaderFactory()->AddResponse(
GaiaUrls::GetInstance()
->oauth_multilogin_url()
.Resolve(base::StringPrintf("?source=%s&reuseCookies=0",
GaiaConstants::kChromeOSSource))
.spec(),
GetResponseFromResult(result));
}
void SetUp() override {
testing::Test::SetUp();
identity_test_env_.MakePrimaryAccountAvailable(
kTestAccountEmail, signin::ConsentLevel::kSignin);
identity_test_env_.SetAutomaticIssueOfAccessTokens(true);
}
private:
content::BrowserTaskEnvironment task_environment_;
content::TestBrowserContext test_context_;
sync_preferences::TestingPrefServiceSyncable prefs_;
TestSigninClient test_signin_client_{&prefs_};
signin::IdentityTestEnvironment identity_test_env_{
/*test_url_loader_factory=*/nullptr, &prefs_, &test_signin_client_};
TestGraduationManager test_graduation_manager_{
identity_test_env_.identity_manager(),
test_signin_client_.GetTestURLLoaderFactory()};
WebviewAuthHandler auth_handler_{&test_context_, kWebviewHostName};
};
TEST_F(WebviewAuthHandlerTest, AuthSuccess) {
base::RunLoop run_loop;
base::HistogramTester histogram_tester;
QueueResponseForResult(signin::SetAccountsInCookieResult::kSuccess);
histogram_tester.ExpectUniqueSample(
WebviewAuthHandler::kAuthResultHistogramName,
WebviewAuthHandler::AuthResult::kSuccess, 0);
auth_handler().AuthenticateWebview(
base::BindLambdaForTesting([&](bool is_success) -> void {
EXPECT_TRUE(is_success);
histogram_tester.ExpectUniqueSample(
WebviewAuthHandler::kAuthResultHistogramName,
WebviewAuthHandler::AuthResult::kSuccess, 1);
run_loop.Quit();
}));
run_loop.Run();
}
TEST_F(WebviewAuthHandlerTest, AuthPersistentFailure) {
base::RunLoop run_loop;
base::HistogramTester histogram_tester;
QueueResponseForResult(signin::SetAccountsInCookieResult::kPersistentError);
histogram_tester.ExpectUniqueSample(
WebviewAuthHandler::kAuthResultHistogramName,
WebviewAuthHandler::AuthResult::kPersistentFailure, 0);
auth_handler().AuthenticateWebview(
base::BindLambdaForTesting([&](bool is_success) -> void {
EXPECT_FALSE(is_success);
histogram_tester.ExpectUniqueSample(
WebviewAuthHandler::kAuthResultHistogramName,
WebviewAuthHandler::AuthResult::kPersistentFailure, 1);
run_loop.Quit();
}));
run_loop.Run();
}
TEST_F(WebviewAuthHandlerTest, AuthTransientFailure_MaxRetry) {
base::RunLoop run_loop;
base::HistogramTester histogram_tester;
QueueResponseForResult(signin::SetAccountsInCookieResult::kTransientError);
histogram_tester.ExpectUniqueSample(
WebviewAuthHandler::kAuthResultHistogramName,
WebviewAuthHandler::AuthResult::kTransientFailure, 0);
auth_handler().AuthenticateWebview(
base::BindLambdaForTesting([&](bool is_success) -> void {
EXPECT_FALSE(is_success);
histogram_tester.ExpectUniqueSample(
WebviewAuthHandler::kAuthResultHistogramName,
WebviewAuthHandler::AuthResult::kTransientFailure,
WebviewAuthHandler::kMaxRetries);
run_loop.Quit();
}));
run_loop.Run();
}
} // namespace ash::graduation