1 // Copyright 2013 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.
5 #include "chrome/browser/chromeos/login/login_manager_test.h"
10 #include "base/bind_helpers.h"
11 #include "base/command_line.h"
12 #include "base/files/file_path.h"
13 #include "base/path_service.h"
14 #include "base/prefs/scoped_user_pref_update.h"
15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/chrome_notification_types.h"
17 #include "chrome/browser/chromeos/login/existing_user_controller.h"
18 #include "chrome/browser/chromeos/login/session/user_session_manager.h"
19 #include "chrome/browser/chromeos/login/session/user_session_manager_test_api.h"
20 #include "chrome/browser/chromeos/login/ui/login_display_host_impl.h"
21 #include "chrome/browser/chromeos/login/ui/webui_login_view.h"
22 #include "chrome/common/chrome_paths.h"
23 #include "chromeos/chromeos_switches.h"
24 #include "chromeos/login/auth/key.h"
25 #include "chromeos/login/auth/user_context.h"
26 #include "components/user_manager/user.h"
27 #include "components/user_manager/user_manager.h"
28 #include "content/public/browser/notification_service.h"
29 #include "content/public/browser/web_contents.h"
30 #include "content/public/test/test_utils.h"
31 #include "google_apis/gaia/gaia_constants.h"
32 #include "google_apis/gaia/gaia_switches.h"
33 #include "google_apis/gaia/gaia_urls.h"
34 #include "net/dns/mock_host_resolver.h"
35 #include "net/test/embedded_test_server/embedded_test_server.h"
36 #include "net/test/embedded_test_server/http_request.h"
37 #include "net/test/embedded_test_server/http_response.h"
38 #include "testing/gtest/include/gtest/gtest.h"
44 const char kGAIAHost
[] = "accounts.google.com";
45 const char kTestUserinfoToken1
[] = "fake-userinfo-token-1";
46 const char kTestRefreshToken1
[] = "fake-refresh-token-1";
47 const char kTestUserinfoToken2
[] = "fake-userinfo-token-2";
48 const char kTestRefreshToken2
[] = "fake-refresh-token-2";
50 UserContext
CreateUserContext(const std::string
& user_id
) {
51 UserContext
user_context(user_id
);
52 user_context
.SetGaiaID(LoginManagerTest::GetGaiaIDForUserID(user_id
));
53 user_context
.SetKey(Key("password"));
54 if (user_id
== LoginManagerTest::kEnterpriseUser1
) {
55 user_context
.SetRefreshToken(kTestRefreshToken1
);
56 } else if (user_id
== LoginManagerTest::kEnterpriseUser2
) {
57 user_context
.SetRefreshToken(kTestRefreshToken2
);
64 const char LoginManagerTest::kEnterpriseUser1
[] = "user-1@example.com";
65 const char LoginManagerTest::kEnterpriseUser2
[] = "user-2@example.com";
67 LoginManagerTest::LoginManagerTest(bool should_launch_browser
)
68 : should_launch_browser_(should_launch_browser
), web_contents_(NULL
) {
69 set_exit_when_last_browser_closes(false);
72 LoginManagerTest::~LoginManagerTest() {}
74 void LoginManagerTest::SetUp() {
75 base::FilePath test_data_dir
;
76 PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
);
77 embedded_test_server()->ServeFilesFromDirectory(test_data_dir
);
79 embedded_test_server()->RegisterRequestHandler(
80 base::Bind(&FakeGaia::HandleRequest
, base::Unretained(&fake_gaia_
)));
82 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
84 // Start https wrapper here so that the URLs can be pointed at it in
85 // SetUpCommandLine().
86 ASSERT_TRUE(gaia_https_forwarder_
.Initialize(
87 kGAIAHost
, embedded_test_server()->base_url()));
89 // Stop IO thread here because no threads are allowed while
90 // spawning sandbox host process. See crbug.com/322732.
91 embedded_test_server()->StopThread();
93 MixinBasedBrowserTest::SetUp();
96 void LoginManagerTest::TearDownOnMainThread() {
97 MixinBasedBrowserTest::TearDownOnMainThread();
98 if (LoginDisplayHostImpl::default_host())
99 LoginDisplayHostImpl::default_host()->Finalize();
100 base::MessageLoop::current()->RunUntilIdle();
101 EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete());
104 void LoginManagerTest::SetUpCommandLine(base::CommandLine
* command_line
) {
105 command_line
->AppendSwitch(chromeos::switches::kLoginManager
);
106 command_line
->AppendSwitch(chromeos::switches::kForceLoginManagerInTests
);
108 const GURL gaia_url
= gaia_https_forwarder_
.GetURLForSSLHost(std::string());
109 command_line
->AppendSwitchASCII(::switches::kGaiaUrl
, gaia_url
.spec());
110 command_line
->AppendSwitchASCII(::switches::kLsoUrl
, gaia_url
.spec());
111 command_line
->AppendSwitchASCII(::switches::kGoogleApisUrl
, gaia_url
.spec());
113 fake_gaia_
.Initialize();
114 fake_gaia_
.set_issue_oauth_code_cookie(true);
116 MixinBasedBrowserTest::SetUpCommandLine(command_line
);
119 void LoginManagerTest::SetUpInProcessBrowserTestFixture() {
120 host_resolver()->AddRule("*", "127.0.0.1");
121 MixinBasedBrowserTest::SetUpInProcessBrowserTestFixture();
124 void LoginManagerTest::SetUpOnMainThread() {
125 // Restart the thread as the sandbox host process has already been spawned.
126 embedded_test_server()->RestartThreadAndListen();
128 FakeGaia::AccessTokenInfo token_info
;
129 token_info
.scopes
.insert(GaiaConstants::kDeviceManagementServiceOAuth
);
130 token_info
.scopes
.insert(GaiaConstants::kOAuthWrapBridgeUserInfoScope
);
131 token_info
.audience
= GaiaUrls::GetInstance()->oauth2_chrome_client_id();
133 token_info
.token
= kTestUserinfoToken1
;
134 token_info
.email
= kEnterpriseUser1
;
135 fake_gaia_
.IssueOAuthToken(kTestRefreshToken1
, token_info
);
137 token_info
.token
= kTestUserinfoToken2
;
138 token_info
.email
= kEnterpriseUser2
;
139 fake_gaia_
.IssueOAuthToken(kTestRefreshToken2
, token_info
);
141 content::WindowedNotificationObserver(
142 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE
,
143 content::NotificationService::AllSources()).Wait();
144 InitializeWebContents();
145 test::UserSessionManagerTestApi
session_manager_test_api(
146 UserSessionManager::GetInstance());
147 session_manager_test_api
.SetShouldLaunchBrowserInTests(
148 should_launch_browser_
);
149 session_manager_test_api
.SetShouldObtainTokenHandleInTests(false);
151 MixinBasedBrowserTest::SetUpOnMainThread();
154 void LoginManagerTest::RegisterUser(const std::string
& user_id
) {
155 ListPrefUpdate
users_pref(g_browser_process
->local_state(), "LoggedInUsers");
156 users_pref
->AppendIfNotPresent(new base::StringValue(user_id
));
159 void LoginManagerTest::SetExpectedCredentials(const UserContext
& user_context
) {
160 test::UserSessionManagerTestApi
session_manager_test_api(
161 UserSessionManager::GetInstance());
162 session_manager_test_api
.InjectStubUserContext(user_context
);
165 bool LoginManagerTest::TryToLogin(const UserContext
& user_context
) {
166 if (!AddUserToSession(user_context
))
168 if (const user_manager::User
* active_user
=
169 user_manager::UserManager::Get()->GetActiveUser())
170 return active_user
->email() == user_context
.GetUserID();
174 bool LoginManagerTest::AddUserToSession(const UserContext
& user_context
) {
175 ExistingUserController
* controller
=
176 ExistingUserController::current_controller();
181 content::WindowedNotificationObserver
observer(
182 chrome::NOTIFICATION_SESSION_STARTED
,
183 content::NotificationService::AllSources());
184 controller
->Login(user_context
, SigninSpecifics());
186 const user_manager::UserList
& logged_users
=
187 user_manager::UserManager::Get()->GetLoggedInUsers();
188 for (user_manager::UserList::const_iterator it
= logged_users
.begin();
189 it
!= logged_users
.end();
191 if ((*it
)->email() == user_context
.GetUserID())
197 void LoginManagerTest::LoginUser(const std::string
& user_id
) {
198 const UserContext user_context
= CreateUserContext(user_id
);
199 SetExpectedCredentials(user_context
);
200 EXPECT_TRUE(TryToLogin(user_context
));
203 void LoginManagerTest::AddUser(const std::string
& user_id
) {
204 const UserContext user_context
= CreateUserContext(user_id
);
205 SetExpectedCredentials(user_context
);
206 EXPECT_TRUE(AddUserToSession(user_context
));
210 std::string
LoginManagerTest::GetGaiaIDForUserID(const std::string
& user_id
) {
211 return "gaia-id-" + user_id
;
214 void LoginManagerTest::JSExpect(const std::string
& expression
) {
215 js_checker_
.ExpectTrue(expression
);
218 void LoginManagerTest::InitializeWebContents() {
219 LoginDisplayHost
* host
= LoginDisplayHostImpl::default_host();
220 EXPECT_TRUE(host
!= NULL
);
222 content::WebContents
* web_contents
=
223 host
->GetWebUILoginView()->GetWebContents();
224 EXPECT_TRUE(web_contents
!= NULL
);
225 set_web_contents(web_contents
);
226 js_checker_
.set_web_contents(web_contents
);
229 } // namespace chromeos