Refactor Extension API test for policy based APIs
Move common helper functions to separate utils file.
This CL will allow not to duplicate setup code for policy based APIs tests.
Bug: 992889
Change-Id: Ie2655454199c660bcf9b9ac61b7d5c5157d6f975
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1792169
Commit-Queue: Nikita Podguzov <[email protected]>
Reviewed-by: Devlin <[email protected]>
Reviewed-by: Alexander Hendrich <[email protected]>
Cr-Commit-Position: refs/heads/master@{#696883}
diff --git a/chrome/browser/extensions/policy_test_utils.cc b/chrome/browser/extensions/policy_test_utils.cc
new file mode 100644
index 0000000..8771b4ae
--- /dev/null
+++ b/chrome/browser/extensions/policy_test_utils.cc
@@ -0,0 +1,90 @@
+// Copyright 2019 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/extensions/policy_test_utils.h"
+
+#include "base/files/file_util.h"
+#include "base/path_service.h"
+#include "base/values.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/common/chrome_paths.h"
+#include "components/policy/core/common/mock_configuration_policy_provider.h"
+#include "components/policy/policy_constants.h"
+#include "extensions/browser/extension_registry.h"
+#include "extensions/browser/test_extension_registry_observer.h"
+#include "net/test/embedded_test_server/embedded_test_server.h"
+#include "net/test/embedded_test_server/http_request.h"
+#include "net/test/embedded_test_server/http_response.h"
+#include "url/gurl.h"
+
+namespace extensions {
+
+namespace policy_test_utils {
+
+namespace {
+
+constexpr char kFileNameToIntercept[] = "update_manifest.xml";
+
+// Replace "mock.http" with "127.0.0.1:<port>" on "update_manifest.xml" files.
+// Host resolver doesn't work here because the test file doesn't know the
+// correct port number.
+std::unique_ptr<net::test_server::HttpResponse> InterceptMockHttp(
+ net::EmbeddedTestServer* embedded_test_server,
+ const net::test_server::HttpRequest& request) {
+ if (request.GetURL().ExtractFileName() != kFileNameToIntercept)
+ return nullptr;
+
+ base::FilePath test_data_dir;
+ base::PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
+ // Remove the leading '/'.
+ std::string relative_manifest_path = request.GetURL().path().substr(1);
+ std::string manifest_response;
+ CHECK(base::ReadFileToString(test_data_dir.Append(relative_manifest_path),
+ &manifest_response));
+
+ base::ReplaceSubstringsAfterOffset(
+ &manifest_response, 0, "mock.http",
+ embedded_test_server->host_port_pair().ToString());
+
+ auto response = std::make_unique<net::test_server::BasicHttpResponse>();
+ response->set_content_type("text/xml");
+ response->set_content(manifest_response);
+ return response;
+}
+
+} // namespace
+
+void SetUpEmbeddedTestServer(net::EmbeddedTestServer* embedded_test_server) {
+ embedded_test_server->RegisterRequestHandler(
+ base::BindRepeating(&InterceptMockHttp, embedded_test_server));
+}
+
+void SetExtensionInstallForcelistPolicy(
+ const ExtensionId& extension_id,
+ const GURL& update_manifest_url,
+ Profile* profile,
+ policy::MockConfigurationPolicyProvider* policy_provider) {
+ // Extensions that are force-installed come from an update URL, which defaults
+ // to the webstore. Use a mock URL for test with an update manifest that
+ // includes the crx file of the test extension.
+ base::Value forcelist(base::Value::Type::LIST);
+ forcelist.Append(base::StringPrintf("%s;%s", extension_id.c_str(),
+ update_manifest_url.spec().c_str()));
+
+ policy::PolicyMap policy;
+ policy.Set(policy::key::kExtensionInstallForcelist,
+ policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_MACHINE,
+ policy::POLICY_SOURCE_CLOUD,
+ base::Value::ToUniquePtrValue(std::move(forcelist)), nullptr);
+
+ // Set the policy and wait until the extension is installed.
+ extensions::TestExtensionRegistryObserver observer(
+ ExtensionRegistry::Get(profile));
+ policy_provider->UpdateChromePolicy(policy);
+ observer.WaitForExtensionLoaded();
+}
+
+} // namespace policy_test_utils
+
+} // namespace extensions