File: payment_method_data_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (111 lines) | stat: -rw-r--r-- 4,262 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
// Copyright 2017 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/payments/core/payment_method_data.h"

#include "base/values.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace payments {

// Tests the success case when populating a PaymentMethodData from a dictionary
// when the supportedMethods is a string.
TEST(PaymentMethodData, FromValueSuccess_SupportedMethodsString) {
  PaymentMethodData expected;
  expected.supported_method = "basic-card";
  expected.data = "{\"supportedNetworks\":[\"mastercard\"]}";
  expected.supported_networks.push_back("mastercard");

  base::Value::Dict method_data_dict;
  method_data_dict.Set("supportedMethods", "basic-card");
  base::Value::Dict data_dict;
  base::Value::List supported_networks_list;
  supported_networks_list.Append("mastercard");
  data_dict.Set("supportedNetworks", std::move(supported_networks_list));
  method_data_dict.Set("data", std::move(data_dict));

  PaymentMethodData actual;
  EXPECT_TRUE(actual.FromValueDict(method_data_dict));

  EXPECT_EQ(expected, actual);
}

// Tests the failure cases when populating a PaymentMethodData from a
// dictionary.
TEST(PaymentMethodData, FromValueFailure) {
  PaymentMethodData actual;

  // At least one supported method is required.
  base::Value::Dict method_data_dict;
  EXPECT_FALSE(actual.FromValueDict(method_data_dict));

  // The value in the supported methods list must be a string.
  base::Value::List supported_methods_list1;
  supported_methods_list1.Append(13);
  method_data_dict.Set("supportedMethods", std::move(supported_methods_list1));
  EXPECT_FALSE(actual.FromValueDict(method_data_dict));

  // The value in the supported methods list must be a non-empty string.
  base::Value::List supported_methods_list2;
  supported_methods_list2.Append("");
  method_data_dict.Set("supportedMethods", std::move(supported_methods_list2));
  EXPECT_FALSE(actual.FromValueDict(method_data_dict));

  // The value in the supported methods must be a string.
  method_data_dict.Set("supportedMethods", 13);
  EXPECT_FALSE(actual.FromValueDict(method_data_dict));

  // The value in the supported methods must be a non-empty string.
  method_data_dict.Set("supportedMethods", "");
  EXPECT_FALSE(actual.FromValueDict(method_data_dict));

  // Supported network list must include ASCII strings.
  method_data_dict.Set("supportedMethods", "some finance thing");
  base::Value::Dict data_dict;
  base::Value::List supported_networks_list;
  supported_networks_list.Append(123456);
  data_dict.Set("supportedNetworks", std::move(supported_networks_list));
  method_data_dict.Set("data", std::move(data_dict));
  EXPECT_FALSE(actual.FromValueDict(method_data_dict));

  method_data_dict.FindDict("data")->FindList("supportedNetworks")->front() =
      base::Value("\xD0\xA2\xD0\xB5\xD1\x81\xD1\x82");
  EXPECT_FALSE(actual.FromValueDict(method_data_dict));
}

// Tests that two method data objects are not equal if their property values
// differ or one is missing a value present in the other, and equal otherwise.
TEST(PaymentMethodData, Equality) {
  PaymentMethodData method_data1;
  PaymentMethodData method_data2;
  EXPECT_EQ(method_data1, method_data2);

  method_data1.supported_method = "basic-card";
  EXPECT_NE(method_data1, method_data2);

  method_data2.supported_method = "http://bobpay.test";
  EXPECT_NE(method_data1, method_data2);

  method_data2.supported_method = "basic-card";
  ;
  EXPECT_EQ(method_data1, method_data2);

  method_data1.data = "{merchantId: '123456'}";
  EXPECT_NE(method_data1, method_data2);
  method_data2.data = "{merchantId: '9999-88'}";
  EXPECT_NE(method_data1, method_data2);
  method_data2.data = "{merchantId: '123456'}";
  EXPECT_EQ(method_data1, method_data2);

  std::vector<std::string> supported_networks1{"visa"};
  method_data1.supported_networks = supported_networks1;
  EXPECT_NE(method_data1, method_data2);
  std::vector<std::string> supported_networks2{"jcb"};
  method_data2.supported_networks = supported_networks2;
  EXPECT_NE(method_data1, method_data2);
  method_data2.supported_networks = supported_networks1;
  EXPECT_EQ(method_data1, method_data2);
}

}  // namespace payments