Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/command_line.h" | |
| 6 #include "base/files/file_path.h" | |
| 7 #include "base/files/file_util.h" | |
| 8 #include "base/json/json_writer.h" | |
| 9 #include "base/values.h" | |
| 10 #include "chrome/browser/chrome_notification_types.h" | |
| 11 #include "chrome/browser/chromeos/login/ui/webui_login_display.h" | |
| 12 #include "chrome/browser/chromeos/login/wizard_controller.h" | |
| 13 #include "chrome/browser/chromeos/policy/login_policy_test_base.h" | |
| 14 #include "chrome/browser/policy/test/local_policy_test_server.h" | |
| 15 #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" | |
| 19 #include "content/public/test/test_utils.h" | |
| 20 #include "google_apis/gaia/fake_gaia.h" | |
| 21 #include "google_apis/gaia/gaia_constants.h" | |
| 22 #include "google_apis/gaia/gaia_urls.h" | |
| 23 #include "testing/gtest/include/gtest/gtest.h" | |
| 24 #include "url/gurl.h" | |
| 25 | |
| 26 namespace policy { | |
| 27 | |
| 28 namespace { | |
| 29 | |
| 30 const char kTestAuthCode[] = "fake-auth-code"; | |
| 31 const char kTestGaiaUberToken[] = "fake-uber-token"; | |
| 32 const char kTestAuthLoginAccessToken[] = "fake-access-token"; | |
| 33 const char kTestRefreshToken[] = "fake-refresh-token"; | |
| 34 const char kTestAuthSIDCookie[] = "fake-auth-SID-cookie"; | |
| 35 const char kTestAuthLSIDCookie[] = "fake-auth-LSID-cookie"; | |
| 36 const char kTestSessionSIDCookie[] = "fake-session-SID-cookie"; | |
| 37 const char kTestSessionLSIDCookie[] = "fake-session-LSID-cookie"; | |
| 38 const char kTestUserinfoToken[] = "fake-userinfo-token"; | |
| 39 | |
| 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 jsonPolicy; | |
| 58 base::JSONWriter::WriteWithOptions( | |
| 59 &root_dict, base::JSONWriter::OPTIONS_PRETTY_PRINT, &jsonPolicy); | |
| 60 return jsonPolicy; | |
| 61 } | |
| 62 | |
| 63 } // namespace | |
| 64 | |
| 65 const char LoginPolicyTestBase::kAccountPassword[] = "letmein"; | |
| 66 const char LoginPolicyTestBase::kAccountId[] = "user@example.com"; | |
| 67 | |
| 68 LoginPolicyTestBase::LoginPolicyTestBase() { | |
| 69 set_open_about_blank_on_browser_launch(false); | |
| 70 } | |
| 71 | |
| 72 LoginPolicyTestBase::~LoginPolicyTestBase() { | |
| 73 } | |
| 74 | |
| 75 void LoginPolicyTestBase::SetUp() { | |
| 76 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 77 SetServerPolicy(); | |
| 78 | |
| 79 test_server_.reset(new LocalPolicyTestServer(PolicyFilePath())); | |
| 80 ASSERT_TRUE(test_server_->Start()); | |
| 81 | |
| 82 OobeBaseTest::SetUp(); | |
| 83 } | |
| 84 | |
| 85 void LoginPolicyTestBase::SetUpCommandLine(base::CommandLine* command_line) { | |
| 86 command_line->AppendSwitchASCII(policy::switches::kDeviceManagementUrl, | |
| 87 test_server_->GetServiceURL().spec()); | |
| 88 OobeBaseTest::SetUpCommandLine(command_line); | |
| 89 } | |
| 90 | |
| 91 void LoginPolicyTestBase::SetUpOnMainThread() { | |
| 92 SetMergeSessionParams(kAccountId); | |
| 93 SetUpGaiaServerWithAccessTokens(); | |
| 94 OobeBaseTest::SetUpOnMainThread(); | |
| 95 } | |
| 96 | |
| 97 scoped_ptr<base::DictionaryValue> | |
| 98 LoginPolicyTestBase::GetMandatoryPoliciesValue() const { | |
| 99 return scoped_ptr<base::DictionaryValue>(new base::DictionaryValue); | |
|
bartfab (slow)
2015/04/07 11:37:17
Nit: You can use make_scoped_ptr() to make this a
peletskyi
2015/04/07 13:14:29
Done.
| |
| 100 } | |
| 101 | |
| 102 scoped_ptr<base::DictionaryValue> | |
| 103 LoginPolicyTestBase::GetRecommendedPoliciesValue() const { | |
| 104 return scoped_ptr<base::DictionaryValue>(new base::DictionaryValue); | |
|
bartfab (slow)
2015/04/07 11:37:17
Nit: You can use make_scoped_ptr() to make this a
peletskyi
2015/04/07 13:14:29
Done.
| |
| 105 } | |
| 106 | |
| 107 void LoginPolicyTestBase::SetUpGaiaServerWithAccessTokens() { | |
| 108 FakeGaia::AccessTokenInfo token_info; | |
| 109 token_info.token = kTestUserinfoToken; | |
| 110 token_info.scopes.insert(GaiaConstants::kDeviceManagementServiceOAuth); | |
| 111 token_info.scopes.insert(GaiaConstants::kOAuthWrapBridgeUserInfoScope); | |
| 112 token_info.audience = GaiaUrls::GetInstance()->oauth2_chrome_client_id(); | |
| 113 token_info.email = kAccountId; | |
| 114 fake_gaia_->IssueOAuthToken(kTestRefreshToken, token_info); | |
| 115 } | |
| 116 | |
| 117 void LoginPolicyTestBase::SetMergeSessionParams(const std::string& email) { | |
| 118 FakeGaia::MergeSessionParams params; | |
| 119 params.auth_sid_cookie = kTestAuthSIDCookie; | |
| 120 params.auth_lsid_cookie = kTestAuthLSIDCookie; | |
| 121 params.auth_code = kTestAuthCode; | |
| 122 params.refresh_token = kTestRefreshToken; | |
| 123 params.access_token = kTestAuthLoginAccessToken; | |
| 124 params.gaia_uber_token = kTestGaiaUberToken; | |
| 125 params.session_sid_cookie = kTestSessionSIDCookie; | |
| 126 params.session_lsid_cookie = kTestSessionLSIDCookie; | |
| 127 params.email = email; | |
| 128 fake_gaia_->SetMergeSessionParams(params); | |
| 129 } | |
| 130 | |
| 131 void LoginPolicyTestBase::SkipToLoginScreen() { | |
| 132 chromeos::WizardController::SkipPostLoginScreensForTesting(); | |
| 133 chromeos::WizardController* const wizard_controller = | |
| 134 chromeos::WizardController::default_controller(); | |
| 135 ASSERT_TRUE(wizard_controller); | |
| 136 wizard_controller->SkipToLoginForTesting(chromeos::LoginScreenContext()); | |
| 137 | |
| 138 content::WindowedNotificationObserver( | |
| 139 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, | |
| 140 content::NotificationService::AllSources()).Wait(); | |
| 141 } | |
| 142 | |
| 143 void LoginPolicyTestBase::LogIn(const std::string& user_id, | |
| 144 const std::string& password) { | |
| 145 GetLoginDisplay()->ShowSigninScreenForCreds(user_id, password); | |
| 146 | |
| 147 content::WindowedNotificationObserver( | |
| 148 chrome::NOTIFICATION_SESSION_STARTED, | |
| 149 content::NotificationService::AllSources()).Wait(); | |
| 150 } | |
| 151 | |
| 152 void LoginPolicyTestBase::SetServerPolicy() { | |
| 153 const std::string policy = | |
| 154 GetPolicy(GetMandatoryPoliciesValue(), GetRecommendedPoliciesValue(), | |
| 155 dm_protocol::kChromeUserPolicyType, kAccountId); | |
| 156 | |
| 157 const int bytes_written = | |
| 158 base::WriteFile(PolicyFilePath(), policy.data(), policy.size()); | |
| 159 ASSERT_EQ(static_cast<int>(policy.size()), bytes_written); | |
| 160 } | |
| 161 | |
| 162 base::FilePath LoginPolicyTestBase::PolicyFilePath() const { | |
| 163 return temp_dir_.path().AppendASCII("policy.json"); | |
| 164 } | |
| 165 | |
| 166 } // namespace policy | |
| OLD | NEW |