File: base_provider_test_helper.cc

package info (click to toggle)
chromium 141.0.7390.107-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,246,132 kB
  • sloc: cpp: 35,264,965; ansic: 7,169,920; javascript: 4,250,185; python: 1,460,635; asm: 950,788; xml: 751,751; pascal: 187,972; sh: 89,459; perl: 88,691; objc: 79,953; sql: 53,924; cs: 44,622; fortran: 24,137; makefile: 22,313; tcl: 15,277; php: 14,018; yacc: 8,995; ruby: 7,553; awk: 3,720; lisp: 3,096; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (99 lines) | stat: -rw-r--r-- 4,088 bytes parent folder | download | duplicates (4)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/manta/base_provider_test_helper.h"

#include "base/strings/stringprintf.h"
#include "components/endpoint_fetcher/endpoint_fetcher.h"
#include "components/manta/base_provider.h"
#include "components/manta/manta_service_callbacks.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"

using endpoint_fetcher::EndpointFetcher;

namespace manta {

namespace {
constexpr base::TimeDelta kMockTimeout = base::Seconds(100);
constexpr char kMockOAuthConsumerName[] = "mock_oauth_consumer_name";
constexpr char kMockScope[] = "mock_scope";
constexpr char kMockEndpoint[] = "https://my-endpoint.com";
constexpr endpoint_fetcher::HttpMethod kHttpMethod =
    endpoint_fetcher::HttpMethod::kPost;
constexpr char kMockContentType[] = "mock_content_type";
constexpr char kEmail[] = "mock_email@gmail.com";
}  // namespace

FakeBaseProvider::FakeBaseProvider(
    scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
    signin::IdentityManager* identity_manager)
    : BaseProvider(url_loader_factory, identity_manager) {}

FakeBaseProvider::~FakeBaseProvider() = default;

void FakeBaseProvider::RequestInternal(
    const GURL& url,
    const std::string& oauth_consumer_name,
    const net::NetworkTrafficAnnotationTag& annotation_tag,
    manta::proto::Request& request,
    const MantaMetricType metric_type,
    MantaProtoResponseCallback done_callback,
    const base::TimeDelta timeout) {
  if (!identity_manager_observation_.IsObserving()) {
    std::move(done_callback)
        .Run(nullptr, {MantaStatusCode::kNoIdentityManager});
    return;
  }
  auto fetcher = std::make_unique<EndpointFetcher>(
      /*url_loader_factory=*/url_loader_factory_,
      identity_manager_observation_.GetSource(),
      EndpointFetcher::RequestParams::Builder(kHttpMethod, annotation_tag)
          .SetConsentLevel(signin::ConsentLevel::kSync)
          .SetContentType(kMockContentType)
          .SetTimeout(kMockTimeout)
          .SetUrl(GURL{kMockEndpoint})
          .SetOauthScopes(std::vector<std::string>{kMockScope})
          .SetOauthConsumerName(kMockOAuthConsumerName)
          .SetPostData(request.SerializeAsString())
          .Build());

  EndpointFetcher* const fetcher_ptr = fetcher.get();
  fetcher_ptr->Fetch(base::BindOnce(&OnEndpointFetcherComplete,
                                    std::move(done_callback), base::Time::Now(),
                                    metric_type, std::move(fetcher)));
}

BaseProviderTest::BaseProviderTest()
    : task_environment_(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}

BaseProviderTest::~BaseProviderTest() = default;

void BaseProviderTest::SetUp() {
  identity_test_env_ = std::make_unique<signin::IdentityTestEnvironment>();
  identity_test_env_->MakePrimaryAccountAvailable(kEmail,
                                                  signin::ConsentLevel::kSync);
  identity_test_env_->SetAutomaticIssueOfAccessTokens(true);
}

void BaseProviderTest::SetEndpointMockResponse(
    const GURL& request_url,
    const std::string& response_data,
    net::HttpStatusCode response_code,
    net::Error error) {
  auto head = network::mojom::URLResponseHead::New();
  std::string headers(base::StringPrintf(
      "HTTP/1.1 %d %s\nContent-type: application/x-protobuf\n\n",
      static_cast<int>(response_code), GetHttpReasonPhrase(response_code)));
  head->headers = base::MakeRefCounted<net::HttpResponseHeaders>(
      net::HttpUtil::AssembleRawHeaders(headers));
  head->mime_type = "application/x-protobuf";
  network::URLLoaderCompletionStatus status(error);
  status.decoded_body_length = response_data.size();
  test_url_loader_factory_.AddResponse(request_url, std::move(head),
                                       response_data, status);
}

}  // namespace manta