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

Side by Side Diff: chrome/browser/chromeos/arc/auth/arc_auth_service_browsertest.cc

Issue 2864433003: [ARC] Add browser test of ArcAuthService (Closed)
Patch Set: Response to review Created 3 years, 7 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
(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 <string>
6
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/macros.h"
11 #include "base/memory/ptr_util.h"
12 #include "base/run_loop.h"
13 #include "chrome/browser/chromeos/arc/arc_auth_context.h"
14 #include "chrome/browser/chromeos/arc/arc_auth_notification.h"
15 #include "chrome/browser/chromeos/arc/arc_auth_service.h"
16 #include "chrome/browser/chromeos/arc/arc_service_launcher.h"
17 #include "chrome/browser/chromeos/arc/arc_session_manager.h"
18 #include "chrome/browser/chromeos/arc/arc_util.h"
19 #include "chrome/browser/chromeos/arc/auth/arc_background_auth_code_fetcher.h"
20 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
21 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
24 #include "chrome/browser/signin/fake_signin_manager_builder.h"
25 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
26 #include "chrome/browser/signin/signin_manager_factory.h"
27 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
28 #include "chrome/common/pref_names.h"
29 #include "chrome/test/base/in_process_browser_test.h"
30 #include "chrome/test/base/testing_profile.h"
31 #include "components/arc/arc_features.h"
32 #include "components/arc/arc_service_manager.h"
33 #include "components/arc/arc_session_runner.h"
34 #include "components/arc/arc_util.h"
35 #include "components/arc/test/fake_arc_session.h"
36 #include "components/prefs/pref_member.h"
37 #include "components/prefs/pref_service.h"
38 #include "components/signin/core/account_id/account_id.h"
39 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
40 #include "components/user_manager/known_user.h"
41 #include "components/user_manager/user_manager.h"
42 #include "net/url_request/test_url_fetcher_factory.h"
43 #include "testing/gtest/include/gtest/gtest.h"
44 #include "url/gurl.h"
45
46 namespace {
47
48 constexpr char kRefreshToken[] = "fake-refresh-token";
49 constexpr char kFakeUserName[] = "test@example.com";
50 constexpr char kFakeGaiaId[] = "1234567890";
51 constexpr char kFakeAuthCode[] = "fake-auth-code";
52
53 } // namespace
54
55 namespace arc {
56
57 class FakeAuthInstance : public mojom::AuthInstance {
khmel 2017/05/12 15:52:28 Up to you and Luis but looks like it is better to
blundell 2017/05/15 08:32:32 Thanks, I'll move it in a followup.
58 public:
59 // mojom::AuthInstance:
60 void Init(mojom::AuthHostPtr host) override { host_ = std::move(host); }
61
62 void OnAccountInfoReady(mojom::AccountInfoPtr account_info) override {
63 account_info_ = std::move(account_info);
64 base::ResetAndReturn(&done_closure_).Run();
65 }
66
67 void RequestAccountInfo(base::Closure done_closure) {
68 done_closure_ = done_closure;
69 host_->RequestAccountInfo();
70 }
71
72 mojom::AccountInfo* account_info() { return account_info_.get(); }
73
74 private:
75 mojom::AuthHostPtr host_;
76 mojom::AccountInfoPtr account_info_;
77 base::Closure done_closure_;
78 };
79
80 class ArcAuthServiceTest : public InProcessBrowserTest {
81 protected:
82 ArcAuthServiceTest() = default;
83
84 // InProcessBrowserTest:
85 ~ArcAuthServiceTest() override = default;
86
87 void SetUpCommandLine(base::CommandLine* command_line) override {
88 InProcessBrowserTest::SetUpCommandLine(command_line);
89 arc::SetArcAvailableCommandLineForTesting(command_line);
90 }
91
92 void SetUpOnMainThread() override {
93 user_manager_enabler_ =
94 base::MakeUnique<chromeos::ScopedUserManagerEnabler>(
95 new chromeos::FakeChromeUserManager());
96 // Init ArcSessionManager for testing.
97 ArcSessionManager::DisableUIForTesting();
98 ArcAuthNotification::DisableForTesting();
99 ArcSessionManager::EnableCheckAndroidManagementForTesting();
100 ArcSessionManager::Get()->SetArcSessionRunnerForTesting(
101 base::MakeUnique<ArcSessionRunner>(base::Bind(FakeArcSession::Create)));
102
103 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
104
105 // Create test profile.
106 TestingProfile::Builder profile_builder;
107 profile_builder.SetPath(temp_dir_.GetPath().AppendASCII("TestArcProfile"));
108 profile_builder.SetProfileName(kFakeUserName);
109
110 profile_builder.AddTestingFactory(
111 ProfileOAuth2TokenServiceFactory::GetInstance(),
112 BuildFakeProfileOAuth2TokenService);
113 profile_builder.AddTestingFactory(SigninManagerFactory::GetInstance(),
114 BuildFakeSigninManagerBase);
115
116 profile_ = profile_builder.Build();
117
118 FakeProfileOAuth2TokenService* token_service =
119 static_cast<FakeProfileOAuth2TokenService*>(
120 ProfileOAuth2TokenServiceFactory::GetForProfile(profile()));
121 token_service->UpdateCredentials(kFakeUserName, kRefreshToken);
122 token_service->set_auto_post_fetch_response_on_message_loop(true);
123
124 FakeSigninManagerBase* signin_manager = static_cast<FakeSigninManagerBase*>(
125 SigninManagerFactory::GetForProfile(profile()));
126 signin_manager->SetAuthenticatedAccountInfo(kFakeGaiaId, kFakeUserName);
127
128 profile()->GetPrefs()->SetBoolean(prefs::kArcSignedIn, true);
129 profile()->GetPrefs()->SetBoolean(prefs::kArcTermsAccepted, true);
130
131 const AccountId account_id(
132 AccountId::FromUserEmailGaiaId(kFakeUserName, kFakeGaiaId));
133 GetFakeUserManager()->AddUser(account_id);
134 GetFakeUserManager()->LoginUser(account_id);
135 GetFakeUserManager()->CreateLocalState();
136
137 // Set up ARC for test profile.
138 // Currently, ArcSessionManager is singleton and set up with the original
139 // Profile instance. This re-initializes the ArcServiceLauncher by
140 // overwriting Profile with profile().
141 // TODO(hidehiko): This way several ArcService instances created with
142 // the original Profile instance on Browser creatuion are kept in the
143 // ArcServiceManager. For proper overwriting, those should be removed.
144 ArcServiceLauncher::Get()->OnPrimaryUserProfilePrepared(profile());
145
146 // It is non-trivial to navigate through the merge session in a testing
147 // context; currently we just skip it.
148 // TODO(blundell): Figure out how to enable this flow.
149 ArcSessionManager::Get()->auth_context()->SkipMergeSessionForTesting();
150
151 user_manager::known_user::SetDeviceId(
152 multi_user_util::GetAccountIdFromProfile(profile()), "dummy");
153 }
154
155 void TearDownOnMainThread() override {
156 // Explicitly removing the user is required; otherwise ProfileHelper keeps
157 // a dangling pointer to the User.
158 // TODO(nya): Consider removing all users from ProfileHelper in the
159 // destructor of FakeChromeUserManager.
160 const AccountId account_id(
161 AccountId::FromUserEmailGaiaId(kFakeUserName, kFakeGaiaId));
162 GetFakeUserManager()->RemoveUserFromList(account_id);
163 // Since ArcServiceLauncher is (re-)set up with profile() in
164 // SetUpOnMainThread() it is necessary to Shutdown() before the profile()
165 // is destroyed. ArcServiceLauncher::Shutdown() will be called again on
166 // fixture destruction (because it is initialized with the original Profile
167 // instance in fixture, once), but it should be no op.
168 // TODO(hidehiko): Think about a way to test the code cleanly.
169 ArcServiceLauncher::Get()->Shutdown();
170 profile_.reset();
171 user_manager_enabler_.reset();
172 }
173
174 chromeos::FakeChromeUserManager* GetFakeUserManager() const {
175 return static_cast<chromeos::FakeChromeUserManager*>(
176 user_manager::UserManager::Get());
177 }
178
179 void EnableArc() {
180 PrefService* const prefs = profile()->GetPrefs();
khmel 2017/05/12 15:52:28 nit: Please use arc:SetArcPlayStoreEnabledForProfi
blundell 2017/05/15 08:32:32 Turns out this function isn't even needed in this
181 prefs->SetBoolean(prefs::kArcEnabled, true);
182 base::RunLoop().RunUntilIdle();
183 }
184
185 void set_profile_name(const std::string& username) {
186 profile_->set_profile_name(username);
187 }
188
189 Profile* profile() { return profile_.get(); }
190
191 private:
192 std::unique_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_;
193 base::ScopedTempDir temp_dir_;
194 std::unique_ptr<TestingProfile> profile_;
195
196 DISALLOW_COPY_AND_ASSIGN(ArcAuthServiceTest);
197 };
198
199 // Tests that when ARC++ requests account info for a non-managed account and
Luis Héctor Chávez 2017/05/12 15:45:56 nit: s/ARC++/ARC/
blundell 2017/05/15 08:32:32 Done.
200 // the system is in silent authentication mode, Chrome supplies the info
201 // configured in SetUpOnMainThread() above.
202 IN_PROC_BROWSER_TEST_F(ArcAuthServiceTest, SuccessfulBackgroundFetch) {
203 ASSERT_TRUE(base::FeatureList::IsEnabled(arc::kArcUseAuthEndpointFeature));
204
205 net::FakeURLFetcherFactory factory(nullptr);
206 factory.SetFakeResponse(
207 GURL(ArcBackgroundAuthCodeFetcher::GetTokenExchangeEndpointForTesting()),
208 "{ \"token\" : \"" + std::string(kFakeAuthCode) + "\" }", net::HTTP_OK,
209 net::URLRequestStatus::SUCCESS);
210
211 FakeAuthInstance auth_instance;
212 ArcAuthService* auth_service =
213 ArcServiceManager::GetGlobalService<ArcAuthService>();
214 ASSERT_TRUE(auth_service);
215
216 ArcBridgeService* arc_bridge_service =
217 ArcServiceManager::Get()->arc_bridge_service();
218 ASSERT_TRUE(arc_bridge_service);
219 arc_bridge_service->auth()->SetInstance(&auth_instance);
220
221 base::RunLoop run_loop;
222 auth_instance.RequestAccountInfo(run_loop.QuitClosure());
223 run_loop.Run();
224
225 EXPECT_TRUE(auth_instance.account_info());
226 EXPECT_EQ(kFakeUserName, auth_instance.account_info()->account_name.value());
227 EXPECT_EQ(kFakeAuthCode, auth_instance.account_info()->auth_code.value());
228 EXPECT_EQ(mojom::ChromeAccountType::USER_ACCOUNT,
229 auth_instance.account_info()->account_type);
230 EXPECT_FALSE(auth_instance.account_info()->enrollment_token);
231 EXPECT_FALSE(auth_instance.account_info()->is_managed);
232 }
233
234 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698