summaryrefslogtreecommitdiff
path: root/chromium/components/payments/core/autofill_payment_instrument_unittest.cc
blob: 57b9f6a22e61aa23a44276c2937cb938c06531a8 (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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// Copyright 2017 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 "components/payments/core/autofill_payment_instrument.h"

#include <memory>

#include "base/macros.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/address_normalizer.h"
#include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/credit_card.h"
#include "components/autofill/core/browser/payments/full_card_request.h"
#include "components/autofill/core/browser/payments/payments_client.h"
#include "components/autofill/core/browser/personal_data_manager.h"
#include "components/autofill/core/browser/test_address_normalizer.h"
#include "components/autofill/core/browser/test_autofill_client.h"
#include "components/autofill/core/browser/test_personal_data_manager.h"
#include "components/payments/core/test_payment_request_delegate.h"
#include "components/strings/grit/components_strings.h"
#include "net/url_request/url_request_test_util.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"

namespace payments {

namespace {

class FakePaymentInstrumentDelegate : public PaymentInstrument::Delegate {
 public:
  FakePaymentInstrumentDelegate() {}

  void OnInstrumentDetailsReady(
      const std::string& method_name,
      const std::string& stringified_details) override {
    on_instrument_details_ready_called_ = true;
  }

  void OnInstrumentDetailsError() override {
    on_instrument_details_error_called_ = true;
  }

  bool WasOnInstrumentDetailsReadyCalled() {
    return on_instrument_details_ready_called_;
  }

  bool WasOnInstrumentDetailsErrorCalled() {
    return on_instrument_details_error_called_;
  }

 private:
  bool on_instrument_details_ready_called_ = false;
  bool on_instrument_details_error_called_ = false;
};

class FakePaymentRequestDelegate
    : public PaymentRequestDelegate,
      public autofill::payments::PaymentsClientUnmaskDelegate {
 public:
  FakePaymentRequestDelegate()
      : locale_("en-US"),
        last_committed_url_("https://shop.com"),
        personal_data_("en-US"),
        test_shared_loader_factory_(
            base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
                &test_url_loader_factory_)),
        payments_client_(test_shared_loader_factory_,
                         nullptr,
                         nullptr,
                         this,
                         nullptr),
        full_card_request_(&autofill_client_,
                           &payments_client_,
                           &personal_data_) {}
  void ShowDialog(PaymentRequest* request) override {}

  void CloseDialog() override {}

  void ShowErrorMessage() override {}

  autofill::PersonalDataManager* GetPersonalDataManager() override {
    return nullptr;
  }

  const std::string& GetApplicationLocale() const override { return locale_; }

  bool IsIncognito() const override { return false; }

  bool IsSslCertificateValid() override { return true; }

  const GURL& GetLastCommittedURL() const override {
    return last_committed_url_;
  }

  void DoFullCardRequest(
      const autofill::CreditCard& credit_card,
      base::WeakPtr<autofill::payments::FullCardRequest::ResultDelegate>
          result_delegate) override {
    full_card_request_card_ = credit_card;
    full_card_result_delegate_ = result_delegate;
  }

  autofill::AddressNormalizer* GetAddressNormalizer() override {
    return &address_normalizer_;
  }

  void CompleteFullCardRequest() {
    full_card_result_delegate_->OnFullCardRequestSucceeded(
        full_card_request_, full_card_request_card_, base::ASCIIToUTF16("123"));
  }

  autofill::RegionDataLoader* GetRegionDataLoader() override { return nullptr; }

  ukm::UkmRecorder* GetUkmRecorder() override { return nullptr; }

 private:
  std::string locale_;
  const GURL last_committed_url_;
  autofill::TestAddressNormalizer address_normalizer_;
  autofill::PersonalDataManager personal_data_;
  network::TestURLLoaderFactory test_url_loader_factory_;
  scoped_refptr<network::SharedURLLoaderFactory> test_shared_loader_factory_;
  autofill::TestAutofillClient autofill_client_;
  autofill::payments::PaymentsClient payments_client_;
  autofill::payments::FullCardRequest full_card_request_;
  autofill::CreditCard full_card_request_card_;
  base::WeakPtr<autofill::payments::FullCardRequest::ResultDelegate>
      full_card_result_delegate_;
  DISALLOW_COPY_AND_ASSIGN(FakePaymentRequestDelegate);
};

}  // namespace

class AutofillPaymentInstrumentTest : public testing::Test {
 protected:
  AutofillPaymentInstrumentTest()
      : address_(autofill::test::GetFullProfile()),
        local_card_(autofill::test::GetCreditCard()),
        billing_profiles_({&address_}) {
    local_card_.set_billing_address_id(address_.guid());
  }

  autofill::CreditCard& local_credit_card() { return local_card_; }
  const std::vector<autofill::AutofillProfile*>& billing_profiles() {
    return billing_profiles_;
  }

 private:
  autofill::AutofillProfile address_;
  autofill::CreditCard local_card_;
  std::vector<autofill::AutofillProfile*> billing_profiles_;

  DISALLOW_COPY_AND_ASSIGN(AutofillPaymentInstrumentTest);
};

// A valid local credit card is a valid instrument for payment.
TEST_F(AutofillPaymentInstrumentTest, IsCompleteForPayment) {
  AutofillPaymentInstrument instrument(
      "visa", local_credit_card(),
      /*matches_merchant_card_type_exactly=*/true, billing_profiles(), "en-US",
      nullptr);
  EXPECT_TRUE(instrument.IsCompleteForPayment());
  EXPECT_TRUE(instrument.GetMissingInfoLabel().empty());
}

// An expired local card is still a valid instrument for payment.
TEST_F(AutofillPaymentInstrumentTest, IsCompleteForPayment_Expired) {
  autofill::CreditCard& card = local_credit_card();
  card.SetExpirationYear(2016);  // Expired.
  AutofillPaymentInstrument instrument(
      "visa", card, /*matches_merchant_card_type_exactly=*/true,
      billing_profiles(), "en-US", nullptr);
  EXPECT_TRUE(instrument.IsCompleteForPayment());
  EXPECT_EQ(base::string16(), instrument.GetMissingInfoLabel());
}

// A local card with no name is not a valid instrument for payment.
TEST_F(AutofillPaymentInstrumentTest, IsCompleteForPayment_NoName) {
  autofill::CreditCard& card = local_credit_card();
  card.SetInfo(autofill::AutofillType(autofill::CREDIT_CARD_NAME_FULL),
               base::ASCIIToUTF16(""), "en-US");
  base::string16 missing_info;
  AutofillPaymentInstrument instrument(
      "visa", card, /*matches_merchant_card_type_exactly=*/true,
      billing_profiles(), "en-US", nullptr);
  EXPECT_FALSE(instrument.IsCompleteForPayment());
  EXPECT_EQ(l10n_util::GetStringUTF16(IDS_PAYMENTS_NAME_ON_CARD_REQUIRED),
            instrument.GetMissingInfoLabel());
}

// A local card with no name is not a valid instrument for payment.
TEST_F(AutofillPaymentInstrumentTest, IsCompleteForPayment_NoNumber) {
  autofill::CreditCard& card = local_credit_card();
  card.SetNumber(base::ASCIIToUTF16(""));
  base::string16 missing_info;
  AutofillPaymentInstrument instrument(
      "visa", card, /*matches_merchant_card_type_exactly=*/true,
      billing_profiles(), "en-US", nullptr);
  EXPECT_FALSE(instrument.IsCompleteForPayment());
  EXPECT_EQ(l10n_util::GetStringUTF16(
                IDS_PAYMENTS_CARD_NUMBER_INVALID_VALIDATION_MESSAGE),
            instrument.GetMissingInfoLabel());
}

// A local card with no billing address id is not a valid instrument for
// payment.
TEST_F(AutofillPaymentInstrumentTest, IsCompleteForPayment_NoBillinbAddressId) {
  autofill::CreditCard& card = local_credit_card();
  card.set_billing_address_id("");
  base::string16 missing_info;
  AutofillPaymentInstrument instrument(
      "visa", card, /*matches_merchant_card_type_exactly=*/true,
      billing_profiles(), "en-US", nullptr);
  EXPECT_FALSE(instrument.IsCompleteForPayment());
  EXPECT_EQ(
      l10n_util::GetStringUTF16(IDS_PAYMENTS_CARD_BILLING_ADDRESS_REQUIRED),
      instrument.GetMissingInfoLabel());
}

// A local card with an invalid billing address id is not a valid instrument for
// payment.
TEST_F(AutofillPaymentInstrumentTest,
       IsCompleteForPayment_InvalidBillinbAddressId) {
  autofill::CreditCard& card = local_credit_card();
  card.set_billing_address_id("InvalidBillingAddressId");
  base::string16 missing_info;
  AutofillPaymentInstrument instrument(
      "visa", card, /*matches_merchant_card_type_exactly=*/true,
      billing_profiles(), "en-US", nullptr);
  EXPECT_FALSE(instrument.IsCompleteForPayment());
  EXPECT_EQ(
      l10n_util::GetStringUTF16(IDS_PAYMENTS_CARD_BILLING_ADDRESS_REQUIRED),
      instrument.GetMissingInfoLabel());
}

// A local card with no name and no number is not a valid instrument for
// payment.
TEST_F(AutofillPaymentInstrumentTest,
       IsCompleteForPayment_MultipleThingsMissing) {
  autofill::CreditCard& card = local_credit_card();
  card.SetNumber(base::ASCIIToUTF16(""));
  card.SetInfo(autofill::AutofillType(autofill::CREDIT_CARD_NAME_FULL),
               base::ASCIIToUTF16(""), "en-US");
  AutofillPaymentInstrument instrument(
      "visa", card, /*matches_merchant_card_type_exactly=*/true,
      billing_profiles(), "en-US", nullptr);
  EXPECT_FALSE(instrument.IsCompleteForPayment());
  EXPECT_EQ(l10n_util::GetStringUTF16(IDS_PAYMENTS_MORE_INFORMATION_REQUIRED),
            instrument.GetMissingInfoLabel());
}

// A Masked (server) card is a valid instrument for payment.
TEST_F(AutofillPaymentInstrumentTest, IsCompleteForPayment_MaskedCard) {
  autofill::CreditCard card = autofill::test::GetMaskedServerCard();
  ASSERT_GT(billing_profiles().size(), 0UL);
  card.set_billing_address_id(billing_profiles()[0]->guid());
  AutofillPaymentInstrument instrument(
      "visa", card, /*matches_merchant_card_type_exactly=*/true,
      billing_profiles(), "en-US", nullptr);
  EXPECT_TRUE(instrument.IsCompleteForPayment());
  EXPECT_TRUE(instrument.GetMissingInfoLabel().empty());
}

// An expired masked (server) card is still a valid instrument for payment.
TEST_F(AutofillPaymentInstrumentTest, IsCompleteForPayment_ExpiredMaskedCard) {
  autofill::CreditCard card = autofill::test::GetMaskedServerCard();
  ASSERT_GT(billing_profiles().size(), 0UL);
  card.set_billing_address_id(billing_profiles()[0]->guid());
  card.SetExpirationYear(2016);  // Expired.
  AutofillPaymentInstrument instrument(
      "visa", card, /*matches_merchant_card_type_exactly=*/true,
      billing_profiles(), "en-US", nullptr);
  EXPECT_TRUE(instrument.IsCompleteForPayment());
  EXPECT_EQ(base::string16(), instrument.GetMissingInfoLabel());
}

// An expired card is a valid instrument for canMakePayment.
TEST_F(AutofillPaymentInstrumentTest, IsValidForCanMakePayment_Minimal) {
  autofill::CreditCard& card = local_credit_card();
  card.SetExpirationYear(2016);  // Expired.
  AutofillPaymentInstrument instrument(
      "visa", card, /*matches_merchant_card_type_exactly=*/true,
      billing_profiles(), "en-US", nullptr);
  EXPECT_TRUE(instrument.IsValidForCanMakePayment());
}

// An expired Masked (server) card is a valid instrument for canMakePayment.
TEST_F(AutofillPaymentInstrumentTest, IsValidForCanMakePayment_MaskedCard) {
  autofill::CreditCard card = autofill::test::GetMaskedServerCard();
  card.SetExpirationYear(2016);  // Expired.
  AutofillPaymentInstrument instrument(
      "visa", card, /*matches_merchant_card_type_exactly=*/true,
      billing_profiles(), "en-US", nullptr);
  EXPECT_TRUE(instrument.IsValidForCanMakePayment());
}

// A card with no name is not a valid instrument for canMakePayment.
TEST_F(AutofillPaymentInstrumentTest, IsValidForCanMakePayment_NoName) {
  autofill::CreditCard& card = local_credit_card();
  card.SetInfo(autofill::AutofillType(autofill::CREDIT_CARD_NAME_FULL),
               base::ASCIIToUTF16(""), "en-US");
  AutofillPaymentInstrument instrument(
      "visa", card, /*matches_merchant_card_type_exactly=*/true,
      billing_profiles(), "en-US", nullptr);
  EXPECT_FALSE(instrument.IsValidForCanMakePayment());
}

// A card with no number is not a valid instrument for canMakePayment.
TEST_F(AutofillPaymentInstrumentTest, IsValidForCanMakePayment_NoNumber) {
  autofill::CreditCard& card = local_credit_card();
  card.SetNumber(base::ASCIIToUTF16(""));
  AutofillPaymentInstrument instrument(
      "visa", card, /*matches_merchant_card_type_exactly=*/true,
      billing_profiles(), "en-US", nullptr);
  EXPECT_FALSE(instrument.IsValidForCanMakePayment());
}

// Tests that the autofill instrument only calls OnInstrumentDetailsReady when
// the billing address has been normalized and the card has been unmasked.
TEST_F(AutofillPaymentInstrumentTest,
       InvokePaymentApp_NormalizationBeforeUnmask) {
  auto personal_data_manager =
      std::make_unique<autofill::TestPersonalDataManager>();
  TestPaymentRequestDelegate delegate(personal_data_manager.get());
  delegate.DelayFullCardRequestCompletion();
  delegate.test_address_normalizer()->DelayNormalization();

  autofill::CreditCard& card = local_credit_card();
  card.SetNumber(base::ASCIIToUTF16(""));
  AutofillPaymentInstrument instrument(
      "visa", card, /*matches_merchant_card_type_exactly=*/true,
      billing_profiles(), "en-US", &delegate);

  FakePaymentInstrumentDelegate instrument_delegate;

  instrument.InvokePaymentApp(&instrument_delegate);
  EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsReadyCalled());
  EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled());

  delegate.test_address_normalizer()->CompleteAddressNormalization();
  EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsReadyCalled());
  EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled());

  delegate.CompleteFullCardRequest();
  EXPECT_TRUE(instrument_delegate.WasOnInstrumentDetailsReadyCalled());
  EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled());
}

// Tests that the autofill instrument only calls OnInstrumentDetailsReady when
// the billing address has been normalized and the card has been unmasked.
TEST_F(AutofillPaymentInstrumentTest,
       InvokePaymentApp_UnmaskBeforeNormalization) {
  auto personal_data_manager =
      std::make_unique<autofill::TestPersonalDataManager>();
  TestPaymentRequestDelegate delegate(personal_data_manager.get());
  delegate.DelayFullCardRequestCompletion();
  delegate.test_address_normalizer()->DelayNormalization();

  autofill::CreditCard& card = local_credit_card();
  card.SetNumber(base::ASCIIToUTF16(""));
  AutofillPaymentInstrument instrument(
      "visa", card, /*matches_merchant_card_type_exactly=*/true,
      billing_profiles(), "en-US", &delegate);

  FakePaymentInstrumentDelegate instrument_delegate;

  instrument.InvokePaymentApp(&instrument_delegate);
  EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsReadyCalled());
  EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled());

  delegate.CompleteFullCardRequest();
  EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsReadyCalled());
  EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled());

  delegate.test_address_normalizer()->CompleteAddressNormalization();
  EXPECT_TRUE(instrument_delegate.WasOnInstrumentDetailsReadyCalled());
  EXPECT_FALSE(instrument_delegate.WasOnInstrumentDetailsErrorCalled());
}

}  // namespace payments