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

Side by Side 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 unified diff | Download patch
OLDNEW
(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 "chrome/browser/chromeos/policy/user_policy_test_helper.h"
6
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/json/json_writer.h"
11 #include "base/run_loop.h"
12 #include "base/values.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h"
15 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chrom eos.h"
16 #include "chrome/browser/policy/profile_policy_connector.h"
17 #include "chrome/browser/policy/profile_policy_connector_factory.h"
18 #include "chrome/browser/policy/test/local_policy_test_server.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "components/policy/core/browser/browser_policy_connector.h"
21 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
22 #include "components/policy/core/common/policy_service.h"
23 #include "components/policy/core/common/policy_switches.h"
24 #include "policy/proto/device_management_backend.pb.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "url/gurl.h"
27
28 namespace policy {
29
30 namespace {
31
32 std::string BuildPolicy(const base::DictionaryValue& mandatory,
33 const base::DictionaryValue& recommended,
34 const std::string& policyType,
35 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.
36 scoped_ptr<base::DictionaryValue> policy_type_dict(new base::DictionaryValue);
37 policy_type_dict->SetWithoutPathExpansion("mandatory",
38 mandatory.CreateDeepCopy());
39 policy_type_dict->SetWithoutPathExpansion("recommended",
40 recommended.CreateDeepCopy());
41
42 scoped_ptr<base::ListValue> managed_users_list(new base::ListValue);
43 managed_users_list->AppendString("*");
44
45 base::DictionaryValue root_dict;
46 root_dict.SetWithoutPathExpansion(policyType, policy_type_dict.Pass());
47 root_dict.SetWithoutPathExpansion("managed_users", managed_users_list.Pass());
48 root_dict.SetStringWithoutPathExpansion("policy_user", account);
49 root_dict.SetIntegerWithoutPathExpansion("current_key_index", 0);
50
51 std::string json_policy;
52 base::JSONWriter::WriteWithOptions(
53 root_dict, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json_policy);
54 return json_policy;
55 }
56
57 } // namespace
58
59 UserPolicyTestHelper::UserPolicyTestHelper(const std::string& user_email)
60 : user_email_(user_email) {
61 }
62
63 UserPolicyTestHelper::~UserPolicyTestHelper() {
64 }
65
66 void UserPolicyTestHelper::Init(
67 const base::DictionaryValue& mandatory_policy,
68 const base::DictionaryValue& recommended_policy) {
69 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
70 WritePolicyFile(mandatory_policy, recommended_policy);
71
72 test_server_.reset(new LocalPolicyTestServer(PolicyFilePath()));
73 ASSERT_TRUE(test_server_->Start());
74 }
75
76 void UserPolicyTestHelper::UpdateCommandLine(
77 base::CommandLine* command_line) const {
78 command_line->AppendSwitchASCII(policy::switches::kDeviceManagementUrl,
79 test_server_->GetServiceURL().spec());
80 }
81
82 void UserPolicyTestHelper::WaitForInitialPolicy(Profile* profile) {
83 BrowserPolicyConnector* const connector =
84 g_browser_process->browser_policy_connector();
85 connector->ScheduleServiceInitialization(0);
86
87 UserCloudPolicyManagerChromeOS* const policy_manager =
88 UserCloudPolicyManagerFactoryChromeOS::GetForProfile(profile);
89 ASSERT_TRUE(policy_manager);
90
91 // Give a bogus OAuth token to the |policy_manager|. This should make its
92 // CloudPolicyClient fetch the DMToken.
93 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.
94 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.
95 const enterprise_management::DeviceRegisterRequest::Type registration_type =
96 enterprise_management::DeviceRegisterRequest::USER;
97 policy_manager->core()->client()->Register(
98 registration_type,
99 enterprise_management::DeviceRegisterRequest::FLAVOR_USER_REGISTRATION,
100 "bogus", std::string(), std::string(), std::string());
101
102 policy::ProfilePolicyConnector* const profile_connector =
103 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..)
104 policy::PolicyService* const policy_service =
105 profile_connector->policy_service();
106
107 base::RunLoop run_loop;
108 policy_service->RefreshPolicies(run_loop.QuitClosure());
109 run_loop.Run();
110 }
111
112 void UserPolicyTestHelper::UpdatePolicy(
113 const base::DictionaryValue& mandatory_policy,
114 const base::DictionaryValue& recommended_policy,
115 Profile* profile) {
116 WritePolicyFile(mandatory_policy, recommended_policy);
117
118 policy::ProfilePolicyConnector* const profile_connector =
119 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
120 policy::PolicyService* const policy_service =
121 profile_connector->policy_service();
122
123 base::RunLoop run_loop;
124 policy_service->RefreshPolicies(run_loop.QuitClosure());
125 run_loop.Run();
126 }
127
128 void UserPolicyTestHelper::WritePolicyFile(
129 const base::DictionaryValue& mandatory,
130 const base::DictionaryValue& recommended) {
131 const std::string policy = BuildPolicy(
132 mandatory, recommended, dm_protocol::kChromeUserPolicyType, user_email_);
133 const int bytes_written =
134 base::WriteFile(PolicyFilePath(), policy.data(), policy.size());
135 ASSERT_EQ(static_cast<int>(policy.size()), bytes_written);
136 }
137
138 base::FilePath UserPolicyTestHelper::PolicyFilePath() const {
139 return temp_dir_.path().AppendASCII("policy.json");
140 }
141
142 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698