Commit 0fdad893 authored by sebsg's avatar sebsg Committed by Commit Bot

[AF] Add methods to set wallet data independently from the metadata.

Change-Id: Ic290185b178ce9944b471d3cfa404684c3ce8aaa
Reviewed-on: https://chromium-review.googlesource.com/c/1303881
Commit-Queue: Sebastien Seguin-Gagnon <sebsg@chromium.org>
Reviewed-by: default avatarJan Krcal <jkrcal@chromium.org>
Cr-Commit-Position: refs/heads/master@{#603587}
parent 218f9a3b
......@@ -1445,6 +1445,111 @@ bool AutofillTable::GetServerAddressesMetadata(
return s.Succeeded();
}
void AutofillTable::SetServerCardsData(
const std::vector<CreditCard>& credit_cards) {
sql::Transaction transaction(db_);
if (!transaction.Begin())
return;
// Delete all old values.
sql::Statement masked_delete(
db_->GetUniqueStatement("DELETE FROM masked_credit_cards"));
masked_delete.Run();
// Add all the masked cards.
sql::Statement masked_insert(
db_->GetUniqueStatement("INSERT INTO masked_credit_cards("
"id," // 0
"network," // 1
"type," // 2
"status," // 3
"name_on_card," // 4
"last_four," // 5
"exp_month," // 6
"exp_year," // 7
"bank_name)" // 8
"VALUES (?,?,?,?,?,?,?,?,?)"));
for (const CreditCard& card : credit_cards) {
DCHECK_EQ(CreditCard::MASKED_SERVER_CARD, card.record_type());
masked_insert.BindString(0, card.server_id());
masked_insert.BindString(1, card.network());
masked_insert.BindInt(2, card.card_type());
masked_insert.BindString(3,
ServerStatusEnumToString(card.GetServerStatus()));
masked_insert.BindString16(4, card.GetRawInfo(CREDIT_CARD_NAME_FULL));
masked_insert.BindString16(5, card.LastFourDigits());
masked_insert.BindString16(6, card.GetRawInfo(CREDIT_CARD_EXP_MONTH));
masked_insert.BindString16(7,
card.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR));
masked_insert.BindString(8, card.bank_name());
masked_insert.Run();
masked_insert.Reset(true);
}
// Delete all items in the unmasked table that aren't in the new set.
sql::Statement unmasked_delete(db_->GetUniqueStatement(
"DELETE FROM unmasked_credit_cards WHERE id NOT IN "
"(SELECT id FROM masked_credit_cards)"));
unmasked_delete.Run();
transaction.Commit();
}
void AutofillTable::SetServerAddressesData(
const std::vector<AutofillProfile>& profiles) {
sql::Transaction transaction(db_);
if (!transaction.Begin())
return;
// Delete existing server addresses.
sql::Statement delete_old(
db_->GetUniqueStatement("DELETE FROM server_addresses"));
delete_old.Run();
// Add the new server addresses.
sql::Statement insert(db_->GetUniqueStatement(
"INSERT INTO server_addresses("
"id,"
"recipient_name,"
"company_name,"
"street_address,"
"address_1," // ADDRESS_HOME_STATE
"address_2," // ADDRESS_HOME_CITY
"address_3," // ADDRESS_HOME_DEPENDENT_LOCALITY
"address_4," // Not supported in AutofillProfile yet.
"postal_code," // ADDRESS_HOME_ZIP
"sorting_code," // ADDRESS_HOME_SORTING_CODE
"country_code," // ADDRESS_HOME_COUNTRY
"phone_number," // PHONE_HOME_WHOLE_NUMBER
"language_code) "
"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)"));
for (const auto& profile : profiles) {
DCHECK(profile.record_type() == AutofillProfile::SERVER_PROFILE);
int index = 0;
insert.BindString(index++, profile.server_id());
insert.BindString16(index++, profile.GetRawInfo(NAME_FULL));
insert.BindString16(index++, profile.GetRawInfo(COMPANY_NAME));
insert.BindString16(index++,
profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS));
insert.BindString16(index++, profile.GetRawInfo(ADDRESS_HOME_STATE));
insert.BindString16(index++, profile.GetRawInfo(ADDRESS_HOME_CITY));
insert.BindString16(index++,
profile.GetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY));
index++; // SKip address_4 which we haven't added to AutofillProfile yet.
insert.BindString16(index++, profile.GetRawInfo(ADDRESS_HOME_ZIP));
insert.BindString16(index++, profile.GetRawInfo(ADDRESS_HOME_SORTING_CODE));
insert.BindString16(index++, profile.GetRawInfo(ADDRESS_HOME_COUNTRY));
insert.BindString16(index++, profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
insert.BindString(index++, profile.language_code());
insert.Run();
insert.Reset(true);
}
transaction.Commit();
}
void AutofillTable::SetPaymentsCustomerData(
const PaymentsCustomerData* customer_data) {
sql::Transaction transaction(db_);
......
......@@ -418,6 +418,11 @@ class AutofillTable : public WebDatabaseTable,
bool GetServerAddressesMetadata(
std::map<std::string, AutofillMetadata>* addresses_metadata) const;
// Methods to add the server cards and addresses data independently from the
// metadata.
void SetServerCardsData(const std::vector<CreditCard>& credit_cards);
void SetServerAddressesData(const std::vector<AutofillProfile>& profiles);
// Setters and getters related to the Google Payments customer data.
// Passing null to the setter will clear the data.
void SetPaymentsCustomerData(const PaymentsCustomerData* customer_data);
......
......@@ -2002,6 +2002,131 @@ TEST_F(AutofillTableTest, RemoveWrongServerCardMetadata) {
ASSERT_EQ(1U, outputs.size());
}
TEST_F(AutofillTableTest, SetServerCardsData) {
// Set a card data.
std::vector<CreditCard> inputs;
inputs.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "card1"));
inputs[0].SetRawInfo(CREDIT_CARD_NAME_FULL, ASCIIToUTF16("Rick Roman"));
inputs[0].SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("12"));
inputs[0].SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, ASCIIToUTF16("1997"));
inputs[0].SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("1111"));
inputs[0].SetNetworkForMaskedCard(kVisaCard);
inputs[0].SetServerStatus(CreditCard::EXPIRED);
table_->SetServerCardsData(inputs);
// Make sure the card was added correctly.
std::vector<std::unique_ptr<CreditCard>> outputs;
ASSERT_TRUE(table_->GetServerCreditCards(&outputs));
ASSERT_EQ(inputs.size(), outputs.size());
// GUIDs for server cards are dynamically generated so will be different
// after reading from the DB. Check they're valid, but otherwise don't count
// them in the comparison.
inputs[0].set_guid(std::string());
outputs[0]->set_guid(std::string());
EXPECT_EQ(inputs[0], *outputs[0]);
EXPECT_EQ(CreditCard::EXPIRED, outputs[0]->GetServerStatus());
// Make sure no metadata was added.
std::map<std::string, AutofillMetadata> metadata_map;
ASSERT_TRUE(table_->GetServerCardsMetadata(&metadata_map));
ASSERT_EQ(0U, metadata_map.size());
// Set a different card.
inputs[0] = CreditCard(CreditCard::MASKED_SERVER_CARD, "card2");
table_->SetServerCardsData(inputs);
// The original one should have been replaced.
ASSERT_TRUE(table_->GetServerCreditCards(&outputs));
ASSERT_EQ(1U, outputs.size());
EXPECT_EQ("card2", outputs[0]->server_id());
// Make sure no metadata was added.
ASSERT_TRUE(table_->GetServerCardsMetadata(&metadata_map));
ASSERT_EQ(0U, metadata_map.size());
}
// Tests that adding server cards data does not delete the existing metadata.
TEST_F(AutofillTableTest, SetServerCardsData_ExistingMetadata) {
// Create and set some metadata.
AutofillMetadata input;
input.id = "server id";
input.use_count = 50;
input.use_date = Time::Now();
input.billing_address_id = "billing id";
table_->AddServerCardMetadata(input);
// Set a card data.
std::vector<CreditCard> inputs;
inputs.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "server id"));
table_->SetServerCardsData(inputs);
// Make sure the metadata is still intact.
std::map<std::string, AutofillMetadata> outputs;
ASSERT_TRUE(table_->GetServerCardsMetadata(&outputs));
ASSERT_EQ(1U, outputs.size());
EXPECT_EQ(input, outputs[input.id]);
}
TEST_F(AutofillTableTest, SetServerAddressesData) {
AutofillProfile one(AutofillProfile::SERVER_PROFILE, "a123");
std::vector<AutofillProfile> inputs;
inputs.push_back(one);
table_->SetServerAddressesData(inputs);
// Make sure the address was added correctly.
std::vector<std::unique_ptr<AutofillProfile>> outputs;
table_->GetServerProfiles(&outputs);
ASSERT_EQ(1u, outputs.size());
EXPECT_EQ(one.server_id(), outputs[0]->server_id());
outputs.clear();
// Make sure no metadata was added.
std::map<std::string, AutofillMetadata> metadata_map;
ASSERT_TRUE(table_->GetServerAddressesMetadata(&metadata_map));
ASSERT_EQ(0U, metadata_map.size());
// Set a different profile.
AutofillProfile two(AutofillProfile::SERVER_PROFILE, "b456");
inputs[0] = two;
table_->SetServerAddressesData(inputs);
// The original one should have been replaced.
table_->GetServerProfiles(&outputs);
ASSERT_EQ(1u, outputs.size());
EXPECT_EQ(two.server_id(), outputs[0]->server_id());
// Make sure no metadata was added.
ASSERT_TRUE(table_->GetServerAddressesMetadata(&metadata_map));
ASSERT_EQ(0U, metadata_map.size());
}
// Tests that adding server addresses data does not delete the existing
// metadata.
TEST_F(AutofillTableTest, SetServerAddressesData_ExistingMetadata) {
// Create and set some metadata.
AutofillMetadata input;
input.id = "server id";
input.use_count = 50;
input.use_date = Time::Now();
input.has_converted = true;
table_->AddServerAddressMetadata(input);
// Set an address data.
std::vector<AutofillProfile> inputs;
inputs.push_back(
AutofillProfile(AutofillProfile::SERVER_PROFILE, "server id"));
table_->SetServerAddressesData(inputs);
// Make sure the metadata is still intact.
std::map<std::string, AutofillMetadata> outputs;
ASSERT_TRUE(table_->GetServerAddressesMetadata(&outputs));
ASSERT_EQ(1U, outputs.size());
EXPECT_EQ(input, outputs[input.id]);
}
TEST_F(AutofillTableTest, RemoveWrongServerAddressMetadata) {
// Crete and set some metadata.
AutofillMetadata input;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment