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

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: Addressed comments 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
« no previous file with comments | « chrome/browser/chromeos/policy/user_policy_test_helper.h ('k') | chrome/chrome_tests.gypi » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..ecc45eb27cec9effbaa581fafbdc55321a9e9515
--- /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_client.h"
+#include "components/policy/core/common/cloud/cloud_policy_constants.h"
+#include "components/policy/core/common/cloud/cloud_policy_core.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_id) {
+ 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_id);
+ 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& account_id)
+ : account_id_(account_id) {
+}
+
+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);
+
+ // Give a bogus OAuth token to the |policy_manager|. This should make its
+ // CloudPolicyClient fetch the DMToken.
+ ASSERT_FALSE(policy_manager->core()->client()->is_registered());
+ 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);
+ 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);
+ 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, account_id_);
+ 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
« no previous file with comments | « chrome/browser/chromeos/policy/user_policy_test_helper.h ('k') | chrome/chrome_tests.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698