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