wallet_client_unittest.cc revision a36e5920737c6adbddd3e43b760e5de8431db6e0
1// Copyright 2013 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/json/json_writer.h" 7#include "base/logging.h" 8#include "base/memory/scoped_ptr.h" 9#include "base/run_loop.h" 10#include "base/strings/string_number_conversions.h" 11#include "base/strings/string_split.h" 12#include "base/strings/string_util.h" 13#include "base/values.h" 14#include "chrome/test/base/testing_profile.h" 15#include "components/autofill/content/browser/autocheckout_steps.h" 16#include "components/autofill/content/browser/wallet/full_wallet.h" 17#include "components/autofill/content/browser/wallet/instrument.h" 18#include "components/autofill/content/browser/wallet/wallet_client.h" 19#include "components/autofill/content/browser/wallet/wallet_client_delegate.h" 20#include "components/autofill/content/browser/wallet/wallet_items.h" 21#include "components/autofill/content/browser/wallet/wallet_test_util.h" 22#include "components/autofill/core/browser/autofill_metrics.h" 23#include "components/autofill/core/common/autocheckout_status.h" 24#include "content/public/test/test_browser_thread_bundle.h" 25#include "net/base/escape.h" 26#include "net/base/net_errors.h" 27#include "net/http/http_request_headers.h" 28#include "net/http/http_status_code.h" 29#include "net/url_request/test_url_fetcher_factory.h" 30#include "net/url_request/url_fetcher_delegate.h" 31#include "net/url_request/url_request_status.h" 32#include "testing/gmock/include/gmock/gmock.h" 33#include "testing/gtest/include/gtest/gtest.h" 34#include "url/gurl.h" 35 36namespace autofill { 37namespace wallet { 38 39namespace { 40 41const char kGoogleTransactionId[] = "google-transaction-id"; 42const char kMerchantUrl[] = "https://example.com/path?key=value"; 43 44const char kGetFullWalletValidResponse[] = 45 "{" 46 " \"expiration_month\":12," 47 " \"expiration_year\":3000," 48 " \"iin\":\"iin\"," 49 " \"rest\":\"rest\"," 50 " \"billing_address\":" 51 " {" 52 " \"id\":\"id\"," 53 " \"phone_number\":\"phone_number\"," 54 " \"postal_address\":" 55 " {" 56 " \"recipient_name\":\"recipient_name\"," 57 " \"address_line\":" 58 " [" 59 " \"address_line_1\"," 60 " \"address_line_2\"" 61 " ]," 62 " \"locality_name\":\"locality_name\"," 63 " \"administrative_area_name\":\"administrative_area_name\"," 64 " \"postal_code_number\":\"postal_code_number\"," 65 " \"country_name_code\":\"US\"" 66 " }" 67 " }," 68 " \"shipping_address\":" 69 " {" 70 " \"id\":\"ship_id\"," 71 " \"phone_number\":\"ship_phone_number\"," 72 " \"postal_address\":" 73 " {" 74 " \"recipient_name\":\"ship_recipient_name\"," 75 " \"address_line\":" 76 " [" 77 " \"ship_address_line_1\"," 78 " \"ship_address_line_2\"" 79 " ]," 80 " \"locality_name\":\"ship_locality_name\"," 81 " \"administrative_area_name\":\"ship_administrative_area_name\"," 82 " \"postal_code_number\":\"ship_postal_code_number\"," 83 " \"country_name_code\":\"US\"" 84 " }" 85 " }," 86 " \"required_action\":" 87 " [" 88 " ]" 89 "}"; 90 91const char kGetFullWalletInvalidResponse[] = 92 "{" 93 " \"garbage\":123" 94 "}"; 95 96const char kGetWalletItemsValidResponse[] = 97 "{" 98 " \"required_action\":" 99 " [" 100 " ]," 101 " \"google_transaction_id\":\"google_transaction_id\"," 102 " \"instrument\":" 103 " [" 104 " {" 105 " \"descriptive_name\":\"descriptive_name\"," 106 " \"type\":\"VISA\"," 107 " \"supported_currency\":\"currency_code\"," 108 " \"last_four_digits\":\"4111\"," 109 " \"expiration_month\":12," 110 " \"expiration_year\":3000," 111 " \"brand\":\"monkeys\"," 112 " \"billing_address\":" 113 " {" 114 " \"name\":\"name\"," 115 " \"address1\":\"address1\"," 116 " \"address2\":\"address2\"," 117 " \"city\":\"city\"," 118 " \"state\":\"state\"," 119 " \"postal_code\":\"postal_code\"," 120 " \"phone_number\":\"phone_number\"," 121 " \"country_code\":\"country_code\"" 122 " }," 123 " \"status\":\"VALID\"," 124 " \"object_id\":\"default_instrument_id\"" 125 " }" 126 " ]," 127 " \"default_instrument_id\":\"default_instrument_id\"," 128 " \"obfuscated_gaia_id\":\"obfuscated_gaia_id\"," 129 " \"address\":" 130 " [" 131 " ]," 132 " \"default_address_id\":\"default_address_id\"," 133 " \"required_legal_document\":" 134 " [" 135 " ]" 136 "}"; 137 138const char kSaveAddressValidResponse[] = 139 "{" 140 " \"shipping_address_id\":\"saved_address_id\"" 141 "}"; 142 143const char kSaveAddressWithRequiredActionsValidResponse[] = 144 "{" 145 " \"form_field_error\":" 146 " [" 147 " {" 148 " \"location\":\"SHIPPING_ADDRESS\"," 149 " \"type\":\"INVALID_POSTAL_CODE\"" 150 " }" 151 " ]," 152 " \"required_action\":" 153 " [" 154 " \" \\treqUIRE_PhOnE_number \\n\\r\"," 155 " \"INVALID_form_field\"" 156 " ]" 157 "}"; 158 159const char kSaveWithInvalidRequiredActionsResponse[] = 160 "{" 161 " \"required_action\":" 162 " [" 163 " \" setup_wallet\"," 164 " \" \\treqUIRE_PhOnE_number \\n\\r\"," 165 " \"INVALID_form_field\"" 166 " ]" 167 "}"; 168 169const char kSaveInvalidResponse[] = 170 "{" 171 " \"garbage\":123" 172 "}"; 173 174const char kSaveInstrumentValidResponse[] = 175 "{" 176 " \"instrument_id\":\"instrument_id\"" 177 "}"; 178 179const char kSaveInstrumentWithRequiredActionsValidResponse[] = 180 "{" 181 " \"form_field_error\":" 182 " [" 183 " {" 184 " \"location\":\"SHIPPING_ADDRESS\"," 185 " \"type\":\"INVALID_POSTAL_CODE\"" 186 " }" 187 " ]," 188 " \"required_action\":" 189 " [" 190 " \" \\treqUIRE_PhOnE_number \\n\\r\"," 191 " \"INVALID_form_field\"" 192 " ]" 193 "}"; 194 195const char kSaveInstrumentAndAddressValidResponse[] = 196 "{" 197 " \"shipping_address_id\":\"saved_address_id\"," 198 " \"instrument_id\":\"saved_instrument_id\"" 199 "}"; 200 201const char kSaveInstrumentAndAddressWithRequiredActionsValidResponse[] = 202 "{" 203 " \"form_field_error\":" 204 " [" 205 " {" 206 " \"location\":\"SHIPPING_ADDRESS\"," 207 " \"type\":\"INVALID_POSTAL_CODE\"" 208 " }" 209 " ]," 210 " \"required_action\":" 211 " [" 212 " \" \\treqUIRE_PhOnE_number \\n\\r\"," 213 " \"INVALID_form_field\"" 214 " ]" 215 "}"; 216 217const char kUpdateInstrumentValidResponse[] = 218 "{" 219 " \"instrument_id\":\"instrument_id\"" 220 "}"; 221 222const char kUpdateAddressValidResponse[] = 223 "{" 224 " \"shipping_address_id\":\"shipping_address_id\"" 225 "}"; 226 227const char kUpdateWithRequiredActionsValidResponse[] = 228 "{" 229 " \"form_field_error\":" 230 " [" 231 " {" 232 " \"location\":\"SHIPPING_ADDRESS\"," 233 " \"type\":\"INVALID_POSTAL_CODE\"" 234 " }" 235 " ]," 236 " \"required_action\":" 237 " [" 238 " \" \\treqUIRE_PhOnE_number \\n\\r\"," 239 " \"INVALID_form_field\"" 240 " ]" 241 "}"; 242 243const char kUpdateMalformedResponse[] = 244 "{" 245 " \"cheese\":\"monkeys\"" 246 "}"; 247 248const char kAuthenticateInstrumentFailureResponse[] = 249 "{" 250 " \"auth_result\":\"anything else\"" 251 "}"; 252 253const char kAuthenticateInstrumentSuccessResponse[] = 254 "{" 255 " \"auth_result\":\"SUCCESS\"" 256 "}"; 257 258const char kErrorResponse[] = 259 "{" 260 " \"error_type\":\"APPLICATION_ERROR\"," 261 " \"error_detail\":\"error_detail\"," 262 " \"application_error\":\"application_error\"," 263 " \"debug_data\":" 264 " {" 265 " \"debug_message\":\"debug_message\"," 266 " \"stack_trace\":\"stack_trace\"" 267 " }," 268 " \"application_error_data\":\"application_error_data\"," 269 " \"wallet_error\":" 270 " {" 271 " \"error_type\":\"SERVICE_UNAVAILABLE\"," 272 " \"error_detail\":\"error_detail\"," 273 " \"message_for_user\":" 274 " {" 275 " \"text\":\"text\"," 276 " \"subtext\":\"subtext\"," 277 " \"details\":\"details\"" 278 " }" 279 " }" 280 "}"; 281 282const char kErrorTypeMissingInResponse[] = 283 "{" 284 " \"error_type\":\"Not APPLICATION_ERROR\"," 285 " \"error_detail\":\"error_detail\"," 286 " \"application_error\":\"application_error\"," 287 " \"debug_data\":" 288 " {" 289 " \"debug_message\":\"debug_message\"," 290 " \"stack_trace\":\"stack_trace\"" 291 " }," 292 " \"application_error_data\":\"application_error_data\"" 293 "}"; 294 295// The JSON below is used to test against the request payload being sent to 296// Online Wallet. It's indented differently since JSONWriter creates compact 297// JSON from DictionaryValues. 298 299const char kAcceptLegalDocumentsValidRequest[] = 300 "{" 301 "\"accepted_legal_document\":" 302 "[" 303 "\"doc_id_1\"," 304 "\"doc_id_2\"" 305 "]," 306 "\"google_transaction_id\":\"google-transaction-id\"," 307 "\"merchant_domain\":\"https://example.com/\"" 308 "}"; 309 310const char kAuthenticateInstrumentValidRequest[] = 311 "{" 312 "\"instrument_id\":\"instrument_id\"," 313 "\"risk_params\":\"risky business\"" 314 "}"; 315 316const char kGetFullWalletValidRequest[] = 317 "{" 318 "\"feature\":\"REQUEST_AUTOCOMPLETE\"," 319 "\"google_transaction_id\":\"google_transaction_id\"," 320 "\"merchant_domain\":\"https://example.com/\"," 321 "\"phone_number_required\":true," 322 "\"risk_params\":\"risky business\"," 323 "\"selected_address_id\":\"shipping_address_id\"," 324 "\"selected_instrument_id\":\"instrument_id\"," 325 "\"supported_risk_challenge\":" 326 "[" 327 "]," 328 "\"use_minimal_addresses\":false" 329 "}"; 330 331const char kGetFullWalletWithRiskCapabilitesValidRequest[] = 332 "{" 333 "\"feature\":\"REQUEST_AUTOCOMPLETE\"," 334 "\"google_transaction_id\":\"google_transaction_id\"," 335 "\"merchant_domain\":\"https://example.com/\"," 336 "\"phone_number_required\":true," 337 "\"risk_params\":\"risky business\"," 338 "\"selected_address_id\":\"shipping_address_id\"," 339 "\"selected_instrument_id\":\"instrument_id\"," 340 "\"supported_risk_challenge\":" 341 "[" 342 "\"VERIFY_CVC\"" 343 "]," 344 "\"use_minimal_addresses\":false" 345 "}"; 346 347const char kGetWalletItemsValidRequest[] = 348 "{" 349 "\"merchant_domain\":\"https://example.com/\"," 350 "\"phone_number_required\":true," 351 "\"shipping_address_required\":true," 352 "\"use_minimal_addresses\":false" 353 "}"; 354 355const char kGetWalletItemsNoShippingRequest[] = 356 "{" 357 "\"merchant_domain\":\"https://example.com/\"," 358 "\"phone_number_required\":true," 359 "\"shipping_address_required\":false," 360 "\"use_minimal_addresses\":false" 361 "}"; 362 363const char kSaveAddressValidRequest[] = 364 "{" 365 "\"merchant_domain\":\"https://example.com/\"," 366 "\"phone_number_required\":true," 367 "\"risk_params\":\"risky business\"," 368 "\"shipping_address\":" 369 "{" 370 "\"phone_number\":\"save_phone_number\"," 371 "\"postal_address\":" 372 "{" 373 "\"address_line\":" 374 "[" 375 "\"save_address_line_1\"," 376 "\"save_address_line_2\"" 377 "]," 378 "\"administrative_area_name\":\"save_admin_area_name\"," 379 "\"country_name_code\":\"US\"," 380 "\"locality_name\":\"save_locality_name\"," 381 "\"postal_code_number\":\"save_postal_code_number\"," 382 "\"recipient_name\":\"save_recipient_name\"" 383 "}" 384 "}," 385 "\"use_minimal_addresses\":false" 386 "}"; 387 388const char kSaveInstrumentValidRequest[] = 389 "{" 390 "\"instrument\":" 391 "{" 392 "\"credit_card\":" 393 "{" 394 "\"address\":" 395 "{" 396 "\"address_line\":" 397 "[" 398 "\"address_line_1\"," 399 "\"address_line_2\"" 400 "]," 401 "\"administrative_area_name\":\"admin_area_name\"," 402 "\"country_name_code\":\"US\"," 403 "\"locality_name\":\"locality_name\"," 404 "\"postal_code_number\":\"postal_code_number\"," 405 "\"recipient_name\":\"recipient_name\"" 406 "}," 407 "\"exp_month\":12," 408 "\"exp_year\":3000," 409 "\"fop_type\":\"VISA\"," 410 "\"last_4_digits\":\"4448\"" 411 "}," 412 "\"type\":\"CREDIT_CARD\"" 413 "}," 414 "\"instrument_phone_number\":\"phone_number\"," 415 "\"merchant_domain\":\"https://example.com/\"," 416 "\"phone_number_required\":true," 417 "\"risk_params\":\"risky business\"," 418 "\"use_minimal_addresses\":false" 419 "}"; 420 421const char kSaveInstrumentAndAddressValidRequest[] = 422 "{" 423 "\"instrument\":" 424 "{" 425 "\"credit_card\":" 426 "{" 427 "\"address\":" 428 "{" 429 "\"address_line\":" 430 "[" 431 "\"address_line_1\"," 432 "\"address_line_2\"" 433 "]," 434 "\"administrative_area_name\":\"admin_area_name\"," 435 "\"country_name_code\":\"US\"," 436 "\"locality_name\":\"locality_name\"," 437 "\"postal_code_number\":\"postal_code_number\"," 438 "\"recipient_name\":\"recipient_name\"" 439 "}," 440 "\"exp_month\":12," 441 "\"exp_year\":3000," 442 "\"fop_type\":\"VISA\"," 443 "\"last_4_digits\":\"4448\"" 444 "}," 445 "\"type\":\"CREDIT_CARD\"" 446 "}," 447 "\"instrument_phone_number\":\"phone_number\"," 448 "\"merchant_domain\":\"https://example.com/\"," 449 "\"phone_number_required\":true," 450 "\"risk_params\":\"risky business\"," 451 "\"shipping_address\":" 452 "{" 453 "\"phone_number\":\"save_phone_number\"," 454 "\"postal_address\":" 455 "{" 456 "\"address_line\":" 457 "[" 458 "\"save_address_line_1\"," 459 "\"save_address_line_2\"" 460 "]," 461 "\"administrative_area_name\":\"save_admin_area_name\"," 462 "\"country_name_code\":\"US\"," 463 "\"locality_name\":\"save_locality_name\"," 464 "\"postal_code_number\":\"save_postal_code_number\"," 465 "\"recipient_name\":\"save_recipient_name\"" 466 "}" 467 "}," 468 "\"use_minimal_addresses\":false" 469 "}"; 470 471const char kSendAutocheckoutStatusOfSuccessValidRequest[] = 472 "{" 473 "\"google_transaction_id\":\"google_transaction_id\"," 474 "\"merchant_domain\":\"https://example.com/\"," 475 "\"success\":true" 476 "}"; 477 478const char kSendAutocheckoutStatusWithStatisticsValidRequest[] = 479 "{" 480 "\"google_transaction_id\":\"google_transaction_id\"," 481 "\"merchant_domain\":\"https://example.com/\"," 482 "\"steps\":[{\"step_description\":\"1_AUTOCHECKOUT_STEP_SHIPPING\"" 483 ",\"time_taken\":100}]," 484 "\"success\":true" 485 "}"; 486 487const char kSendAutocheckoutStatusOfFailureValidRequest[] = 488 "{" 489 "\"google_transaction_id\":\"google_transaction_id\"," 490 "\"merchant_domain\":\"https://example.com/\"," 491 "\"reason\":\"CANNOT_PROCEED\"," 492 "\"success\":false" 493 "}"; 494 495const char kUpdateAddressValidRequest[] = 496 "{" 497 "\"merchant_domain\":\"https://example.com/\"," 498 "\"phone_number_required\":true," 499 "\"risk_params\":\"risky business\"," 500 "\"shipping_address\":" 501 "{" 502 "\"id\":\"shipping_address_id\"," 503 "\"phone_number\":\"ship_phone_number\"," 504 "\"postal_address\":" 505 "{" 506 "\"address_line\":" 507 "[" 508 "\"ship_address_line_1\"," 509 "\"ship_address_line_2\"" 510 "]," 511 "\"administrative_area_name\":\"ship_admin_area_name\"," 512 "\"country_name_code\":\"US\"," 513 "\"locality_name\":\"ship_locality_name\"," 514 "\"postal_code_number\":\"ship_postal_code_number\"," 515 "\"recipient_name\":\"ship_recipient_name\"" 516 "}" 517 "}," 518 "\"use_minimal_addresses\":false" 519 "}"; 520 521const char kUpdateInstrumentAddressValidRequest[] = 522 "{" 523 "\"instrument_phone_number\":\"phone_number\"," 524 "\"merchant_domain\":\"https://example.com/\"," 525 "\"phone_number_required\":true," 526 "\"risk_params\":\"risky business\"," 527 "\"upgraded_billing_address\":" 528 "{" 529 "\"address_line\":" 530 "[" 531 "\"address_line_1\"," 532 "\"address_line_2\"" 533 "]," 534 "\"administrative_area_name\":\"admin_area_name\"," 535 "\"country_name_code\":\"US\"," 536 "\"locality_name\":\"locality_name\"," 537 "\"postal_code_number\":\"postal_code_number\"," 538 "\"recipient_name\":\"recipient_name\"" 539 "}," 540 "\"upgraded_instrument_id\":\"instrument_id\"," 541 "\"use_minimal_addresses\":false" 542 "}"; 543 544const char kUpdateInstrumentAddressWithNameChangeValidRequest[] = 545 "{" 546 "\"instrument_phone_number\":\"phone_number\"," 547 "\"merchant_domain\":\"https://example.com/\"," 548 "\"phone_number_required\":true," 549 "\"risk_params\":\"risky business\"," 550 "\"upgraded_billing_address\":" 551 "{" 552 "\"address_line\":" 553 "[" 554 "\"address_line_1\"," 555 "\"address_line_2\"" 556 "]," 557 "\"administrative_area_name\":\"admin_area_name\"," 558 "\"country_name_code\":\"US\"," 559 "\"locality_name\":\"locality_name\"," 560 "\"postal_code_number\":\"postal_code_number\"," 561 "\"recipient_name\":\"recipient_name\"" 562 "}," 563 "\"upgraded_instrument_id\":\"instrument_id\"," 564 "\"use_minimal_addresses\":false" 565 "}"; 566 567const char kUpdateInstrumentExpirationDateValidRequest[] = 568 "{" 569 "\"instrument\":" 570 "{" 571 "\"credit_card\":" 572 "{" 573 "\"exp_month\":12," 574 "\"exp_year\":3000" 575 "}," 576 "\"type\":\"CREDIT_CARD\"" 577 "}," 578 "\"merchant_domain\":\"https://example.com/\"," 579 "\"phone_number_required\":true," 580 "\"risk_params\":\"risky business\"," 581 "\"upgraded_instrument_id\":\"instrument_id\"," 582 "\"use_minimal_addresses\":false" 583 "}"; 584 585class MockAutofillMetrics : public AutofillMetrics { 586 public: 587 MockAutofillMetrics() {} 588 MOCK_CONST_METHOD2(LogWalletApiCallDuration, 589 void(WalletApiCallMetric metric, 590 const base::TimeDelta& duration)); 591 MOCK_CONST_METHOD2(LogWalletErrorMetric, 592 void(DialogType dialog_type, WalletErrorMetric metric)); 593 MOCK_CONST_METHOD2(LogWalletRequiredActionMetric, 594 void(DialogType dialog_type, 595 WalletRequiredActionMetric action)); 596 private: 597 DISALLOW_COPY_AND_ASSIGN(MockAutofillMetrics); 598}; 599 600class MockWalletClientDelegate : public WalletClientDelegate { 601 public: 602 MockWalletClientDelegate() 603 : full_wallets_received_(0), 604 wallet_items_received_(0), 605 is_shipping_required_(true) {} 606 ~MockWalletClientDelegate() {} 607 608 virtual const AutofillMetrics& GetMetricLogger() const OVERRIDE { 609 return metric_logger_; 610 } 611 612 virtual DialogType GetDialogType() const OVERRIDE { 613 return DIALOG_TYPE_REQUEST_AUTOCOMPLETE; 614 } 615 616 virtual std::string GetRiskData() const OVERRIDE { 617 return "risky business"; 618 } 619 620 virtual std::string GetWalletCookieValue() const OVERRIDE { 621 return "gdToken"; 622 } 623 624 virtual bool IsShippingAddressRequired() const OVERRIDE { 625 return is_shipping_required_; 626 } 627 628 void SetIsShippingAddressRequired(bool is_shipping_required) { 629 is_shipping_required_ = is_shipping_required; 630 } 631 632 void ExpectLogWalletApiCallDuration( 633 AutofillMetrics::WalletApiCallMetric metric, 634 size_t times) { 635 EXPECT_CALL(metric_logger_, 636 LogWalletApiCallDuration(metric, testing::_)).Times(times); 637 } 638 639 void ExpectWalletErrorMetric(AutofillMetrics::WalletErrorMetric metric) { 640 EXPECT_CALL( 641 metric_logger_, 642 LogWalletErrorMetric( 643 DIALOG_TYPE_REQUEST_AUTOCOMPLETE, metric)).Times(1); 644 } 645 646 void ExpectWalletRequiredActionMetric( 647 AutofillMetrics::WalletRequiredActionMetric metric) { 648 EXPECT_CALL( 649 metric_logger_, 650 LogWalletRequiredActionMetric( 651 DIALOG_TYPE_REQUEST_AUTOCOMPLETE, metric)).Times(1); 652 } 653 654 void ExpectBaselineMetrics() { 655 EXPECT_CALL( 656 metric_logger_, 657 LogWalletErrorMetric( 658 DIALOG_TYPE_REQUEST_AUTOCOMPLETE, 659 AutofillMetrics::WALLET_ERROR_BASELINE_ISSUED_REQUEST)) 660 .Times(1); 661 ExpectWalletRequiredActionMetric( 662 AutofillMetrics::WALLET_REQUIRED_ACTION_BASELINE_ISSUED_REQUEST); 663 } 664 665 MockAutofillMetrics* metric_logger() { 666 return &metric_logger_; 667 } 668 669 MOCK_METHOD0(OnDidAcceptLegalDocuments, void()); 670 MOCK_METHOD1(OnDidAuthenticateInstrument, void(bool success)); 671 MOCK_METHOD4(OnDidSaveToWallet, 672 void(const std::string& instrument_id, 673 const std::string& shipping_address_id, 674 const std::vector<RequiredAction>& required_actions, 675 const std::vector<FormFieldError>& form_field_errors)); 676 MOCK_METHOD1(OnWalletError, void(WalletClient::ErrorType error_type)); 677 678 virtual void OnDidGetFullWallet(scoped_ptr<FullWallet> full_wallet) OVERRIDE { 679 EXPECT_TRUE(full_wallet); 680 ++full_wallets_received_; 681 } 682 virtual void OnDidGetWalletItems(scoped_ptr<WalletItems> wallet_items) 683 OVERRIDE { 684 EXPECT_TRUE(wallet_items); 685 ++wallet_items_received_; 686 } 687 size_t full_wallets_received() const { return full_wallets_received_; } 688 size_t wallet_items_received() const { return wallet_items_received_; } 689 690 private: 691 size_t full_wallets_received_; 692 size_t wallet_items_received_; 693 bool is_shipping_required_; 694 695 testing::StrictMock<MockAutofillMetrics> metric_logger_; 696}; 697 698} // namespace 699 700class WalletClientTest : public testing::Test { 701 public: 702 virtual void SetUp() OVERRIDE { 703 wallet_client_.reset( 704 new WalletClient(browser_context_.GetRequestContext(), &delegate_)); 705 } 706 707 virtual void TearDown() OVERRIDE { 708 wallet_client_.reset(); 709 } 710 711 void VerifyAndFinishRequest(net::HttpStatusCode response_code, 712 const std::string& request_body, 713 const std::string& response_body) { 714 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); 715 ASSERT_TRUE(fetcher); 716 717 const std::string& upload_data = fetcher->upload_data(); 718 EXPECT_EQ(request_body, GetData(upload_data)); 719 net::HttpRequestHeaders request_headers; 720 fetcher->GetExtraRequestHeaders(&request_headers); 721 std::string auth_header_value; 722 EXPECT_TRUE(request_headers.GetHeader( 723 net::HttpRequestHeaders::kAuthorization, 724 &auth_header_value)); 725 EXPECT_EQ("GoogleLogin auth=gdToken", auth_header_value); 726 727 fetcher->set_response_code(response_code); 728 fetcher->SetResponseString(response_body); 729 fetcher->delegate()->OnURLFetchComplete(fetcher); 730 731 // Pump the message loop to catch up to any asynchronous tasks that might 732 // have been posted from OnURLFetchComplete(). 733 base::RunLoop().RunUntilIdle(); 734 } 735 736 void VerifyAndFinishFormEncodedRequest(net::HttpStatusCode response_code, 737 const std::string& json_payload, 738 const std::string& response_body, 739 size_t expected_parameter_number) { 740 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); 741 ASSERT_TRUE(fetcher); 742 743 net::HttpRequestHeaders request_headers; 744 fetcher->GetExtraRequestHeaders(&request_headers); 745 std::string auth_header_value; 746 EXPECT_TRUE(request_headers.GetHeader( 747 net::HttpRequestHeaders::kAuthorization, 748 &auth_header_value)); 749 EXPECT_EQ("GoogleLogin auth=gdToken", auth_header_value); 750 751 const std::string& upload_data = fetcher->upload_data(); 752 std::vector<std::pair<std::string, std::string> > tokens; 753 base::SplitStringIntoKeyValuePairs(upload_data, '=', '&', &tokens); 754 EXPECT_EQ(tokens.size(), expected_parameter_number); 755 756 size_t num_params = 0U; 757 for (size_t i = 0; i < tokens.size(); ++i) { 758 const std::string& key = tokens[i].first; 759 const std::string& value = tokens[i].second; 760 761 if (key == "request_content_type") { 762 EXPECT_EQ("application/json", value); 763 num_params++; 764 } 765 766 if (key == "request") { 767 EXPECT_EQ(json_payload, 768 GetData( 769 net::UnescapeURLComponent( 770 value, net::UnescapeRule::URL_SPECIAL_CHARS | 771 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE))); 772 num_params++; 773 } 774 775 if (key == "cvn") { 776 EXPECT_EQ("123", value); 777 num_params++; 778 } 779 780 if (key == "card_number") { 781 EXPECT_EQ("4444444444444448", value); 782 num_params++; 783 } 784 785 if (key == "otp") { 786 EXPECT_FALSE(value.empty()); 787 num_params++; 788 } 789 } 790 EXPECT_EQ(expected_parameter_number, num_params); 791 792 fetcher->set_response_code(response_code); 793 fetcher->SetResponseString(response_body); 794 fetcher->delegate()->OnURLFetchComplete(fetcher); 795 } 796 797 protected: 798 content::TestBrowserThreadBundle thread_bundle_; 799 scoped_ptr<WalletClient> wallet_client_; 800 TestingProfile browser_context_; 801 MockWalletClientDelegate delegate_; 802 803 private: 804 std::string GetData(const std::string& upload_data) { 805 scoped_ptr<Value> root(base::JSONReader::Read(upload_data)); 806 807 // If this is not a JSON dictionary, return plain text. 808 if (!root || !root->IsType(Value::TYPE_DICTIONARY)) 809 return upload_data; 810 811 // Remove api_key entry (to prevent accidental leak), return JSON as text. 812 DictionaryValue* dict = static_cast<DictionaryValue*>(root.get()); 813 dict->Remove("api_key", NULL); 814 std::string clean_upload_data; 815 base::JSONWriter::Write(dict, &clean_upload_data); 816 return clean_upload_data; 817 } 818 819 net::TestURLFetcherFactory factory_; 820}; 821 822TEST_F(WalletClientTest, WalletError) { 823 EXPECT_CALL(delegate_, OnWalletError( 824 WalletClient::SERVICE_UNAVAILABLE)).Times(1); 825 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SEND_STATUS, 1); 826 delegate_.ExpectBaselineMetrics(); 827 delegate_.ExpectWalletErrorMetric( 828 AutofillMetrics::WALLET_SERVICE_UNAVAILABLE); 829 830 std::vector<AutocheckoutStatistic> statistics; 831 wallet_client_->SendAutocheckoutStatus(autofill::SUCCESS, 832 GURL(kMerchantUrl), 833 statistics, 834 "google_transaction_id"); 835 VerifyAndFinishRequest(net::HTTP_INTERNAL_SERVER_ERROR, 836 kSendAutocheckoutStatusOfSuccessValidRequest, 837 kErrorResponse); 838} 839 840TEST_F(WalletClientTest, WalletErrorResponseMissing) { 841 EXPECT_CALL(delegate_, OnWalletError( 842 WalletClient::UNKNOWN_ERROR)).Times(1); 843 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SEND_STATUS, 1); 844 delegate_.ExpectBaselineMetrics(); 845 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_UNKNOWN_ERROR); 846 847 std::vector<AutocheckoutStatistic> statistics; 848 wallet_client_->SendAutocheckoutStatus(autofill::SUCCESS, 849 GURL(kMerchantUrl), 850 statistics, 851 "google_transaction_id"); 852 VerifyAndFinishRequest(net::HTTP_INTERNAL_SERVER_ERROR, 853 kSendAutocheckoutStatusOfSuccessValidRequest, 854 kErrorTypeMissingInResponse); 855} 856 857TEST_F(WalletClientTest, NetworkFailureOnExpectedVoidResponse) { 858 EXPECT_CALL(delegate_, OnWalletError(WalletClient::NETWORK_ERROR)).Times(1); 859 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SEND_STATUS, 1); 860 delegate_.ExpectBaselineMetrics(); 861 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_NETWORK_ERROR); 862 863 std::vector<AutocheckoutStatistic> statistics; 864 wallet_client_->SendAutocheckoutStatus(autofill::SUCCESS, 865 GURL(kMerchantUrl), 866 statistics, 867 "google_transaction_id"); 868 VerifyAndFinishRequest(net::HTTP_UNAUTHORIZED, 869 kSendAutocheckoutStatusOfSuccessValidRequest, 870 std::string()); 871} 872 873TEST_F(WalletClientTest, NetworkFailureOnExpectedResponse) { 874 EXPECT_CALL(delegate_, OnWalletError(WalletClient::NETWORK_ERROR)).Times(1); 875 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS, 876 1); 877 delegate_.ExpectBaselineMetrics(); 878 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_NETWORK_ERROR); 879 880 wallet_client_->GetWalletItems(GURL(kMerchantUrl)); 881 VerifyAndFinishRequest(net::HTTP_UNAUTHORIZED, 882 kGetWalletItemsValidRequest, 883 std::string()); 884} 885 886TEST_F(WalletClientTest, RequestError) { 887 EXPECT_CALL(delegate_, OnWalletError(WalletClient::BAD_REQUEST)).Times(1); 888 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SEND_STATUS, 1); 889 delegate_.ExpectBaselineMetrics(); 890 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_BAD_REQUEST); 891 892 std::vector<AutocheckoutStatistic> statistics; 893 wallet_client_->SendAutocheckoutStatus(autofill::SUCCESS, 894 GURL(kMerchantUrl), 895 statistics, 896 "google_transaction_id"); 897 VerifyAndFinishRequest(net::HTTP_BAD_REQUEST, 898 kSendAutocheckoutStatusOfSuccessValidRequest, 899 std::string()); 900} 901 902TEST_F(WalletClientTest, GetFullWalletSuccess) { 903 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_FULL_WALLET, 1); 904 delegate_.ExpectBaselineMetrics(); 905 906 WalletClient::FullWalletRequest full_wallet_request( 907 "instrument_id", 908 "shipping_address_id", 909 GURL(kMerchantUrl), 910 "google_transaction_id", 911 std::vector<WalletClient::RiskCapability>()); 912 wallet_client_->GetFullWallet(full_wallet_request); 913 914 VerifyAndFinishFormEncodedRequest(net::HTTP_OK, 915 kGetFullWalletValidRequest, 916 kGetFullWalletValidResponse, 917 3U); 918 EXPECT_EQ(1U, delegate_.full_wallets_received()); 919} 920 921TEST_F(WalletClientTest, GetFullWalletWithRiskCapabilitesSuccess) { 922 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_FULL_WALLET, 1); 923 delegate_.ExpectBaselineMetrics(); 924 925 std::vector<WalletClient::RiskCapability> risk_capabilities; 926 risk_capabilities.push_back(WalletClient::VERIFY_CVC); 927 WalletClient::FullWalletRequest full_wallet_request( 928 "instrument_id", 929 "shipping_address_id", 930 GURL(kMerchantUrl), 931 "google_transaction_id", 932 risk_capabilities); 933 wallet_client_->GetFullWallet(full_wallet_request); 934 935 VerifyAndFinishFormEncodedRequest( 936 net::HTTP_OK, 937 kGetFullWalletWithRiskCapabilitesValidRequest, 938 kGetFullWalletValidResponse, 939 3U); 940 EXPECT_EQ(1U, delegate_.full_wallets_received()); 941} 942 943 944TEST_F(WalletClientTest, GetFullWalletMalformedResponse) { 945 EXPECT_CALL(delegate_, 946 OnWalletError(WalletClient::MALFORMED_RESPONSE)).Times(1); 947 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_FULL_WALLET, 1); 948 delegate_.ExpectBaselineMetrics(); 949 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_MALFORMED_RESPONSE); 950 951 WalletClient::FullWalletRequest full_wallet_request( 952 "instrument_id", 953 "shipping_address_id", 954 GURL(kMerchantUrl), 955 "google_transaction_id", 956 std::vector<WalletClient::RiskCapability>()); 957 wallet_client_->GetFullWallet(full_wallet_request); 958 959 VerifyAndFinishFormEncodedRequest(net::HTTP_OK, 960 kGetFullWalletValidRequest, 961 kGetFullWalletInvalidResponse, 962 3U); 963 EXPECT_EQ(0U, delegate_.full_wallets_received()); 964} 965 966TEST_F(WalletClientTest, AcceptLegalDocuments) { 967 EXPECT_CALL(delegate_, OnDidAcceptLegalDocuments()).Times(1); 968 delegate_.ExpectLogWalletApiCallDuration( 969 AutofillMetrics::ACCEPT_LEGAL_DOCUMENTS, 970 1); 971 delegate_.ExpectBaselineMetrics(); 972 973 ScopedVector<WalletItems::LegalDocument> docs; 974 base::DictionaryValue document; 975 document.SetString("legal_document_id", "doc_id_1"); 976 document.SetString("display_name", "doc_1"); 977 docs.push_back( 978 WalletItems::LegalDocument::CreateLegalDocument(document).release()); 979 document.SetString("legal_document_id", "doc_id_2"); 980 document.SetString("display_name", "doc_2"); 981 docs.push_back( 982 WalletItems::LegalDocument::CreateLegalDocument(document).release()); 983 docs.push_back( 984 WalletItems::LegalDocument::CreatePrivacyPolicyDocument().release()); 985 wallet_client_->AcceptLegalDocuments(docs.get(), 986 kGoogleTransactionId, 987 GURL(kMerchantUrl)); 988 VerifyAndFinishRequest(net::HTTP_OK, 989 kAcceptLegalDocumentsValidRequest, 990 ")}'"); // Invalid JSON. Should be ignored. 991} 992 993TEST_F(WalletClientTest, AuthenticateInstrumentSucceeded) { 994 EXPECT_CALL(delegate_, OnDidAuthenticateInstrument(true)).Times(1); 995 delegate_.ExpectLogWalletApiCallDuration( 996 AutofillMetrics::AUTHENTICATE_INSTRUMENT, 997 1); 998 delegate_.ExpectBaselineMetrics(); 999 1000 wallet_client_->AuthenticateInstrument("instrument_id", "123"); 1001 1002 VerifyAndFinishFormEncodedRequest(net::HTTP_OK, 1003 kAuthenticateInstrumentValidRequest, 1004 kAuthenticateInstrumentSuccessResponse, 1005 3U); 1006} 1007 1008TEST_F(WalletClientTest, AuthenticateInstrumentFailed) { 1009 EXPECT_CALL(delegate_, OnDidAuthenticateInstrument(false)).Times(1); 1010 delegate_.ExpectLogWalletApiCallDuration( 1011 AutofillMetrics::AUTHENTICATE_INSTRUMENT, 1012 1); 1013 delegate_.ExpectBaselineMetrics(); 1014 1015 wallet_client_->AuthenticateInstrument("instrument_id", "123"); 1016 1017 VerifyAndFinishFormEncodedRequest(net::HTTP_OK, 1018 kAuthenticateInstrumentValidRequest, 1019 kAuthenticateInstrumentFailureResponse, 1020 3U); 1021} 1022 1023TEST_F(WalletClientTest, AuthenticateInstrumentFailedMalformedResponse) { 1024 EXPECT_CALL(delegate_, 1025 OnWalletError(WalletClient::MALFORMED_RESPONSE)).Times(1); 1026 delegate_.ExpectLogWalletApiCallDuration( 1027 AutofillMetrics::AUTHENTICATE_INSTRUMENT, 1028 1); 1029 delegate_.ExpectBaselineMetrics(); 1030 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_MALFORMED_RESPONSE); 1031 1032 wallet_client_->AuthenticateInstrument("instrument_id", "123"); 1033 1034 VerifyAndFinishFormEncodedRequest(net::HTTP_OK, 1035 kAuthenticateInstrumentValidRequest, 1036 kSaveInvalidResponse, 1037 3U); 1038} 1039 1040// TODO(ahutter): Add failure tests for GetWalletItems. 1041 1042TEST_F(WalletClientTest, GetWalletItems) { 1043 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS, 1044 1); 1045 delegate_.ExpectBaselineMetrics(); 1046 1047 wallet_client_->GetWalletItems(GURL(kMerchantUrl)); 1048 1049 VerifyAndFinishRequest(net::HTTP_OK, 1050 kGetWalletItemsValidRequest, 1051 kGetWalletItemsValidResponse); 1052 EXPECT_EQ(1U, delegate_.wallet_items_received()); 1053} 1054 1055TEST_F(WalletClientTest, GetWalletItemsRespectsDelegateForShippingRequired) { 1056 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS, 1057 1); 1058 delegate_.ExpectBaselineMetrics(); 1059 delegate_.SetIsShippingAddressRequired(false); 1060 1061 wallet_client_->GetWalletItems(GURL(kMerchantUrl)); 1062 1063 VerifyAndFinishRequest(net::HTTP_OK, 1064 kGetWalletItemsNoShippingRequest, 1065 kGetWalletItemsValidResponse); 1066 EXPECT_EQ(1U, delegate_.wallet_items_received()); 1067} 1068 1069TEST_F(WalletClientTest, SaveAddressSucceeded) { 1070 EXPECT_CALL(delegate_, 1071 OnDidSaveToWallet(std::string(), 1072 "saved_address_id", 1073 std::vector<RequiredAction>(), 1074 std::vector<FormFieldError>())).Times(1); 1075 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1); 1076 delegate_.ExpectBaselineMetrics(); 1077 1078 scoped_ptr<Address> address = GetTestSaveableAddress(); 1079 wallet_client_->SaveToWallet(scoped_ptr<Instrument>(), 1080 address.Pass(), 1081 GURL(kMerchantUrl)); 1082 VerifyAndFinishRequest(net::HTTP_OK, 1083 kSaveAddressValidRequest, 1084 kSaveAddressValidResponse); 1085} 1086 1087TEST_F(WalletClientTest, SaveAddressWithRequiredActionsSucceeded) { 1088 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1); 1089 delegate_.ExpectBaselineMetrics(); 1090 delegate_.ExpectWalletRequiredActionMetric( 1091 AutofillMetrics::REQUIRE_PHONE_NUMBER); 1092 delegate_.ExpectWalletRequiredActionMetric( 1093 AutofillMetrics::INVALID_FORM_FIELD); 1094 1095 std::vector<RequiredAction> required_actions; 1096 required_actions.push_back(REQUIRE_PHONE_NUMBER); 1097 required_actions.push_back(INVALID_FORM_FIELD); 1098 1099 std::vector<FormFieldError> form_errors; 1100 form_errors.push_back(FormFieldError(FormFieldError::INVALID_POSTAL_CODE, 1101 FormFieldError::SHIPPING_ADDRESS)); 1102 1103 EXPECT_CALL(delegate_, 1104 OnDidSaveToWallet(std::string(), 1105 std::string(), 1106 required_actions, 1107 form_errors)).Times(1); 1108 1109 scoped_ptr<Address> address = GetTestSaveableAddress(); 1110 wallet_client_->SaveToWallet(scoped_ptr<Instrument>(), 1111 address.Pass(), 1112 GURL(kMerchantUrl)); 1113 VerifyAndFinishRequest(net::HTTP_OK, 1114 kSaveAddressValidRequest, 1115 kSaveAddressWithRequiredActionsValidResponse); 1116} 1117 1118TEST_F(WalletClientTest, SaveAddressFailedInvalidRequiredAction) { 1119 EXPECT_CALL(delegate_, 1120 OnWalletError(WalletClient::MALFORMED_RESPONSE)).Times(1); 1121 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1); 1122 delegate_.ExpectBaselineMetrics(); 1123 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_MALFORMED_RESPONSE); 1124 1125 scoped_ptr<Address> address = GetTestSaveableAddress(); 1126 wallet_client_->SaveToWallet(scoped_ptr<Instrument>(), 1127 address.Pass(), 1128 GURL(kMerchantUrl)); 1129 VerifyAndFinishRequest(net::HTTP_OK, 1130 kSaveAddressValidRequest, 1131 kSaveWithInvalidRequiredActionsResponse); 1132} 1133 1134TEST_F(WalletClientTest, SaveAddressFailedMalformedResponse) { 1135 EXPECT_CALL(delegate_, 1136 OnWalletError(WalletClient::MALFORMED_RESPONSE)).Times(1); 1137 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1); 1138 delegate_.ExpectBaselineMetrics(); 1139 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_MALFORMED_RESPONSE); 1140 1141 scoped_ptr<Address> address = GetTestSaveableAddress(); 1142 wallet_client_->SaveToWallet(scoped_ptr<Instrument>(), 1143 address.Pass(), 1144 GURL(kMerchantUrl)); 1145 VerifyAndFinishRequest(net::HTTP_OK, 1146 kSaveAddressValidRequest, 1147 kSaveInvalidResponse); 1148} 1149 1150TEST_F(WalletClientTest, SaveInstrumentSucceeded) { 1151 EXPECT_CALL(delegate_, 1152 OnDidSaveToWallet("instrument_id", 1153 std::string(), 1154 std::vector<RequiredAction>(), 1155 std::vector<FormFieldError>())).Times(1); 1156 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1); 1157 delegate_.ExpectBaselineMetrics(); 1158 1159 scoped_ptr<Instrument> instrument = GetTestInstrument(); 1160 wallet_client_->SaveToWallet(instrument.Pass(), 1161 scoped_ptr<Address>(), 1162 GURL(kMerchantUrl)); 1163 1164 VerifyAndFinishFormEncodedRequest(net::HTTP_OK, 1165 kSaveInstrumentValidRequest, 1166 kSaveInstrumentValidResponse, 1167 4U); 1168} 1169 1170TEST_F(WalletClientTest, SaveInstrumentWithRequiredActionsSucceeded) { 1171 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1); 1172 delegate_.ExpectBaselineMetrics(); 1173 delegate_.ExpectWalletRequiredActionMetric( 1174 AutofillMetrics::REQUIRE_PHONE_NUMBER); 1175 delegate_.ExpectWalletRequiredActionMetric( 1176 AutofillMetrics::INVALID_FORM_FIELD); 1177 1178 std::vector<RequiredAction> required_actions; 1179 required_actions.push_back(REQUIRE_PHONE_NUMBER); 1180 required_actions.push_back(INVALID_FORM_FIELD); 1181 1182 std::vector<FormFieldError> form_errors; 1183 form_errors.push_back(FormFieldError(FormFieldError::INVALID_POSTAL_CODE, 1184 FormFieldError::SHIPPING_ADDRESS)); 1185 1186 EXPECT_CALL(delegate_, 1187 OnDidSaveToWallet(std::string(), 1188 std::string(), 1189 required_actions, 1190 form_errors)).Times(1); 1191 1192 scoped_ptr<Instrument> instrument = GetTestInstrument(); 1193 wallet_client_->SaveToWallet(instrument.Pass(), 1194 scoped_ptr<Address>(), 1195 GURL(kMerchantUrl)); 1196 1197 VerifyAndFinishFormEncodedRequest( 1198 net::HTTP_OK, 1199 kSaveInstrumentValidRequest, 1200 kSaveInstrumentWithRequiredActionsValidResponse, 1201 4U); 1202} 1203 1204TEST_F(WalletClientTest, SaveInstrumentFailedInvalidRequiredActions) { 1205 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1); 1206 delegate_.ExpectBaselineMetrics(); 1207 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_MALFORMED_RESPONSE); 1208 1209 EXPECT_CALL(delegate_, 1210 OnWalletError(WalletClient::MALFORMED_RESPONSE)); 1211 1212 scoped_ptr<Instrument> instrument = GetTestInstrument(); 1213 wallet_client_->SaveToWallet(instrument.Pass(), 1214 scoped_ptr<Address>(), 1215 GURL(kMerchantUrl)); 1216 1217 VerifyAndFinishFormEncodedRequest(net::HTTP_OK, 1218 kSaveInstrumentValidRequest, 1219 kSaveWithInvalidRequiredActionsResponse, 1220 4U); 1221} 1222 1223TEST_F(WalletClientTest, SaveInstrumentFailedMalformedResponse) { 1224 EXPECT_CALL(delegate_, 1225 OnWalletError(WalletClient::MALFORMED_RESPONSE)).Times(1); 1226 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1); 1227 delegate_.ExpectBaselineMetrics(); 1228 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_MALFORMED_RESPONSE); 1229 1230 scoped_ptr<Instrument> instrument = GetTestInstrument(); 1231 wallet_client_->SaveToWallet(instrument.Pass(), 1232 scoped_ptr<Address>(), 1233 GURL(kMerchantUrl)); 1234 1235 VerifyAndFinishFormEncodedRequest(net::HTTP_OK, 1236 kSaveInstrumentValidRequest, 1237 kSaveInvalidResponse, 1238 4U); 1239} 1240 1241TEST_F(WalletClientTest, SaveInstrumentAndAddressSucceeded) { 1242 EXPECT_CALL(delegate_, 1243 OnDidSaveToWallet("saved_instrument_id", 1244 "saved_address_id", 1245 std::vector<RequiredAction>(), 1246 std::vector<FormFieldError>())).Times(1); 1247 delegate_.ExpectLogWalletApiCallDuration( 1248 AutofillMetrics::SAVE_TO_WALLET, 1249 1); 1250 delegate_.ExpectBaselineMetrics(); 1251 1252 scoped_ptr<Instrument> instrument = GetTestInstrument(); 1253 scoped_ptr<Address> address = GetTestSaveableAddress(); 1254 wallet_client_->SaveToWallet(instrument.Pass(), 1255 address.Pass(), 1256 GURL(kMerchantUrl)); 1257 1258 VerifyAndFinishFormEncodedRequest(net::HTTP_OK, 1259 kSaveInstrumentAndAddressValidRequest, 1260 kSaveInstrumentAndAddressValidResponse, 1261 4U); 1262} 1263 1264TEST_F(WalletClientTest, SaveInstrumentAndAddressWithRequiredActionsSucceeded) { 1265 delegate_.ExpectLogWalletApiCallDuration( 1266 AutofillMetrics::SAVE_TO_WALLET, 1267 1); 1268 delegate_.ExpectBaselineMetrics(); 1269 delegate_.ExpectWalletRequiredActionMetric( 1270 AutofillMetrics::REQUIRE_PHONE_NUMBER); 1271 delegate_.ExpectWalletRequiredActionMetric( 1272 AutofillMetrics::INVALID_FORM_FIELD); 1273 1274 std::vector<RequiredAction> required_actions; 1275 required_actions.push_back(REQUIRE_PHONE_NUMBER); 1276 required_actions.push_back(INVALID_FORM_FIELD); 1277 1278 std::vector<FormFieldError> form_errors; 1279 form_errors.push_back(FormFieldError(FormFieldError::INVALID_POSTAL_CODE, 1280 FormFieldError::SHIPPING_ADDRESS)); 1281 1282 EXPECT_CALL(delegate_, 1283 OnDidSaveToWallet(std::string(), 1284 std::string(), 1285 required_actions, 1286 form_errors)).Times(1); 1287 1288 scoped_ptr<Instrument> instrument = GetTestInstrument(); 1289 scoped_ptr<Address> address = GetTestSaveableAddress(); 1290 wallet_client_->SaveToWallet(instrument.Pass(), 1291 address.Pass(), 1292 GURL(kMerchantUrl)); 1293 1294 VerifyAndFinishFormEncodedRequest( 1295 net::HTTP_OK, 1296 kSaveInstrumentAndAddressValidRequest, 1297 kSaveInstrumentAndAddressWithRequiredActionsValidResponse, 1298 4U); 1299} 1300 1301TEST_F(WalletClientTest, SaveInstrumentAndAddressFailedInvalidRequiredAction) { 1302 EXPECT_CALL(delegate_, 1303 OnWalletError(WalletClient::MALFORMED_RESPONSE)).Times(1); 1304 delegate_.ExpectLogWalletApiCallDuration( 1305 AutofillMetrics::SAVE_TO_WALLET, 1306 1); 1307 delegate_.ExpectBaselineMetrics(); 1308 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_MALFORMED_RESPONSE); 1309 1310 scoped_ptr<Instrument> instrument = GetTestInstrument(); 1311 scoped_ptr<Address> address = GetTestSaveableAddress(); 1312 wallet_client_->SaveToWallet(instrument.Pass(), 1313 address.Pass(), 1314 GURL(kMerchantUrl)); 1315 1316 VerifyAndFinishFormEncodedRequest(net::HTTP_OK, 1317 kSaveInstrumentAndAddressValidRequest, 1318 kSaveWithInvalidRequiredActionsResponse, 1319 4U); 1320} 1321 1322TEST_F(WalletClientTest, UpdateAddressSucceeded) { 1323 EXPECT_CALL(delegate_, 1324 OnDidSaveToWallet(std::string(), 1325 "shipping_address_id", 1326 std::vector<RequiredAction>(), 1327 std::vector<FormFieldError>())).Times(1); 1328 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1); 1329 delegate_.ExpectBaselineMetrics(); 1330 1331 scoped_ptr<Address> address = GetTestShippingAddress(); 1332 address->set_object_id("shipping_address_id"); 1333 1334 wallet_client_->SaveToWallet(scoped_ptr<Instrument>(), 1335 address.Pass(), 1336 GURL(kMerchantUrl)); 1337 VerifyAndFinishRequest(net::HTTP_OK, 1338 kUpdateAddressValidRequest, 1339 kUpdateAddressValidResponse); 1340} 1341 1342TEST_F(WalletClientTest, UpdateAddressWithRequiredActionsSucceeded) { 1343 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1); 1344 delegate_.ExpectBaselineMetrics(); 1345 delegate_.ExpectWalletRequiredActionMetric( 1346 AutofillMetrics::REQUIRE_PHONE_NUMBER); 1347 delegate_.ExpectWalletRequiredActionMetric( 1348 AutofillMetrics::INVALID_FORM_FIELD); 1349 1350 std::vector<RequiredAction> required_actions; 1351 required_actions.push_back(REQUIRE_PHONE_NUMBER); 1352 required_actions.push_back(INVALID_FORM_FIELD); 1353 1354 std::vector<FormFieldError> form_errors; 1355 form_errors.push_back(FormFieldError(FormFieldError::INVALID_POSTAL_CODE, 1356 FormFieldError::SHIPPING_ADDRESS)); 1357 1358 EXPECT_CALL(delegate_, OnDidSaveToWallet(std::string(), 1359 std::string(), 1360 required_actions, 1361 form_errors)).Times(1); 1362 1363 scoped_ptr<Address> address = GetTestShippingAddress(); 1364 address->set_object_id("shipping_address_id"); 1365 1366 wallet_client_->SaveToWallet(scoped_ptr<Instrument>(), 1367 address.Pass(), 1368 GURL(kMerchantUrl)); 1369 VerifyAndFinishRequest(net::HTTP_OK, 1370 kUpdateAddressValidRequest, 1371 kUpdateWithRequiredActionsValidResponse); 1372} 1373 1374TEST_F(WalletClientTest, UpdateAddressFailedInvalidRequiredAction) { 1375 EXPECT_CALL(delegate_, 1376 OnWalletError(WalletClient::MALFORMED_RESPONSE)).Times(1); 1377 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1); 1378 delegate_.ExpectBaselineMetrics(); 1379 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_MALFORMED_RESPONSE); 1380 1381 scoped_ptr<Address> address = GetTestShippingAddress(); 1382 address->set_object_id("shipping_address_id"); 1383 1384 wallet_client_->SaveToWallet(scoped_ptr<Instrument>(), 1385 address.Pass(), 1386 GURL(kMerchantUrl)); 1387 VerifyAndFinishRequest(net::HTTP_OK, 1388 kUpdateAddressValidRequest, 1389 kSaveWithInvalidRequiredActionsResponse); 1390} 1391 1392TEST_F(WalletClientTest, UpdateAddressMalformedResponse) { 1393 EXPECT_CALL(delegate_, 1394 OnWalletError(WalletClient::MALFORMED_RESPONSE)).Times(1); 1395 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1); 1396 delegate_.ExpectBaselineMetrics(); 1397 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_MALFORMED_RESPONSE); 1398 1399 scoped_ptr<Address> address = GetTestShippingAddress(); 1400 address->set_object_id("shipping_address_id"); 1401 1402 wallet_client_->SaveToWallet(scoped_ptr<Instrument>(), 1403 address.Pass(), 1404 GURL(kMerchantUrl)); 1405 VerifyAndFinishRequest(net::HTTP_OK, 1406 kUpdateAddressValidRequest, 1407 kUpdateMalformedResponse); 1408} 1409 1410TEST_F(WalletClientTest, UpdateInstrumentAddressSucceeded) { 1411 EXPECT_CALL(delegate_, 1412 OnDidSaveToWallet("instrument_id", 1413 std::string(), 1414 std::vector<RequiredAction>(), 1415 std::vector<FormFieldError>())).Times(1); 1416 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1417 1); 1418 delegate_.ExpectBaselineMetrics(); 1419 1420 wallet_client_->SaveToWallet(GetTestAddressUpgradeInstrument(), 1421 scoped_ptr<Address>(), 1422 GURL(kMerchantUrl)); 1423 1424 VerifyAndFinishRequest(net::HTTP_OK, 1425 kUpdateInstrumentAddressValidRequest, 1426 kUpdateInstrumentValidResponse); 1427} 1428 1429TEST_F(WalletClientTest, UpdateInstrumentExpirationDateSuceeded) { 1430 EXPECT_CALL(delegate_, 1431 OnDidSaveToWallet("instrument_id", 1432 std::string(), 1433 std::vector<RequiredAction>(), 1434 std::vector<FormFieldError>())).Times(1); 1435 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1436 1); 1437 delegate_.ExpectBaselineMetrics(); 1438 1439 wallet_client_->SaveToWallet(GetTestExpirationDateChangeInstrument(), 1440 scoped_ptr<Address>(), 1441 GURL(kMerchantUrl)); 1442 1443 VerifyAndFinishFormEncodedRequest(net::HTTP_OK, 1444 kUpdateInstrumentExpirationDateValidRequest, 1445 kUpdateInstrumentValidResponse, 1446 3U); 1447} 1448 1449TEST_F(WalletClientTest, UpdateInstrumentAddressWithNameChangeSucceeded) { 1450 EXPECT_CALL(delegate_, 1451 OnDidSaveToWallet("instrument_id", 1452 std::string(), 1453 std::vector<RequiredAction>(), 1454 std::vector<FormFieldError>())).Times(1); 1455 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1456 1); 1457 delegate_.ExpectBaselineMetrics(); 1458 1459 wallet_client_->SaveToWallet(GetTestAddressNameChangeInstrument(), 1460 scoped_ptr<Address>(), 1461 GURL(kMerchantUrl)); 1462 1463 VerifyAndFinishFormEncodedRequest( 1464 net::HTTP_OK, 1465 kUpdateInstrumentAddressWithNameChangeValidRequest, 1466 kUpdateInstrumentValidResponse, 1467 3U); 1468} 1469 1470TEST_F(WalletClientTest, UpdateInstrumentWithRequiredActionsSucceeded) { 1471 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1472 1); 1473 delegate_.ExpectBaselineMetrics(); 1474 delegate_.ExpectWalletRequiredActionMetric( 1475 AutofillMetrics::REQUIRE_PHONE_NUMBER); 1476 delegate_.ExpectWalletRequiredActionMetric( 1477 AutofillMetrics::INVALID_FORM_FIELD); 1478 1479 std::vector<RequiredAction> required_actions; 1480 required_actions.push_back(REQUIRE_PHONE_NUMBER); 1481 required_actions.push_back(INVALID_FORM_FIELD); 1482 1483 std::vector<FormFieldError> form_errors; 1484 form_errors.push_back(FormFieldError(FormFieldError::INVALID_POSTAL_CODE, 1485 FormFieldError::SHIPPING_ADDRESS)); 1486 1487 EXPECT_CALL(delegate_, 1488 OnDidSaveToWallet(std::string(), 1489 std::string(), 1490 required_actions, 1491 form_errors)).Times(1); 1492 1493 wallet_client_->SaveToWallet(GetTestAddressUpgradeInstrument(), 1494 scoped_ptr<Address>(), 1495 GURL(kMerchantUrl)); 1496 1497 VerifyAndFinishRequest(net::HTTP_OK, 1498 kUpdateInstrumentAddressValidRequest, 1499 kUpdateWithRequiredActionsValidResponse); 1500} 1501 1502TEST_F(WalletClientTest, UpdateInstrumentFailedInvalidRequiredAction) { 1503 EXPECT_CALL(delegate_, 1504 OnWalletError(WalletClient::MALFORMED_RESPONSE)).Times(1); 1505 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1506 1); 1507 delegate_.ExpectBaselineMetrics(); 1508 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_MALFORMED_RESPONSE); 1509 1510 wallet_client_->SaveToWallet(GetTestAddressUpgradeInstrument(), 1511 scoped_ptr<Address>(), 1512 GURL(kMerchantUrl)); 1513 1514 VerifyAndFinishRequest(net::HTTP_OK, 1515 kUpdateInstrumentAddressValidRequest, 1516 kSaveWithInvalidRequiredActionsResponse); 1517} 1518 1519TEST_F(WalletClientTest, UpdateInstrumentMalformedResponse) { 1520 EXPECT_CALL(delegate_, 1521 OnWalletError(WalletClient::MALFORMED_RESPONSE)).Times(1); 1522 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1523 1); 1524 delegate_.ExpectBaselineMetrics(); 1525 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_MALFORMED_RESPONSE); 1526 1527 wallet_client_->SaveToWallet(GetTestAddressUpgradeInstrument(), 1528 scoped_ptr<Address>(), 1529 GURL(kMerchantUrl)); 1530 1531 VerifyAndFinishRequest(net::HTTP_OK, 1532 kUpdateInstrumentAddressValidRequest, 1533 kUpdateMalformedResponse); 1534} 1535 1536TEST_F(WalletClientTest, SendAutocheckoutOfStatusSuccess) { 1537 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SEND_STATUS, 1); 1538 delegate_.ExpectBaselineMetrics(); 1539 1540 AutocheckoutStatistic statistic; 1541 statistic.page_number = 1; 1542 statistic.steps.push_back(AUTOCHECKOUT_STEP_SHIPPING); 1543 statistic.time_taken = base::TimeDelta::FromMilliseconds(100); 1544 std::vector<AutocheckoutStatistic> statistics; 1545 statistics.push_back(statistic); 1546 wallet_client_->SendAutocheckoutStatus(autofill::SUCCESS, 1547 GURL(kMerchantUrl), 1548 statistics, 1549 "google_transaction_id"); 1550 VerifyAndFinishRequest(net::HTTP_OK, 1551 kSendAutocheckoutStatusWithStatisticsValidRequest, 1552 ")]}"); // Invalid JSON. Should be ignored. 1553} 1554 1555TEST_F(WalletClientTest, SendAutocheckoutStatusOfFailure) { 1556 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SEND_STATUS, 1); 1557 delegate_.ExpectBaselineMetrics(); 1558 1559 std::vector<AutocheckoutStatistic> statistics; 1560 wallet_client_->SendAutocheckoutStatus(autofill::CANNOT_PROCEED, 1561 GURL(kMerchantUrl), 1562 statistics, 1563 "google_transaction_id"); 1564 VerifyAndFinishRequest(net::HTTP_OK, 1565 kSendAutocheckoutStatusOfFailureValidRequest, 1566 ")]}"); // Invalid JSON. Should be ignored. 1567} 1568 1569TEST_F(WalletClientTest, HasRequestInProgress) { 1570 EXPECT_FALSE(wallet_client_->HasRequestInProgress()); 1571 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS, 1572 1); 1573 delegate_.ExpectBaselineMetrics(); 1574 1575 wallet_client_->GetWalletItems(GURL(kMerchantUrl)); 1576 EXPECT_TRUE(wallet_client_->HasRequestInProgress()); 1577 1578 VerifyAndFinishRequest(net::HTTP_OK, 1579 kGetWalletItemsValidRequest, 1580 kGetWalletItemsValidResponse); 1581 EXPECT_FALSE(wallet_client_->HasRequestInProgress()); 1582} 1583 1584TEST_F(WalletClientTest, PendingRequest) { 1585 EXPECT_EQ(0U, wallet_client_->pending_requests_.size()); 1586 1587 // Shouldn't queue the first request. 1588 delegate_.ExpectBaselineMetrics(); 1589 wallet_client_->GetWalletItems(GURL(kMerchantUrl)); 1590 EXPECT_EQ(0U, wallet_client_->pending_requests_.size()); 1591 testing::Mock::VerifyAndClear(delegate_.metric_logger()); 1592 1593 wallet_client_->GetWalletItems(GURL(kMerchantUrl)); 1594 EXPECT_EQ(1U, wallet_client_->pending_requests_.size()); 1595 1596 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS, 1597 1); 1598 delegate_.ExpectBaselineMetrics(); 1599 VerifyAndFinishRequest(net::HTTP_OK, 1600 kGetWalletItemsValidRequest, 1601 kGetWalletItemsValidResponse); 1602 EXPECT_EQ(0U, wallet_client_->pending_requests_.size()); 1603 testing::Mock::VerifyAndClear(delegate_.metric_logger()); 1604 1605 EXPECT_CALL(delegate_, OnWalletError( 1606 WalletClient::SERVICE_UNAVAILABLE)).Times(1); 1607 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS, 1608 1); 1609 delegate_.ExpectWalletErrorMetric( 1610 AutofillMetrics::WALLET_SERVICE_UNAVAILABLE); 1611 1612 // Finish the second request. 1613 VerifyAndFinishRequest(net::HTTP_INTERNAL_SERVER_ERROR, 1614 kGetWalletItemsValidRequest, 1615 kErrorResponse); 1616} 1617 1618TEST_F(WalletClientTest, CancelRequests) { 1619 ASSERT_EQ(0U, wallet_client_->pending_requests_.size()); 1620 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS, 1621 0); 1622 delegate_.ExpectBaselineMetrics(); 1623 1624 wallet_client_->GetWalletItems(GURL(kMerchantUrl)); 1625 wallet_client_->GetWalletItems(GURL(kMerchantUrl)); 1626 wallet_client_->GetWalletItems(GURL(kMerchantUrl)); 1627 EXPECT_EQ(2U, wallet_client_->pending_requests_.size()); 1628 1629 wallet_client_->CancelRequests(); 1630 EXPECT_EQ(0U, wallet_client_->pending_requests_.size()); 1631 EXPECT_FALSE(wallet_client_->HasRequestInProgress()); 1632} 1633 1634} // namespace wallet 1635} // namespace autofill 1636