| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/json/json_reader.h" | |
| 6 #include "base/logging.h" | |
| 7 #include "base/memory/scoped_ptr.h" | |
| 8 #include "base/utf_string_conversions.h" | |
| 9 #include "base/values.h" | |
| 10 #include "chrome/browser/autofill/wallet/full_wallet.h" | |
| 11 #include "chrome/browser/autofill/wallet/required_action.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 const char kFullWalletValidResponse[] = | |
| 17 "{" | |
| 18 " \"expiration_month\":12," | |
| 19 " \"expiration_year\":2012," | |
| 20 " \"iin\":\"iin\"," | |
| 21 " \"rest\":\"rest\"," | |
| 22 " \"billing_address\":" | |
| 23 " {" | |
| 24 " \"phone_number\":\"phone_number\"," | |
| 25 " \"postal_address\":" | |
| 26 " {" | |
| 27 " \"recipient_name\":\"recipient_name\"," | |
| 28 " \"address_line\":" | |
| 29 " [" | |
| 30 " \"address_line_1\"," | |
| 31 " \"address_line_2\"" | |
| 32 " ]," | |
| 33 " \"locality_name\":\"locality_name\"," | |
| 34 " \"administrative_area_name\":\"administrative_area_name\"," | |
| 35 " \"postal_code_number\":\"postal_code_number\"," | |
| 36 " \"country_name_code\":\"country_name_code\"" | |
| 37 " }" | |
| 38 " }," | |
| 39 " \"shipping_address\":" | |
| 40 " {" | |
| 41 " \"id\":\"ship_id\"," | |
| 42 " \"phone_number\":\"ship_phone_number\"," | |
| 43 " \"postal_address\":" | |
| 44 " {" | |
| 45 " \"recipient_name\":\"ship_recipient_name\"," | |
| 46 " \"address_line\":" | |
| 47 " [" | |
| 48 " \"ship_address_line_1\"," | |
| 49 " \"ship_address_line_2\"" | |
| 50 " ]," | |
| 51 " \"locality_name\":\"ship_locality_name\"," | |
| 52 " \"administrative_area_name\":\"ship_admin_area_name\"," | |
| 53 " \"postal_code_number\":\"ship_postal_code_number\"," | |
| 54 " \"country_name_code\":\"ship_country_name_code\"" | |
| 55 " }" | |
| 56 " }," | |
| 57 " \"required_action\":" | |
| 58 " [" | |
| 59 " ]" | |
| 60 "}"; | |
| 61 | |
| 62 const char kFullWalletMissingExpirationMonth[] = | |
| 63 "{" | |
| 64 " \"expiration_year\":2012," | |
| 65 " \"iin\":\"iin\"," | |
| 66 " \"rest\":\"rest\"," | |
| 67 " \"billing_address\":" | |
| 68 " {" | |
| 69 " \"id\":\"id\"," | |
| 70 " \"phone_number\":\"phone_number\"," | |
| 71 " \"postal_address\":" | |
| 72 " {" | |
| 73 " \"recipient_name\":\"recipient_name\"," | |
| 74 " \"address_line\":" | |
| 75 " [" | |
| 76 " \"address_line_1\"," | |
| 77 " \"address_line_2\"" | |
| 78 " ]," | |
| 79 " \"locality_name\":\"locality_name\"," | |
| 80 " \"administrative_area_name\":\"administrative_area_name\"," | |
| 81 " \"postal_code_number\":\"postal_code_number\"," | |
| 82 " \"country_name_code\":\"country_name_code\"" | |
| 83 " }" | |
| 84 " }," | |
| 85 " \"shipping_address\":" | |
| 86 " {" | |
| 87 " \"id\":\"ship_id\"," | |
| 88 " \"phone_number\":\"ship_phone_number\"," | |
| 89 " \"postal_address\":" | |
| 90 " {" | |
| 91 " \"recipient_name\":\"ship_recipient_name\"," | |
| 92 " \"address_line\":" | |
| 93 " [" | |
| 94 " \"ship_address_line_1\"," | |
| 95 " \"ship_address_line_2\"" | |
| 96 " ]," | |
| 97 " \"locality_name\":\"ship_locality_name\"," | |
| 98 " \"administrative_area_name\":\"ship_admin_area_name\"," | |
| 99 " \"postal_code_number\":\"ship_postal_code_number\"," | |
| 100 " \"country_name_code\":\"ship_country_name_code\"" | |
| 101 " }" | |
| 102 " }," | |
| 103 " \"required_action\":" | |
| 104 " [" | |
| 105 " ]" | |
| 106 "}"; | |
| 107 | |
| 108 const char kFullWalletMissingExpirationYear[] = | |
| 109 "{" | |
| 110 " \"expiration_month\":12," | |
| 111 " \"iin\":\"iin\"," | |
| 112 " \"rest\":\"rest\"," | |
| 113 " \"billing_address\":" | |
| 114 " {" | |
| 115 " \"id\":\"id\"," | |
| 116 " \"phone_number\":\"phone_number\"," | |
| 117 " \"postal_address\":" | |
| 118 " {" | |
| 119 " \"recipient_name\":\"recipient_name\"," | |
| 120 " \"address_line\":" | |
| 121 " [" | |
| 122 " \"address_line_1\"," | |
| 123 " \"address_line_2\"" | |
| 124 " ]," | |
| 125 " \"locality_name\":\"locality_name\"," | |
| 126 " \"administrative_area_name\":\"administrative_area_name\"," | |
| 127 " \"postal_code_number\":\"postal_code_number\"," | |
| 128 " \"country_name_code\":\"country_name_code\"" | |
| 129 " }" | |
| 130 " }," | |
| 131 " \"shipping_address\":" | |
| 132 " {" | |
| 133 " \"id\":\"ship_id\"," | |
| 134 " \"phone_number\":\"ship_phone_number\"," | |
| 135 " \"postal_address\":" | |
| 136 " {" | |
| 137 " \"recipient_name\":\"ship_recipient_name\"," | |
| 138 " \"address_line\":" | |
| 139 " [" | |
| 140 " \"ship_address_line_1\"," | |
| 141 " \"ship_address_line_2\"" | |
| 142 " ]," | |
| 143 " \"locality_name\":\"ship_locality_name\"," | |
| 144 " \"administrative_area_name\":\"ship_admin_area_name\"," | |
| 145 " \"postal_code_number\":\"ship_postal_code_number\"," | |
| 146 " \"country_name_code\":\"ship_country_name_code\"" | |
| 147 " }" | |
| 148 " }," | |
| 149 " \"required_action\":" | |
| 150 " [" | |
| 151 " ]" | |
| 152 "}"; | |
| 153 | |
| 154 const char kFullWalletMissingIin[] = | |
| 155 "{" | |
| 156 " \"expiration_month\":12," | |
| 157 " \"expiration_year\":2012," | |
| 158 " \"rest\":\"rest\"," | |
| 159 " \"billing_address\":" | |
| 160 " {" | |
| 161 " \"id\":\"id\"," | |
| 162 " \"phone_number\":\"phone_number\"," | |
| 163 " \"postal_address\":" | |
| 164 " {" | |
| 165 " \"recipient_name\":\"recipient_name\"," | |
| 166 " \"address_line\":" | |
| 167 " [" | |
| 168 " \"address_line_1\"," | |
| 169 " \"address_line_2\"" | |
| 170 " ]," | |
| 171 " \"locality_name\":\"locality_name\"," | |
| 172 " \"administrative_area_name\":\"administrative_area_name\"," | |
| 173 " \"postal_code_number\":\"postal_code_number\"," | |
| 174 " \"country_name_code\":\"country_name_code\"" | |
| 175 " }" | |
| 176 " }," | |
| 177 " \"shipping_address\":" | |
| 178 " {" | |
| 179 " \"id\":\"ship_id\"," | |
| 180 " \"phone_number\":\"ship_phone_number\"," | |
| 181 " \"postal_address\":" | |
| 182 " {" | |
| 183 " \"recipient_name\":\"ship_recipient_name\"," | |
| 184 " \"address_line\":" | |
| 185 " [" | |
| 186 " \"ship_address_line_1\"," | |
| 187 " \"ship_address_line_2\"" | |
| 188 " ]," | |
| 189 " \"locality_name\":\"ship_locality_name\"," | |
| 190 " \"administrative_area_name\":\"ship_admin_area_name\"," | |
| 191 " \"postal_code_number\":\"ship_postal_code_number\"," | |
| 192 " \"country_name_code\":\"ship_country_name_code\"" | |
| 193 " }" | |
| 194 " }," | |
| 195 " \"required_action\":" | |
| 196 " [" | |
| 197 " ]" | |
| 198 "}"; | |
| 199 | |
| 200 const char kFullWalletMissingRest[] = | |
| 201 "{" | |
| 202 " \"expiration_month\":12," | |
| 203 " \"expiration_year\":2012," | |
| 204 " \"iin\":\"iin\"," | |
| 205 " \"billing_address\":" | |
| 206 " {" | |
| 207 " \"id\":\"id\"," | |
| 208 " \"phone_number\":\"phone_number\"," | |
| 209 " \"postal_address\":" | |
| 210 " {" | |
| 211 " \"recipient_name\":\"recipient_name\"," | |
| 212 " \"address_line\":" | |
| 213 " [" | |
| 214 " \"address_line_1\"," | |
| 215 " \"address_line_2\"" | |
| 216 " ]," | |
| 217 " \"locality_name\":\"locality_name\"," | |
| 218 " \"administrative_area_name\":\"administrative_area_name\"," | |
| 219 " \"postal_code_number\":\"postal_code_number\"," | |
| 220 " \"country_name_code\":\"country_name_code\"" | |
| 221 " }" | |
| 222 " }," | |
| 223 " \"shipping_address\":" | |
| 224 " {" | |
| 225 " \"id\":\"ship_id\"," | |
| 226 " \"phone_number\":\"ship_phone_number\"," | |
| 227 " \"postal_address\":" | |
| 228 " {" | |
| 229 " \"recipient_name\":\"ship_recipient_name\"," | |
| 230 " \"address_line\":" | |
| 231 " [" | |
| 232 " \"ship_address_line_1\"," | |
| 233 " \"ship_address_line_2\"" | |
| 234 " ]," | |
| 235 " \"locality_name\":\"ship_locality_name\"," | |
| 236 " \"administrative_area_name\":\"ship_admin_area_name\"," | |
| 237 " \"postal_code_number\":\"ship_postal_code_number\"," | |
| 238 " \"country_name_code\":\"ship_country_name_code\"" | |
| 239 " }" | |
| 240 " }," | |
| 241 " \"required_action\":" | |
| 242 " [" | |
| 243 " ]" | |
| 244 "}"; | |
| 245 | |
| 246 const char kFullWalletMissingBillingAddress[] = | |
| 247 "{" | |
| 248 " \"expiration_month\":12," | |
| 249 " \"expiration_year\":2012," | |
| 250 " \"iin\":\"iin\"," | |
| 251 " \"rest\":\"rest\"," | |
| 252 " \"shipping_address\":" | |
| 253 " {" | |
| 254 " \"id\":\"ship_id\"," | |
| 255 " \"phone_number\":\"ship_phone_number\"," | |
| 256 " \"postal_address\":" | |
| 257 " {" | |
| 258 " \"recipient_name\":\"ship_recipient_name\"," | |
| 259 " \"address_line\":" | |
| 260 " [" | |
| 261 " \"ship_address_line_1\"," | |
| 262 " \"ship_address_line_2\"" | |
| 263 " ]," | |
| 264 " \"locality_name\":\"ship_locality_name\"," | |
| 265 " \"administrative_area_name\":\"ship_admin_area_name\"," | |
| 266 " \"postal_code_number\":\"ship_postal_code_number\"," | |
| 267 " \"country_name_code\":\"ship_country_name_code\"" | |
| 268 " }" | |
| 269 " }," | |
| 270 " \"required_action\":" | |
| 271 " [" | |
| 272 " ]" | |
| 273 "}"; | |
| 274 | |
| 275 const char kFullWalletWithRequiredActions[] = | |
| 276 "{" | |
| 277 " \"required_action\":" | |
| 278 " [" | |
| 279 " \"CHOOSE_ANOTHER_INSTRUMENT_OR_ADDRESS\"," | |
| 280 " \"update_EXPIRATION_date\"," | |
| 281 " \"verify_CVV\"," | |
| 282 " \" REQuIrE_PHONE_NumBER\t\n\r \"" | |
| 283 " ]" | |
| 284 "}"; | |
| 285 | |
| 286 const char kFullWalletWithInvalidRequiredActions[] = | |
| 287 "{" | |
| 288 " \"required_action\":" | |
| 289 " [" | |
| 290 " \" setup_wallet\"," | |
| 291 " \"AcCePt_ToS \"," | |
| 292 " \"UPGRADE_MIN_ADDRESS\"," | |
| 293 " \"INVALID_form_field\"," | |
| 294 " \" \\tGAIA_auth \\n\\r\"," | |
| 295 " \"PASSIVE_GAIA_AUTH\"," | |
| 296 " \" 忍者の正体 \"" | |
| 297 " ]" | |
| 298 "}"; | |
| 299 | |
| 300 const char kFullWalletMalformedBillingAddress[] = | |
| 301 "{" | |
| 302 " \"expiration_month\":12," | |
| 303 " \"expiration_year\":2012," | |
| 304 " \"iin\":\"iin\"," | |
| 305 " \"rest\":\"rest\"," | |
| 306 " \"billing_address\":" | |
| 307 " {" | |
| 308 " \"phone_number\":\"phone_number\"," | |
| 309 " \"postal_address\":" | |
| 310 " {" | |
| 311 " \"recipient_name\":\"recipient_name\"," | |
| 312 " \"address_line\":" | |
| 313 " [" | |
| 314 " \"address_line_1\"," | |
| 315 " \"address_line_2\"" | |
| 316 " ]," | |
| 317 " \"locality_name\":\"locality_name\"," | |
| 318 " \"administrative_area_name\":\"administrative_area_name\"" | |
| 319 " }" | |
| 320 " }," | |
| 321 " \"shipping_address\":" | |
| 322 " {" | |
| 323 " \"id\":\"ship_id\"," | |
| 324 " \"phone_number\":\"ship_phone_number\"," | |
| 325 " \"postal_address\":" | |
| 326 " {" | |
| 327 " \"recipient_name\":\"ship_recipient_name\"," | |
| 328 " \"address_line\":" | |
| 329 " [" | |
| 330 " \"ship_address_line_1\"," | |
| 331 " \"ship_address_line_2\"" | |
| 332 " ]," | |
| 333 " \"locality_name\":\"ship_locality_name\"," | |
| 334 " \"administrative_area_name\":\"ship_admin_area_name\"," | |
| 335 " \"postal_code_number\":\"ship_postal_code_number\"," | |
| 336 " \"country_name_code\":\"ship_country_name_code\"" | |
| 337 " }" | |
| 338 " }," | |
| 339 " \"required_action\":" | |
| 340 " [" | |
| 341 " ]" | |
| 342 "}"; | |
| 343 | |
| 344 } // anonymous namespace | |
| 345 | |
| 346 namespace autofill { | |
| 347 namespace wallet { | |
| 348 | |
| 349 class FullWalletTest : public testing::Test { | |
| 350 public: | |
| 351 FullWalletTest() {} | |
| 352 protected: | |
| 353 void SetUpDictionary(const std::string& json) { | |
| 354 scoped_ptr<Value> value(base::JSONReader::Read(json)); | |
| 355 ASSERT_TRUE(value.get()); | |
| 356 ASSERT_TRUE(value->IsType(Value::TYPE_DICTIONARY)); | |
| 357 dict.reset(static_cast<DictionaryValue*>(value.release())); | |
| 358 } | |
| 359 scoped_ptr<DictionaryValue> dict; | |
| 360 }; | |
| 361 | |
| 362 TEST_F(FullWalletTest, CreateFullWalletMissingExpirationMonth) { | |
| 363 SetUpDictionary(kFullWalletMissingExpirationMonth); | |
| 364 EXPECT_EQ(NULL, FullWallet::CreateFullWallet(*dict).get()); | |
| 365 } | |
| 366 | |
| 367 TEST_F(FullWalletTest, CreateFullWalletMissingExpirationYear) { | |
| 368 SetUpDictionary(kFullWalletMissingExpirationYear); | |
| 369 EXPECT_EQ(NULL, FullWallet::CreateFullWallet(*dict).get()); | |
| 370 } | |
| 371 | |
| 372 TEST_F(FullWalletTest, CreateFullWalletMissingIin) { | |
| 373 SetUpDictionary(kFullWalletMissingIin); | |
| 374 EXPECT_EQ(NULL, FullWallet::CreateFullWallet(*dict).get()); | |
| 375 } | |
| 376 | |
| 377 TEST_F(FullWalletTest, CreateFullWalletMissingRest) { | |
| 378 SetUpDictionary(kFullWalletMissingRest); | |
| 379 EXPECT_EQ(NULL, FullWallet::CreateFullWallet(*dict).get()); | |
| 380 } | |
| 381 | |
| 382 TEST_F(FullWalletTest, CreateFullWalletMissingBillingAddress) { | |
| 383 SetUpDictionary(kFullWalletMissingBillingAddress); | |
| 384 EXPECT_EQ(NULL, FullWallet::CreateFullWallet(*dict).get()); | |
| 385 } | |
| 386 | |
| 387 TEST_F(FullWalletTest, CreateFullWalletMalformedBillingAddress) { | |
| 388 SetUpDictionary(kFullWalletMalformedBillingAddress); | |
| 389 EXPECT_EQ(NULL, FullWallet::CreateFullWallet(*dict).get()); | |
| 390 } | |
| 391 | |
| 392 TEST_F(FullWalletTest, CreateFullWalletWithRequiredActions) { | |
| 393 SetUpDictionary(kFullWalletWithRequiredActions); | |
| 394 | |
| 395 std::vector<RequiredAction> required_actions; | |
| 396 required_actions.push_back(CHOOSE_ANOTHER_INSTRUMENT_OR_ADDRESS); | |
| 397 required_actions.push_back(UPDATE_EXPIRATION_DATE); | |
| 398 required_actions.push_back(VERIFY_CVV); | |
| 399 required_actions.push_back(REQUIRE_PHONE_NUMBER); | |
| 400 | |
| 401 FullWallet full_wallet(-1, | |
| 402 -1, | |
| 403 "", | |
| 404 "", | |
| 405 scoped_ptr<Address>(), | |
| 406 scoped_ptr<Address>(), | |
| 407 required_actions); | |
| 408 EXPECT_EQ(full_wallet, *FullWallet::CreateFullWallet(*dict)); | |
| 409 | |
| 410 ASSERT_FALSE(required_actions.empty()); | |
| 411 required_actions.pop_back(); | |
| 412 FullWallet different_required_actions( | |
| 413 -1, | |
| 414 -1, | |
| 415 "", | |
| 416 "", | |
| 417 scoped_ptr<Address>(), | |
| 418 scoped_ptr<Address>(), | |
| 419 required_actions); | |
| 420 EXPECT_NE(full_wallet, different_required_actions); | |
| 421 } | |
| 422 | |
| 423 TEST_F(FullWalletTest, CreateFullWalletWithInvalidRequiredActions) { | |
| 424 SetUpDictionary(kFullWalletWithInvalidRequiredActions); | |
| 425 EXPECT_EQ(NULL, FullWallet::CreateFullWallet(*dict).get()); | |
| 426 } | |
| 427 | |
| 428 TEST_F(FullWalletTest, CreateFullWallet) { | |
| 429 SetUpDictionary(kFullWalletValidResponse); | |
| 430 // NOTE: FullWallet billing address doesn't require an ID. | |
| 431 scoped_ptr<Address> billing_address(new Address( | |
| 432 "country_name_code", | |
| 433 ASCIIToUTF16("recipient_name"), | |
| 434 ASCIIToUTF16("address_line_1"), | |
| 435 ASCIIToUTF16("address_line_2"), | |
| 436 ASCIIToUTF16("locality_name"), | |
| 437 ASCIIToUTF16("administrative_area_name"), | |
| 438 ASCIIToUTF16("postal_code_number"), | |
| 439 ASCIIToUTF16("phone_number"), | |
| 440 "")); | |
| 441 scoped_ptr<Address> shipping_address(new Address( | |
| 442 "ship_country_name_code", | |
| 443 ASCIIToUTF16("ship_recipient_name"), | |
| 444 ASCIIToUTF16("ship_address_line_1"), | |
| 445 ASCIIToUTF16("ship_address_line_2"), | |
| 446 ASCIIToUTF16("ship_locality_name"), | |
| 447 ASCIIToUTF16("ship_admin_area_name"), | |
| 448 ASCIIToUTF16("ship_postal_code_number"), | |
| 449 ASCIIToUTF16("ship_phone_number"), | |
| 450 "ship_id")); | |
| 451 std::vector<RequiredAction> required_actions; | |
| 452 FullWallet full_wallet(12, | |
| 453 2012, | |
| 454 "iin", | |
| 455 "rest", | |
| 456 billing_address.Pass(), | |
| 457 shipping_address.Pass(), | |
| 458 required_actions); | |
| 459 EXPECT_EQ(full_wallet, *FullWallet::CreateFullWallet(*dict)); | |
| 460 } | |
| 461 | |
| 462 // TODO(ahutter): Add tests for GetPan and GetCvn. | |
| 463 | |
| 464 } // namespace wallet | |
| 465 } // namespace autofill | |
| OLD | NEW |