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