| // Copyright 2013 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "chrome/browser/ash/login/login_manager_test.h" |
| |
| #include <string> |
| |
| #include "ash/components/login/auth/key.h" |
| #include "ash/components/login/auth/user_context.h" |
| #include "ash/constants/ash_switches.h" |
| #include "ash/metrics/login_unlock_throughput_recorder.h" |
| #include "ash/shell.h" |
| #include "base/bind.h" |
| #include "base/callback_helpers.h" |
| #include "base/command_line.h" |
| #include "base/containers/contains.h" |
| #include "chrome/browser/ash/login/existing_user_controller.h" |
| #include "chrome/browser/ash/login/session/user_session_manager.h" |
| #include "chrome/browser/ash/login/session/user_session_manager_test_api.h" |
| #include "chrome/browser/ash/login/test/fake_gaia_mixin.h" |
| #include "chrome/browser/ash/login/test/profile_prepared_waiter.h" |
| #include "chrome/browser/ash/login/ui/login_display_host_webui.h" |
| #include "chrome/browser/browser_process.h" |
| #include "components/account_id/account_id.h" |
| #include "components/prefs/scoped_user_pref_update.h" |
| #include "components/user_manager/known_user.h" |
| #include "components/user_manager/user.h" |
| #include "components/user_manager/user_manager.h" |
| #include "net/dns/mock_host_resolver.h" |
| #include "net/test/embedded_test_server/embedded_test_server.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| |
| namespace ash { |
| |
| LoginManagerTest::LoginManagerTest() { |
| set_exit_when_last_browser_closes(false); |
| } |
| |
| LoginManagerTest::~LoginManagerTest() {} |
| |
| void LoginManagerTest::SetUpCommandLine(base::CommandLine* command_line) { |
| command_line->AppendSwitch(switches::kLoginManager); |
| command_line->AppendSwitch(switches::kForceLoginManagerInTests); |
| command_line->AppendSwitch( |
| switches::kDisableOOBEChromeVoxHintTimerForTesting); |
| |
| MixinBasedInProcessBrowserTest::SetUpCommandLine(command_line); |
| } |
| |
| void LoginManagerTest::SetUpOnMainThread() { |
| LoginDisplayHostWebUI::DisableRestrictiveProxyCheckForTest(); |
| |
| host_resolver()->AddRule("*", "127.0.0.1"); |
| |
| test::UserSessionManagerTestApi session_manager_test_api( |
| UserSessionManager::GetInstance()); |
| session_manager_test_api.SetShouldLaunchBrowserInTests( |
| should_launch_browser_); |
| session_manager_test_api.SetShouldObtainTokenHandleInTests(false); |
| |
| MixinBasedInProcessBrowserTest::SetUpOnMainThread(); |
| } |
| |
| void LoginManagerTest::RegisterUser(const AccountId& account_id) { |
| ListPrefUpdateDeprecated users_pref(g_browser_process->local_state(), |
| "LoggedInUsers"); |
| base::Value email_value(account_id.GetUserEmail()); |
| if (!base::Contains(users_pref->GetList(), email_value)) |
| users_pref->Append(std::move(email_value)); |
| if (user_manager::UserManager::IsInitialized()) { |
| user_manager::known_user::SaveKnownUser(account_id); |
| user_manager::UserManager::Get()->SaveUserOAuthStatus( |
| account_id, user_manager::User::OAUTH2_TOKEN_STATUS_VALID); |
| } |
| } |
| |
| constexpr char LoginManagerTest::kPassword[] = "password"; |
| |
| UserContext LoginManagerTest::CreateUserContext(const AccountId& account_id, |
| const std::string& password) { |
| UserContext user_context(user_manager::UserType::USER_TYPE_REGULAR, |
| account_id); |
| user_context.SetKey(Key(password)); |
| user_context.SetPasswordKey(Key(password)); |
| if (account_id.GetUserEmail() == FakeGaiaMixin::kEnterpriseUser1) { |
| user_context.SetRefreshToken(FakeGaiaMixin::kTestRefreshToken1); |
| } else if (account_id.GetUserEmail() == FakeGaiaMixin::kEnterpriseUser2) { |
| user_context.SetRefreshToken(FakeGaiaMixin::kTestRefreshToken2); |
| } |
| return user_context; |
| } |
| |
| void LoginManagerTest::SetExpectedCredentials(const UserContext& user_context) { |
| test::UserSessionManagerTestApi session_manager_test_api( |
| UserSessionManager::GetInstance()); |
| session_manager_test_api.InjectStubUserContext(user_context); |
| } |
| |
| bool LoginManagerTest::TryToLogin(const UserContext& user_context) { |
| if (!AddUserToSession(user_context)) |
| return false; |
| if (const user_manager::User* active_user = |
| user_manager::UserManager::Get()->GetActiveUser()) |
| return active_user->GetAccountId() == user_context.GetAccountId(); |
| return false; |
| } |
| |
| bool LoginManagerTest::AddUserToSession(const UserContext& user_context) { |
| ExistingUserController* controller = |
| ExistingUserController::current_controller(); |
| if (!controller) { |
| ADD_FAILURE(); |
| return false; |
| } |
| test::ProfilePreparedWaiter profile_prepared(user_context.GetAccountId()); |
| controller->Login(user_context, SigninSpecifics()); |
| profile_prepared.Wait(); |
| const user_manager::UserList& logged_users = |
| user_manager::UserManager::Get()->GetLoggedInUsers(); |
| for (user_manager::UserList::const_iterator it = logged_users.begin(); |
| it != logged_users.end(); ++it) { |
| if ((*it)->GetAccountId() == user_context.GetAccountId()) |
| return true; |
| } |
| return false; |
| } |
| |
| void LoginManagerTest::LoginUser(const AccountId& account_id) { |
| const UserContext user_context = CreateUserContext(account_id, kPassword); |
| SetExpectedCredentials(user_context); |
| EXPECT_TRUE(TryToLogin(user_context)); |
| } |
| |
| void LoginManagerTest::AddUser(const AccountId& account_id) { |
| const UserContext user_context = CreateUserContext(account_id, kPassword); |
| SetExpectedCredentials(user_context); |
| EXPECT_TRUE(AddUserToSession(user_context)); |
| } |
| |
| } // namespace ash |