Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / enterprise_device_attributes / enterprise_device_attributes_apitest.cc
blob0896dfaa3515b0d4bffcdcdf346039b5b08bb131
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.
5 #include "base/strings/stringprintf.h"
6 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
7 #include "chrome/browser/chromeos/policy/device_policy_cros_browser_test.h"
8 #include "chrome/browser/chromeos/policy/stub_enterprise_install_attributes.h"
9 #include "chrome/browser/extensions/extension_apitest.h"
10 #include "chrome/browser/net/url_request_mock_util.h"
11 #include "chromeos/dbus/fake_session_manager_client.h"
12 #include "chromeos/login/user_names.h"
13 #include "components/policy/core/common/mock_configuration_policy_provider.h"
14 #include "components/policy/core/common/policy_types.h"
15 #include "content/public/browser/notification_service.h"
16 #include "content/public/test/test_utils.h"
17 #include "extensions/browser/api_test_utils.h"
18 #include "extensions/browser/extension_registry.h"
19 #include "extensions/browser/test_extension_registry_observer.h"
20 #include "net/test/url_request/url_request_mock_http_job.h"
21 #include "policy/policy_constants.h"
23 namespace {
25 const char kDeviceId[] = "device_id";
26 const base::FilePath::CharType kTestExtensionDir[] =
27 FILE_PATH_LITERAL("extensions/api_test/enterprise_device_attributes");
28 const base::FilePath::CharType kUpdateManifestFileName[] =
29 FILE_PATH_LITERAL("update_manifest.xml");
31 // The managed_storage extension has a key defined in its manifest, so that
32 // its extension ID is well-known and the policy system can push policies for
33 // the extension.
34 const char kTestExtensionID[] = "nbiliclbejdndfpchgkbmfoppjplbdok";
36 } // namespace
38 namespace extensions {
40 class EnterpriseDeviceAttributesTest : public ExtensionApiTest {
41 public:
42 explicit EnterpriseDeviceAttributesTest(const std::string& domain)
43 : fake_session_manager_client_(new chromeos::FakeSessionManagerClient),
44 test_domain_(domain) {}
46 protected:
47 void SetUpInProcessBrowserTestFixture() override {
48 chromeos::DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient(
49 make_scoped_ptr(fake_session_manager_client_));
50 ExtensionApiTest::SetUpInProcessBrowserTestFixture();
52 // Set up fake install attributes.
53 scoped_ptr<policy::StubEnterpriseInstallAttributes> attributes(
54 new policy::StubEnterpriseInstallAttributes());
56 attributes->SetDomain(test_domain_);
57 attributes->SetRegistrationUser(chromeos::login::kStubUser);
58 policy::BrowserPolicyConnectorChromeOS::SetInstallAttributesForTesting(
59 attributes.release());
61 test_helper_.InstallOwnerKey();
62 // Init the device policy.
63 policy::DevicePolicyBuilder* device_policy = test_helper_.device_policy();
64 device_policy->SetDefaultSigningKey();
65 device_policy->policy_data().set_directory_api_id(kDeviceId);
66 device_policy->Build();
68 fake_session_manager_client_->set_device_policy(device_policy->GetBlob());
69 fake_session_manager_client_->OnPropertyChangeComplete(true);
71 // Init the user policy provider.
72 EXPECT_CALL(policy_provider_, IsInitializationComplete(testing::_))
73 .WillRepeatedly(testing::Return(true));
74 policy_provider_.SetAutoRefresh();
75 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(
76 &policy_provider_);
79 void SetUpOnMainThread() override {
80 ExtensionApiTest::SetUpOnMainThread();
82 // Enable the URLRequestMock, which is required for force-installing the
83 // test extension through policy.
84 content::BrowserThread::PostTask(
85 content::BrowserThread::IO, FROM_HERE,
86 base::Bind(chrome_browser_net::SetUrlRequestMocksEnabled, true));
88 SetPolicy();
91 private:
92 void SetPolicy() {
93 // Extensions that are force-installed come from an update URL, which
94 // defaults to the webstore. Use a mock URL for this test with an update
95 // manifest that includes the crx file of the test extension.
96 base::FilePath update_manifest_path =
97 base::FilePath(kTestExtensionDir).Append(kUpdateManifestFileName);
98 GURL update_manifest_url(
99 net::URLRequestMockHTTPJob::GetMockUrl(update_manifest_path));
101 scoped_ptr<base::ListValue> forcelist(new base::ListValue);
102 forcelist->AppendString(base::StringPrintf(
103 "%s;%s", kTestExtensionID, update_manifest_url.spec().c_str()));
105 policy::PolicyMap policy;
106 policy.Set(policy::key::kExtensionInstallForcelist,
107 policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_MACHINE,
108 policy::POLICY_SOURCE_CLOUD, forcelist.release(), nullptr);
110 // Set the policy and wait until the extension is installed.
111 extensions::TestExtensionRegistryObserver observer(
112 ExtensionRegistry::Get(profile()));
113 policy_provider_.UpdateChromePolicy(policy);
114 observer.WaitForExtensionLoaded();
117 chromeos::FakeSessionManagerClient* const fake_session_manager_client_;
118 policy::MockConfigurationPolicyProvider policy_provider_;
119 policy::DevicePolicyCrosTestHelper test_helper_;
120 const std::string test_domain_;
123 // Creates affiliated user before browser initializes.
124 class EnterpriseDeviceAttributesAffiliatedTest
125 : public EnterpriseDeviceAttributesTest {
126 public:
127 EnterpriseDeviceAttributesAffiliatedTest()
128 : EnterpriseDeviceAttributesTest("gmail.com") {}
131 // Creates non-affiliated user before browser init.
132 class EnterpriseDeviceAttributesNonAffiliatedTest
133 : public EnterpriseDeviceAttributesTest {
134 public:
135 EnterpriseDeviceAttributesNonAffiliatedTest()
136 : EnterpriseDeviceAttributesTest("example.com") {}
139 // Tests the case of an affiliated user and pre-installed extension. Fetches
140 // the valid cloud directory device id.
141 IN_PROC_BROWSER_TEST_F(EnterpriseDeviceAttributesAffiliatedTest, Success) {
142 // Pass the expected value (device_id) to test.
143 ASSERT_TRUE(RunExtensionSubtest(
144 "", base::StringPrintf("chrome-extension://%s/basic.html?%s",
145 kTestExtensionID, kDeviceId)))
146 << message_;
149 // Test the case of non-affiliated user and pre-installed by policy extension.
150 // Extension API is available, but fetches the empty string.
151 IN_PROC_BROWSER_TEST_F(EnterpriseDeviceAttributesNonAffiliatedTest,
152 EmptyString) {
153 // Pass the expected value (empty string) to test.
154 ASSERT_TRUE(RunExtensionSubtest(
155 "", base::StringPrintf("chrome-extension://%s/basic.html?%s",
156 kTestExtensionID, "")))
157 << message_;
160 // Ensure that extensions that are not pre-installed by policy throw an install
161 // warning if they request the enterprise.deviceAttributes permission in the
162 // manifest and that such extensions don't see the
163 // chrome.enterprise.deviceAttributes namespace.
164 IN_PROC_BROWSER_TEST_F(
165 ExtensionApiTest,
166 EnterpriseDeviceAttributesIsRestrictedToPolicyExtension) {
167 ASSERT_TRUE(RunExtensionSubtest("enterprise_device_attributes",
168 "api_not_available.html",
169 kFlagIgnoreManifestWarnings));
171 base::FilePath extension_path =
172 test_data_dir_.AppendASCII("enterprise_device_attributes");
173 extensions::ExtensionRegistry* registry =
174 extensions::ExtensionRegistry::Get(profile());
175 const extensions::Extension* extension =
176 GetExtensionByPath(registry->enabled_extensions(), extension_path);
177 ASSERT_FALSE(extension->install_warnings().empty());
178 EXPECT_EQ(
179 "'enterprise.deviceAttributes' is not allowed for specified install "
180 "location.",
181 extension->install_warnings()[0].message);
184 } // namespace extensions