Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(123)

Unified Diff: chrome/browser/chromeos/policy/user_policy_test_helper.cc

Issue 1185593002: Move reusable part of LoginPolicyTestBase into a helper class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@key_perm
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/chromeos/policy/user_policy_test_helper.cc
diff --git a/chrome/browser/chromeos/policy/user_policy_test_helper.cc b/chrome/browser/chromeos/policy/user_policy_test_helper.cc
new file mode 100644
index 0000000000000000000000000000000000000000..83f9c8c0de5b2f086706daaa7ee33bcb117867cd
--- /dev/null
+++ b/chrome/browser/chromeos/policy/user_policy_test_helper.cc
@@ -0,0 +1,142 @@
+// Copyright 2015 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/chromeos/policy/user_policy_test_helper.h"
+
+#include "base/command_line.h"
+#include "base/files/file_path.h"
+#include "base/files/file_util.h"
+#include "base/json/json_writer.h"
+#include "base/run_loop.h"
+#include "base/values.h"
+#include "chrome/browser/browser_process.h"
+#include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h"
+#include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chromeos.h"
+#include "chrome/browser/policy/profile_policy_connector.h"
+#include "chrome/browser/policy/profile_policy_connector_factory.h"
+#include "chrome/browser/policy/test/local_policy_test_server.h"
+#include "chrome/browser/profiles/profile.h"
+#include "components/policy/core/browser/browser_policy_connector.h"
+#include "components/policy/core/common/cloud/cloud_policy_constants.h"
+#include "components/policy/core/common/policy_service.h"
+#include "components/policy/core/common/policy_switches.h"
+#include "policy/proto/device_management_backend.pb.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "url/gurl.h"
+
+namespace policy {
+
+namespace {
+
+std::string BuildPolicy(const base::DictionaryValue& mandatory,
+ const base::DictionaryValue& recommended,
+ const std::string& policyType,
+ const std::string& account) {
bartfab (slow) 2015/06/15 17:12:27 Nit: s/account/account_id/ for consistency
pneubeck (no reviews) 2015/06/16 09:16:21 Done.
+ scoped_ptr<base::DictionaryValue> policy_type_dict(new base::DictionaryValue);
+ policy_type_dict->SetWithoutPathExpansion("mandatory",
+ mandatory.CreateDeepCopy());
+ policy_type_dict->SetWithoutPathExpansion("recommended",
+ recommended.CreateDeepCopy());
+
+ scoped_ptr<base::ListValue> managed_users_list(new base::ListValue);
+ managed_users_list->AppendString("*");
+
+ base::DictionaryValue root_dict;
+ root_dict.SetWithoutPathExpansion(policyType, policy_type_dict.Pass());
+ root_dict.SetWithoutPathExpansion("managed_users", managed_users_list.Pass());
+ root_dict.SetStringWithoutPathExpansion("policy_user", account);
+ root_dict.SetIntegerWithoutPathExpansion("current_key_index", 0);
+
+ std::string json_policy;
+ base::JSONWriter::WriteWithOptions(
+ root_dict, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json_policy);
+ return json_policy;
+}
+
+} // namespace
+
+UserPolicyTestHelper::UserPolicyTestHelper(const std::string& user_email)
+ : user_email_(user_email) {
+}
+
+UserPolicyTestHelper::~UserPolicyTestHelper() {
+}
+
+void UserPolicyTestHelper::Init(
+ const base::DictionaryValue& mandatory_policy,
+ const base::DictionaryValue& recommended_policy) {
+ ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
+ WritePolicyFile(mandatory_policy, recommended_policy);
+
+ test_server_.reset(new LocalPolicyTestServer(PolicyFilePath()));
+ ASSERT_TRUE(test_server_->Start());
+}
+
+void UserPolicyTestHelper::UpdateCommandLine(
+ base::CommandLine* command_line) const {
+ command_line->AppendSwitchASCII(policy::switches::kDeviceManagementUrl,
+ test_server_->GetServiceURL().spec());
+}
+
+void UserPolicyTestHelper::WaitForInitialPolicy(Profile* profile) {
+ BrowserPolicyConnector* const connector =
+ g_browser_process->browser_policy_connector();
+ connector->ScheduleServiceInitialization(0);
+
+ UserCloudPolicyManagerChromeOS* const policy_manager =
+ UserCloudPolicyManagerFactoryChromeOS::GetForProfile(profile);
+ ASSERT_TRUE(policy_manager);
+
+ // Give a bogus OAuth token to the |policy_manager|. This should make its
+ // CloudPolicyClient fetch the DMToken.
+ ASSERT_TRUE(policy_manager->core()->client());
bartfab (slow) 2015/06/15 17:12:27 Nit: #include "components/policy/core/common/cloud
pneubeck (no reviews) 2015/06/16 09:16:21 Removed this assert.
+ ASSERT_FALSE(policy_manager->core()->client()->is_registered());
bartfab (slow) 2015/06/15 17:12:27 Nit: #include "components/policy/core/common/cloud
pneubeck (no reviews) 2015/06/16 09:16:21 Done.
+ const enterprise_management::DeviceRegisterRequest::Type registration_type =
+ enterprise_management::DeviceRegisterRequest::USER;
+ policy_manager->core()->client()->Register(
+ registration_type,
+ enterprise_management::DeviceRegisterRequest::FLAVOR_USER_REGISTRATION,
+ "bogus", std::string(), std::string(), std::string());
+
+ policy::ProfilePolicyConnector* const profile_connector =
+ policy::ProfilePolicyConnectorFactory::GetForBrowserContext(profile);
bartfab (slow) 2015/06/15 17:12:27 Nit: Why do you ASSERT_TRUE() the |policy_manager|
pneubeck (no reviews) 2015/06/16 09:16:21 Removed the assert(..manager..)
+ policy::PolicyService* const policy_service =
+ profile_connector->policy_service();
+
+ base::RunLoop run_loop;
+ policy_service->RefreshPolicies(run_loop.QuitClosure());
+ run_loop.Run();
+}
+
+void UserPolicyTestHelper::UpdatePolicy(
+ const base::DictionaryValue& mandatory_policy,
+ const base::DictionaryValue& recommended_policy,
+ Profile* profile) {
+ WritePolicyFile(mandatory_policy, recommended_policy);
+
+ policy::ProfilePolicyConnector* const profile_connector =
+ policy::ProfilePolicyConnectorFactory::GetForBrowserContext(profile);
bartfab (slow) 2015/06/15 17:12:27 Nit: As above: Should this method ASSERT_TRUE() to
pneubeck (no reviews) 2015/06/16 09:16:21 I removed the asserts above, as I don't think that
+ policy::PolicyService* const policy_service =
+ profile_connector->policy_service();
+
+ base::RunLoop run_loop;
+ policy_service->RefreshPolicies(run_loop.QuitClosure());
+ run_loop.Run();
+}
+
+void UserPolicyTestHelper::WritePolicyFile(
+ const base::DictionaryValue& mandatory,
+ const base::DictionaryValue& recommended) {
+ const std::string policy = BuildPolicy(
+ mandatory, recommended, dm_protocol::kChromeUserPolicyType, user_email_);
+ const int bytes_written =
+ base::WriteFile(PolicyFilePath(), policy.data(), policy.size());
+ ASSERT_EQ(static_cast<int>(policy.size()), bytes_written);
+}
+
+base::FilePath UserPolicyTestHelper::PolicyFilePath() const {
+ return temp_dir_.path().AppendASCII("policy.json");
+}
+
+} // namespace policy

Powered by Google App Engine
This is Rietveld 408576698