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