Add diagnostics_writer.cc to the list of files allowed to printf.
[chromium-blink-merge.git] / components / autofill / content / browser / wallet / wallet_items_unittest.cc
blob03276e1819c774d031ba5ee5d742466e48e31d93
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"
15 #include "url/gurl.h"
17 using base::ASCIIToUTF16;
19 namespace {
21 const char kMaskedInstrument[] =
22 "{"
23 " \"descriptive_name\":\"descriptive_name\","
24 " \"type\":\"VISA\","
25 " \"last_four_digits\":\"last_four_digits\","
26 " \"expiration_month\":12,"
27 " \"expiration_year\":2012,"
28 " \"billing_address\":"
29 " {"
30 " \"name\":\"name\","
31 " \"address1\":\"address1\","
32 " \"address2\":\"address2\","
33 " \"city\":\"city\","
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\","
40 " \"type\":\"FULL\","
41 " \"language_code\":\"language_code\""
42 " },"
43 " \"status\":\"VALID\","
44 " \"object_id\":\"object_id\""
45 "}";
47 const char kMaskedInstrumentMissingStatus[] =
48 "{"
49 " \"descriptive_name\":\"descriptive_name\","
50 " \"type\":\"VISA\","
51 " \"last_four_digits\":\"last_four_digits\","
52 " \"expiration_month\":12,"
53 " \"expiration_year\":2012,"
54 " \"billing_address\":"
55 " {"
56 " \"name\":\"name\","
57 " \"address1\":\"address1\","
58 " \"address2\":\"address2\","
59 " \"city\":\"city\","
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\""
67 " },"
68 " \"object_id\":\"object_id\","
69 " \"amex_disallowed\":true"
70 "}";
72 const char kMaskedInstrumentMissingType[] =
73 "{"
74 " \"descriptive_name\":\"descriptive_name\","
75 " \"last_four_digits\":\"last_four_digits\","
76 " \"expiration_month\":12,"
77 " \"expiration_year\":2012,"
78 " \"billing_address\":"
79 " {"
80 " \"name\":\"name\","
81 " \"address1\":\"address1\","
82 " \"address2\":\"address2\","
83 " \"city\":\"city\","
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\""
91 " },"
92 " \"status\":\"VALID\","
93 " \"object_id\":\"object_id\""
94 "}";
96 const char kMaskedInstrumentMissingLastFourDigits[] =
97 "{"
98 " \"descriptive_name\":\"descriptive_name\","
99 " \"type\":\"VISA\","
100 " \"expiration_month\":12,"
101 " \"expiration_year\":2012,"
102 " \"billing_address\":"
103 " {"
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\""
115 " },"
116 " \"status\":\"VALID\","
117 " \"object_id\":\"object_id\""
118 "}";
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\""
129 "}";
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\":"
139 " {"
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\""
147 " },"
148 " \"status\":\"VALID\","
149 " \"object_id\":\"object_id\""
150 "}";
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\":"
160 " {"
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\""
171 " },"
172 " \"status\":\"VALID\""
173 "}";
175 const char kLegalDocument[] =
177 " \"legal_document_id\":\"doc_id\","
178 " \"display_name\":\"display_name\""
179 "}";
181 const char kLegalDocumentMissingDocumentId[] =
183 " \"display_name\":\"display_name\""
184 "}";
186 const char kLegalDocumentMissingDisplayName[] =
188 " \"legal_document_id\":\"doc_id\""
189 "}";
191 const char kWalletItemsWithRequiredActions[] =
193 " \"required_action\":"
194 " ["
195 " \" setup_wallet\","
196 " \" CHOOse_ANother_INSTRUMENT_OR_ADDRESS\","
197 " \"AcCePt_ToS \","
198 " \" \\tGAIA_auth \\n\\r\","
199 " \"UPDATE_expiration_date\","
200 " \"UPGRADE_min_ADDRESS \","
201 " \" pAsSiVe_GAIA_auth \","
202 " \" REQUIRE_PHONE_NUMBER\\t \""
203 " ]"
204 "}";
206 const char kWalletItemsWithInvalidRequiredActions[] =
208 " \"required_action\":"
209 " ["
210 " \"verify_CVV\","
211 " \"invalid_FORM_FIELD\","
212 " \" 忍者の正体 \""
213 " ]"
214 "}";
216 const char kWalletItemsMissingGoogleTransactionId[] =
218 " \"required_action\":"
219 " ["
220 " ],"
221 " \"instrument\":"
222 " ["
223 " {"
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\":"
230 " {"
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\""
240 " },"
241 " \"status\":\"VALID\","
242 " \"object_id\":\"object_id\""
243 " }"
244 " ],"
245 " \"default_instrument_id\":\"default_instrument_id\","
246 " \"address\":"
247 " ["
248 " {"
249 " \"id\":\"id\","
250 " \"phone_number\":\"phone_number\","
251 " \"postal_address\":"
252 " {"
253 " \"recipient_name\":\"recipient_name\","
254 " \"address_line\":"
255 " ["
256 " \"address_line_1\","
257 " \"address_line_2\""
258 " ],"
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\""
264 " }"
265 " }"
266 " ],"
267 " \"default_address_id\":\"default_address_id\","
268 " \"amex_disallowed\":true,"
269 " \"required_legal_document\":"
270 " ["
271 " {"
272 " \"legal_document_id\":\"doc_id\","
273 " \"display_name\":\"display_name\""
274 " }"
275 " ]"
276 "}";
278 const char kWalletItems[] =
280 " \"required_action\":"
281 " ["
282 " ],"
283 " \"google_transaction_id\":\"google_transaction_id\","
284 " \"instrument\":"
285 " ["
286 " {"
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\":"
293 " {"
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\""
306 " },"
307 " \"status\":\"VALID\","
308 " \"object_id\":\"object_id\""
309 " }"
310 " ],"
311 " \"default_instrument_id\":\"default_instrument_id\","
312 " \"address\":"
313 " ["
314 " {"
315 " \"id\":\"id\","
316 " \"phone_number\":\"phone_number\","
317 " \"postal_address\":"
318 " {"
319 " \"recipient_name\":\"recipient_name\","
320 " \"address_line\":"
321 " ["
322 " \"address_line_1\","
323 " \"address_line_2\""
324 " ],"
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\""
332 " }"
333 " }"
334 " ],"
335 " \"default_address_id\":\"default_address_id\","
336 " \"obfuscated_gaia_id\":\"ignore_this_value\","
337 " \"amex_disallowed\":true,"
338 " \"gaia_profile\":"
339 " ["
340 " {"
341 " \"buyer_email\":\"user@chromium.org\","
342 " \"gaia_index\":0,"
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\""
350 " },"
351 " {"
352 " \"buyer_email\":\"user2@chromium.org\","
353 " \"gaia_index\":1,"
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\""
361 " }"
362 " ],"
363 " \"allowed_shipping_spec_by_country\":"
364 " ["
365 " {\"country_code\":\"AC\"},"
366 " {\"country_code\":\"AD\"},"
367 " {\"country_code\":\"US\"}"
368 " ]";
370 const char kRequiredLegalDocument[] =
371 " ,"
372 " \"required_legal_document\":"
373 " ["
374 " {"
375 " \"legal_document_id\":\"doc_id\","
376 " \"display_name\":\"display_name\""
377 " }"
378 " ]";
380 const char kCloseJson[] = "}";
382 } // anonymous namespace
384 namespace autofill {
385 namespace wallet {
387 class WalletItemsTest : public testing::Test {
388 public:
389 WalletItemsTest() {}
390 protected:
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);
402 EXPECT_EQ(NULL,
403 WalletItems::MaskedInstrument::CreateMaskedInstrument(*dict).get());
406 TEST_F(WalletItemsTest, CreateMaskedInstrumentMissingType) {
407 SetUpDictionary(kMaskedInstrumentMissingType);
408 EXPECT_EQ(NULL,
409 WalletItems::MaskedInstrument::CreateMaskedInstrument(*dict).get());
412 TEST_F(WalletItemsTest, CreateMaskedInstrumentMissingLastFourDigits) {
413 SetUpDictionary(kMaskedInstrumentMissingLastFourDigits);
414 EXPECT_EQ(NULL,
415 WalletItems::MaskedInstrument::CreateMaskedInstrument(*dict).get());
418 TEST_F(WalletItemsTest, CreateMaskedInstrumentMissingAddress) {
419 SetUpDictionary(kMaskedInstrumentMissingAddress);
420 EXPECT_EQ(NULL,
421 WalletItems::MaskedInstrument::CreateMaskedInstrument(*dict).get());
424 TEST_F(WalletItemsTest, CreateMaskedInstrumentMalformedAddress) {
425 SetUpDictionary(kMaskedInstrumentMalformedAddress);
426 EXPECT_EQ(NULL,
427 WalletItems::MaskedInstrument::CreateMaskedInstrument(*dict).get());
430 TEST_F(WalletItemsTest, CreateMaskedInstrumentMissingObjectId) {
431 SetUpDictionary(kMaskedInstrumentMissingObjectId);
432 EXPECT_EQ(NULL,
433 WalletItems::MaskedInstrument::CreateMaskedInstrument(*dict).get());
436 TEST_F(WalletItemsTest, CreateMaskedInstrument) {
437 SetUpDictionary(kMaskedInstrument);
438 scoped_ptr<Address> address(
439 new Address("US",
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"),
448 std::string(),
449 "language_code"));
450 WalletItems::MaskedInstrument masked_instrument(
451 ASCIIToUTF16("descriptive_name"),
452 WalletItems::MaskedInstrument::VISA,
453 ASCIIToUTF16("last_four_digits"),
455 2012,
456 address.Pass(),
457 WalletItems::MaskedInstrument::VALID,
458 "object_id");
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"));
476 EXPECT_EQ(expected,
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,
506 std::string(),
507 std::string(),
508 std::string(),
509 AMEX_DISALLOWED);
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,
515 std::string(),
516 std::string(),
517 std::string(),
518 AMEX_DISALLOWED);
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,
538 &message));
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",
549 AMEX_DISALLOWED);
551 scoped_ptr<GaiaAccount> user1(GaiaAccount::CreateForTesting(
552 "user@chromium.org",
553 "123456789",
555 true));
556 expected.AddAccount(user1.Pass());
557 scoped_ptr<GaiaAccount> user2(GaiaAccount::CreateForTesting(
558 "user2@chromium.org",
559 "obfuscated_gaia_id",
561 false));
562 expected.AddAccount(user2.Pass());
563 EXPECT_EQ("123456789", expected.ObfuscatedGaiaId());
565 scoped_ptr<Address> billing_address(
566 new Address("US",
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"),
575 std::string(),
576 "language_code"));
577 scoped_ptr<WalletItems::MaskedInstrument> masked_instrument(
578 new WalletItems::MaskedInstrument(ASCIIToUTF16("descriptive_name"),
579 WalletItems::MaskedInstrument::VISA,
580 ASCIIToUTF16("last_four_digits"),
582 2012,
583 billing_address.Pass(),
584 WalletItems::MaskedInstrument::VALID,
585 "object_id"));
586 expected.AddInstrument(masked_instrument.Pass());
588 scoped_ptr<Address> shipping_address(
589 new Address("US",
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"),
598 "id",
599 "language_code"));
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