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

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 reviews 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 {
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 set_profile_name(const std::string& username) {
180 profile_->set_profile_name(username);
181 }
182
183 Profile* profile() { return profile_.get(); }
184
185 private:
186 std::unique_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_;
187 base::ScopedTempDir temp_dir_;
188 std::unique_ptr<TestingProfile> profile_;
189
190 DISALLOW_COPY_AND_ASSIGN(ArcAuthServiceTest);
191 };
192
193 // Tests that when ARC requests account info for a non-managed account and
194 // the system is in silent authentication mode, Chrome supplies the info
195 // configured in SetUpOnMainThread() above.
196 IN_PROC_BROWSER_TEST_F(ArcAuthServiceTest, SuccessfulBackgroundFetch) {
197 ASSERT_TRUE(base::FeatureList::IsEnabled(arc::kArcUseAuthEndpointFeature));
198
199 net::FakeURLFetcherFactory factory(nullptr);
200 factory.SetFakeResponse(
201 GURL(arc::kAuthTokenExchangeEndPoint),
202 "{ \"token\" : \"" + std::string(kFakeAuthCode) + "\" }", net::HTTP_OK,
203 net::URLRequestStatus::SUCCESS);
204
205 FakeAuthInstance auth_instance;
206 ArcAuthService* auth_service =
207 ArcServiceManager::GetGlobalService<ArcAuthService>();
208 ASSERT_TRUE(auth_service);
209
210 ArcBridgeService* arc_bridge_service =
211 ArcServiceManager::Get()->arc_bridge_service();
212 ASSERT_TRUE(arc_bridge_service);
213 arc_bridge_service->auth()->SetInstance(&auth_instance);
214
215 base::RunLoop run_loop;
216 auth_instance.RequestAccountInfo(run_loop.QuitClosure());
217 run_loop.Run();
218
219 EXPECT_TRUE(auth_instance.account_info());
220 EXPECT_EQ(kFakeUserName, auth_instance.account_info()->account_name.value());
221 EXPECT_EQ(kFakeAuthCode, auth_instance.account_info()->auth_code.value());
222 EXPECT_EQ(mojom::ChromeAccountType::USER_ACCOUNT,
223 auth_instance.account_info()->account_type);
224 EXPECT_FALSE(auth_instance.account_info()->enrollment_token);
225 EXPECT_FALSE(auth_instance.account_info()->is_managed);
226 }
227
228 } // namespace arc
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/arc/arc_auth_service.cc ('k') | chrome/browser/chromeos/arc/auth/arc_background_auth_code_fetcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698