File: plus_address_test_utils.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; 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 (196 lines) | stat: -rw-r--r-- 7,162 bytes parent folder | download | duplicates (2)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// 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.

#include "components/plus_addresses/plus_address_test_utils.h"

#include <string>

#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/json/values_util.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/affiliations/core/browser/affiliation_utils.h"
#include "components/autofill/core/browser/suggestions/suggestion.h"
#include "components/autofill/core/browser/suggestions/suggestion_test_helpers.h"
#include "components/feature_engagement/public/feature_constants.h"
#include "components/plus_addresses/grit/plus_addresses_strings.h"
#include "components/plus_addresses/plus_address_preallocator.h"
#include "components/plus_addresses/plus_address_types.h"
#include "ui/base/l10n/l10n_util.h"

namespace plus_addresses::test {
namespace {
using autofill::EqualsSuggestion;
using autofill::Suggestion;
using autofill::SuggestionType;
using ::testing::Field;
using ::testing::Matcher;
}  // namespace

PlusProfile CreatePlusProfile(std::string plus_address, bool is_confirmed) {
  affiliations::FacetURI facet =
      affiliations::FacetURI::FromCanonicalSpec("https://foo.com");
  return PlusProfile(/*profile_id=*/"123", facet,
                     PlusAddress(std::move(plus_address)),
                     /*is_confirmed=*/is_confirmed);
}

PlusProfile CreatePlusProfile() {
  return CreatePlusProfile("plus+foo@plus.plus", /*is_confirmed=*/true);
}

PlusProfile CreatePlusProfile2() {
  affiliations::FacetURI facet =
      affiliations::FacetURI::FromCanonicalSpec("https://bar.com");
  return PlusProfile(/*profile_id=*/"234", facet,
                     PlusAddress("plus+bar@plus.plus"),
                     /*is_confirmed=*/true);
}

PlusProfile CreatePlusProfileWithFacet(const affiliations::FacetURI& facet) {
  PlusProfile profile = CreatePlusProfile();
  profile.facet = facet;
  return profile;
}

std::string MakeCreationResponse(const PlusProfile& profile) {
  std::string json = base::ReplaceStringPlaceholders(
      R"(
          {
            "plusProfile": $1
          }
        )",
      {MakePlusProfile(profile)}, nullptr);
  DCHECK(base::JSONReader::Read(json));
  return json;
}

std::string MakeListResponse(const std::vector<PlusProfile>& profiles) {
  base::Value::Dict response;
  base::Value::List list;
  for (const PlusProfile& profile : profiles) {
    std::string json = MakePlusProfile(profile);
    std::optional<base::Value::Dict> dict = base::JSONReader::ReadDict(json);
    DCHECK(dict.has_value());
    list.Append(std::move(dict.value()));
  }
  response.Set("plusProfiles", std::move(list));
  std::optional<std::string> json = base::WriteJson(response);
  DCHECK(json.has_value());
  return json.value();
}

std::string MakePreallocateResponse(
    const std::vector<PreallocatedPlusAddress>& addresses) {
  base::Value::List profiles;
  for (const PreallocatedPlusAddress& address : addresses) {
    profiles.Append(
        base::Value::Dict()
            .Set("emailAddress", *address.plus_address)
            .Set("reservationLifetime",
                 base::NumberToString(address.lifetime.InSeconds()) + "s"));
  }
  return base::WriteJson(
             base::Value::Dict().Set("emailAddresses", std::move(profiles)))
      .value();
}

std::string MakePlusProfile(const PlusProfile& profile) {
  // Note: the below must be kept in-line with the PlusAddressParser behavior.
  std::string mode = profile.is_confirmed ? "anyMode" : "UNSPECIFIED";
  std::string json = base::ReplaceStringPlaceholders(
      R"(
          {
            "ProfileId": "$1",
            "facet": "$2",
            "plusEmail": {
              "plusAddress": "$3",
              "plusMode": "$4"
            }
          }
        )",
      {*profile.profile_id, profile.facet.canonical_spec(),
       *profile.plus_address, mode},
      nullptr);
  DCHECK(base::JSONReader::Read(json));
  return json;
}

std::unique_ptr<net::test_server::HttpResponse>
HandleRequestToPlusAddressWithSuccess(
    const net::test_server::HttpRequest& request) {
  // Ignore unrecognized path.
  if (request.GetURL().path() != kReservePath &&
      request.GetURL().path() != kConfirmPath) {
    return nullptr;
  }

  bool is_refresh = [&]() {
    std::optional<base::Value::Dict> body =
        base::JSONReader::ReadDict(request.content);
    if (!body) {
      return false;
    }
    return body->FindBool("refresh_email_address").value_or(false);
  }();
  std::unique_ptr<net::test_server::BasicHttpResponse> http_response(
      new net::test_server::BasicHttpResponse);
  http_response->set_code(net::HTTP_OK);
  http_response->set_content_type("application/json");
  PlusProfile profile = CreatePlusProfile(
      /*plus_address=*/is_refresh ? kFakePlusAddressRefresh : kFakePlusAddress,
      /*is_confirmed=*/request.GetURL().path() == kConfirmPath);
  http_response->set_content(MakeCreationResponse(profile));
  return http_response;
}

base::Value CreatePreallocatedPlusAddress(base::Time end_of_life,
                                          std::string address) {
  return base::Value(
      base::Value::Dict()
          .Set(PlusAddressPreallocator::kEndOfLifeKey,
               base::TimeToValue(end_of_life))
          .Set(PlusAddressPreallocator::kPlusAddressKey, std::move(address)));
}

Matcher<Suggestion> EqualsFillPlusAddressSuggestion(std::string_view address) {
  std::vector<std::vector<Suggestion::Text>> labels;
  if constexpr (!BUILDFLAG(IS_ANDROID)) {
    labels = {{Suggestion::Text(l10n_util::GetStringUTF16(
        IDS_PLUS_ADDRESS_FILL_SUGGESTION_SECONDARY_TEXT))}};
  }
  return AllOf(EqualsSuggestion(SuggestionType::kFillExistingPlusAddress,
                                /*main_text=*/base::UTF8ToUTF16(address)),
               Field(&Suggestion::icon, Suggestion::Icon::kPlusAddress),
               Field(&Suggestion::labels, labels));
}

Matcher<std::vector<Suggestion>> IsSingleCreatePlusAddressSuggestion() {
  std::vector<std::vector<Suggestion::Text>> labels;
  if constexpr (!BUILDFLAG(IS_ANDROID)) {
    labels = {{Suggestion::Text(l10n_util::GetStringUTF16(
        IDS_PLUS_ADDRESS_CREATE_SUGGESTION_SECONDARY_TEXT))}};
  }
  return ElementsAre(AllOf(
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
      EqualsSuggestion(SuggestionType::kCreateNewPlusAddressInline),
#else
      EqualsSuggestion(SuggestionType::kCreateNewPlusAddress,
                       /*main_text=*/l10n_util::GetStringUTF16(
                           IDS_PLUS_ADDRESS_CREATE_SUGGESTION_MAIN_TEXT)),
      Field(&Suggestion::iph_metadata,
            Suggestion::IPHMetadata(
                &feature_engagement::kIPHPlusAddressCreateSuggestionFeature)),
#endif
      Field(&Suggestion::icon, Suggestion::Icon::kPlusAddress),
      Field(&Suggestion::labels, labels)));
}

Matcher<std::vector<Suggestion>> IsSingleFillPlusAddressSuggestion(
    std::string_view address) {
  return ElementsAre(EqualsFillPlusAddressSuggestion(address));
}

}  // namespace plus_addresses::test