Home | History | Annotate | Download | only in wallet
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "components/autofill/content/browser/wallet/wallet_test_util.h"
      6 
      7 #include <string>
      8 #include <vector>
      9 
     10 #include "base/strings/string16.h"
     11 #include "base/strings/string_number_conversions.h"
     12 #include "base/strings/utf_string_conversions.h"
     13 #include "base/values.h"
     14 #include "components/autofill/content/browser/wallet/full_wallet.h"
     15 #include "components/autofill/content/browser/wallet/gaia_account.h"
     16 #include "components/autofill/content/browser/wallet/instrument.h"
     17 #include "components/autofill/content/browser/wallet/required_action.h"
     18 #include "components/autofill/content/browser/wallet/wallet_address.h"
     19 
     20 namespace autofill {
     21 namespace wallet {
     22 
     23 namespace {
     24 
     25 int FutureYear() {
     26   // "In the Year 3000." - Richie "LaBamba" Rosenberg
     27   return 3000;
     28 }
     29 
     30 }  // namespace
     31 
     32 scoped_ptr<WalletItems::MaskedInstrument> GetTestMaskedInstrumentWithDetails(
     33     const std::string& id,
     34     scoped_ptr<Address> address,
     35     WalletItems::MaskedInstrument::Type type,
     36     WalletItems::MaskedInstrument::Status status) {
     37   return make_scoped_ptr(
     38       new WalletItems::MaskedInstrument(ASCIIToUTF16("descriptive_name"),
     39                                         type,
     40                                         std::vector<base::string16>(),
     41                                         ASCIIToUTF16("1111"),
     42                                         12,
     43                                         FutureYear(),
     44                                         address.Pass(),
     45                                         status,
     46                                         id));
     47 }
     48 
     49 scoped_ptr<WalletItems::MaskedInstrument> GetTestMaskedInstrumentWithId(
     50     const std::string& id) {
     51   return GetTestMaskedInstrumentWithDetails(
     52       id,
     53       GetTestAddress(),
     54       WalletItems::MaskedInstrument::VISA,
     55       WalletItems::MaskedInstrument::VALID);
     56 }
     57 
     58 scoped_ptr<WalletItems::MaskedInstrument>
     59 GetTestMaskedInstrumentWithIdAndAddress(
     60     const std::string& id, scoped_ptr<Address> address) {
     61   return GetTestMaskedInstrumentWithDetails(
     62       id,
     63       address.Pass(),
     64       WalletItems::MaskedInstrument::VISA,
     65       WalletItems::MaskedInstrument::VALID);
     66 }
     67 
     68 scoped_ptr<GaiaAccount> GetTestGaiaAccount() {
     69   return GaiaAccount::CreateForTesting("user (at) chromium.org",
     70                                        "obfuscated_id",
     71                                        0,
     72                                        true);
     73 }
     74 
     75 scoped_ptr<Address> GetTestAddress() {
     76   return make_scoped_ptr(new Address("US",
     77                                      ASCIIToUTF16("recipient_name"),
     78                                      ASCIIToUTF16("address_line_1"),
     79                                      ASCIIToUTF16("address_line_2"),
     80                                      ASCIIToUTF16("locality_name"),
     81                                      ASCIIToUTF16("admin_area_name"),
     82                                      ASCIIToUTF16("postal_code_number"),
     83                                      ASCIIToUTF16("phone_number"),
     84                                      std::string()));
     85 }
     86 
     87 scoped_ptr<Address> GetTestMinimalAddress() {
     88   scoped_ptr<Address> address = GetTestAddress();
     89   address->set_is_complete_address(false);
     90   return address.Pass();
     91 }
     92 
     93 scoped_ptr<FullWallet> GetTestFullWallet() {
     94   return GetTestFullWalletWithRequiredActions(std::vector<RequiredAction>());
     95 }
     96 
     97 scoped_ptr<FullWallet> GetTestFullWalletWithRequiredActions(
     98     const std::vector<RequiredAction>& actions) {
     99   scoped_ptr<FullWallet> wallet(new FullWallet(FutureYear(),
    100                                                12,
    101                                                "528512",
    102                                                "5ec4feecf9d6",
    103                                                GetTestAddress(),
    104                                                GetTestShippingAddress(),
    105                                                actions));
    106   std::vector<uint8> one_time_pad;
    107   base::HexStringToBytes("5F04A8704183", &one_time_pad);
    108   wallet->set_one_time_pad(one_time_pad);
    109   return wallet.Pass();
    110 }
    111 
    112 scoped_ptr<FullWallet> GetTestFullWalletInstrumentOnly() {
    113   scoped_ptr<FullWallet> wallet(new FullWallet(FutureYear(),
    114                                                12,
    115                                                "528512",
    116                                                "5ec4feecf9d6",
    117                                                GetTestAddress(),
    118                                                scoped_ptr<Address>(),
    119                                                std::vector<RequiredAction>()));
    120   std::vector<uint8> one_time_pad;
    121   base::HexStringToBytes("5F04A8704183", &one_time_pad);
    122   wallet->set_one_time_pad(one_time_pad);
    123   return wallet.Pass();
    124 }
    125 
    126 scoped_ptr<Instrument> GetTestInstrument() {
    127   return make_scoped_ptr(new Instrument(ASCIIToUTF16("4444444444444448"),
    128                                         ASCIIToUTF16("123"),
    129                                         12,
    130                                         FutureYear(),
    131                                         Instrument::VISA,
    132                                         GetTestAddress()));
    133 }
    134 
    135 scoped_ptr<Instrument> GetTestAddressUpgradeInstrument() {
    136   return make_scoped_ptr(new Instrument(base::string16(),
    137                                         base::string16(),
    138                                         12,
    139                                         FutureYear(),
    140                                         Instrument::UNKNOWN,
    141                                         GetTestAddress()));
    142 }
    143 
    144 scoped_ptr<Instrument> GetTestExpirationDateChangeInstrument() {
    145   return make_scoped_ptr(new Instrument(base::string16(),
    146                                         ASCIIToUTF16("123"),
    147                                         12,
    148                                         FutureYear() + 1,
    149                                         Instrument::UNKNOWN,
    150                                         scoped_ptr<Address>()));
    151 }
    152 
    153 scoped_ptr<Instrument> GetTestAddressNameChangeInstrument() {
    154   return make_scoped_ptr(new Instrument(base::string16(),
    155                                         ASCIIToUTF16("123"),
    156                                         12,
    157                                         FutureYear(),
    158                                         Instrument::UNKNOWN,
    159                                         GetTestAddress()));
    160 }
    161 
    162 scoped_ptr<WalletItems::LegalDocument> GetTestLegalDocument() {
    163   base::DictionaryValue dict;
    164   dict.SetString("legal_document_id", "document_id");
    165   dict.SetString("display_name", "display_name");
    166   return wallet::WalletItems::LegalDocument::CreateLegalDocument(dict);
    167 }
    168 
    169 scoped_ptr<WalletItems::MaskedInstrument> GetTestMaskedInstrument() {
    170   return GetTestMaskedInstrumentWithId("default_instrument_id");
    171 }
    172 
    173 scoped_ptr<WalletItems::MaskedInstrument> GetTestMaskedInstrumentExpired() {
    174   return GetTestMaskedInstrumentWithDetails(
    175       "default_instrument_id",
    176       GetTestAddress(),
    177       WalletItems::MaskedInstrument::VISA,
    178       WalletItems::MaskedInstrument::EXPIRED);
    179 }
    180 
    181 scoped_ptr<WalletItems::MaskedInstrument> GetTestMaskedInstrumentInvalid() {
    182   return GetTestMaskedInstrumentWithDetails(
    183       "default_instrument_id",
    184       GetTestAddress(),
    185       WalletItems::MaskedInstrument::VISA,
    186       WalletItems::MaskedInstrument::DECLINED);
    187 }
    188 
    189 scoped_ptr<WalletItems::MaskedInstrument> GetTestMaskedInstrumentAmex(
    190     AmexPermission amex_permission) {
    191   return GetTestMaskedInstrumentWithDetails(
    192       "default_instrument_id",
    193       GetTestAddress(),
    194       WalletItems::MaskedInstrument::AMEX,
    195       amex_permission == AMEX_ALLOWED ?
    196           WalletItems::MaskedInstrument::VALID :
    197           WalletItems::MaskedInstrument::AMEX_NOT_SUPPORTED);
    198 }
    199 
    200 scoped_ptr<WalletItems::MaskedInstrument> GetTestNonDefaultMaskedInstrument() {
    201   return GetTestMaskedInstrumentWithId("instrument_id");
    202 }
    203 
    204 scoped_ptr<Address> GetTestSaveableAddress() {
    205   return make_scoped_ptr(new Address("US",
    206                                      ASCIIToUTF16("save_recipient_name"),
    207                                      ASCIIToUTF16("save_address_line_1"),
    208                                      ASCIIToUTF16("save_address_line_2"),
    209                                      ASCIIToUTF16("save_locality_name"),
    210                                      ASCIIToUTF16("save_admin_area_name"),
    211                                      ASCIIToUTF16("save_postal_code_number"),
    212                                      ASCIIToUTF16("save_phone_number"),
    213                                      std::string()));
    214 }
    215 
    216 scoped_ptr<Address> GetTestShippingAddress() {
    217   return make_scoped_ptr(new Address("US",
    218                                      ASCIIToUTF16("ship_recipient_name"),
    219                                      ASCIIToUTF16("ship_address_line_1"),
    220                                      ASCIIToUTF16("ship_address_line_2"),
    221                                      ASCIIToUTF16("ship_locality_name"),
    222                                      ASCIIToUTF16("ship_admin_area_name"),
    223                                      ASCIIToUTF16("ship_postal_code_number"),
    224                                      ASCIIToUTF16("ship_phone_number"),
    225                                      "default_address_id"));
    226 }
    227 
    228 scoped_ptr<Address> GetTestNonDefaultShippingAddress() {
    229   scoped_ptr<Address> address = GetTestShippingAddress();
    230   address->set_object_id("address_id");
    231   return address.Pass();
    232 }
    233 
    234 scoped_ptr<WalletItems> GetTestWalletItemsWithDetails(
    235     const std::vector<RequiredAction>& required_actions,
    236     const std::string& default_instrument_id,
    237     const std::string& default_address_id,
    238     AmexPermission amex_permission) {
    239   return make_scoped_ptr(new wallet::WalletItems(required_actions,
    240                                                  "google_transaction_id",
    241                                                  default_instrument_id,
    242                                                  default_address_id,
    243                                                  amex_permission));
    244 }
    245 
    246 scoped_ptr<WalletItems> GetTestWalletItemsWithRequiredAction(
    247     RequiredAction action) {
    248   std::vector<RequiredAction> required_actions(1, action);
    249   scoped_ptr<WalletItems> items =
    250       GetTestWalletItemsWithDetails(required_actions,
    251                                     "default_instrument_id",
    252                                     "default_address_id",
    253                                     AMEX_ALLOWED);
    254 
    255   if (action != GAIA_AUTH)
    256     items->AddAccount(GetTestGaiaAccount());
    257 
    258   return items.Pass();
    259 }
    260 
    261 scoped_ptr<WalletItems> GetTestWalletItems(AmexPermission amex_permission) {
    262   return GetTestWalletItemsWithDefaultIds("default_instrument_id",
    263                                           "default_address_id",
    264                                           amex_permission);
    265 }
    266 
    267 scoped_ptr<WalletItems> GetTestWalletItemsWithUsers(
    268     const std::vector<std::string>& users, size_t active_index) {
    269   scoped_ptr<WalletItems> items =
    270       GetTestWalletItemsWithDetails(std::vector<RequiredAction>(),
    271                                     "default_instrument_id",
    272                                     "default_address_id",
    273                                     AMEX_ALLOWED);
    274   for (size_t i = 0; i < users.size(); ++i) {
    275     scoped_ptr<GaiaAccount> account(GaiaAccount::CreateForTesting(
    276         users[i], "obfuscated_id", i, i == active_index));
    277     items->AddAccount(account.Pass());
    278   }
    279   return items.Pass();
    280 }
    281 
    282 scoped_ptr<WalletItems> GetTestWalletItemsWithDefaultIds(
    283     const std::string& default_instrument_id,
    284     const std::string& default_address_id,
    285     AmexPermission amex_permission) {
    286   scoped_ptr<WalletItems> items =
    287       GetTestWalletItemsWithDetails(std::vector<RequiredAction>(),
    288                                     default_instrument_id,
    289                                     default_address_id,
    290                                     amex_permission);
    291   items->AddAccount(GetTestGaiaAccount());
    292   return items.Pass();
    293 }
    294 
    295 }  // namespace wallet
    296 }  // namespace autofill
    297