1 // Copyright 2019 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 "chrome/browser/supervised_user/logged_in_user_mixin.h"
6 
7 #include <vector>
8 
9 #include "chromeos/constants/chromeos_switches.h"
10 #include "chromeos/login/auth/stub_authenticator_builder.h"
11 #include "chromeos/login/auth/user_context.h"
12 #include "components/account_id/account_id.h"
13 #include "net/dns/mock_host_resolver.h"
14 
15 namespace chromeos {
16 
17 namespace {
18 
ConvertUserType(LoggedInUserMixin::LogInType type)19 user_manager::UserType ConvertUserType(LoggedInUserMixin::LogInType type) {
20   switch (type) {
21     case LoggedInUserMixin::LogInType::kChild:
22       return user_manager::USER_TYPE_CHILD;
23     case LoggedInUserMixin::LogInType::kRegular:
24       return user_manager::USER_TYPE_REGULAR;
25   }
26 }
27 
GetInitialUsers(const LoginManagerMixin::TestUserInfo & user,bool include_initial_user)28 std::vector<LoginManagerMixin::TestUserInfo> GetInitialUsers(
29     const LoginManagerMixin::TestUserInfo& user,
30     bool include_initial_user) {
31   if (include_initial_user)
32     return {user};
33   return {};
34 }
35 
36 }  // namespace
37 
LoggedInUserMixin(InProcessBrowserTestMixinHost * mixin_host,LogInType type,net::EmbeddedTestServer * embedded_test_server,InProcessBrowserTest * test_base,bool should_launch_browser,base::Optional<AccountId> account_id,bool include_initial_user,bool use_local_policy_server)38 LoggedInUserMixin::LoggedInUserMixin(
39     InProcessBrowserTestMixinHost* mixin_host,
40     LogInType type,
41     net::EmbeddedTestServer* embedded_test_server,
42     InProcessBrowserTest* test_base,
43     bool should_launch_browser,
44     base::Optional<AccountId> account_id,
45     bool include_initial_user,
46     bool use_local_policy_server)
47     : InProcessBrowserTestMixin(mixin_host),
48       user_(account_id.value_or(
49                 AccountId::FromUserEmailGaiaId(FakeGaiaMixin::kFakeUserEmail,
50                                                FakeGaiaMixin::kFakeUserGaiaId)),
51             ConvertUserType(type)),
52       login_manager_(mixin_host, GetInitialUsers(user_, include_initial_user)),
53       local_policy_server_(mixin_host),
54       user_policy_(mixin_host,
55                    user_.account_id,
56                    use_local_policy_server ? &local_policy_server_ : nullptr),
57       user_policy_helper_(user_.account_id.GetUserEmail(),
58                           &local_policy_server_),
59       embedded_test_server_setup_(mixin_host, embedded_test_server),
60       fake_gaia_(mixin_host, embedded_test_server),
61       test_base_(test_base) {
62   // By default, LoginManagerMixin will set up user session manager not to
63   // launch browser as part of user session setup - use this to override that
64   // behavior.
65   login_manager_.set_should_launch_browser(should_launch_browser);
66 }
67 
68 LoggedInUserMixin::~LoggedInUserMixin() = default;
69 
SetUpOnMainThread()70 void LoggedInUserMixin::SetUpOnMainThread() {
71   // By default, browser tests block anything that doesn't go to localhost, so
72   // account.google.com requests would never reach fake GAIA server without
73   // this.
74   test_base_->host_resolver()->AddRule("*", "127.0.0.1");
75 }
76 
LogInUser(bool issue_any_scope_token,bool wait_for_active_session,bool request_policy_update)77 void LoggedInUserMixin::LogInUser(bool issue_any_scope_token,
78                                   bool wait_for_active_session,
79                                   bool request_policy_update) {
80   UserContext user_context = LoginManagerMixin::CreateDefaultUserContext(user_);
81   user_context.SetRefreshToken(FakeGaiaMixin::kFakeRefreshToken);
82   if (user_.user_type == user_manager::USER_TYPE_CHILD) {
83     fake_gaia_.SetupFakeGaiaForChildUser(
84         user_.account_id.GetUserEmail(), user_.account_id.GetGaiaId(),
85         FakeGaiaMixin::kFakeRefreshToken, issue_any_scope_token);
86   } else {
87     fake_gaia_.SetupFakeGaiaForLogin(user_.account_id.GetUserEmail(),
88                                      user_.account_id.GetGaiaId(),
89                                      FakeGaiaMixin::kFakeRefreshToken);
90   }
91   if (request_policy_update) {
92     // Set up policy, which prevents the call to LoginAndWaitForActiveSession()
93     // below from hanging indefinitely in some test scenarios.
94     GetUserPolicyMixin()->RequestPolicyUpdate();
95   }
96   if (wait_for_active_session) {
97     login_manager_.LoginAndWaitForActiveSession(user_context);
98     // Set the private |browser_| member in InProcessBrowserTest.
99     // Otherwise calls to InProcessBrowserTest::browser() returns null and leads
100     // to segmentation faults.
101     // Note: |browser_| is only non-null if should_launch_browser was set to
102     // true in the constructor.
103     test_base_->SelectFirstBrowser();
104   } else {
105     login_manager_.AttemptLoginUsingAuthenticator(
106         user_context, std::make_unique<StubAuthenticatorBuilder>(user_context));
107   }
108 }
109 
110 }  // namespace chromeos
111