| Index: chrome/browser/autofill/wallet/wallet_data_retriever_unittest.cc
|
| diff --git a/chrome/browser/autofill/wallet/wallet_data_retriever_unittest.cc b/chrome/browser/autofill/wallet/wallet_data_retriever_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..31432082ac6bd920aaaba92389677c4348c7de22
|
| --- /dev/null
|
| +++ b/chrome/browser/autofill/wallet/wallet_data_retriever_unittest.cc
|
| @@ -0,0 +1,350 @@
|
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "base/logging.h"
|
| +#include "base/message_loop.h"
|
| +#include "base/string_number_conversions.h"
|
| +#include "base/string_util.h"
|
| +#include "chrome/browser/autofill/wallet/cart.h"
|
| +#include "chrome/browser/autofill/wallet/full_wallet.h"
|
| +#include "chrome/browser/autofill/wallet/wallet_data_retriever.h"
|
| +#include "chrome/browser/autofill/wallet/wallet_items.h"
|
| +#include "chrome/test/base/testing_profile.h"
|
| +#include "googleurl/src/gurl.h"
|
| +#include "net/base/net_errors.h"
|
| +#include "net/http/http_status_code.h"
|
| +#include "net/url_request/test_url_fetcher_factory.h"
|
| +#include "net/url_request/url_fetcher_delegate.h"
|
| +#include "net/url_request/url_request_status.h"
|
| +#include "testing/gmock/include/gmock/gmock.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace {
|
| +
|
| +static const char kGoogleTransactionId[] = "google-transaction-id";
|
| +
|
| +static const char kGetFullWalletValidResponse[] =
|
| + "{"
|
| + " \"expiration_month\":12,"
|
| + " \"expiration_year\":2012,"
|
| + " \"iin\":\"iin\","
|
| + " \"rest\":\"rest\","
|
| + " \"billing_address\":"
|
| + " {"
|
| + " \"id\":\"id\","
|
| + " \"phone_number\":\"phone_number\","
|
| + " \"postal_address\":"
|
| + " {"
|
| + " \"recipient_name\":\"recipient_name\","
|
| + " \"address_line\":"
|
| + " ["
|
| + " \"address_line_1\","
|
| + " \"address_line_2\""
|
| + " ],"
|
| + " \"locality_name\":\"locality_name\","
|
| + " \"administrative_area_name\":\"administrative_area_name\","
|
| + " \"postal_code_number\":\"postal_code_number\","
|
| + " \"country_name_code\":\"country_name_code\""
|
| + " }"
|
| + " },"
|
| + " \"shipping_address\":"
|
| + " {"
|
| + " \"id\":\"ship_id\","
|
| + " \"phone_number\":\"ship_phone_number\","
|
| + " \"postal_address\":"
|
| + " {"
|
| + " \"recipient_name\":\"ship_recipient_name\","
|
| + " \"address_line\":"
|
| + " ["
|
| + " \"ship_address_line_1\","
|
| + " \"ship_address_line_2\""
|
| + " ],"
|
| + " \"locality_name\":\"ship_locality_name\","
|
| + " \"administrative_area_name\":\"ship_administrative_area_name\","
|
| + " \"postal_code_number\":\"ship_postal_code_number\","
|
| + " \"country_name_code\":\"ship_country_name_code\""
|
| + " }"
|
| + " },"
|
| + " \"required_action\":"
|
| + " ["
|
| + " ]"
|
| + "}";
|
| +
|
| +static const char kGetWalletItemsValidResponse[] =
|
| + "{"
|
| + " \"required_action\":"
|
| + " ["
|
| + " ],"
|
| + " \"google_transaction_id\":\"google_transaction_id\","
|
| + " \"instrument\":"
|
| + " ["
|
| + " {"
|
| + " \"descriptive_name\":\"descriptive_name\","
|
| + " \"type\":\"VISA\","
|
| + " \"supported_currency\":\"currency_code\","
|
| + " \"last_four_digits\":\"last_four_digits\","
|
| + " \"expiration_month\":12,"
|
| + " \"expiration_year\":2012,"
|
| + " \"brand\":\"monkeys\","
|
| + " \"billing_address\":"
|
| + " {"
|
| + " \"name\":\"name\","
|
| + " \"address1\":\"address1\","
|
| + " \"address2\":\"address2\","
|
| + " \"city\":\"city\","
|
| + " \"state\":\"state\","
|
| + " \"postal_code\":\"postal_code\","
|
| + " \"phone_number\":\"phone_number\","
|
| + " \"country_code\":\"country_code\""
|
| + " },"
|
| + " \"status\":\"VALID\","
|
| + " \"object_id\":\"default_instrument_id\""
|
| + " }"
|
| + " ],"
|
| + " \"default_instrument_id\":\"default_instrument_id\","
|
| + " \"address\":"
|
| + " ["
|
| + " ],"
|
| + " \"default_address_id\":\"default_address_id\","
|
| + " \"required_legal_document\":"
|
| + " ["
|
| + " ]"
|
| + "}";
|
| +
|
| +static const char kAcceptLegalDocumentsValidRequest[] =
|
| + "{"
|
| + "\"accepted_legal_document\":"
|
| + "["
|
| + "\"doc_1\","
|
| + "\"doc_2\""
|
| + "],"
|
| + "\"api_key\":\"abcdefg\","
|
| + "\"google_transaction_id\":\"google-transaction-id\""
|
| + "}";
|
| +
|
| +static const char kGetFullWalletValidRequest[] =
|
| + "{"
|
| + "\"api_key\":\"abcdefg\","
|
| + "\"cart\":"
|
| + "{"
|
| + "\"currency_code\":\"currency_code\","
|
| + "\"total_price\":\"currency_code\""
|
| + "},"
|
| + "\"encrypted_otp\":\"encrypted_otp\","
|
| + "\"google_transaction_id\":\"google_transaction_id\","
|
| + "\"merchant_domain\":\"merchant_domain\","
|
| + "\"risk_params\":\"\","
|
| + "\"selected_address_id\":\"shipping_address_id\","
|
| + "\"selected_instrument_id\":\"instrument_id\","
|
| + "\"session_material\":\"session_material\""
|
| + "}";
|
| +
|
| +static const char kGetWalletItemsValidRequest[] =
|
| + "{"
|
| + "\"api_key\":\"abcdefg\","
|
| + "\"risk_params\":\"\""
|
| + "}";
|
| +
|
| +static const char kSendExtendedAutofillStatusOfSuccessValidRequest[] =
|
| + "{"
|
| + "\"api_key\":\"abcdefg\","
|
| + "\"google_transaction_id\":\"google_transaction_id\","
|
| + "\"hostname\":\"hostname\","
|
| + "\"success\":true"
|
| + "}";
|
| +
|
| +static const char kSendExtendedAutofillStatusOfFailureValidRequest[] =
|
| + "{"
|
| + "\"api_key\":\"abcdefg\","
|
| + "\"google_transaction_id\":\"google_transaction_id\","
|
| + "\"hostname\":\"hostname\","
|
| + "\"reason\":\"CANNOT_PROCEED\","
|
| + "\"success\":false"
|
| + "}";
|
| +
|
| +} // end anonymous namespace
|
| +
|
| +
|
| +namespace wallet {
|
| +
|
| +class WalletDataRetrieverTest : public testing::Test {
|
| + public:
|
| + WalletDataRetrieverTest() {}
|
| +
|
| + protected:
|
| + MessageLoop message_loop_;
|
| + TestingProfile profile_;
|
| +};
|
| +
|
| +class MockWalletDataRetrieverDelegate
|
| + : public wallet::WalletDataRetriever::Delegate {
|
| + public:
|
| + MockWalletDataRetrieverDelegate() {}
|
| + ~MockWalletDataRetrieverDelegate() {}
|
| +
|
| + MOCK_METHOD0(OnAcceptLegalDocuments, void());
|
| + MOCK_METHOD2(OnEncryptOtp, void(const std::string& encrypted_otp,
|
| + const std::string& session_material));
|
| + MOCK_METHOD1(OnGetFullWallet, void(FullWallet* full_wallet));
|
| + MOCK_METHOD1(OnGetWalletItems, void(WalletItems* wallet_items));
|
| + MOCK_METHOD0(OnSendExtendedAutofillStatus, void());
|
| + MOCK_METHOD0(OnWalletError, void());
|
| + MOCK_METHOD1(OnNetworkError, void(int response_code));
|
| +};
|
| +
|
| +
|
| +TEST_F(WalletDataRetrieverTest, NetworkFailureOnExpectedVoidResponse) {
|
| + int response_code = net::HTTP_INTERNAL_SERVER_ERROR;
|
| + MockWalletDataRetrieverDelegate delegate;
|
| + EXPECT_CALL(delegate, OnNetworkError(response_code)).Times(1);
|
| +
|
| + net::TestURLFetcherFactory factory;
|
| +
|
| + WalletDataRetriever wallet_data_retriever(profile_.GetRequestContext());
|
| + wallet_data_retriever.SendExtendedAutofillStatus(true, "", "", "", &delegate);
|
| + net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
|
| + DCHECK(fetcher);
|
| + fetcher->set_response_code(response_code);
|
| + fetcher->delegate()->OnURLFetchComplete(fetcher);
|
| +}
|
| +
|
| +TEST_F(WalletDataRetrieverTest, NetworkFailureOnExpectedResponse) {
|
| + int response_code = net::HTTP_INTERNAL_SERVER_ERROR;
|
| + MockWalletDataRetrieverDelegate delegate;
|
| + EXPECT_CALL(delegate, OnNetworkError(response_code)).Times(1);
|
| +
|
| + net::TestURLFetcherFactory factory;
|
| +
|
| + WalletDataRetriever wallet_data_retriever(profile_.GetRequestContext());
|
| + Cart cart("currency_code", "currency_code");
|
| + wallet_data_retriever.GetFullWallet("", "", "", cart, "", "", "", &delegate);
|
| + net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
|
| + DCHECK(fetcher);
|
| + fetcher->set_response_code(response_code);
|
| + fetcher->delegate()->OnURLFetchComplete(fetcher);
|
| +}
|
| +
|
| +TEST_F(WalletDataRetrieverTest, RequestError) {
|
| + int response_code = net::HTTP_BAD_REQUEST;
|
| + MockWalletDataRetrieverDelegate delegate;
|
| + EXPECT_CALL(delegate, OnWalletError()).Times(1);
|
| +
|
| + net::TestURLFetcherFactory factory;
|
| +
|
| + WalletDataRetriever wallet_data_retriever(profile_.GetRequestContext());
|
| + wallet_data_retriever.SendExtendedAutofillStatus(true, "", "", "", &delegate);
|
| + net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
|
| + DCHECK(fetcher);
|
| + fetcher->set_response_code(response_code);
|
| + fetcher->delegate()->OnURLFetchComplete(fetcher);
|
| +}
|
| +
|
| +TEST_F(WalletDataRetrieverTest, GetFullWallet) {
|
| + int response_code = net::HTTP_OK;
|
| + MockWalletDataRetrieverDelegate delegate;
|
| + // TODO(ahutter): make this more specific
|
| + EXPECT_CALL(delegate, OnGetFullWallet(testing::_)).Times(1);
|
| +
|
| + net::TestURLFetcherFactory factory;
|
| +
|
| + WalletDataRetriever wallet_data_retriever(profile_.GetRequestContext());
|
| + Cart cart("currency_code", "currency_code");
|
| + wallet_data_retriever.GetFullWallet("instrument_id",
|
| + "shipping_address_id",
|
| + "merchant_domain",
|
| + cart,
|
| + "google_transaction_id",
|
| + "encrypted_otp",
|
| + "session_material",
|
| + &delegate);
|
| + net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
|
| + DCHECK(fetcher);
|
| + ASSERT_EQ(kGetFullWalletValidRequest, fetcher->upload_data());
|
| + fetcher->set_response_code(response_code);
|
| + fetcher->SetResponseString(kGetFullWalletValidResponse);
|
| + fetcher->delegate()->OnURLFetchComplete(fetcher);
|
| +}
|
| +
|
| +TEST_F(WalletDataRetrieverTest, AcceptLegalDocuments) {
|
| + int response_code = net::HTTP_OK;
|
| + MockWalletDataRetrieverDelegate delegate;
|
| + EXPECT_CALL(delegate, OnAcceptLegalDocuments()).Times(1);
|
| +
|
| + net::TestURLFetcherFactory factory;
|
| +
|
| + WalletDataRetriever wallet_data_retriever(profile_.GetRequestContext());
|
| + std::vector<std::string> doc_ids;
|
| + doc_ids.push_back("doc_1");
|
| + doc_ids.push_back("doc_2");
|
| + wallet_data_retriever.AcceptLegalDocuments(doc_ids,
|
| + kGoogleTransactionId,
|
| + &delegate);
|
| + net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
|
| + DCHECK(fetcher);
|
| + ASSERT_EQ(kAcceptLegalDocumentsValidRequest, fetcher->upload_data());
|
| + fetcher->set_response_code(response_code);
|
| + fetcher->delegate()->OnURLFetchComplete(fetcher);
|
| +}
|
| +
|
| +TEST_F(WalletDataRetrieverTest, GetWalletItems) {
|
| + int response_code = net::HTTP_OK;
|
| + MockWalletDataRetrieverDelegate delegate;
|
| + // TODO(ahutter): make this more specific
|
| + EXPECT_CALL(delegate, OnGetWalletItems(testing::_)).Times(1);
|
| +
|
| + net::TestURLFetcherFactory factory;
|
| +
|
| + WalletDataRetriever wallet_data_retriever(profile_.GetRequestContext());
|
| + wallet_data_retriever.GetWalletItems(&delegate);
|
| + net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
|
| + DCHECK(fetcher);
|
| + ASSERT_EQ(kGetWalletItemsValidRequest, fetcher->upload_data());
|
| + fetcher->set_response_code(response_code);
|
| + fetcher->SetResponseString(kGetWalletItemsValidResponse);
|
| + fetcher->delegate()->OnURLFetchComplete(fetcher);
|
| +}
|
| +
|
| +TEST_F(WalletDataRetrieverTest, SendExtendedAutofillOfStatusSuccess) {
|
| + int response_code = net::HTTP_OK;
|
| + MockWalletDataRetrieverDelegate delegate;
|
| + EXPECT_CALL(delegate, OnSendExtendedAutofillStatus()).Times(1);
|
| +
|
| + net::TestURLFetcherFactory factory;
|
| +
|
| + WalletDataRetriever wallet_data_retriever(profile_.GetRequestContext());
|
| + wallet_data_retriever.SendExtendedAutofillStatus(true,
|
| + "hostname",
|
| + "",
|
| + "google_transaction_id",
|
| + &delegate);
|
| + net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
|
| + DCHECK(fetcher);
|
| + ASSERT_EQ(kSendExtendedAutofillStatusOfSuccessValidRequest,
|
| + fetcher->upload_data());
|
| + fetcher->set_response_code(response_code);
|
| + fetcher->delegate()->OnURLFetchComplete(fetcher);
|
| +}
|
| +
|
| +TEST_F(WalletDataRetrieverTest, SendExtendedAutofillStatusOfFailure) {
|
| + int response_code = net::HTTP_OK;
|
| + MockWalletDataRetrieverDelegate delegate;
|
| + EXPECT_CALL(delegate, OnSendExtendedAutofillStatus()).Times(1);
|
| +
|
| + net::TestURLFetcherFactory factory;
|
| +
|
| + WalletDataRetriever wallet_data_retriever(profile_.GetRequestContext());
|
| + wallet_data_retriever.SendExtendedAutofillStatus(false,
|
| + "hostname",
|
| + "CANNOT_PROCEED",
|
| + "google_transaction_id",
|
| + &delegate);
|
| + net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
|
| + DCHECK(fetcher);
|
| + ASSERT_EQ(kSendExtendedAutofillStatusOfFailureValidRequest,
|
| + fetcher->upload_data());
|
| + fetcher->set_response_code(response_code);
|
| + fetcher->delegate()->OnURLFetchComplete(fetcher);
|
| +}
|
| +
|
| +} // end wallet namespace
|
|
|