summaryrefslogtreecommitdiff
path: root/chromium/components/policy/test_support/request_handler_for_device_state_retrieval_unittest.cc
blob: edc202ad5f4eb6a273475fca762da8b20930387a (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
// Copyright 2021 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 "base/strings/strcat.h"
#include "components/policy/core/common/cloud/cloud_policy_constants.h"
#include "components/policy/test_support/client_storage.h"
#include "components/policy/test_support/embedded_policy_test_server_test_base.h"
#include "components/policy/test_support/policy_storage.h"
#include "components/policy/test_support/request_handler_for_register_browser.h"
#include "device_management_backend.pb.h"
#include "net/http/http_status_code.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace em = enterprise_management;

namespace policy {

namespace {

constexpr char kDeviceId[] = "fake_device_id";
constexpr char kStateKey1[] = "fake_state_key_1";
constexpr char kStateKey2[] = "fake_state_key_2";
constexpr char kManagementDomain[] = "example.com";

}  // namespace

class RequestHandlerForDeviceStateRetrievalTest
    : public EmbeddedPolicyTestServerTestBase {
 protected:
  RequestHandlerForDeviceStateRetrievalTest() = default;
  ~RequestHandlerForDeviceStateRetrievalTest() override = default;

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

    SetRequestTypeParam(dm_protocol::kValueRequestDeviceStateRetrieval);
    SetAppType(dm_protocol::kValueAppType);
    SetDeviceIdParam(kDeviceId);
    SetDeviceType(dm_protocol::kValueDeviceType);
  }
};

TEST_F(RequestHandlerForDeviceStateRetrievalTest, HandleRequest) {
  ClientStorage::ClientInfo client_info;
  client_info.device_id = kDeviceId;
  client_info.state_keys.push_back(kStateKey1);
  client_info.state_keys.push_back(kStateKey2);
  client_storage()->RegisterClient(client_info);
  policy_storage()->set_device_state(PolicyStorage::DeviceState{
      .management_domain = kManagementDomain,
      .restore_mode = enterprise_management::DeviceStateRetrievalResponse::
          RESTORE_MODE_REENROLLMENT_ZERO_TOUCH});

  em::DeviceManagementRequest device_management_request;
  em::DeviceStateRetrievalRequest* request =
      device_management_request.mutable_device_state_retrieval_request();
  request->set_server_backed_state_key(kStateKey2);
  SetPayload(device_management_request);

  StartRequestAndWait();

  EXPECT_EQ(GetResponseCode(), net::HTTP_OK);

  ASSERT_TRUE(HasResponseBody());
  auto response = GetDeviceManagementResponse();
  EXPECT_EQ(
      response.device_state_retrieval_response().restore_mode(),
      em::DeviceStateRetrievalResponse::RESTORE_MODE_REENROLLMENT_ZERO_TOUCH);
  EXPECT_EQ(response.device_state_retrieval_response().management_domain(),
            kManagementDomain);
}

}  // namespace policy