summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/policy/consumer_unenrollment_handler_unittest.cc
blob: cacf39604d8b261a5425e82c7f1767e091b7fb2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2014 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/chromeos/policy/consumer_unenrollment_handler.h"

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/memory/scoped_ptr.h"
#include "base/thread_task_runner_handle.h"
#include "chrome/browser/chromeos/ownership/fake_owner_settings_service.h"
#include "chrome/browser/chromeos/policy/consumer_management_service.h"
#include "chrome/browser/chromeos/policy/consumer_management_stage.h"
#include "chrome/browser/chromeos/policy/device_cloud_policy_store_chromeos.h"
#include "chrome/browser/chromeos/policy/enterprise_install_attributes.h"
#include "chrome/browser/chromeos/policy/fake_consumer_management_service.h"
#include "chrome/browser/chromeos/policy/fake_device_cloud_policy_manager.h"
#include "chrome/browser/chromeos/settings/device_settings_service.h"
#include "chrome/browser/chromeos/settings/device_settings_test_helper.h"
#include "chrome/test/base/testing_profile.h"
#include "chromeos/dbus/fake_cryptohome_client.h"
#include "components/ownership/mock_owner_key_util.h"
#include "policy/proto/device_management_backend.pb.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace policy {

class ConsumerUnenrollmentHandlerTest
    : public chromeos::DeviceSettingsTestBase {
 public:
  ConsumerUnenrollmentHandlerTest()
      : fake_service_(new FakeConsumerManagementService()),
        fake_cryptohome_client_(new chromeos::FakeCryptohomeClient()),
        install_attributes_(
            new EnterpriseInstallAttributes(fake_cryptohome_client_.get())) {
    // Set up FakeConsumerManagementService.
    fake_service_->SetStatusAndStage(
        ConsumerManagementService::STATUS_ENROLLED,
        ConsumerManagementStage::None());
  }

  void SetUp() override {
    DeviceSettingsTestBase::SetUp();

    // Set up the ownership, so that we can modify device settings.
    owner_key_util_->SetPrivateKey(device_policy_.GetSigningKey());
    InitOwner(device_policy_.policy_data().username(), true);
    FlushDeviceSettings();


    // Set up FakeDeviceCloudPolicyManager.
    scoped_ptr<DeviceCloudPolicyStoreChromeOS> store_(
        new DeviceCloudPolicyStoreChromeOS(
            &device_settings_service_,
            install_attributes_.get(),
            base::ThreadTaskRunnerHandle::Get()));
    fake_manager_.reset(new FakeDeviceCloudPolicyManager(
        store_.Pass(),
        base::ThreadTaskRunnerHandle::Get()));

    // Set up FakeOwnerSettingsService.
    fake_owner_settings_service_.reset(
        new chromeos::FakeOwnerSettingsService(
            profile_.get(), owner_key_util_));
    chromeos::OwnerSettingsServiceChromeOS::ManagementSettings settings;
    settings.management_mode = policy::MANAGEMENT_MODE_CONSUMER_MANAGED;
    settings.request_token = "fake_request_token";
    settings.device_id = "fake_device_id";
    fake_owner_settings_service_->SetManagementSettings(
        settings,
        base::Bind(&ConsumerUnenrollmentHandlerTest::OnManagementSettingsSet,
                   base::Unretained(this)));
  }

  void OnManagementSettingsSet(bool success) {
    EXPECT_TRUE(success);
  }

  void RunUnenrollment() {
    handler_.reset(new ConsumerUnenrollmentHandler(
        &device_settings_service_,
        fake_service_.get(),
        fake_manager_.get(),
        fake_owner_settings_service_.get()));
    handler_->Start();
    FlushDeviceSettings();
  }

  scoped_ptr<FakeConsumerManagementService> fake_service_;
  scoped_ptr<chromeos::FakeCryptohomeClient> fake_cryptohome_client_;
  scoped_ptr<EnterpriseInstallAttributes> install_attributes_;
  scoped_ptr<FakeDeviceCloudPolicyManager> fake_manager_;
  scoped_ptr<chromeos::FakeOwnerSettingsService> fake_owner_settings_service_;

  scoped_ptr<ConsumerUnenrollmentHandler> handler_;
};

TEST_F(ConsumerUnenrollmentHandlerTest, UnenrollmentSucceeds) {
  EXPECT_EQ(ConsumerManagementStage::None(), fake_service_->GetStage());

  RunUnenrollment();

  EXPECT_EQ(ConsumerManagementStage::UnenrollmentSuccess(),
            fake_service_->GetStage());
  const chromeos::OwnerSettingsServiceChromeOS::ManagementSettings& settings =
      fake_owner_settings_service_->last_settings();
  EXPECT_EQ(policy::MANAGEMENT_MODE_LOCAL_OWNER, settings.management_mode);
  EXPECT_EQ("", settings.request_token);
  EXPECT_EQ("", settings.device_id);
}

TEST_F(ConsumerUnenrollmentHandlerTest,
       UnenrollmentFailsOnServerError) {
  EXPECT_EQ(ConsumerManagementStage::None(), fake_service_->GetStage());
  fake_manager_->set_unregister_result(false);

  RunUnenrollment();

  EXPECT_EQ(ConsumerManagementStage::UnenrollmentDMServerFailed(),
            fake_service_->GetStage());
  const chromeos::OwnerSettingsServiceChromeOS::ManagementSettings& settings =
      fake_owner_settings_service_->last_settings();
  EXPECT_EQ(policy::MANAGEMENT_MODE_CONSUMER_MANAGED, settings.management_mode);
  EXPECT_EQ("fake_request_token", settings.request_token);
  EXPECT_EQ("fake_device_id", settings.device_id);
}

}  // namespace policy