Add diagnostics_writer.cc to the list of files allowed to printf.
[chromium-blink-merge.git] / components / autofill / content / browser / wallet / wallet_client_unittest.cc
blob66ce1e6ec9e65ddb2250a468f0a6b3a1b9f4234e
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.
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/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/values.h"
16 #include "components/autofill/content/browser/wallet/full_wallet.h"
17 #include "components/autofill/content/browser/wallet/instrument.h"
18 #include "components/autofill/content/browser/wallet/wallet_client.h"
19 #include "components/autofill/content/browser/wallet/wallet_client_delegate.h"
20 #include "components/autofill/content/browser/wallet/wallet_items.h"
21 #include "components/autofill/content/browser/wallet/wallet_test_util.h"
22 #include "components/autofill/core/browser/autofill_metrics.h"
23 #include "content/public/test/test_browser_thread_bundle.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 "net/url_request/url_request_test_util.h"
32 #include "testing/gmock/include/gmock/gmock.h"
33 #include "testing/gtest/include/gtest/gtest.h"
34 #include "url/gurl.h"
36 namespace autofill {
37 namespace wallet {
39 namespace {
41 const char kGoogleTransactionId[] = "google-transaction-id";
42 const char kMerchantUrl[] = "https://example.com/path?key=value";
44 const char kGetFullWalletValidResponse[] =
45 "{"
46 " \"expiration_month\":12,"
47 " \"expiration_year\":3000,"
48 " \"iin\":\"iin\","
49 " \"rest\":\"rest\","
50 " \"billing_address\":"
51 " {"
52 " \"id\":\"id\","
53 " \"phone_number\":\"phone_number\","
54 " \"postal_address\":"
55 " {"
56 " \"recipient_name\":\"recipient_name\","
57 " \"address_line\":"
58 " ["
59 " \"address_line_1\","
60 " \"address_line_2\""
61 " ],"
62 " \"locality_name\":\"locality_name\","
63 " \"administrative_area_name\":\"administrative_area_name\","
64 " \"postal_code_number\":\"postal_code_number\","
65 " \"country_name_code\":\"US\","
66 " \"language_code\":\"language_code\""
67 " }"
68 " },"
69 " \"shipping_address\":"
70 " {"
71 " \"id\":\"ship_id\","
72 " \"phone_number\":\"ship_phone_number\","
73 " \"postal_address\":"
74 " {"
75 " \"recipient_name\":\"ship_recipient_name\","
76 " \"address_line\":"
77 " ["
78 " \"ship_address_line_1\","
79 " \"ship_address_line_2\""
80 " ],"
81 " \"locality_name\":\"ship_locality_name\","
82 " \"administrative_area_name\":\"ship_administrative_area_name\","
83 " \"postal_code_number\":\"ship_postal_code_number\","
84 " \"country_name_code\":\"US\","
85 " \"language_code\":\"ship_language_code\""
86 " }"
87 " },"
88 " \"required_action\":"
89 " ["
90 " ]"
91 "}";
93 const char kGetFullWalletInvalidResponse[] =
94 "{"
95 " \"garbage\":123"
96 "}";
98 const char kGetWalletItemsValidResponse[] =
99 "{"
100 " \"required_action\":"
101 " ["
102 " ],"
103 " \"google_transaction_id\":\"google_transaction_id\","
104 " \"instrument\":"
105 " ["
106 " {"
107 " \"descriptive_name\":\"descriptive_name\","
108 " \"type\":\"VISA\","
109 " \"last_four_digits\":\"4111\","
110 " \"expiration_month\":12,"
111 " \"expiration_year\":3000,"
112 " \"brand\":\"monkeys\","
113 " \"billing_address\":"
114 " {"
115 " \"name\":\"name\","
116 " \"address1\":\"address1\","
117 " \"address2\":\"address2\","
118 " \"city\":\"city\","
119 " \"state\":\"state\","
120 " \"postal_code\":\"postal_code\","
121 " \"phone_number\":\"phone_number\","
122 " \"country_code\":\"US\","
123 " \"language_code\":\"language_code\""
124 " },"
125 " \"status\":\"VALID\","
126 " \"object_id\":\"default_instrument_id\""
127 " }"
128 " ],"
129 " \"default_instrument_id\":\"default_instrument_id\","
130 " \"obfuscated_gaia_id\":\"obfuscated_gaia_id\","
131 " \"address\":"
132 " ["
133 " ],"
134 " \"default_address_id\":\"default_address_id\","
135 " \"required_legal_document\":"
136 " ["
137 " ]"
138 "}";
140 const char kSaveAddressValidResponse[] =
142 " \"shipping_address_id\":\"saved_address_id\""
143 "}";
145 const char kSaveAddressWithRequiredActionsValidResponse[] =
147 " \"form_field_error\":"
148 " ["
149 " {"
150 " \"location\":\"SHIPPING_ADDRESS\","
151 " \"type\":\"INVALID_POSTAL_CODE\""
152 " }"
153 " ],"
154 " \"required_action\":"
155 " ["
156 " \" \\treqUIRE_PhOnE_number \\n\\r\","
157 " \"INVALID_form_field\""
158 " ]"
159 "}";
161 const char kSaveWithInvalidRequiredActionsResponse[] =
163 " \"required_action\":"
164 " ["
165 " \" setup_wallet\","
166 " \" \\treqUIRE_PhOnE_number \\n\\r\","
167 " \"INVALID_form_field\""
168 " ]"
169 "}";
171 const char kSaveInvalidResponse[] =
173 " \"garbage\":123"
174 "}";
176 const char kSaveInstrumentValidResponse[] =
178 " \"instrument_id\":\"instrument_id\""
179 "}";
181 const char kSaveInstrumentWithRequiredActionsValidResponse[] =
183 " \"form_field_error\":"
184 " ["
185 " {"
186 " \"location\":\"SHIPPING_ADDRESS\","
187 " \"type\":\"INVALID_POSTAL_CODE\""
188 " }"
189 " ],"
190 " \"required_action\":"
191 " ["
192 " \" \\treqUIRE_PhOnE_number \\n\\r\","
193 " \"INVALID_form_field\""
194 " ]"
195 "}";
197 const char kSaveInstrumentAndAddressValidResponse[] =
199 " \"shipping_address_id\":\"saved_address_id\","
200 " \"instrument_id\":\"saved_instrument_id\""
201 "}";
203 const char kSaveInstrumentAndAddressWithRequiredActionsValidResponse[] =
205 " \"form_field_error\":"
206 " ["
207 " {"
208 " \"location\":\"SHIPPING_ADDRESS\","
209 " \"type\":\"INVALID_POSTAL_CODE\""
210 " }"
211 " ],"
212 " \"required_action\":"
213 " ["
214 " \" \\treqUIRE_PhOnE_number \\n\\r\","
215 " \"INVALID_form_field\""
216 " ]"
217 "}";
219 const char kUpdateInstrumentValidResponse[] =
221 " \"instrument_id\":\"instrument_id\""
222 "}";
224 const char kUpdateAddressValidResponse[] =
226 " \"shipping_address_id\":\"shipping_address_id\""
227 "}";
229 const char kUpdateWithRequiredActionsValidResponse[] =
231 " \"form_field_error\":"
232 " ["
233 " {"
234 " \"location\":\"SHIPPING_ADDRESS\","
235 " \"type\":\"INVALID_POSTAL_CODE\""
236 " }"
237 " ],"
238 " \"required_action\":"
239 " ["
240 " \" \\treqUIRE_PhOnE_number \\n\\r\","
241 " \"INVALID_form_field\""
242 " ]"
243 "}";
245 const char kUpdateMalformedResponse[] =
247 " \"cheese\":\"monkeys\""
248 "}";
250 const char kAuthenticateInstrumentFailureResponse[] =
252 " \"auth_result\":\"anything else\""
253 "}";
255 const char kAuthenticateInstrumentSuccessResponse[] =
257 " \"auth_result\":\"SUCCESS\""
258 "}";
260 const char kErrorResponse[] =
262 " \"error_type\":\"APPLICATION_ERROR\","
263 " \"error_detail\":\"error_detail\","
264 " \"application_error\":\"application_error\","
265 " \"debug_data\":"
266 " {"
267 " \"debug_message\":\"debug_message\","
268 " \"stack_trace\":\"stack_trace\""
269 " },"
270 " \"application_error_data\":\"application_error_data\","
271 " \"wallet_error\":"
272 " {"
273 " \"error_type\":\"SERVICE_UNAVAILABLE\","
274 " \"error_detail\":\"error_detail\","
275 " \"message_for_user\":"
276 " {"
277 " \"text\":\"text\","
278 " \"subtext\":\"subtext\","
279 " \"details\":\"details\""
280 " }"
281 " }"
282 "}";
284 const char kErrorTypeMissingInResponse[] =
286 " \"error_type\":\"Not APPLICATION_ERROR\","
287 " \"error_detail\":\"error_detail\","
288 " \"application_error\":\"application_error\","
289 " \"debug_data\":"
290 " {"
291 " \"debug_message\":\"debug_message\","
292 " \"stack_trace\":\"stack_trace\""
293 " },"
294 " \"application_error_data\":\"application_error_data\""
295 "}";
297 // The JSON below is used to test against the request payload being sent to
298 // Online Wallet. It's indented differently since JSONWriter creates compact
299 // JSON from DictionaryValues. NB: The values must be alphabetical to pass
300 // the tests.
302 const char kAcceptLegalDocumentsValidRequest[] =
304 "\"accepted_legal_document\":"
306 "\"doc_id_1\","
307 "\"doc_id_2\""
308 "],"
309 "\"google_transaction_id\":\"google-transaction-id\","
310 "\"merchant_domain\":\"https://example.com/\""
311 "}";
313 const char kAuthenticateInstrumentValidRequest[] =
315 "\"instrument_id\":\"instrument_id\","
316 "\"risk_params\":\"risky business\""
317 "}";
319 const char kGetFullWalletValidRequest[] =
321 "\"feature\":\"REQUEST_AUTOCOMPLETE\","
322 "\"google_transaction_id\":\"google_transaction_id\","
323 "\"merchant_domain\":\"https://example.com/\","
324 "\"new_wallet_user\":false,"
325 "\"phone_number_required\":true,"
326 "\"risk_params\":\"risky business\","
327 "\"selected_address_id\":\"shipping_address_id\","
328 "\"selected_instrument_id\":\"instrument_id\","
329 "\"supported_risk_challenge\":"
331 "],"
332 "\"use_minimal_addresses\":false"
333 "}";
335 const char kGetFullWalletValidRequestNewUser[] =
337 "\"feature\":\"REQUEST_AUTOCOMPLETE\","
338 "\"google_transaction_id\":\"google_transaction_id\","
339 "\"merchant_domain\":\"https://example.com/\","
340 "\"new_wallet_user\":true,"
341 "\"phone_number_required\":true,"
342 "\"risk_params\":\"risky business\","
343 "\"selected_address_id\":\"shipping_address_id\","
344 "\"selected_instrument_id\":\"instrument_id\","
345 "\"supported_risk_challenge\":"
347 "],"
348 "\"use_minimal_addresses\":false"
349 "}";
351 const char kGetFullWalletWithRiskCapabilitesValidRequest[] =
353 "\"feature\":\"REQUEST_AUTOCOMPLETE\","
354 "\"google_transaction_id\":\"google_transaction_id\","
355 "\"merchant_domain\":\"https://example.com/\","
356 "\"new_wallet_user\":false,"
357 "\"phone_number_required\":true,"
358 "\"risk_params\":\"risky business\","
359 "\"selected_address_id\":\"shipping_address_id\","
360 "\"selected_instrument_id\":\"instrument_id\","
361 "\"supported_risk_challenge\":"
363 "\"VERIFY_CVC\""
364 "],"
365 "\"use_minimal_addresses\":false"
366 "}";
368 const char kGetWalletItemsValidRequest[] =
370 "\"merchant_domain\":\"https://example.com/\","
371 "\"phone_number_required\":true,"
372 "\"shipping_address_required\":true,"
373 "\"use_minimal_addresses\":false"
374 "}";
376 const char kGetWalletItemsWithTransactionDetails[] =
378 "\"currency_code\":\"USD\","
379 "\"estimated_total_price\":\"100.00\","
380 "\"merchant_domain\":\"https://example.com/\","
381 "\"phone_number_required\":true,"
382 "\"shipping_address_required\":true,"
383 "\"use_minimal_addresses\":false"
384 "}";
386 const char kGetWalletItemsNoShippingRequest[] =
388 "\"merchant_domain\":\"https://example.com/\","
389 "\"phone_number_required\":true,"
390 "\"shipping_address_required\":false,"
391 "\"use_minimal_addresses\":false"
392 "}";
394 const char kSaveAddressValidRequest[] =
396 "\"merchant_domain\":\"https://example.com/\","
397 "\"phone_number_required\":true,"
398 "\"risk_params\":\"risky business\","
399 "\"shipping_address\":"
401 "\"phone_number\":\"save_phone_number\","
402 "\"postal_address\":"
404 "\"address_line\":"
406 "\"save_address_line_1\","
407 "\"save_address_line_2\""
408 "],"
409 "\"administrative_area_name\":\"save_admin_area_name\","
410 "\"country_name_code\":\"US\","
411 "\"dependent_locality_name\":\"save_dependent_locality_name\","
412 "\"language_code\":\"save_language_code\","
413 "\"locality_name\":\"save_locality_name\","
414 "\"postal_code_number\":\"save_postal_code_number\","
415 "\"recipient_name\":\"save_recipient_name\","
416 "\"sorting_code\":\"save_sorting_code\""
418 "},"
419 "\"use_minimal_addresses\":false"
420 "}";
422 const char kSaveInstrumentValidRequest[] =
424 "\"instrument\":"
426 "\"credit_card\":"
428 "\"address\":"
430 "\"address_line\":"
432 "\"address_line_1\","
433 "\"address_line_2\""
434 "],"
435 "\"administrative_area_name\":\"admin_area_name\","
436 "\"country_name_code\":\"US\","
437 "\"dependent_locality_name\":\"dependent_locality_name\","
438 "\"language_code\":\"language_code\","
439 "\"locality_name\":\"locality_name\","
440 "\"postal_code_number\":\"postal_code_number\","
441 "\"recipient_name\":\"recipient_name\","
442 "\"sorting_code\":\"sorting_code\""
443 "},"
444 "\"exp_month\":12,"
445 "\"exp_year\":3000,"
446 "\"fop_type\":\"VISA\","
447 "\"last_4_digits\":\"4448\""
448 "},"
449 "\"type\":\"CREDIT_CARD\""
450 "},"
451 "\"instrument_phone_number\":\"phone_number\","
452 "\"merchant_domain\":\"https://example.com/\","
453 "\"phone_number_required\":true,"
454 "\"risk_params\":\"risky business\","
455 "\"use_minimal_addresses\":false"
456 "}";
458 const char kSaveInstrumentAndAddressValidRequest[] =
460 "\"instrument\":"
462 "\"credit_card\":"
464 "\"address\":"
466 "\"address_line\":"
468 "\"address_line_1\","
469 "\"address_line_2\""
470 "],"
471 "\"administrative_area_name\":\"admin_area_name\","
472 "\"country_name_code\":\"US\","
473 "\"dependent_locality_name\":\"dependent_locality_name\","
474 "\"language_code\":\"language_code\","
475 "\"locality_name\":\"locality_name\","
476 "\"postal_code_number\":\"postal_code_number\","
477 "\"recipient_name\":\"recipient_name\","
478 "\"sorting_code\":\"sorting_code\""
479 "},"
480 "\"exp_month\":12,"
481 "\"exp_year\":3000,"
482 "\"fop_type\":\"VISA\","
483 "\"last_4_digits\":\"4448\""
484 "},"
485 "\"type\":\"CREDIT_CARD\""
486 "},"
487 "\"instrument_phone_number\":\"phone_number\","
488 "\"merchant_domain\":\"https://example.com/\","
489 "\"phone_number_required\":true,"
490 "\"risk_params\":\"risky business\","
491 "\"shipping_address\":"
493 "\"phone_number\":\"save_phone_number\","
494 "\"postal_address\":"
496 "\"address_line\":"
498 "\"save_address_line_1\","
499 "\"save_address_line_2\""
500 "],"
501 "\"administrative_area_name\":\"save_admin_area_name\","
502 "\"country_name_code\":\"US\","
503 "\"dependent_locality_name\":\"save_dependent_locality_name\","
504 "\"language_code\":\"save_language_code\","
505 "\"locality_name\":\"save_locality_name\","
506 "\"postal_code_number\":\"save_postal_code_number\","
507 "\"recipient_name\":\"save_recipient_name\","
508 "\"sorting_code\":\"save_sorting_code\""
510 "},"
511 "\"use_minimal_addresses\":false"
512 "}";
514 const char kUpdateAddressValidRequest[] =
516 "\"merchant_domain\":\"https://example.com/\","
517 "\"phone_number_required\":true,"
518 "\"risk_params\":\"risky business\","
519 "\"shipping_address\":"
521 "\"id\":\"address_id\","
522 "\"phone_number\":\"ship_phone_number\","
523 "\"postal_address\":"
525 "\"address_line\":"
527 "\"ship_address_line_1\","
528 "\"ship_address_line_2\""
529 "],"
530 "\"administrative_area_name\":\"ship_admin_area_name\","
531 "\"country_name_code\":\"US\","
532 "\"dependent_locality_name\":\"ship_dependent_locality_name\","
533 "\"language_code\":\"ship_language_code\","
534 "\"locality_name\":\"ship_locality_name\","
535 "\"postal_code_number\":\"ship_postal_code_number\","
536 "\"recipient_name\":\"ship_recipient_name\","
537 "\"sorting_code\":\"ship_sorting_code\""
539 "},"
540 "\"use_minimal_addresses\":false"
541 "}";
543 const char kUpdateInstrumentAddressValidRequest[] =
545 "\"instrument_phone_number\":\"phone_number\","
546 "\"merchant_domain\":\"https://example.com/\","
547 "\"phone_number_required\":true,"
548 "\"risk_params\":\"risky business\","
549 "\"upgraded_billing_address\":"
551 "\"address_line\":"
553 "\"address_line_1\","
554 "\"address_line_2\""
555 "],"
556 "\"administrative_area_name\":\"admin_area_name\","
557 "\"country_name_code\":\"US\","
558 "\"dependent_locality_name\":\"dependent_locality_name\","
559 "\"language_code\":\"language_code\","
560 "\"locality_name\":\"locality_name\","
561 "\"postal_code_number\":\"postal_code_number\","
562 "\"recipient_name\":\"recipient_name\","
563 "\"sorting_code\":\"sorting_code\""
564 "},"
565 "\"upgraded_instrument_id\":\"default_instrument_id\","
566 "\"use_minimal_addresses\":false"
567 "}";
569 const char kUpdateInstrumentAddressWithNameChangeValidRequest[] =
571 "\"instrument_phone_number\":\"phone_number\","
572 "\"merchant_domain\":\"https://example.com/\","
573 "\"phone_number_required\":true,"
574 "\"risk_params\":\"risky business\","
575 "\"upgraded_billing_address\":"
577 "\"address_line\":"
579 "\"address_line_1\","
580 "\"address_line_2\""
581 "],"
582 "\"administrative_area_name\":\"admin_area_name\","
583 "\"country_name_code\":\"US\","
584 "\"dependent_locality_name\":\"dependent_locality_name\","
585 "\"language_code\":\"language_code\","
586 "\"locality_name\":\"locality_name\","
587 "\"postal_code_number\":\"postal_code_number\","
588 "\"recipient_name\":\"recipient_name\","
589 "\"sorting_code\":\"sorting_code\""
590 "},"
591 "\"upgraded_instrument_id\":\"default_instrument_id\","
592 "\"use_minimal_addresses\":false"
593 "}";
595 const char kUpdateInstrumentExpirationDateValidRequest[] =
597 "\"instrument\":"
599 "\"credit_card\":"
601 "\"exp_month\":12,"
602 "\"exp_year\":3001"
603 "},"
604 "\"type\":\"CREDIT_CARD\""
605 "},"
606 "\"merchant_domain\":\"https://example.com/\","
607 "\"phone_number_required\":true,"
608 "\"risk_params\":\"risky business\","
609 "\"upgraded_instrument_id\":\"instrument_id\","
610 "\"use_minimal_addresses\":false"
611 "}";
613 class MockAutofillMetrics : public AutofillMetrics {
614 public:
615 MockAutofillMetrics() {}
616 MOCK_CONST_METHOD2(LogWalletApiCallDuration,
617 void(WalletApiCallMetric metric,
618 const base::TimeDelta& duration));
619 MOCK_CONST_METHOD1(LogWalletErrorMetric, void(WalletErrorMetric metric));
620 MOCK_CONST_METHOD1(LogWalletRequiredActionMetric,
621 void(WalletRequiredActionMetric action));
622 MOCK_CONST_METHOD1(LogWalletMalformedResponseMetric,
623 void(WalletApiCallMetric metric));
624 private:
625 DISALLOW_COPY_AND_ASSIGN(MockAutofillMetrics);
628 class MockWalletClientDelegate : public WalletClientDelegate {
629 public:
630 MockWalletClientDelegate()
631 : full_wallets_received_(0),
632 wallet_items_received_(0),
633 is_shipping_required_(true) {}
634 ~MockWalletClientDelegate() {}
636 virtual const AutofillMetrics& GetMetricLogger() const OVERRIDE {
637 return metric_logger_;
640 virtual std::string GetRiskData() const OVERRIDE {
641 return "risky business";
644 virtual std::string GetWalletCookieValue() const OVERRIDE {
645 return "gdToken";
648 virtual bool IsShippingAddressRequired() const OVERRIDE {
649 return is_shipping_required_;
652 void SetIsShippingAddressRequired(bool is_shipping_required) {
653 is_shipping_required_ = is_shipping_required;
656 void ExpectLogWalletApiCallDuration(
657 AutofillMetrics::WalletApiCallMetric metric,
658 size_t times) {
659 EXPECT_CALL(metric_logger_,
660 LogWalletApiCallDuration(metric, testing::_)).Times(times);
663 void ExpectLogWalletMalformedResponse(
664 AutofillMetrics::WalletApiCallMetric metric) {
665 EXPECT_CALL(metric_logger_,
666 LogWalletMalformedResponseMetric(metric)).Times(1);
669 void ExpectWalletErrorMetric(AutofillMetrics::WalletErrorMetric metric) {
670 EXPECT_CALL(metric_logger_, LogWalletErrorMetric(metric)).Times(1);
673 void ExpectWalletRequiredActionMetric(
674 AutofillMetrics::WalletRequiredActionMetric metric) {
675 EXPECT_CALL(metric_logger_,
676 LogWalletRequiredActionMetric(metric)).Times(1);
679 void ExpectBaselineMetrics() {
680 EXPECT_CALL(
681 metric_logger_,
682 LogWalletErrorMetric(
683 AutofillMetrics::WALLET_ERROR_BASELINE_ISSUED_REQUEST))
684 .Times(1);
685 ExpectWalletRequiredActionMetric(
686 AutofillMetrics::WALLET_REQUIRED_ACTION_BASELINE_ISSUED_REQUEST);
689 MockAutofillMetrics* metric_logger() {
690 return &metric_logger_;
693 MOCK_METHOD0(OnDidAcceptLegalDocuments, void());
694 MOCK_METHOD1(OnDidAuthenticateInstrument, void(bool success));
695 MOCK_METHOD4(OnDidSaveToWallet,
696 void(const std::string& instrument_id,
697 const std::string& shipping_address_id,
698 const std::vector<RequiredAction>& required_actions,
699 const std::vector<FormFieldError>& form_field_errors));
700 MOCK_METHOD1(OnWalletError, void(WalletClient::ErrorType error_type));
702 virtual void OnDidGetFullWallet(scoped_ptr<FullWallet> full_wallet) OVERRIDE {
703 EXPECT_TRUE(full_wallet);
704 ++full_wallets_received_;
706 virtual void OnDidGetWalletItems(scoped_ptr<WalletItems> wallet_items)
707 OVERRIDE {
708 EXPECT_TRUE(wallet_items);
709 ++wallet_items_received_;
711 size_t full_wallets_received() const { return full_wallets_received_; }
712 size_t wallet_items_received() const { return wallet_items_received_; }
714 private:
715 size_t full_wallets_received_;
716 size_t wallet_items_received_;
717 bool is_shipping_required_;
719 testing::StrictMock<MockAutofillMetrics> metric_logger_;
722 } // namespace
724 class WalletClientTest : public testing::Test {
725 public:
726 WalletClientTest()
727 : request_context_(new net::TestURLRequestContextGetter(
728 base::MessageLoopProxy::current())) {}
729 virtual ~WalletClientTest() {}
731 virtual void SetUp() OVERRIDE {
732 wallet_client_.reset(
733 new WalletClient(request_context_,
734 &delegate_,
735 GURL(kMerchantUrl)));
738 virtual void TearDown() OVERRIDE {
739 wallet_client_.reset();
742 void VerifyAndFinishRequest(net::HttpStatusCode response_code,
743 const std::string& request_body,
744 const std::string& response_body) {
745 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
746 ASSERT_TRUE(fetcher);
748 const std::string& upload_data = fetcher->upload_data();
749 EXPECT_EQ(request_body, GetData(upload_data));
750 net::HttpRequestHeaders request_headers;
751 fetcher->GetExtraRequestHeaders(&request_headers);
752 std::string auth_header_value;
753 EXPECT_TRUE(request_headers.GetHeader(
754 net::HttpRequestHeaders::kAuthorization,
755 &auth_header_value));
756 EXPECT_EQ("GoogleLogin auth=gdToken", auth_header_value);
758 fetcher->set_response_code(response_code);
759 fetcher->SetResponseString(response_body);
760 fetcher->delegate()->OnURLFetchComplete(fetcher);
762 // Pump the message loop to catch up to any asynchronous tasks that might
763 // have been posted from OnURLFetchComplete().
764 base::RunLoop().RunUntilIdle();
767 void VerifyAndFinishFormEncodedRequest(net::HttpStatusCode response_code,
768 const std::string& json_payload,
769 const std::string& response_body,
770 size_t expected_parameter_number) {
771 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
772 ASSERT_TRUE(fetcher);
774 net::HttpRequestHeaders request_headers;
775 fetcher->GetExtraRequestHeaders(&request_headers);
776 std::string auth_header_value;
777 EXPECT_TRUE(request_headers.GetHeader(
778 net::HttpRequestHeaders::kAuthorization,
779 &auth_header_value));
780 EXPECT_EQ("GoogleLogin auth=gdToken", auth_header_value);
782 const std::string& upload_data = fetcher->upload_data();
783 std::vector<std::pair<std::string, std::string> > tokens;
784 base::SplitStringIntoKeyValuePairs(upload_data, '=', '&', &tokens);
785 EXPECT_EQ(tokens.size(), expected_parameter_number);
787 size_t num_params = 0U;
788 for (size_t i = 0; i < tokens.size(); ++i) {
789 const std::string& key = tokens[i].first;
790 const std::string& value = tokens[i].second;
792 if (key == "request_content_type") {
793 EXPECT_EQ("application/json", value);
794 num_params++;
797 if (key == "request") {
798 EXPECT_EQ(json_payload,
799 GetData(
800 net::UnescapeURLComponent(
801 value, net::UnescapeRule::URL_SPECIAL_CHARS |
802 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE)));
803 num_params++;
806 if (key == "cvn") {
807 EXPECT_EQ("123", value);
808 num_params++;
811 if (key == "card_number") {
812 EXPECT_EQ("4444444444444448", value);
813 num_params++;
816 if (key == "otp") {
817 EXPECT_FALSE(value.empty());
818 num_params++;
821 EXPECT_EQ(expected_parameter_number, num_params);
823 fetcher->set_response_code(response_code);
824 fetcher->SetResponseString(response_body);
825 fetcher->delegate()->OnURLFetchComplete(fetcher);
828 void TestWalletErrorCode(
829 const std::string& error_type_string,
830 const std::string& message_type_for_buyer_string,
831 WalletClient::ErrorType expected_error_type,
832 AutofillMetrics::WalletErrorMetric expected_autofill_metric) {
833 static const char kResponseTemplate[] =
835 " \"error_type\":\"APPLICATION_ERROR\","
836 " \"error_detail\":\"error_detail\","
837 " \"application_error\":\"application_error\","
838 " \"debug_data\":"
839 " {"
840 " \"debug_message\":\"debug_message\","
841 " \"stack_trace\":\"stack_trace\""
842 " },"
843 " \"application_error_data\":\"application_error_data\","
844 " \"wallet_error\":"
845 " {"
846 " \"error_type\":\"%s\","
847 " %s" // Placeholder for |user_error_type|.
848 " \"error_detail\":\"error_detail\","
849 " \"message_for_user\":"
850 " {"
851 " \"text\":\"text\","
852 " \"subtext\":\"subtext\","
853 " \"details\":\"details\""
854 " }"
855 " }"
856 "}";
857 EXPECT_CALL(delegate_, OnWalletError(expected_error_type)).Times(1);
858 delegate_.ExpectLogWalletApiCallDuration(
859 AutofillMetrics::GET_WALLET_ITEMS, 1);
860 delegate_.ExpectBaselineMetrics();
861 delegate_.ExpectWalletErrorMetric(expected_autofill_metric);
863 wallet_client_->GetWalletItems(base::string16(), base::string16());
864 std::string buyer_error;
865 if (!message_type_for_buyer_string.empty()) {
866 buyer_error = base::StringPrintf("\"message_type_for_buyer\":\"%s\",",
867 message_type_for_buyer_string.c_str());
869 std::string response = base::StringPrintf(kResponseTemplate,
870 error_type_string.c_str(),
871 buyer_error.c_str());
872 VerifyAndFinishRequest(net::HTTP_INTERNAL_SERVER_ERROR,
873 kGetWalletItemsValidRequest,
874 response);
877 protected:
878 content::TestBrowserThreadBundle thread_bundle_;
879 scoped_ptr<WalletClient> wallet_client_;
880 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
881 MockWalletClientDelegate delegate_;
883 private:
884 std::string GetData(const std::string& upload_data) {
885 scoped_ptr<base::Value> root(base::JSONReader::Read(upload_data));
887 // If this is not a JSON dictionary, return plain text.
888 if (!root || !root->IsType(base::Value::TYPE_DICTIONARY))
889 return upload_data;
891 // Remove api_key entry (to prevent accidental leak), return JSON as text.
892 base::DictionaryValue* dict =
893 static_cast<base::DictionaryValue*>(root.get());
894 dict->Remove("api_key", NULL);
895 std::string clean_upload_data;
896 base::JSONWriter::Write(dict, &clean_upload_data);
897 return clean_upload_data;
900 net::TestURLFetcherFactory factory_;
903 TEST_F(WalletClientTest, WalletErrorCodes) {
904 struct {
905 std::string error_type_string;
906 std::string message_type_for_buyer_string;
907 WalletClient::ErrorType expected_error_type;
908 AutofillMetrics::WalletErrorMetric expected_autofill_metric;
909 } test_cases[] = {
910 // General |BUYER_ACCOUNT_ERROR| with no |message_type_for_buyer_string|.
912 "buyer_account_error",
914 WalletClient::BUYER_ACCOUNT_ERROR,
915 AutofillMetrics::WALLET_BUYER_ACCOUNT_ERROR
917 // |BUYER_ACCOUNT_ERROR| with "buyer_legal_address_not_supported" in
918 // message_type_for_buyer field.
920 "buyer_account_error",
921 "bla_country_not_supported",
922 WalletClient::BUYER_LEGAL_ADDRESS_NOT_SUPPORTED,
923 AutofillMetrics::WALLET_BUYER_LEGAL_ADDRESS_NOT_SUPPORTED
925 // |BUYER_ACCOUNT_ERROR| with KYC error code in message_type_for_buyer
926 // field.
928 "buyer_account_error",
929 "buyer_kyc_error",
930 WalletClient::UNVERIFIED_KNOW_YOUR_CUSTOMER_STATUS,
931 AutofillMetrics::WALLET_UNVERIFIED_KNOW_YOUR_CUSTOMER_STATUS
933 // |BUYER_ACCOUNT_ERROR| with un-recognizable |message_type_for_buyer|.
935 "buyer_account_error",
936 "random_string",
937 WalletClient::BUYER_ACCOUNT_ERROR,
938 AutofillMetrics::WALLET_BUYER_ACCOUNT_ERROR
940 // The following are other error types we could get from Wallet.
942 "unsupported_merchant",
944 WalletClient::UNSUPPORTED_MERCHANT,
945 AutofillMetrics::WALLET_UNSUPPORTED_MERCHANT
948 "internal_error",
950 WalletClient::INTERNAL_ERROR,
951 AutofillMetrics::WALLET_INTERNAL_ERROR
954 "invalid_params",
956 WalletClient::INVALID_PARAMS,
957 AutofillMetrics::WALLET_INVALID_PARAMS
960 "service_unavailable",
962 WalletClient::SERVICE_UNAVAILABLE,
963 AutofillMetrics::WALLET_SERVICE_UNAVAILABLE
966 "unsupported_api_version",
968 WalletClient::UNSUPPORTED_API_VERSION,
969 AutofillMetrics::WALLET_UNSUPPORTED_API_VERSION
971 // Any un-recognizable |error_type| is a |UNKNOWN_ERROR|.
973 "random_string_1",
975 WalletClient::UNKNOWN_ERROR,
976 AutofillMetrics::WALLET_UNKNOWN_ERROR
979 "random_string_2",
981 WalletClient::UNKNOWN_ERROR,
982 AutofillMetrics::WALLET_UNKNOWN_ERROR
986 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
987 SCOPED_TRACE(
988 base::StringPrintf(
989 "%s - %s",
990 test_cases[i].error_type_string.c_str(),
991 test_cases[i].message_type_for_buyer_string.c_str()));
992 TestWalletErrorCode(test_cases[i].error_type_string,
993 test_cases[i].message_type_for_buyer_string,
994 test_cases[i].expected_error_type,
995 test_cases[i].expected_autofill_metric);
1000 TEST_F(WalletClientTest, WalletErrorResponseMissing) {
1001 EXPECT_CALL(delegate_, OnWalletError(
1002 WalletClient::UNKNOWN_ERROR)).Times(1);
1003 delegate_.ExpectLogWalletApiCallDuration(
1004 AutofillMetrics::GET_WALLET_ITEMS, 1);
1005 delegate_.ExpectBaselineMetrics();
1006 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_UNKNOWN_ERROR);
1008 wallet_client_->GetWalletItems(base::string16(), base::string16());
1009 VerifyAndFinishRequest(net::HTTP_INTERNAL_SERVER_ERROR,
1010 kGetWalletItemsValidRequest,
1011 kErrorTypeMissingInResponse);
1014 TEST_F(WalletClientTest, NetworkFailureOnExpectedResponse) {
1015 EXPECT_CALL(delegate_, OnWalletError(WalletClient::NETWORK_ERROR)).Times(1);
1016 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS,
1018 delegate_.ExpectBaselineMetrics();
1019 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_NETWORK_ERROR);
1021 wallet_client_->GetWalletItems(base::string16(), base::string16());
1022 VerifyAndFinishRequest(net::HTTP_UNAUTHORIZED,
1023 kGetWalletItemsValidRequest,
1024 std::string());
1027 TEST_F(WalletClientTest, RequestError) {
1028 EXPECT_CALL(delegate_, OnWalletError(WalletClient::BAD_REQUEST)).Times(1);
1029 delegate_.ExpectLogWalletApiCallDuration(
1030 AutofillMetrics::GET_WALLET_ITEMS, 1);
1031 delegate_.ExpectBaselineMetrics();
1032 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_BAD_REQUEST);
1034 wallet_client_->GetWalletItems(base::string16(), base::string16());
1035 VerifyAndFinishRequest(net::HTTP_BAD_REQUEST,
1036 kGetWalletItemsValidRequest,
1037 std::string());
1040 TEST_F(WalletClientTest, GetFullWalletSuccess) {
1041 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_FULL_WALLET, 1);
1042 delegate_.ExpectBaselineMetrics();
1044 WalletClient::FullWalletRequest full_wallet_request(
1045 "instrument_id",
1046 "shipping_address_id",
1047 "google_transaction_id",
1048 std::vector<WalletClient::RiskCapability>(),
1049 false);
1050 wallet_client_->GetFullWallet(full_wallet_request);
1052 VerifyAndFinishFormEncodedRequest(net::HTTP_OK,
1053 kGetFullWalletValidRequest,
1054 kGetFullWalletValidResponse,
1055 3U);
1056 EXPECT_EQ(1U, delegate_.full_wallets_received());
1059 TEST_F(WalletClientTest, GetFullWalletSuccessNewuser) {
1060 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_FULL_WALLET, 1);
1061 delegate_.ExpectBaselineMetrics();
1063 WalletClient::FullWalletRequest full_wallet_request(
1064 "instrument_id",
1065 "shipping_address_id",
1066 "google_transaction_id",
1067 std::vector<WalletClient::RiskCapability>(),
1068 true);
1069 wallet_client_->GetFullWallet(full_wallet_request);
1071 VerifyAndFinishFormEncodedRequest(net::HTTP_OK,
1072 kGetFullWalletValidRequestNewUser,
1073 kGetFullWalletValidResponse,
1074 3U);
1075 EXPECT_EQ(1U, delegate_.full_wallets_received());
1078 TEST_F(WalletClientTest, GetFullWalletWithRiskCapabilitesSuccess) {
1079 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_FULL_WALLET, 1);
1080 delegate_.ExpectBaselineMetrics();
1082 std::vector<WalletClient::RiskCapability> risk_capabilities;
1083 risk_capabilities.push_back(WalletClient::VERIFY_CVC);
1084 WalletClient::FullWalletRequest full_wallet_request(
1085 "instrument_id",
1086 "shipping_address_id",
1087 "google_transaction_id",
1088 risk_capabilities,
1089 false);
1090 wallet_client_->GetFullWallet(full_wallet_request);
1092 VerifyAndFinishFormEncodedRequest(
1093 net::HTTP_OK,
1094 kGetFullWalletWithRiskCapabilitesValidRequest,
1095 kGetFullWalletValidResponse,
1096 3U);
1097 EXPECT_EQ(1U, delegate_.full_wallets_received());
1101 TEST_F(WalletClientTest, GetFullWalletMalformedResponse) {
1102 EXPECT_CALL(delegate_,
1103 OnWalletError(WalletClient::MALFORMED_RESPONSE)).Times(1);
1104 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_FULL_WALLET, 1);
1105 delegate_.ExpectBaselineMetrics();
1106 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_MALFORMED_RESPONSE);
1107 delegate_.ExpectLogWalletMalformedResponse(AutofillMetrics::GET_FULL_WALLET);
1109 WalletClient::FullWalletRequest full_wallet_request(
1110 "instrument_id",
1111 "shipping_address_id",
1112 "google_transaction_id",
1113 std::vector<WalletClient::RiskCapability>(),
1114 false);
1115 wallet_client_->GetFullWallet(full_wallet_request);
1117 VerifyAndFinishFormEncodedRequest(net::HTTP_OK,
1118 kGetFullWalletValidRequest,
1119 kGetFullWalletInvalidResponse,
1120 3U);
1121 EXPECT_EQ(0U, delegate_.full_wallets_received());
1124 TEST_F(WalletClientTest, AcceptLegalDocuments) {
1125 EXPECT_CALL(delegate_, OnDidAcceptLegalDocuments()).Times(1);
1126 delegate_.ExpectLogWalletApiCallDuration(
1127 AutofillMetrics::ACCEPT_LEGAL_DOCUMENTS,
1129 delegate_.ExpectBaselineMetrics();
1131 ScopedVector<WalletItems::LegalDocument> docs;
1132 base::DictionaryValue document;
1133 document.SetString("legal_document_id", "doc_id_1");
1134 document.SetString("display_name", "doc_1");
1135 docs.push_back(
1136 WalletItems::LegalDocument::CreateLegalDocument(document).release());
1137 document.SetString("legal_document_id", "doc_id_2");
1138 document.SetString("display_name", "doc_2");
1139 docs.push_back(
1140 WalletItems::LegalDocument::CreateLegalDocument(document).release());
1141 docs.push_back(
1142 WalletItems::LegalDocument::CreatePrivacyPolicyDocument().release());
1143 wallet_client_->AcceptLegalDocuments(docs.get(),
1144 kGoogleTransactionId);
1145 VerifyAndFinishRequest(net::HTTP_OK,
1146 kAcceptLegalDocumentsValidRequest,
1147 ")}'"); // Invalid JSON. Should be ignored.
1150 TEST_F(WalletClientTest, AuthenticateInstrumentSucceeded) {
1151 EXPECT_CALL(delegate_, OnDidAuthenticateInstrument(true)).Times(1);
1152 delegate_.ExpectLogWalletApiCallDuration(
1153 AutofillMetrics::AUTHENTICATE_INSTRUMENT,
1155 delegate_.ExpectBaselineMetrics();
1157 wallet_client_->AuthenticateInstrument("instrument_id", "123");
1159 VerifyAndFinishFormEncodedRequest(net::HTTP_OK,
1160 kAuthenticateInstrumentValidRequest,
1161 kAuthenticateInstrumentSuccessResponse,
1162 3U);
1165 TEST_F(WalletClientTest, AuthenticateInstrumentFailed) {
1166 EXPECT_CALL(delegate_, OnDidAuthenticateInstrument(false)).Times(1);
1167 delegate_.ExpectLogWalletApiCallDuration(
1168 AutofillMetrics::AUTHENTICATE_INSTRUMENT,
1170 delegate_.ExpectBaselineMetrics();
1172 wallet_client_->AuthenticateInstrument("instrument_id", "123");
1174 VerifyAndFinishFormEncodedRequest(net::HTTP_OK,
1175 kAuthenticateInstrumentValidRequest,
1176 kAuthenticateInstrumentFailureResponse,
1177 3U);
1180 TEST_F(WalletClientTest, AuthenticateInstrumentFailedMalformedResponse) {
1181 EXPECT_CALL(delegate_,
1182 OnWalletError(WalletClient::MALFORMED_RESPONSE)).Times(1);
1183 delegate_.ExpectLogWalletApiCallDuration(
1184 AutofillMetrics::AUTHENTICATE_INSTRUMENT,
1186 delegate_.ExpectBaselineMetrics();
1187 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_MALFORMED_RESPONSE);
1188 delegate_.ExpectLogWalletMalformedResponse(
1189 AutofillMetrics::AUTHENTICATE_INSTRUMENT);
1191 wallet_client_->AuthenticateInstrument("instrument_id", "123");
1193 VerifyAndFinishFormEncodedRequest(net::HTTP_OK,
1194 kAuthenticateInstrumentValidRequest,
1195 kSaveInvalidResponse,
1196 3U);
1199 // TODO(ahutter): Add failure tests for GetWalletItems.
1201 TEST_F(WalletClientTest, GetWalletItems) {
1202 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS,
1204 delegate_.ExpectBaselineMetrics();
1206 wallet_client_->GetWalletItems(base::string16(), base::string16());
1208 VerifyAndFinishRequest(net::HTTP_OK,
1209 kGetWalletItemsValidRequest,
1210 kGetWalletItemsValidResponse);
1211 EXPECT_EQ(1U, delegate_.wallet_items_received());
1214 TEST_F(WalletClientTest, GetWalletItemsWithTransactionDetails) {
1215 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS,
1217 delegate_.ExpectBaselineMetrics();
1219 wallet_client_->GetWalletItems(base::ASCIIToUTF16("100.00"),
1220 base::ASCIIToUTF16("USD"));
1222 VerifyAndFinishRequest(net::HTTP_OK,
1223 kGetWalletItemsWithTransactionDetails,
1224 kGetWalletItemsValidResponse);
1225 EXPECT_EQ(1U, delegate_.wallet_items_received());
1228 TEST_F(WalletClientTest, GetWalletItemsRespectsDelegateForShippingRequired) {
1229 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS,
1231 delegate_.ExpectBaselineMetrics();
1232 delegate_.SetIsShippingAddressRequired(false);
1234 wallet_client_->GetWalletItems(base::string16(), base::string16());
1236 VerifyAndFinishRequest(net::HTTP_OK,
1237 kGetWalletItemsNoShippingRequest,
1238 kGetWalletItemsValidResponse);
1239 EXPECT_EQ(1U, delegate_.wallet_items_received());
1242 TEST_F(WalletClientTest, SaveAddressSucceeded) {
1243 EXPECT_CALL(delegate_,
1244 OnDidSaveToWallet(std::string(),
1245 "saved_address_id",
1246 std::vector<RequiredAction>(),
1247 std::vector<FormFieldError>())).Times(1);
1248 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1);
1249 delegate_.ExpectBaselineMetrics();
1251 scoped_ptr<Address> address = GetTestSaveableAddress();
1252 wallet_client_->SaveToWallet(scoped_ptr<Instrument>(),
1253 address.Pass(),
1254 NULL,
1255 NULL);
1256 VerifyAndFinishRequest(net::HTTP_OK,
1257 kSaveAddressValidRequest,
1258 kSaveAddressValidResponse);
1261 TEST_F(WalletClientTest, SaveAddressWithRequiredActionsSucceeded) {
1262 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1);
1263 delegate_.ExpectBaselineMetrics();
1264 delegate_.ExpectWalletRequiredActionMetric(
1265 AutofillMetrics::REQUIRE_PHONE_NUMBER);
1266 delegate_.ExpectWalletRequiredActionMetric(
1267 AutofillMetrics::INVALID_FORM_FIELD);
1269 std::vector<RequiredAction> required_actions;
1270 required_actions.push_back(REQUIRE_PHONE_NUMBER);
1271 required_actions.push_back(INVALID_FORM_FIELD);
1273 std::vector<FormFieldError> form_errors;
1274 form_errors.push_back(FormFieldError(FormFieldError::INVALID_POSTAL_CODE,
1275 FormFieldError::SHIPPING_ADDRESS));
1277 EXPECT_CALL(delegate_,
1278 OnDidSaveToWallet(std::string(),
1279 std::string(),
1280 required_actions,
1281 form_errors)).Times(1);
1283 scoped_ptr<Address> address = GetTestSaveableAddress();
1284 wallet_client_->SaveToWallet(scoped_ptr<Instrument>(),
1285 address.Pass(),
1286 NULL,
1287 NULL);
1288 VerifyAndFinishRequest(net::HTTP_OK,
1289 kSaveAddressValidRequest,
1290 kSaveAddressWithRequiredActionsValidResponse);
1293 TEST_F(WalletClientTest, SaveAddressFailedInvalidRequiredAction) {
1294 EXPECT_CALL(delegate_,
1295 OnWalletError(WalletClient::MALFORMED_RESPONSE)).Times(1);
1296 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1);
1297 delegate_.ExpectBaselineMetrics();
1298 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_MALFORMED_RESPONSE);
1299 delegate_.ExpectLogWalletMalformedResponse(AutofillMetrics::SAVE_TO_WALLET);
1301 scoped_ptr<Address> address = GetTestSaveableAddress();
1302 wallet_client_->SaveToWallet(scoped_ptr<Instrument>(),
1303 address.Pass(),
1304 NULL,
1305 NULL);
1306 VerifyAndFinishRequest(net::HTTP_OK,
1307 kSaveAddressValidRequest,
1308 kSaveWithInvalidRequiredActionsResponse);
1311 TEST_F(WalletClientTest, SaveAddressFailedMalformedResponse) {
1312 EXPECT_CALL(delegate_,
1313 OnWalletError(WalletClient::MALFORMED_RESPONSE)).Times(1);
1314 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1);
1315 delegate_.ExpectBaselineMetrics();
1316 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_MALFORMED_RESPONSE);
1317 delegate_.ExpectLogWalletMalformedResponse(AutofillMetrics::SAVE_TO_WALLET);
1319 scoped_ptr<Address> address = GetTestSaveableAddress();
1320 wallet_client_->SaveToWallet(scoped_ptr<Instrument>(),
1321 address.Pass(),
1322 NULL,
1323 NULL);
1324 VerifyAndFinishRequest(net::HTTP_OK,
1325 kSaveAddressValidRequest,
1326 kSaveInvalidResponse);
1329 TEST_F(WalletClientTest, SaveInstrumentSucceeded) {
1330 EXPECT_CALL(delegate_,
1331 OnDidSaveToWallet("instrument_id",
1332 std::string(),
1333 std::vector<RequiredAction>(),
1334 std::vector<FormFieldError>())).Times(1);
1335 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1);
1336 delegate_.ExpectBaselineMetrics();
1338 scoped_ptr<Instrument> instrument = GetTestInstrument();
1339 wallet_client_->SaveToWallet(instrument.Pass(),
1340 scoped_ptr<Address>(),
1341 NULL,
1342 NULL);
1344 VerifyAndFinishFormEncodedRequest(net::HTTP_OK,
1345 kSaveInstrumentValidRequest,
1346 kSaveInstrumentValidResponse,
1347 4U);
1350 TEST_F(WalletClientTest, SaveInstrumentWithRequiredActionsSucceeded) {
1351 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1);
1352 delegate_.ExpectBaselineMetrics();
1353 delegate_.ExpectWalletRequiredActionMetric(
1354 AutofillMetrics::REQUIRE_PHONE_NUMBER);
1355 delegate_.ExpectWalletRequiredActionMetric(
1356 AutofillMetrics::INVALID_FORM_FIELD);
1358 std::vector<RequiredAction> required_actions;
1359 required_actions.push_back(REQUIRE_PHONE_NUMBER);
1360 required_actions.push_back(INVALID_FORM_FIELD);
1362 std::vector<FormFieldError> form_errors;
1363 form_errors.push_back(FormFieldError(FormFieldError::INVALID_POSTAL_CODE,
1364 FormFieldError::SHIPPING_ADDRESS));
1366 EXPECT_CALL(delegate_,
1367 OnDidSaveToWallet(std::string(),
1368 std::string(),
1369 required_actions,
1370 form_errors)).Times(1);
1372 scoped_ptr<Instrument> instrument = GetTestInstrument();
1373 wallet_client_->SaveToWallet(instrument.Pass(),
1374 scoped_ptr<Address>(),
1375 NULL,
1376 NULL);
1378 VerifyAndFinishFormEncodedRequest(
1379 net::HTTP_OK,
1380 kSaveInstrumentValidRequest,
1381 kSaveInstrumentWithRequiredActionsValidResponse,
1382 4U);
1385 TEST_F(WalletClientTest, SaveInstrumentFailedInvalidRequiredActions) {
1386 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1);
1387 delegate_.ExpectBaselineMetrics();
1388 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_MALFORMED_RESPONSE);
1389 delegate_.ExpectLogWalletMalformedResponse(AutofillMetrics::SAVE_TO_WALLET);
1391 EXPECT_CALL(delegate_,
1392 OnWalletError(WalletClient::MALFORMED_RESPONSE));
1394 scoped_ptr<Instrument> instrument = GetTestInstrument();
1395 wallet_client_->SaveToWallet(instrument.Pass(),
1396 scoped_ptr<Address>(),
1397 NULL,
1398 NULL);
1400 VerifyAndFinishFormEncodedRequest(net::HTTP_OK,
1401 kSaveInstrumentValidRequest,
1402 kSaveWithInvalidRequiredActionsResponse,
1403 4U);
1406 TEST_F(WalletClientTest, SaveInstrumentFailedMalformedResponse) {
1407 EXPECT_CALL(delegate_,
1408 OnWalletError(WalletClient::MALFORMED_RESPONSE)).Times(1);
1409 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1);
1410 delegate_.ExpectBaselineMetrics();
1411 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_MALFORMED_RESPONSE);
1412 delegate_.ExpectLogWalletMalformedResponse(AutofillMetrics::SAVE_TO_WALLET);
1414 scoped_ptr<Instrument> instrument = GetTestInstrument();
1415 wallet_client_->SaveToWallet(instrument.Pass(),
1416 scoped_ptr<Address>(),
1417 NULL,
1418 NULL);
1420 VerifyAndFinishFormEncodedRequest(net::HTTP_OK,
1421 kSaveInstrumentValidRequest,
1422 kSaveInvalidResponse,
1423 4U);
1426 TEST_F(WalletClientTest, SaveInstrumentAndAddressSucceeded) {
1427 EXPECT_CALL(delegate_,
1428 OnDidSaveToWallet("saved_instrument_id",
1429 "saved_address_id",
1430 std::vector<RequiredAction>(),
1431 std::vector<FormFieldError>())).Times(1);
1432 delegate_.ExpectLogWalletApiCallDuration(
1433 AutofillMetrics::SAVE_TO_WALLET,
1435 delegate_.ExpectBaselineMetrics();
1437 scoped_ptr<Instrument> instrument = GetTestInstrument();
1438 scoped_ptr<Address> address = GetTestSaveableAddress();
1439 wallet_client_->SaveToWallet(instrument.Pass(), address.Pass(), NULL, NULL);
1441 VerifyAndFinishFormEncodedRequest(net::HTTP_OK,
1442 kSaveInstrumentAndAddressValidRequest,
1443 kSaveInstrumentAndAddressValidResponse,
1444 4U);
1447 TEST_F(WalletClientTest, SaveInstrumentAndAddressWithRequiredActionsSucceeded) {
1448 delegate_.ExpectLogWalletApiCallDuration(
1449 AutofillMetrics::SAVE_TO_WALLET,
1451 delegate_.ExpectBaselineMetrics();
1452 delegate_.ExpectWalletRequiredActionMetric(
1453 AutofillMetrics::REQUIRE_PHONE_NUMBER);
1454 delegate_.ExpectWalletRequiredActionMetric(
1455 AutofillMetrics::INVALID_FORM_FIELD);
1457 std::vector<RequiredAction> required_actions;
1458 required_actions.push_back(REQUIRE_PHONE_NUMBER);
1459 required_actions.push_back(INVALID_FORM_FIELD);
1461 std::vector<FormFieldError> form_errors;
1462 form_errors.push_back(FormFieldError(FormFieldError::INVALID_POSTAL_CODE,
1463 FormFieldError::SHIPPING_ADDRESS));
1465 EXPECT_CALL(delegate_,
1466 OnDidSaveToWallet(std::string(),
1467 std::string(),
1468 required_actions,
1469 form_errors)).Times(1);
1471 scoped_ptr<Instrument> instrument = GetTestInstrument();
1472 scoped_ptr<Address> address = GetTestSaveableAddress();
1473 wallet_client_->SaveToWallet(instrument.Pass(), address.Pass(), NULL, NULL);
1475 VerifyAndFinishFormEncodedRequest(
1476 net::HTTP_OK,
1477 kSaveInstrumentAndAddressValidRequest,
1478 kSaveInstrumentAndAddressWithRequiredActionsValidResponse,
1479 4U);
1482 TEST_F(WalletClientTest, SaveInstrumentAndAddressFailedInvalidRequiredAction) {
1483 EXPECT_CALL(delegate_,
1484 OnWalletError(WalletClient::MALFORMED_RESPONSE)).Times(1);
1485 delegate_.ExpectLogWalletApiCallDuration(
1486 AutofillMetrics::SAVE_TO_WALLET,
1488 delegate_.ExpectBaselineMetrics();
1489 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_MALFORMED_RESPONSE);
1490 delegate_.ExpectLogWalletMalformedResponse(AutofillMetrics::SAVE_TO_WALLET);
1492 scoped_ptr<Instrument> instrument = GetTestInstrument();
1493 scoped_ptr<Address> address = GetTestSaveableAddress();
1494 wallet_client_->SaveToWallet(instrument.Pass(), address.Pass(), NULL, NULL);
1496 VerifyAndFinishFormEncodedRequest(net::HTTP_OK,
1497 kSaveInstrumentAndAddressValidRequest,
1498 kSaveWithInvalidRequiredActionsResponse,
1499 4U);
1502 TEST_F(WalletClientTest, UpdateAddressSucceeded) {
1503 EXPECT_CALL(delegate_,
1504 OnDidSaveToWallet(std::string(),
1505 "shipping_address_id",
1506 std::vector<RequiredAction>(),
1507 std::vector<FormFieldError>())).Times(1);
1508 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1);
1509 delegate_.ExpectBaselineMetrics();
1511 scoped_ptr<Address> reference_address = GetTestNonDefaultShippingAddress();
1512 wallet_client_->SaveToWallet(scoped_ptr<Instrument>(),
1513 GetTestShippingAddress(),
1514 NULL,
1515 reference_address.get());
1517 VerifyAndFinishRequest(net::HTTP_OK,
1518 kUpdateAddressValidRequest,
1519 kUpdateAddressValidResponse);
1522 TEST_F(WalletClientTest, UpdateAddressWithRequiredActionsSucceeded) {
1523 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1);
1524 delegate_.ExpectBaselineMetrics();
1525 delegate_.ExpectWalletRequiredActionMetric(
1526 AutofillMetrics::REQUIRE_PHONE_NUMBER);
1527 delegate_.ExpectWalletRequiredActionMetric(
1528 AutofillMetrics::INVALID_FORM_FIELD);
1530 std::vector<RequiredAction> required_actions;
1531 required_actions.push_back(REQUIRE_PHONE_NUMBER);
1532 required_actions.push_back(INVALID_FORM_FIELD);
1534 std::vector<FormFieldError> form_errors;
1535 form_errors.push_back(FormFieldError(FormFieldError::INVALID_POSTAL_CODE,
1536 FormFieldError::SHIPPING_ADDRESS));
1538 EXPECT_CALL(delegate_, OnDidSaveToWallet(std::string(),
1539 std::string(),
1540 required_actions,
1541 form_errors)).Times(1);
1543 scoped_ptr<Address> reference_address = GetTestNonDefaultShippingAddress();
1544 wallet_client_->SaveToWallet(scoped_ptr<Instrument>(),
1545 GetTestShippingAddress(),
1546 NULL,
1547 reference_address.get());
1549 VerifyAndFinishRequest(net::HTTP_OK,
1550 kUpdateAddressValidRequest,
1551 kUpdateWithRequiredActionsValidResponse);
1554 TEST_F(WalletClientTest, UpdateAddressFailedInvalidRequiredAction) {
1555 EXPECT_CALL(delegate_,
1556 OnWalletError(WalletClient::MALFORMED_RESPONSE)).Times(1);
1557 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1);
1558 delegate_.ExpectBaselineMetrics();
1559 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_MALFORMED_RESPONSE);
1560 delegate_.ExpectLogWalletMalformedResponse(AutofillMetrics::SAVE_TO_WALLET);
1562 scoped_ptr<Address> reference_address = GetTestNonDefaultShippingAddress();
1563 wallet_client_->SaveToWallet(scoped_ptr<Instrument>(),
1564 GetTestShippingAddress(),
1565 NULL,
1566 reference_address.get());
1568 VerifyAndFinishRequest(net::HTTP_OK,
1569 kUpdateAddressValidRequest,
1570 kSaveWithInvalidRequiredActionsResponse);
1573 TEST_F(WalletClientTest, UpdateAddressMalformedResponse) {
1574 EXPECT_CALL(delegate_,
1575 OnWalletError(WalletClient::MALFORMED_RESPONSE)).Times(1);
1576 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET, 1);
1577 delegate_.ExpectBaselineMetrics();
1578 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_MALFORMED_RESPONSE);
1579 delegate_.ExpectLogWalletMalformedResponse(AutofillMetrics::SAVE_TO_WALLET);
1581 scoped_ptr<Address> reference_address = GetTestNonDefaultShippingAddress();
1582 wallet_client_->SaveToWallet(scoped_ptr<Instrument>(),
1583 GetTestShippingAddress(),
1584 NULL,
1585 reference_address.get());
1587 VerifyAndFinishRequest(net::HTTP_OK,
1588 kUpdateAddressValidRequest,
1589 kUpdateMalformedResponse);
1592 TEST_F(WalletClientTest, UpdateInstrumentAddressSucceeded) {
1593 EXPECT_CALL(delegate_,
1594 OnDidSaveToWallet("instrument_id",
1595 std::string(),
1596 std::vector<RequiredAction>(),
1597 std::vector<FormFieldError>())).Times(1);
1598 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET,
1600 delegate_.ExpectBaselineMetrics();
1602 scoped_ptr<WalletItems::MaskedInstrument> reference_instrument =
1603 GetTestMaskedInstrument();
1604 wallet_client_->SaveToWallet(GetTestAddressUpgradeInstrument(),
1605 scoped_ptr<Address>(),
1606 reference_instrument.get(),
1607 NULL);
1609 VerifyAndFinishRequest(net::HTTP_OK,
1610 kUpdateInstrumentAddressValidRequest,
1611 kUpdateInstrumentValidResponse);
1614 TEST_F(WalletClientTest, UpdateInstrumentExpirationDateSuceeded) {
1615 EXPECT_CALL(delegate_,
1616 OnDidSaveToWallet("instrument_id",
1617 std::string(),
1618 std::vector<RequiredAction>(),
1619 std::vector<FormFieldError>())).Times(1);
1620 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET,
1622 delegate_.ExpectBaselineMetrics();
1624 scoped_ptr<Instrument> instrument = GetTestExpirationDateChangeInstrument();
1625 scoped_ptr<WalletItems::MaskedInstrument> reference_instrument =
1626 GetTestMaskedInstrumentWithId("instrument_id");
1628 int new_month = instrument->expiration_month();
1629 int new_year = instrument->expiration_year();
1630 ASSERT_TRUE(new_month != reference_instrument->expiration_month() ||
1631 new_year != reference_instrument->expiration_year());
1633 wallet_client_->SaveToWallet(instrument.Pass(),
1634 scoped_ptr<Address>(),
1635 reference_instrument.get(),
1636 NULL);
1638 VerifyAndFinishFormEncodedRequest(net::HTTP_OK,
1639 kUpdateInstrumentExpirationDateValidRequest,
1640 kUpdateInstrumentValidResponse,
1641 3U);
1644 TEST_F(WalletClientTest, UpdateInstrumentAddressWithNameChangeSucceeded) {
1645 EXPECT_CALL(delegate_,
1646 OnDidSaveToWallet("instrument_id",
1647 std::string(),
1648 std::vector<RequiredAction>(),
1649 std::vector<FormFieldError>())).Times(1);
1650 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET,
1652 delegate_.ExpectBaselineMetrics();
1654 scoped_ptr<WalletItems::MaskedInstrument> reference_instrument =
1655 GetTestMaskedInstrument();
1656 wallet_client_->SaveToWallet(GetTestAddressNameChangeInstrument(),
1657 scoped_ptr<Address>(),
1658 reference_instrument.get(),
1659 NULL);
1661 VerifyAndFinishFormEncodedRequest(
1662 net::HTTP_OK,
1663 kUpdateInstrumentAddressWithNameChangeValidRequest,
1664 kUpdateInstrumentValidResponse,
1665 3U);
1668 TEST_F(WalletClientTest, UpdateInstrumentWithRequiredActionsSucceeded) {
1669 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET,
1671 delegate_.ExpectBaselineMetrics();
1672 delegate_.ExpectWalletRequiredActionMetric(
1673 AutofillMetrics::REQUIRE_PHONE_NUMBER);
1674 delegate_.ExpectWalletRequiredActionMetric(
1675 AutofillMetrics::INVALID_FORM_FIELD);
1677 std::vector<RequiredAction> required_actions;
1678 required_actions.push_back(REQUIRE_PHONE_NUMBER);
1679 required_actions.push_back(INVALID_FORM_FIELD);
1681 std::vector<FormFieldError> form_errors;
1682 form_errors.push_back(FormFieldError(FormFieldError::INVALID_POSTAL_CODE,
1683 FormFieldError::SHIPPING_ADDRESS));
1685 EXPECT_CALL(delegate_,
1686 OnDidSaveToWallet(std::string(),
1687 std::string(),
1688 required_actions,
1689 form_errors)).Times(1);
1691 scoped_ptr<WalletItems::MaskedInstrument> reference_instrument =
1692 GetTestMaskedInstrument();
1693 wallet_client_->SaveToWallet(GetTestAddressUpgradeInstrument(),
1694 scoped_ptr<Address>(),
1695 reference_instrument.get(),
1696 NULL);
1698 VerifyAndFinishRequest(net::HTTP_OK,
1699 kUpdateInstrumentAddressValidRequest,
1700 kUpdateWithRequiredActionsValidResponse);
1703 TEST_F(WalletClientTest, UpdateInstrumentFailedInvalidRequiredAction) {
1704 EXPECT_CALL(delegate_,
1705 OnWalletError(WalletClient::MALFORMED_RESPONSE)).Times(1);
1706 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET,
1708 delegate_.ExpectBaselineMetrics();
1709 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_MALFORMED_RESPONSE);
1710 delegate_.ExpectLogWalletMalformedResponse(AutofillMetrics::SAVE_TO_WALLET);
1712 scoped_ptr<WalletItems::MaskedInstrument> reference_instrument =
1713 GetTestMaskedInstrument();
1714 wallet_client_->SaveToWallet(GetTestAddressUpgradeInstrument(),
1715 scoped_ptr<Address>(),
1716 reference_instrument.get(),
1717 NULL);
1719 VerifyAndFinishRequest(net::HTTP_OK,
1720 kUpdateInstrumentAddressValidRequest,
1721 kSaveWithInvalidRequiredActionsResponse);
1724 TEST_F(WalletClientTest, UpdateInstrumentMalformedResponse) {
1725 EXPECT_CALL(delegate_,
1726 OnWalletError(WalletClient::MALFORMED_RESPONSE)).Times(1);
1727 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SAVE_TO_WALLET,
1729 delegate_.ExpectBaselineMetrics();
1730 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_MALFORMED_RESPONSE);
1731 delegate_.ExpectLogWalletMalformedResponse(AutofillMetrics::SAVE_TO_WALLET);
1733 scoped_ptr<WalletItems::MaskedInstrument> reference_instrument =
1734 GetTestMaskedInstrument();
1735 wallet_client_->SaveToWallet(GetTestAddressUpgradeInstrument(),
1736 scoped_ptr<Address>(),
1737 reference_instrument.get(),
1738 NULL);
1740 VerifyAndFinishRequest(net::HTTP_OK,
1741 kUpdateInstrumentAddressValidRequest,
1742 kUpdateMalformedResponse);
1745 TEST_F(WalletClientTest, HasRequestInProgress) {
1746 EXPECT_FALSE(wallet_client_->HasRequestInProgress());
1747 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS,
1749 delegate_.ExpectBaselineMetrics();
1751 wallet_client_->GetWalletItems(base::string16(), base::string16());
1752 EXPECT_TRUE(wallet_client_->HasRequestInProgress());
1754 VerifyAndFinishRequest(net::HTTP_OK,
1755 kGetWalletItemsValidRequest,
1756 kGetWalletItemsValidResponse);
1757 EXPECT_FALSE(wallet_client_->HasRequestInProgress());
1760 TEST_F(WalletClientTest, ErrorResponse) {
1761 EXPECT_FALSE(wallet_client_->HasRequestInProgress());
1762 delegate_.ExpectBaselineMetrics();
1763 wallet_client_->GetWalletItems(base::string16(), base::string16());
1764 EXPECT_TRUE(wallet_client_->HasRequestInProgress());
1765 testing::Mock::VerifyAndClear(delegate_.metric_logger());
1767 EXPECT_CALL(delegate_, OnWalletError(
1768 WalletClient::SERVICE_UNAVAILABLE)).Times(1);
1769 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS,
1771 delegate_.ExpectWalletErrorMetric(
1772 AutofillMetrics::WALLET_SERVICE_UNAVAILABLE);
1774 VerifyAndFinishRequest(net::HTTP_INTERNAL_SERVER_ERROR,
1775 kGetWalletItemsValidRequest,
1776 kErrorResponse);
1779 TEST_F(WalletClientTest, CancelRequest) {
1780 EXPECT_FALSE(wallet_client_->HasRequestInProgress());
1781 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS,
1783 delegate_.ExpectBaselineMetrics();
1785 wallet_client_->GetWalletItems(base::string16(), base::string16());
1786 EXPECT_TRUE(wallet_client_->HasRequestInProgress());
1787 wallet_client_->CancelRequest();
1788 EXPECT_FALSE(wallet_client_->HasRequestInProgress());
1791 } // namespace wallet
1792 } // namespace autofill