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 |
| 14 namespace { |
| 15 const std::string kDeviceId = "device_id"; |
| 16 } // namespace |
| 17 |
| 18 namespace extensions { |
| 19 |
| 20 class EnterpriseDeviceAttributesTest : public ExtensionApiTest { |
| 21 public: |
| 22 explicit EnterpriseDeviceAttributesTest(const std::string& domain) |
| 23 : fake_session_manager_client_(new chromeos::FakeSessionManagerClient), |
| 24 test_domain_(domain), |
| 25 get_device_id_function_( |
| 26 new EnterpriseDeviceAttributesGetDirectoryDeviceIdFunction()) {} |
| 27 |
| 28 protected: |
| 29 void SetUpInProcessBrowserTestFixture() override { |
| 30 chromeos::DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient( |
| 31 make_scoped_ptr(fake_session_manager_client_)); |
| 32 ExtensionApiTest::SetUpInProcessBrowserTestFixture(); |
| 33 |
| 34 // Set up fake install attributes. |
| 35 scoped_ptr<policy::StubEnterpriseInstallAttributes> attributes( |
| 36 new policy::StubEnterpriseInstallAttributes()); |
| 37 |
| 38 attributes->SetDomain(test_domain_); |
| 39 attributes->SetRegistrationUser(chromeos::login::kStubUser); |
| 40 policy::BrowserPolicyConnectorChromeOS::SetInstallAttributesForTesting( |
| 41 attributes.release()); |
| 42 |
| 43 test_helper_.InstallOwnerKey(); |
| 44 // Init the device policy. |
| 45 policy::DevicePolicyBuilder* device_policy = test_helper_.device_policy(); |
| 46 device_policy->SetDefaultSigningKey(); |
| 47 device_policy->policy_data().set_directory_api_id(kDeviceId); |
| 48 device_policy->Build(); |
| 49 |
| 50 fake_session_manager_client_->set_device_policy(device_policy->GetBlob()); |
| 51 fake_session_manager_client_->OnPropertyChangeComplete(true); |
| 52 } |
| 53 |
| 54 void RunAffiliationTest(const std::string& expect_value) { |
| 55 scoped_ptr<base::Value> result( |
| 56 api_test_utils::RunFunctionAndReturnSingleResult( |
| 57 get_device_id_function_.get(), "[]", browser()->profile())); |
| 58 |
| 59 std::string value; |
| 60 EXPECT_TRUE(result->GetAsString(&value)); |
| 61 |
| 62 EXPECT_EQ(expect_value, value); |
| 63 } |
| 64 |
| 65 private: |
| 66 chromeos::FakeSessionManagerClient* const fake_session_manager_client_; |
| 67 policy::DevicePolicyCrosTestHelper test_helper_; |
| 68 const std::string test_domain_; |
| 69 const scoped_refptr<EnterpriseDeviceAttributesGetDirectoryDeviceIdFunction> |
| 70 get_device_id_function_; |
| 71 }; |
| 72 |
| 73 // Creates affiliated user before browser initializes. |
| 74 class EnterpriseDeviceAttributesAffiliatedTest |
| 75 : public EnterpriseDeviceAttributesTest { |
| 76 public: |
| 77 EnterpriseDeviceAttributesAffiliatedTest() |
| 78 : EnterpriseDeviceAttributesTest("gmail.com") {} |
| 79 }; |
| 80 |
| 81 // Creates non-affiliated user before browser init. |
| 82 class EnterpriseDeviceAttributesNonAffiliatedTest |
| 83 : public EnterpriseDeviceAttributesTest { |
| 84 public: |
| 85 EnterpriseDeviceAttributesNonAffiliatedTest() |
| 86 : EnterpriseDeviceAttributesTest("example.com") {} |
| 87 }; |
| 88 |
| 89 // Tests the case when the user is affiliated. |
| 90 // Expected result: successful device id fetch. |
| 91 IN_PROC_BROWSER_TEST_F(EnterpriseDeviceAttributesAffiliatedTest, Success) { |
| 92 RunAffiliationTest(kDeviceId); |
| 93 } |
| 94 |
| 95 // Tests the case when the user is not affiliated Expected result: fetch empty |
| 96 // string as a device id.. |
| 97 IN_PROC_BROWSER_TEST_F(EnterpriseDeviceAttributesNonAffiliatedTest, |
| 98 EmptyString) { |
| 99 RunAffiliationTest(""); |
| 100 } |
| 101 |
| 102 } // namespace extensions |
OLD | NEW |