Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 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/browser_policy_connector_chromeos.h" | |
| 6 #include "chrome/browser/chromeos/policy/device_policy_cros_browser_test.h" | |
| 7 #include "chrome/browser/chromeos/policy/stub_enterprise_install_attributes.h" | |
| 8 #include "chrome/browser/extensions/api/enterprise_device_attributes/enterprise_ device_attributes_api.h" | |
| 9 #include "chrome/browser/extensions/extension_apitest.h" | |
| 10 #include "chromeos/dbus/fake_session_manager_client.h" | |
| 11 #include "chromeos/login/user_names.h" | |
| 12 #include "extensions/browser/api_test_utils.h" | |
| 13 #include "extensions/common/manifest_constants.h" | |
| 14 #include "extensions/common/permissions/permissions_info.h" | |
| 15 | |
| 16 namespace { | |
| 17 const std::string kDeviceId = "device_id"; | |
|
pneubeck (no reviews)
2015/07/23 12:53:22
nit: indentation is wrong (just run 'git cl format
Polina Bondarenko
2015/07/24 16:47:58
Done.
| |
| 18 } | |
| 19 | |
| 20 namespace extensions { | |
| 21 | |
| 22 // Base class for apitests - inits the testing enviroment init, provides API for | |
|
pneubeck (no reviews)
2015/07/23 12:53:22
this comment can be removed
instead rename it to
Polina Bondarenko
2015/07/24 16:47:59
Done.
| |
| 23 // testing. | |
| 24 class EnterpriseDeviceAttributesBase : public ExtensionApiTest { | |
| 25 public: | |
| 26 EnterpriseDeviceAttributesBase() | |
| 27 : fake_session_manager_client_(new chromeos::FakeSessionManagerClient) { | |
| 28 | |
|
pneubeck (no reviews)
2015/07/23 12:53:22
nit: remove empty line
Polina Bondarenko
2015/07/24 16:47:59
Done.
| |
| 29 } | |
| 30 | |
| 31 protected: | |
| 32 void SetUpInProcessBrowserTestFixture() override { | |
| 33 chromeos::DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient( | |
| 34 scoped_ptr<chromeos::SessionManagerClient>(fake_session_manager_client_)); | |
| 35 ExtensionApiTest::SetUpInProcessBrowserTestFixture(); | |
| 36 } | |
| 37 | |
| 38 // Inits the device policy and install attributes, should be invoked before | |
| 39 // browser init. | |
| 40 void InitTest(const std::string& domain) { | |
| 41 // Set up fake install attributes. | |
| 42 scoped_ptr<policy::StubEnterpriseInstallAttributes> attributes( | |
| 43 new policy::StubEnterpriseInstallAttributes()); | |
| 44 | |
| 45 attributes->SetDomain(domain); | |
| 46 attributes->SetRegistrationUser(chromeos::login::kStubUser); | |
| 47 policy::BrowserPolicyConnectorChromeOS::SetInstallAttributesForTesting( | |
| 48 attributes.release()); | |
| 49 | |
| 50 test_helper_.InstallOwnerKey(); | |
| 51 // Init the device policy. | |
| 52 policy::DevicePolicyBuilder* device_policy = test_helper_.device_policy(); | |
| 53 device_policy->SetDefaultSigningKey(); | |
| 54 device_policy->policy_data().set_directory_api_id(kDeviceId); | |
| 55 device_policy->Build(); | |
| 56 | |
| 57 fake_session_manager_client_->set_device_policy(device_policy->GetBlob()); | |
| 58 fake_session_manager_client_->OnPropertyChangeComplete(true); | |
| 59 } | |
| 60 | |
| 61 // Runs test with the expected directory device ID value and the extension | |
| 62 // location. | |
| 63 void RunTest(const std::string& expect_value, | |
| 64 const Manifest::Location location) { | |
| 65 // Init the testing extension function. | |
| 66 scoped_refptr<EnterpriseDeviceAttributesGetDirectoryDeviceIdFunction> | |
| 67 get_device_id_function( | |
| 68 new EnterpriseDeviceAttributesGetDirectoryDeviceIdFunction()); | |
| 69 | |
| 70 // |feature_full_name| = permission:<Extension API name> | |
| 71 // The get_device_id_function - is a permission feature. | |
|
pneubeck (no reviews)
2015/07/23 12:53:22
I don't understand this part of the comment
Polina Bondarenko
2015/07/24 16:47:59
Done.
| |
| 72 std::string feature_full_name = std::string("permission:") + | |
| 73 EnterpriseDeviceAttributesGetDirectoryDeviceIdFunction::function_name(); | |
| 74 feature_full_name.erase(feature_full_name.find_last_of('.'), | |
| 75 feature_full_name.size()); | |
| 76 get_device_id_function->set_name(feature_full_name.c_str()); | |
|
pneubeck (no reviews)
2015/07/23 12:53:22
as far as I understand, you're testing several ort
Polina Bondarenko
2015/07/24 16:47:58
Done.
| |
| 77 get_device_id_function->set_has_callback(true); | |
| 78 | |
| 79 // Init manifest for the extension. | |
| 80 base::DictionaryValue manifest; | |
| 81 manifest.SetString(manifest_keys::kVersion, "1.0.0.0"); | |
| 82 manifest.SetString(manifest_keys::kName, "test"); | |
| 83 base::ListValue* permission_list = new base::ListValue; | |
| 84 // Should have permission to use the testing extension API. | |
| 85 permission_list->Append( | |
| 86 new base::StringValue(PermissionsInfo::GetInstance()-> | |
| 87 GetByID(APIPermission::kEnterpriseDeviceAttributes)->name())); | |
| 88 manifest.Set(manifest_keys::kPermissions, permission_list); | |
| 89 | |
| 90 // Creates the extension with appropriate location and permissions. | |
| 91 scoped_refptr<extensions::Extension> extension = | |
| 92 api_test_utils::CreateExtension(location, &manifest, ""); | |
| 93 get_device_id_function->set_extension(extension); | |
| 94 | |
| 95 // Check if the extension has permissions to the testing function. | |
| 96 EXPECT_EQ(location == Manifest::EXTERNAL_POLICY, | |
| 97 get_device_id_function->HasPermission()); | |
| 98 | |
| 99 // Run the testing function and check result. | |
| 100 scoped_ptr<base::Value> result( | |
| 101 api_test_utils::RunFunctionAndReturnSingleResult( | |
| 102 get_device_id_function.get(), "[]", browser()->profile())); | |
| 103 | |
| 104 std::string value; | |
| 105 EXPECT_TRUE(result->GetAsString(&value)); | |
| 106 EXPECT_EQ(expect_value, value); | |
| 107 } | |
| 108 | |
| 109 private: | |
| 110 chromeos::FakeSessionManagerClient* fake_session_manager_client_; | |
| 111 policy::DevicePolicyCrosTestHelper test_helper_; | |
| 112 }; | |
| 113 | |
| 114 // Creates affiliated user before browser init. | |
|
pneubeck (no reviews)
2015/07/23 12:53:22
nit: s/init/initializes/
Polina Bondarenko
2015/07/24 16:47:59
Done.
| |
| 115 class EnterpriseDeviceAttributesAffiliatedTest : | |
| 116 public EnterpriseDeviceAttributesBase { | |
| 117 protected: | |
| 118 void SetUpInProcessBrowserTestFixture() override { | |
| 119 EnterpriseDeviceAttributesBase::SetUpInProcessBrowserTestFixture(); | |
| 120 | |
| 121 InitTest(kDomain); | |
| 122 } | |
| 123 private: | |
| 124 const std::string kDomain = "gmail.com"; | |
|
pneubeck (no reviews)
2015/07/23 12:53:22
could you instead pass this to the Base constructo
Polina Bondarenko
2015/07/24 16:47:58
Done.
| |
| 125 }; | |
| 126 | |
| 127 // Creates non-affiliated user before browser init. | |
|
pneubeck (no reviews)
2015/07/23 12:53:22
nit: s/init/initializes/
Polina Bondarenko
2015/07/24 16:47:58
Done.
| |
| 128 class EnterpriseDeviceAttributesNonAffiliatedTest : | |
| 129 public EnterpriseDeviceAttributesBase { | |
| 130 protected: | |
| 131 void SetUpInProcessBrowserTestFixture() override { | |
| 132 EnterpriseDeviceAttributesBase::SetUpInProcessBrowserTestFixture(); | |
| 133 | |
| 134 InitTest(kDomain); | |
| 135 } | |
| 136 private: | |
| 137 const std::string kDomain = "example.com"; | |
| 138 }; | |
| 139 | |
| 140 // Tests the case when the user is affiliated and the extension has been | |
|
pneubeck (no reviews)
2015/07/23 12:53:22
better describe what the rule or fact is that you
Polina Bondarenko
2015/07/24 16:47:59
Done.
| |
| 141 // force installed by policy. | |
| 142 IN_PROC_BROWSER_TEST_F(EnterpriseDeviceAttributesAffiliatedTest, Success) { | |
| 143 RunTest(kDeviceId, Manifest::EXTERNAL_POLICY); | |
| 144 } | |
| 145 | |
| 146 // Tests the case when the user is affiliated and the extension is the part of | |
| 147 // external component of Chrome OS. | |
| 148 IN_PROC_BROWSER_TEST_F(EnterpriseDeviceAttributesAffiliatedTest, Failure) { | |
| 149 RunTest(kDeviceId, Manifest::EXTERNAL_COMPONENT); | |
|
pneubeck (no reviews)
2015/07/23 12:53:22
why component?
you might instead want to ensure th
Polina Bondarenko
2015/07/24 16:47:58
Done.
| |
| 150 } | |
| 151 | |
| 152 // Tests the case when the user is non affiliated and the extension has been | |
| 153 // force installed by policy. | |
| 154 IN_PROC_BROWSER_TEST_F(EnterpriseDeviceAttributesNonAffiliatedTest, | |
| 155 EmptyString) { | |
| 156 RunTest("", Manifest::EXTERNAL_POLICY); | |
| 157 } | |
| 158 | |
| 159 // Tests the case when the user is non affiliated and the extension is the part | |
| 160 // of an integrat component of Chrome. | |
| 161 IN_PROC_BROWSER_TEST_F(EnterpriseDeviceAttributesNonAffiliatedTest, Failure) { | |
| 162 RunTest("", Manifest::COMPONENT); | |
| 163 } | |
| 164 } // namespace extensions | |
| OLD | NEW |