| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/command_line.h" | 5 #include "chrome/browser/chromeos/policy/login_policy_test_base.h" |
| 6 #include "base/files/file_path.h" | 6 |
| 7 #include "base/files/file_util.h" | |
| 8 #include "base/json/json_writer.h" | |
| 9 #include "base/values.h" | 7 #include "base/values.h" |
| 10 #include "chrome/browser/chrome_notification_types.h" | 8 #include "chrome/browser/chrome_notification_types.h" |
| 11 #include "chrome/browser/chromeos/login/ui/webui_login_display.h" | 9 #include "chrome/browser/chromeos/login/ui/webui_login_display.h" |
| 12 #include "chrome/browser/chromeos/login/wizard_controller.h" | 10 #include "chrome/browser/chromeos/login/wizard_controller.h" |
| 13 #include "chrome/browser/chromeos/policy/login_policy_test_base.h" | 11 #include "chrome/browser/chromeos/policy/user_policy_test_helper.h" |
| 14 #include "chrome/browser/policy/test/local_policy_test_server.h" | |
| 15 #include "chrome/browser/ui/webui/chromeos/login/signin_screen_handler.h" | 12 #include "chrome/browser/ui/webui/chromeos/login/signin_screen_handler.h" |
| 16 #include "components/policy/core/common/cloud/cloud_policy_constants.h" | |
| 17 #include "components/policy/core/common/policy_switches.h" | |
| 18 #include "content/public/browser/notification_service.h" | 13 #include "content/public/browser/notification_service.h" |
| 19 #include "content/public/test/test_utils.h" | 14 #include "content/public/test/test_utils.h" |
| 20 #include "google_apis/gaia/fake_gaia.h" | 15 #include "google_apis/gaia/fake_gaia.h" |
| 21 #include "google_apis/gaia/gaia_constants.h" | 16 #include "google_apis/gaia/gaia_constants.h" |
| 22 #include "google_apis/gaia/gaia_urls.h" | 17 #include "google_apis/gaia/gaia_urls.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 24 #include "url/gurl.h" | |
| 25 | 19 |
| 26 namespace policy { | 20 namespace policy { |
| 27 | 21 |
| 28 namespace { | 22 namespace { |
| 29 | 23 |
| 30 const char kTestAuthCode[] = "fake-auth-code"; | 24 const char kTestAuthCode[] = "fake-auth-code"; |
| 31 const char kTestGaiaUberToken[] = "fake-uber-token"; | 25 const char kTestGaiaUberToken[] = "fake-uber-token"; |
| 32 const char kTestAuthLoginAccessToken[] = "fake-access-token"; | 26 const char kTestAuthLoginAccessToken[] = "fake-access-token"; |
| 33 const char kTestRefreshToken[] = "fake-refresh-token"; | 27 const char kTestRefreshToken[] = "fake-refresh-token"; |
| 34 const char kTestAuthSIDCookie[] = "fake-auth-SID-cookie"; | 28 const char kTestAuthSIDCookie[] = "fake-auth-SID-cookie"; |
| 35 const char kTestAuthLSIDCookie[] = "fake-auth-LSID-cookie"; | 29 const char kTestAuthLSIDCookie[] = "fake-auth-LSID-cookie"; |
| 36 const char kTestSessionSIDCookie[] = "fake-session-SID-cookie"; | 30 const char kTestSessionSIDCookie[] = "fake-session-SID-cookie"; |
| 37 const char kTestSessionLSIDCookie[] = "fake-session-LSID-cookie"; | 31 const char kTestSessionLSIDCookie[] = "fake-session-LSID-cookie"; |
| 38 const char kTestUserinfoToken[] = "fake-userinfo-token"; | 32 const char kTestUserinfoToken[] = "fake-userinfo-token"; |
| 39 | 33 |
| 40 std::string GetPolicy(scoped_ptr<base::DictionaryValue> mandatory, | |
| 41 scoped_ptr<base::DictionaryValue> recommended, | |
| 42 const std::string& policyType, | |
| 43 const std::string& account) { | |
| 44 scoped_ptr<base::DictionaryValue> policy_type_dict(new base::DictionaryValue); | |
| 45 policy_type_dict->Set("mandatory", mandatory.Pass()); | |
| 46 policy_type_dict->Set("recommended", recommended.Pass()); | |
| 47 | |
| 48 scoped_ptr<base::ListValue> managed_users_list(new base::ListValue); | |
| 49 managed_users_list->AppendString("*"); | |
| 50 | |
| 51 base::DictionaryValue root_dict; | |
| 52 root_dict.Set(policyType, policy_type_dict.Pass()); | |
| 53 root_dict.Set("managed_users", managed_users_list.Pass()); | |
| 54 root_dict.SetString("policy_user", account); | |
| 55 root_dict.SetInteger("current_key_index", 0); | |
| 56 | |
| 57 std::string json_policy; | |
| 58 base::JSONWriter::WriteWithOptions( | |
| 59 root_dict, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json_policy); | |
| 60 return json_policy; | |
| 61 } | |
| 62 | |
| 63 } // namespace | 34 } // namespace |
| 64 | 35 |
| 65 const char LoginPolicyTestBase::kAccountPassword[] = "letmein"; | 36 const char LoginPolicyTestBase::kAccountPassword[] = "letmein"; |
| 66 const char LoginPolicyTestBase::kAccountId[] = "user@example.com"; | 37 const char LoginPolicyTestBase::kAccountId[] = "user@example.com"; |
| 67 | 38 |
| 68 LoginPolicyTestBase::LoginPolicyTestBase() { | 39 LoginPolicyTestBase::LoginPolicyTestBase() { |
| 69 // TODO(nkostylev): Fix this test harness for webview. http://crbug.com/477402 | 40 // TODO(nkostylev): Fix this test harness for webview. http://crbug.com/477402 |
| 70 set_use_webview(false); | 41 set_use_webview(false); |
| 71 set_open_about_blank_on_browser_launch(false); | 42 set_open_about_blank_on_browser_launch(false); |
| 72 } | 43 } |
| 73 | 44 |
| 74 LoginPolicyTestBase::~LoginPolicyTestBase() { | 45 LoginPolicyTestBase::~LoginPolicyTestBase() { |
| 75 } | 46 } |
| 76 | 47 |
| 77 void LoginPolicyTestBase::SetUp() { | 48 void LoginPolicyTestBase::SetUp() { |
| 78 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 49 base::DictionaryValue mandatory; |
| 79 SetServerPolicy(); | 50 GetMandatoryPoliciesValue(&mandatory); |
| 80 | 51 base::DictionaryValue recommended; |
| 81 test_server_.reset(new LocalPolicyTestServer(PolicyFilePath())); | 52 GetRecommendedPoliciesValue(&recommended); |
| 82 ASSERT_TRUE(test_server_->Start()); | 53 user_policy_helper_.reset(new UserPolicyTestHelper(kAccountId)); |
| 83 | 54 user_policy_helper_->Init(mandatory, recommended); |
| 84 OobeBaseTest::SetUp(); | 55 OobeBaseTest::SetUp(); |
| 85 } | 56 } |
| 86 | 57 |
| 87 void LoginPolicyTestBase::SetUpCommandLine(base::CommandLine* command_line) { | 58 void LoginPolicyTestBase::SetUpCommandLine(base::CommandLine* command_line) { |
| 88 command_line->AppendSwitchASCII(policy::switches::kDeviceManagementUrl, | 59 user_policy_helper_->UpdateCommandLine(command_line); |
| 89 test_server_->GetServiceURL().spec()); | |
| 90 OobeBaseTest::SetUpCommandLine(command_line); | 60 OobeBaseTest::SetUpCommandLine(command_line); |
| 91 } | 61 } |
| 92 | 62 |
| 93 void LoginPolicyTestBase::SetUpOnMainThread() { | 63 void LoginPolicyTestBase::SetUpOnMainThread() { |
| 94 SetMergeSessionParams(kAccountId); | 64 SetMergeSessionParams(); |
| 95 SetUpGaiaServerWithAccessTokens(); | 65 SetUpGaiaServerWithAccessTokens(); |
| 96 OobeBaseTest::SetUpOnMainThread(); | 66 OobeBaseTest::SetUpOnMainThread(); |
| 97 } | 67 } |
| 98 | 68 |
| 99 scoped_ptr<base::DictionaryValue> | 69 void LoginPolicyTestBase::GetMandatoryPoliciesValue( |
| 100 LoginPolicyTestBase::GetMandatoryPoliciesValue() const { | 70 base::DictionaryValue* policy) const { |
| 101 return make_scoped_ptr(new base::DictionaryValue); | |
| 102 } | 71 } |
| 103 | 72 |
| 104 scoped_ptr<base::DictionaryValue> | 73 void LoginPolicyTestBase::GetRecommendedPoliciesValue( |
| 105 LoginPolicyTestBase::GetRecommendedPoliciesValue() const { | 74 base::DictionaryValue* policy) const { |
| 106 return make_scoped_ptr(new base::DictionaryValue); | |
| 107 } | 75 } |
| 108 | 76 |
| 109 void LoginPolicyTestBase::SetUpGaiaServerWithAccessTokens() { | 77 void LoginPolicyTestBase::SetUpGaiaServerWithAccessTokens() { |
| 110 FakeGaia::AccessTokenInfo token_info; | 78 FakeGaia::AccessTokenInfo token_info; |
| 111 token_info.token = kTestUserinfoToken; | 79 token_info.token = kTestUserinfoToken; |
| 112 token_info.scopes.insert(GaiaConstants::kDeviceManagementServiceOAuth); | 80 token_info.scopes.insert(GaiaConstants::kDeviceManagementServiceOAuth); |
| 113 token_info.scopes.insert(GaiaConstants::kOAuthWrapBridgeUserInfoScope); | 81 token_info.scopes.insert(GaiaConstants::kOAuthWrapBridgeUserInfoScope); |
| 114 token_info.audience = GaiaUrls::GetInstance()->oauth2_chrome_client_id(); | 82 token_info.audience = GaiaUrls::GetInstance()->oauth2_chrome_client_id(); |
| 115 token_info.email = kAccountId; | 83 token_info.email = kAccountId; |
| 116 fake_gaia_->IssueOAuthToken(kTestRefreshToken, token_info); | 84 fake_gaia_->IssueOAuthToken(kTestRefreshToken, token_info); |
| 117 } | 85 } |
| 118 | 86 |
| 119 void LoginPolicyTestBase::SetMergeSessionParams(const std::string& email) { | 87 void LoginPolicyTestBase::SetMergeSessionParams() { |
| 120 FakeGaia::MergeSessionParams params; | 88 FakeGaia::MergeSessionParams params; |
| 121 params.auth_sid_cookie = kTestAuthSIDCookie; | 89 params.auth_sid_cookie = kTestAuthSIDCookie; |
| 122 params.auth_lsid_cookie = kTestAuthLSIDCookie; | 90 params.auth_lsid_cookie = kTestAuthLSIDCookie; |
| 123 params.auth_code = kTestAuthCode; | 91 params.auth_code = kTestAuthCode; |
| 124 params.refresh_token = kTestRefreshToken; | 92 params.refresh_token = kTestRefreshToken; |
| 125 params.access_token = kTestAuthLoginAccessToken; | 93 params.access_token = kTestAuthLoginAccessToken; |
| 126 params.gaia_uber_token = kTestGaiaUberToken; | 94 params.gaia_uber_token = kTestGaiaUberToken; |
| 127 params.session_sid_cookie = kTestSessionSIDCookie; | 95 params.session_sid_cookie = kTestSessionSIDCookie; |
| 128 params.session_lsid_cookie = kTestSessionLSIDCookie; | 96 params.session_lsid_cookie = kTestSessionLSIDCookie; |
| 129 params.email = email; | 97 params.email = kAccountId; |
| 130 fake_gaia_->SetMergeSessionParams(params); | 98 fake_gaia_->SetMergeSessionParams(params); |
| 131 } | 99 } |
| 132 | 100 |
| 133 void LoginPolicyTestBase::SkipToLoginScreen() { | 101 void LoginPolicyTestBase::SkipToLoginScreen() { |
| 134 chromeos::WizardController::SkipPostLoginScreensForTesting(); | 102 chromeos::WizardController::SkipPostLoginScreensForTesting(); |
| 135 chromeos::WizardController* const wizard_controller = | 103 chromeos::WizardController* const wizard_controller = |
| 136 chromeos::WizardController::default_controller(); | 104 chromeos::WizardController::default_controller(); |
| 137 ASSERT_TRUE(wizard_controller); | 105 ASSERT_TRUE(wizard_controller); |
| 138 wizard_controller->SkipToLoginForTesting(chromeos::LoginScreenContext()); | 106 wizard_controller->SkipToLoginForTesting(chromeos::LoginScreenContext()); |
| 139 | 107 |
| 140 content::WindowedNotificationObserver( | 108 content::WindowedNotificationObserver( |
| 141 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, | 109 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, |
| 142 content::NotificationService::AllSources()).Wait(); | 110 content::NotificationService::AllSources()).Wait(); |
| 143 } | 111 } |
| 144 | 112 |
| 145 void LoginPolicyTestBase::LogIn(const std::string& user_id, | 113 void LoginPolicyTestBase::LogIn(const std::string& user_id, |
| 146 const std::string& password) { | 114 const std::string& password) { |
| 147 GetLoginDisplay()->ShowSigninScreenForCreds(user_id, password); | 115 GetLoginDisplay()->ShowSigninScreenForCreds(user_id, password); |
| 148 | 116 |
| 149 content::WindowedNotificationObserver( | 117 content::WindowedNotificationObserver( |
| 150 chrome::NOTIFICATION_SESSION_STARTED, | 118 chrome::NOTIFICATION_SESSION_STARTED, |
| 151 content::NotificationService::AllSources()).Wait(); | 119 content::NotificationService::AllSources()).Wait(); |
| 152 } | 120 } |
| 153 | 121 |
| 154 void LoginPolicyTestBase::SetServerPolicy() { | |
| 155 const std::string policy = | |
| 156 GetPolicy(GetMandatoryPoliciesValue(), GetRecommendedPoliciesValue(), | |
| 157 dm_protocol::kChromeUserPolicyType, kAccountId); | |
| 158 | |
| 159 const int bytes_written = | |
| 160 base::WriteFile(PolicyFilePath(), policy.data(), policy.size()); | |
| 161 ASSERT_EQ(static_cast<int>(policy.size()), bytes_written); | |
| 162 } | |
| 163 | |
| 164 base::FilePath LoginPolicyTestBase::PolicyFilePath() const { | |
| 165 return temp_dir_.path().AppendASCII("policy.json"); | |
| 166 } | |
| 167 | |
| 168 } // namespace policy | 122 } // namespace policy |
| OLD | NEW |