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
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_SUPERVISED_USER_TEST_SUPPORT_KIDS_MANAGEMENT_API_SERVER_MOCK_H_
#define COMPONENTS_SUPERVISED_USER_TEST_SUPPORT_KIDS_MANAGEMENT_API_SERVER_MOCK_H_
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include "base/callback_list.h"
#include "base/functional/callback_forward.h"
#include "base/test/scoped_feature_list.h"
#include "components/supervised_user/core/browser/fetcher_config.h"
#include "components/supervised_user/core/browser/proto/kidsmanagement_messages.pb.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "url/gurl.h"
namespace supervised_user {
// arg is of type net::test_server::HttpRequest
MATCHER_P(Classifies, url, "") {
kidsmanagement::ClassifyUrlRequest request;
CHECK(request.ParseFromString(arg.content));
return GURL(request.url()) == url;
}
// arg is of type net::test_server::HttpRequest
MATCHER_P(SetsRegionCode, region_code, "") {
kidsmanagement::ClassifyUrlRequest request;
CHECK(request.ParseFromString(arg.content));
return request.region_code() == region_code;
}
extern const std::multimap<kidsmanagement::FamilyRole, std::string>
kSimpsonFamily;
// Configures the scoped feature list so that the related feature is initialized
// with right parameters to divert kids management api traffic to an http
// endpoint. See supervised_user::FetcherConfig::service_endpoint for details.
void SetHttpEndpointsForKidsManagementApis(
base::test::ScopedFeatureList& feature_list,
std::string_view hostname);
// Component of `KidsManagementApiServerMock`. Implements ClassifyUrl as both
// mock and fake, allowing to account the calls but also providing a default
// response (allow or restrict) for every request. Newly created instance has
// purportedly unconfigured default response and will crash on first use - this
// is to limit unintended / unnoticed uses of the mock during test.
class KidsManagementClassifyUrlMock {
public:
KidsManagementClassifyUrlMock();
~KidsManagementClassifyUrlMock();
MOCK_METHOD(kidsmanagement::ClassifyUrlResponse::DisplayClassification,
ClassifyUrl,
(const net::test_server::HttpRequest& request));
void set_display_classification(
kidsmanagement::ClassifyUrlResponse::DisplayClassification
classification);
private:
// The classification response for every request. Needs to be set (see
// `set_display_classification`) before first use.
std::optional<kidsmanagement::ClassifyUrlResponse::DisplayClassification>
display_classification_;
};
// Simplified implementation of the real Kids Management API server, purposed to
// serve as request handlers for the net::test_server::EmbeddedTestServer.
class KidsManagementApiServerMock {
public:
KidsManagementApiServerMock();
KidsManagementApiServerMock(KidsManagementApiServerMock&& other) = delete;
KidsManagementApiServerMock& operator=(KidsManagementApiServerMock&& other) =
delete;
~KidsManagementApiServerMock();
// Installs this mock on a given Embedded Test Server. The server must outlive
// this instance and must not be started prior to calling this method.
// Caution: installed handlers are executed until one matches the request.
void InstallOn(net::test_server::EmbeddedTestServer& test_server_);
// Set the mock to respond with allow or restrict url for all subsequent
// requests to ClassifyUrl.
void AllowSubsequentClassifyUrl();
void RestrictSubsequentClassifyUrl();
KidsManagementClassifyUrlMock& classify_url_mock() {
return classify_url_mock_;
}
private:
// Api handler for /kidsmanagement/v1/people/me:classifyUrl
std::unique_ptr<net::test_server::HttpResponse> ClassifyUrl(
const net::test_server::HttpRequest& request);
// Api handler for /kidsmanagement/v1/families/mine/members
std::unique_ptr<net::test_server::HttpResponse> ListFamilyMembers(
const net::test_server::HttpRequest& request);
KidsManagementClassifyUrlMock classify_url_mock_;
};
} // namespace supervised_user
#endif // COMPONENTS_SUPERVISED_USER_TEST_SUPPORT_KIDS_MANAGEMENT_API_SERVER_MOCK_H_
|