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/logging.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/values.h"
10 #include "components/autofill/content/browser/wallet/gaia_account.h"
11 #include "components/autofill/content/browser/wallet/required_action.h"
12 #include "components/autofill/content/browser/wallet/wallet_items.h"
13 #include "components/autofill/content/browser/wallet/wallet_test_util.h"
14 #include "testing/gtest/include/gtest/gtest.h"
17 using base::ASCIIToUTF16
;
21 const char kMaskedInstrument
[] =
23 " \"descriptive_name\":\"descriptive_name\","
25 " \"last_four_digits\":\"last_four_digits\","
26 " \"expiration_month\":12,"
27 " \"expiration_year\":2012,"
28 " \"billing_address\":"
31 " \"address1\":\"address1\","
32 " \"address2\":\"address2\","
34 " \"dependent_locality_name\":\"burough\","
35 " \"state\":\"state\","
36 " \"postal_code\":\"postal_code\","
37 " \"sorting_code\":\"sorting_code\","
38 " \"phone_number\":\"phone_number\","
39 " \"country_code\":\"US\","
41 " \"language_code\":\"language_code\""
43 " \"status\":\"VALID\","
44 " \"object_id\":\"object_id\""
47 const char kMaskedInstrumentMissingStatus
[] =
49 " \"descriptive_name\":\"descriptive_name\","
51 " \"last_four_digits\":\"last_four_digits\","
52 " \"expiration_month\":12,"
53 " \"expiration_year\":2012,"
54 " \"billing_address\":"
57 " \"address1\":\"address1\","
58 " \"address2\":\"address2\","
60 " \"dependent_locality_name\":\"burough\","
61 " \"state\":\"state\","
62 " \"postal_code\":\"postal_code\","
63 " \"sorting_code\":\"sorting_code\","
64 " \"phone_number\":\"phone_number\","
65 " \"country_code\":\"US\","
66 " \"language_code\":\"language_code\""
68 " \"object_id\":\"object_id\","
69 " \"amex_disallowed\":true"
72 const char kMaskedInstrumentMissingType
[] =
74 " \"descriptive_name\":\"descriptive_name\","
75 " \"last_four_digits\":\"last_four_digits\","
76 " \"expiration_month\":12,"
77 " \"expiration_year\":2012,"
78 " \"billing_address\":"
81 " \"address1\":\"address1\","
82 " \"address2\":\"address2\","
84 " \"dependent_locality_name\":\"burough\","
85 " \"state\":\"state\","
86 " \"postal_code\":\"postal_code\","
87 " \"sorting_code\":\"sorting_code\","
88 " \"phone_number\":\"phone_number\","
89 " \"country_code\":\"US\","
90 " \"language_code\":\"language_code\""
92 " \"status\":\"VALID\","
93 " \"object_id\":\"object_id\""
96 const char kMaskedInstrumentMissingLastFourDigits
[] =
98 " \"descriptive_name\":\"descriptive_name\","
100 " \"expiration_month\":12,"
101 " \"expiration_year\":2012,"
102 " \"billing_address\":"
104 " \"name\":\"name\","
105 " \"address1\":\"address1\","
106 " \"address2\":\"address2\","
107 " \"city\":\"city\","
108 " \"dependent_locality_name\":\"burough\","
109 " \"state\":\"state\","
110 " \"postal_code\":\"postal_code\","
111 " \"sorting_code\":\"sorting_code\","
112 " \"phone_number\":\"phone_number\","
113 " \"country_code\":\"US\","
114 " \"language_code\":\"language_code\""
116 " \"status\":\"VALID\","
117 " \"object_id\":\"object_id\""
120 const char kMaskedInstrumentMissingAddress
[] =
122 " \"descriptive_name\":\"descriptive_name\","
123 " \"type\":\"VISA\","
124 " \"last_four_digits\":\"last_four_digits\","
125 " \"expiration_month\":12,"
126 " \"expiration_year\":2012,"
127 " \"status\":\"VALID\","
128 " \"object_id\":\"object_id\""
131 const char kMaskedInstrumentMalformedAddress
[] =
133 " \"descriptive_name\":\"descriptive_name\","
134 " \"type\":\"VISA\","
135 " \"last_four_digits\":\"last_four_digits\","
136 " \"expiration_month\":12,"
137 " \"expiration_year\":2012,"
138 " \"billing_address\":"
140 " \"address1\":\"address1\","
141 " \"address2\":\"address2\","
142 " \"city\":\"city\","
143 " \"dependent_locality_name\":\"burough\","
144 " \"state\":\"state\","
145 " \"phone_number\":\"phone_number\","
146 " \"country_code\":\"US\""
148 " \"status\":\"VALID\","
149 " \"object_id\":\"object_id\""
152 const char kMaskedInstrumentMissingObjectId
[] =
154 " \"descriptive_name\":\"descriptive_name\","
155 " \"type\":\"VISA\","
156 " \"last_four_digits\":\"last_four_digits\","
157 " \"expiration_month\":12,"
158 " \"expiration_year\":2012,"
159 " \"billing_address\":"
161 " \"name\":\"name\","
162 " \"address1\":\"address1\","
163 " \"address2\":\"address2\","
164 " \"city\":\"city\","
165 " \"dependent_locality_name\":\"burough\","
166 " \"state\":\"state\","
167 " \"postal_code\":\"postal_code\","
168 " \"phone_number\":\"phone_number\","
169 " \"country_code\":\"US\","
170 " \"language_code\":\"language_code\""
172 " \"status\":\"VALID\""
175 const char kLegalDocument
[] =
177 " \"legal_document_id\":\"doc_id\","
178 " \"display_name\":\"display_name\""
181 const char kLegalDocumentMissingDocumentId
[] =
183 " \"display_name\":\"display_name\""
186 const char kLegalDocumentMissingDisplayName
[] =
188 " \"legal_document_id\":\"doc_id\""
191 const char kWalletItemsWithRequiredActions
[] =
193 " \"required_action\":"
195 " \" setup_wallet\","
196 " \" CHOOse_ANother_INSTRUMENT_OR_ADDRESS\","
198 " \" \\tGAIA_auth \\n\\r\","
199 " \"UPDATE_expiration_date\","
200 " \"UPGRADE_min_ADDRESS \","
201 " \" pAsSiVe_GAIA_auth \","
202 " \" REQUIRE_PHONE_NUMBER\\t \""
206 const char kWalletItemsWithInvalidRequiredActions
[] =
208 " \"required_action\":"
211 " \"invalid_FORM_FIELD\","
216 const char kWalletItemsMissingGoogleTransactionId
[] =
218 " \"required_action\":"
224 " \"descriptive_name\":\"descriptive_name\","
225 " \"type\":\"VISA\","
226 " \"last_four_digits\":\"last_four_digits\","
227 " \"expiration_month\":12,"
228 " \"expiration_year\":2012,"
229 " \"billing_address\":"
231 " \"name\":\"name\","
232 " \"address1\":\"address1\","
233 " \"address2\":\"address2\","
234 " \"city\":\"city\","
235 " \"state\":\"state\","
236 " \"postal_code\":\"postal_code\","
237 " \"phone_number\":\"phone_number\","
238 " \"country_code\":\"US\","
239 " \"language_code\":\"language_code\""
241 " \"status\":\"VALID\","
242 " \"object_id\":\"object_id\""
245 " \"default_instrument_id\":\"default_instrument_id\","
250 " \"phone_number\":\"phone_number\","
251 " \"postal_address\":"
253 " \"recipient_name\":\"recipient_name\","
256 " \"address_line_1\","
257 " \"address_line_2\""
259 " \"locality_name\":\"locality_name\","
260 " \"administrative_area_name\":\"administrative_area_name\","
261 " \"postal_code_number\":\"postal_code_number\","
262 " \"country_name_code\":\"US\","
263 " \"language_code\":\"language_code\""
267 " \"default_address_id\":\"default_address_id\","
268 " \"amex_disallowed\":true,"
269 " \"required_legal_document\":"
272 " \"legal_document_id\":\"doc_id\","
273 " \"display_name\":\"display_name\""
278 const char kWalletItems
[] =
280 " \"required_action\":"
283 " \"google_transaction_id\":\"google_transaction_id\","
287 " \"descriptive_name\":\"descriptive_name\","
288 " \"type\":\"VISA\","
289 " \"last_four_digits\":\"last_four_digits\","
290 " \"expiration_month\":12,"
291 " \"expiration_year\":2012,"
292 " \"billing_address\":"
294 " \"name\":\"name\","
295 " \"address1\":\"address1\","
296 " \"address2\":\"address2\","
297 " \"city\":\"city\","
298 " \"dependent_locality_name\":\"burough\","
299 " \"state\":\"state\","
300 " \"postal_code\":\"postal_code\","
301 " \"sorting_code\":\"sorting_code\","
302 " \"phone_number\":\"phone_number\","
303 " \"country_code\":\"US\","
304 " \"type\":\"FULL\","
305 " \"language_code\":\"language_code\""
307 " \"status\":\"VALID\","
308 " \"object_id\":\"object_id\""
311 " \"default_instrument_id\":\"default_instrument_id\","
316 " \"phone_number\":\"phone_number\","
317 " \"postal_address\":"
319 " \"recipient_name\":\"recipient_name\","
322 " \"address_line_1\","
323 " \"address_line_2\""
325 " \"locality_name\":\"locality_name\","
326 " \"dependent_locality_name\":\"dependent_locality_name\","
327 " \"administrative_area_name\":\"administrative_area_name\","
328 " \"postal_code_number\":\"postal_code_number\","
329 " \"sorting_code\":\"sorting_code\","
330 " \"country_name_code\":\"US\","
331 " \"language_code\":\"language_code\""
335 " \"default_address_id\":\"default_address_id\","
336 " \"obfuscated_gaia_id\":\"ignore_this_value\","
337 " \"amex_disallowed\":true,"
341 " \"buyer_email\":\"user@chromium.org\","
343 " \"gaia_id\":\"123456789\","
344 " \"buyer_name\":\"Joe Usecase\","
345 " \"is_active\":true,"
346 " \"avatar_url_27x27\":\"https://lh3.googleusercontent.com/27.jpg\","
347 " \"avatar_url_54x54\":\"https://lh3.googleusercontent.com/54.jpg\","
348 " \"avatar_url_48x48\":\"https://lh3.googleusercontent.com/48.jpg\","
349 " \"avatar_url_96x96\":\"https://lh3.googleusercontent.com/96.jpg\""
352 " \"buyer_email\":\"user2@chromium.org\","
354 " \"gaia_id\":\"obfuscated_gaia_id\","
355 " \"buyer_name\":\"Jill Usecase\","
356 " \"is_active\":false,"
357 " \"avatar_url_27x27\":\"https://lh3.googleusercontent.com/27.jpg\","
358 " \"avatar_url_54x54\":\"https://lh3.googleusercontent.com/54.jpg\","
359 " \"avatar_url_48x48\":\"https://lh3.googleusercontent.com/48.jpg\","
360 " \"avatar_url_96x96\":\"https://lh3.googleusercontent.com/96.jpg\""
363 " \"allowed_shipping_spec_by_country\":"
365 " {\"country_code\":\"AC\"},"
366 " {\"country_code\":\"AD\"},"
367 " {\"country_code\":\"US\"}"
370 const char kRequiredLegalDocument
[] =
372 " \"required_legal_document\":"
375 " \"legal_document_id\":\"doc_id\","
376 " \"display_name\":\"display_name\""
380 const char kCloseJson
[] = "}";
382 } // anonymous namespace
387 class WalletItemsTest
: public testing::Test
{
391 void SetUpDictionary(const std::string
& json
) {
392 scoped_ptr
<base::Value
> value(base::JSONReader::Read(json
));
393 ASSERT_TRUE(value
.get());
394 ASSERT_TRUE(value
->IsType(base::Value::TYPE_DICTIONARY
));
395 dict
.reset(static_cast<base::DictionaryValue
*>(value
.release()));
397 scoped_ptr
<base::DictionaryValue
> dict
;
400 TEST_F(WalletItemsTest
, CreateMaskedInstrumentMissingStatus
) {
401 SetUpDictionary(kMaskedInstrumentMissingStatus
);
403 WalletItems::MaskedInstrument::CreateMaskedInstrument(*dict
).get());
406 TEST_F(WalletItemsTest
, CreateMaskedInstrumentMissingType
) {
407 SetUpDictionary(kMaskedInstrumentMissingType
);
409 WalletItems::MaskedInstrument::CreateMaskedInstrument(*dict
).get());
412 TEST_F(WalletItemsTest
, CreateMaskedInstrumentMissingLastFourDigits
) {
413 SetUpDictionary(kMaskedInstrumentMissingLastFourDigits
);
415 WalletItems::MaskedInstrument::CreateMaskedInstrument(*dict
).get());
418 TEST_F(WalletItemsTest
, CreateMaskedInstrumentMissingAddress
) {
419 SetUpDictionary(kMaskedInstrumentMissingAddress
);
421 WalletItems::MaskedInstrument::CreateMaskedInstrument(*dict
).get());
424 TEST_F(WalletItemsTest
, CreateMaskedInstrumentMalformedAddress
) {
425 SetUpDictionary(kMaskedInstrumentMalformedAddress
);
427 WalletItems::MaskedInstrument::CreateMaskedInstrument(*dict
).get());
430 TEST_F(WalletItemsTest
, CreateMaskedInstrumentMissingObjectId
) {
431 SetUpDictionary(kMaskedInstrumentMissingObjectId
);
433 WalletItems::MaskedInstrument::CreateMaskedInstrument(*dict
).get());
436 TEST_F(WalletItemsTest
, CreateMaskedInstrument
) {
437 SetUpDictionary(kMaskedInstrument
);
438 scoped_ptr
<Address
> address(
440 ASCIIToUTF16("name"),
441 StreetAddress("address1", "address2"),
442 ASCIIToUTF16("city"),
443 ASCIIToUTF16("burough"),
444 ASCIIToUTF16("state"),
445 ASCIIToUTF16("postal_code"),
446 ASCIIToUTF16("sorting_code"),
447 ASCIIToUTF16("phone_number"),
450 WalletItems::MaskedInstrument
masked_instrument(
451 ASCIIToUTF16("descriptive_name"),
452 WalletItems::MaskedInstrument::VISA
,
453 ASCIIToUTF16("last_four_digits"),
457 WalletItems::MaskedInstrument::VALID
,
459 EXPECT_EQ(masked_instrument
,
460 *WalletItems::MaskedInstrument::CreateMaskedInstrument(*dict
));
463 TEST_F(WalletItemsTest
, CreateLegalDocumentMissingDocId
) {
464 SetUpDictionary(kLegalDocumentMissingDocumentId
);
465 EXPECT_EQ(NULL
, WalletItems::LegalDocument::CreateLegalDocument(*dict
).get());
468 TEST_F(WalletItemsTest
, CreateLegalDocumentMissingDisplayName
) {
469 SetUpDictionary(kLegalDocumentMissingDisplayName
);
470 EXPECT_EQ(NULL
, WalletItems::LegalDocument::CreateLegalDocument(*dict
).get());
473 TEST_F(WalletItemsTest
, CreateLegalDocument
) {
474 SetUpDictionary(kLegalDocument
);
475 WalletItems::LegalDocument
expected("doc_id", ASCIIToUTF16("display_name"));
477 *WalletItems::LegalDocument::CreateLegalDocument(*dict
));
480 TEST_F(WalletItemsTest
, LegalDocumentUrl
) {
481 WalletItems::LegalDocument
legal_doc("doc_id", ASCIIToUTF16("display_name"));
482 EXPECT_EQ("https://wallet.google.com/legaldocument?docId=doc_id",
483 legal_doc
.url().spec());
486 TEST_F(WalletItemsTest
, LegalDocumentEmptyId
) {
487 WalletItems::LegalDocument
legal_doc(GURL("http://example.com"),
488 ASCIIToUTF16("display_name"));
489 EXPECT_TRUE(legal_doc
.id().empty());
492 TEST_F(WalletItemsTest
, CreateWalletItemsWithRequiredActions
) {
493 SetUpDictionary(kWalletItemsWithRequiredActions
);
495 std::vector
<RequiredAction
> required_actions
;
496 required_actions
.push_back(SETUP_WALLET
);
497 required_actions
.push_back(CHOOSE_ANOTHER_INSTRUMENT_OR_ADDRESS
);
498 required_actions
.push_back(ACCEPT_TOS
);
499 required_actions
.push_back(GAIA_AUTH
);
500 required_actions
.push_back(UPDATE_EXPIRATION_DATE
);
501 required_actions
.push_back(UPGRADE_MIN_ADDRESS
);
502 required_actions
.push_back(PASSIVE_GAIA_AUTH
);
503 required_actions
.push_back(REQUIRE_PHONE_NUMBER
);
505 WalletItems
expected(required_actions
,
510 EXPECT_EQ(expected
, *WalletItems::CreateWalletItems(*dict
));
512 ASSERT_FALSE(required_actions
.empty());
513 required_actions
.pop_back();
514 WalletItems
different_required_actions(required_actions
,
519 EXPECT_NE(expected
, different_required_actions
);
522 TEST_F(WalletItemsTest
, CreateWalletItemsWithInvalidRequiredActions
) {
523 SetUpDictionary(kWalletItemsWithInvalidRequiredActions
);
524 EXPECT_EQ(NULL
, WalletItems::CreateWalletItems(*dict
).get());
527 TEST_F(WalletItemsTest
, CreateWalletItemsMissingGoogleTransactionId
) {
528 SetUpDictionary(kWalletItemsMissingGoogleTransactionId
);
529 EXPECT_EQ(NULL
, WalletItems::CreateWalletItems(*dict
).get());
532 TEST_F(WalletItemsTest
, CreateWalletItemsMissingAmexDisallowed
) {
533 SetUpDictionary(std::string(kWalletItems
) + std::string(kCloseJson
));
534 EXPECT_TRUE(dict
->Remove("amex_disallowed", NULL
));
535 base::string16 amex_number
= ASCIIToUTF16("378282246310005");
536 base::string16 message
;
537 EXPECT_FALSE(WalletItems::CreateWalletItems(*dict
)->SupportsCard(amex_number
,
539 EXPECT_FALSE(message
.empty());
542 TEST_F(WalletItemsTest
, CreateWalletItems
) {
543 SetUpDictionary(std::string(kWalletItems
) + std::string(kCloseJson
));
544 std::vector
<RequiredAction
> required_actions
;
545 WalletItems
expected(required_actions
,
546 "google_transaction_id",
547 "default_instrument_id",
548 "default_address_id",
551 scoped_ptr
<GaiaAccount
> user1(GaiaAccount::CreateForTesting(
556 expected
.AddAccount(user1
.Pass());
557 scoped_ptr
<GaiaAccount
> user2(GaiaAccount::CreateForTesting(
558 "user2@chromium.org",
559 "obfuscated_gaia_id",
562 expected
.AddAccount(user2
.Pass());
563 EXPECT_EQ("123456789", expected
.ObfuscatedGaiaId());
565 scoped_ptr
<Address
> billing_address(
567 ASCIIToUTF16("name"),
568 StreetAddress("address1", "address2"),
569 ASCIIToUTF16("city"),
570 ASCIIToUTF16("burough"),
571 ASCIIToUTF16("state"),
572 ASCIIToUTF16("postal_code"),
573 ASCIIToUTF16("sorting_code"),
574 ASCIIToUTF16("phone_number"),
577 scoped_ptr
<WalletItems::MaskedInstrument
> masked_instrument(
578 new WalletItems::MaskedInstrument(ASCIIToUTF16("descriptive_name"),
579 WalletItems::MaskedInstrument::VISA
,
580 ASCIIToUTF16("last_four_digits"),
583 billing_address
.Pass(),
584 WalletItems::MaskedInstrument::VALID
,
586 expected
.AddInstrument(masked_instrument
.Pass());
588 scoped_ptr
<Address
> shipping_address(
590 ASCIIToUTF16("recipient_name"),
591 StreetAddress("address_line_1", "address_line_2"),
592 ASCIIToUTF16("locality_name"),
593 ASCIIToUTF16("dependent_locality_name"),
594 ASCIIToUTF16("administrative_area_name"),
595 ASCIIToUTF16("postal_code_number"),
596 ASCIIToUTF16("sorting_code"),
597 ASCIIToUTF16("phone_number"),
600 expected
.AddAddress(shipping_address
.Pass());
602 expected
.AddAllowedShippingCountry("AC");
603 expected
.AddAllowedShippingCountry("AD");
604 expected
.AddAllowedShippingCountry("US");
606 EXPECT_EQ(expected
, *WalletItems::CreateWalletItems(*dict
));
608 // Now try with a legal document as well.
609 SetUpDictionary(std::string(kWalletItems
) +
610 std::string(kRequiredLegalDocument
) +
611 std::string(kCloseJson
));
612 scoped_ptr
<WalletItems::LegalDocument
> legal_document(
613 new WalletItems::LegalDocument("doc_id",
614 ASCIIToUTF16("display_name")));
615 expected
.AddLegalDocument(legal_document
.Pass());
616 expected
.AddLegalDocument(
617 WalletItems::LegalDocument::CreatePrivacyPolicyDocument());
619 EXPECT_EQ(expected
, *WalletItems::CreateWalletItems(*dict
));
622 } // namespace wallet
623 } // namespace autofill