File: student_heartbeat_request_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (167 lines) | stat: -rw-r--r-- 6,020 bytes parent folder | download | duplicates (8)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/boca/session_api/student_heartbeat_request.h"

#include <memory>

#include "base/functional/bind.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "base/time/time.h"
#include "base/types/expected.h"
#include "chromeos/ash/components/boca/session_api/constants.h"
#include "google_apis/common/api_error_codes.h"
#include "google_apis/common/dummy_auth_service.h"
#include "google_apis/common/request_sender.h"
#include "google_apis/common/test_util.h"
#include "google_apis/gaia/gaia_id.h"
#include "google_apis/gaia/gaia_urls.h"
#include "google_apis/gaia/gaia_urls_overrider_for_testing.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "services/network/test/test_shared_url_loader_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::net::test_server::BasicHttpResponse;
using ::net::test_server::HttpMethod;
using ::net::test_server::HttpRequest;
using ::net::test_server::HttpResponse;
using ::testing::_;
using ::testing::AllOf;
using ::testing::ByMove;
using ::testing::DoAll;
using ::testing::Eq;
using ::testing::Field;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::SaveArg;
using ::testing::Truly;

// TODO: crbug.com/394633053 Move all tests under namespace ash::boca.
namespace {
const char kTestUserAgent[] = "test-user-agent";

class MockRequestHandler {
 public:
  static std::unique_ptr<HttpResponse> CreateSuccessfulResponse() {
    auto response = std::make_unique<BasicHttpResponse>();
    response->set_code(net::HTTP_OK);
    return response;
  }

  static std::unique_ptr<HttpResponse> CreateFailedResponse() {
    auto response = std::make_unique<BasicHttpResponse>();
    response->set_code(net::HTTP_INTERNAL_SERVER_ERROR);
    return response;
  }

  MOCK_METHOD(std::unique_ptr<HttpResponse>,
              HandleRequest,
              (const HttpRequest&));
};

}  // namespace

namespace ash::boca {

class StudentHeartbeatRequestTest : public testing::Test {
 public:
  void SetUp() override {
    test_shared_loader_factory_ =
        base::MakeRefCounted<network::TestSharedURLLoaderFactory>(
            /*network_service=*/nullptr,
            /*is_trusted=*/true);
    request_sender_ = std::make_unique<google_apis::RequestSender>(
        std::make_unique<google_apis::DummyAuthService>(),
        test_shared_loader_factory_,
        task_environment_.GetMainThreadTaskRunner(), kTestUserAgent,
        TRAFFIC_ANNOTATION_FOR_TESTS);

    test_server_.RegisterRequestHandler(
        base::BindRepeating(&MockRequestHandler::HandleRequest,
                            base::Unretained(&request_handler_)));

    ASSERT_TRUE(test_server_.Start());
  }

  MockRequestHandler* request_handler() { return &request_handler_; }
  google_apis::RequestSender* request_sender() { return request_sender_.get(); }
  net::EmbeddedTestServer test_server_;

 private:
  base::test::TaskEnvironment task_environment_{
      base::test::TaskEnvironment::MainThreadType::IO};
  std::unique_ptr<google_apis::RequestSender> request_sender_;
  testing::StrictMock<MockRequestHandler> request_handler_;
  std::unique_ptr<GaiaUrlsOverriderForTesting> urls_overrider_;
  scoped_refptr<network::TestSharedURLLoaderFactory>
      test_shared_loader_factory_;
};

TEST_F(StudentHeartbeatRequestTest, StudentHeartbeatSucceed) {
  net::test_server::HttpRequest http_request;
  EXPECT_CALL(*request_handler(), HandleRequest(_))
      .WillOnce([&](const HttpRequest& request) {
        http_request = request;
        return MockRequestHandler::CreateSuccessfulResponse();
      });
  const GaiaId gaiaId("1");
  const std::string sessionId = "session_id";
  const std::string deviceId = "device_id";
  const std::string studentGroupId = "student_group_id";

  base::test::TestFuture<base::expected<bool, google_apis::ApiErrorCode>>
      future;
  std::unique_ptr<StudentHeartbeatRequest> request =
      std::make_unique<StudentHeartbeatRequest>(
          request_sender(), "https://test", sessionId, gaiaId, deviceId,
          studentGroupId, future.GetCallback());

  request->OverrideURLForTesting(test_server_.base_url().spec());
  request_sender()->StartRequestWithAuthRetry(std::move(request));

  auto result = future.Get();

  EXPECT_EQ(
      "/v1/sessions/session_id/students/1/devices/"
      "device_id:heartbeat?studentGroupId=student_group_id",
      http_request.relative_url);
  EXPECT_EQ(true, result.value());
}

TEST_F(StudentHeartbeatRequestTest, StudentHeartbeatFail) {
  net::test_server::HttpRequest http_request;
  EXPECT_CALL(*request_handler(), HandleRequest(_))
      .WillOnce([&](const HttpRequest& request) {
        http_request = request;
        return MockRequestHandler::CreateFailedResponse();
      });
  const GaiaId gaiaId("1");
  const std::string sessionId = "session_id";
  const std::string deviceId = "device_id";
  const std::string studentGroupId = "student_group_id";

  base::test::TestFuture<base::expected<bool, google_apis::ApiErrorCode>>
      future;
  std::unique_ptr<StudentHeartbeatRequest> request =
      std::make_unique<StudentHeartbeatRequest>(
          request_sender(), "https://test", sessionId, gaiaId, deviceId,
          studentGroupId, future.GetCallback());

  request->OverrideURLForTesting(test_server_.base_url().spec());
  request_sender()->StartRequestWithAuthRetry(std::move(request));

  auto result = future.Get();

  EXPECT_EQ(
      "/v1/sessions/session_id/students/1/devices/"
      "device_id:heartbeat?studentGroupId=student_group_id",
      http_request.relative_url);
  EXPECT_EQ(google_apis::HTTP_INTERNAL_SERVER_ERROR, result.error());
}

}  // namespace ash::boca