OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 "base/logging.h" |
| 6 #include "base/message_loop.h" |
| 7 #include "base/string_number_conversions.h" |
| 8 #include "base/string_util.h" |
| 9 #include "chrome/browser/autofill/wallet/cart.h" |
| 10 #include "chrome/browser/autofill/wallet/full_wallet.h" |
| 11 #include "chrome/browser/autofill/wallet/wallet_data_retriever.h" |
| 12 #include "chrome/browser/autofill/wallet/wallet_items.h" |
| 13 #include "chrome/test/base/testing_profile.h" |
| 14 #include "googleurl/src/gurl.h" |
| 15 #include "net/base/net_errors.h" |
| 16 #include "net/http/http_status_code.h" |
| 17 #include "net/url_request/test_url_fetcher_factory.h" |
| 18 #include "net/url_request/url_fetcher_delegate.h" |
| 19 #include "net/url_request/url_request_status.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 |
| 23 namespace { |
| 24 |
| 25 static const char kGoogleTransactionId[] = "google-transaction-id"; |
| 26 |
| 27 static const char kGetFullWalletValidResponse[] = |
| 28 "{" |
| 29 " \"expiration_month\":12," |
| 30 " \"expiration_year\":2012," |
| 31 " \"iin\":\"iin\"," |
| 32 " \"rest\":\"rest\"," |
| 33 " \"billing_address\":" |
| 34 " {" |
| 35 " \"id\":\"id\"," |
| 36 " \"phone_number\":\"phone_number\"," |
| 37 " \"postal_address\":" |
| 38 " {" |
| 39 " \"recipient_name\":\"recipient_name\"," |
| 40 " \"address_line\":" |
| 41 " [" |
| 42 " \"address_line_1\"," |
| 43 " \"address_line_2\"" |
| 44 " ]," |
| 45 " \"locality_name\":\"locality_name\"," |
| 46 " \"administrative_area_name\":\"administrative_area_name\"," |
| 47 " \"postal_code_number\":\"postal_code_number\"," |
| 48 " \"country_name_code\":\"country_name_code\"" |
| 49 " }" |
| 50 " }," |
| 51 " \"shipping_address\":" |
| 52 " {" |
| 53 " \"id\":\"ship_id\"," |
| 54 " \"phone_number\":\"ship_phone_number\"," |
| 55 " \"postal_address\":" |
| 56 " {" |
| 57 " \"recipient_name\":\"ship_recipient_name\"," |
| 58 " \"address_line\":" |
| 59 " [" |
| 60 " \"ship_address_line_1\"," |
| 61 " \"ship_address_line_2\"" |
| 62 " ]," |
| 63 " \"locality_name\":\"ship_locality_name\"," |
| 64 " \"administrative_area_name\":\"ship_administrative_area_name\"," |
| 65 " \"postal_code_number\":\"ship_postal_code_number\"," |
| 66 " \"country_name_code\":\"ship_country_name_code\"" |
| 67 " }" |
| 68 " }," |
| 69 " \"required_action\":" |
| 70 " [" |
| 71 " ]" |
| 72 "}"; |
| 73 |
| 74 static const char kGetWalletItemsValidResponse[] = |
| 75 "{" |
| 76 " \"required_action\":" |
| 77 " [" |
| 78 " ]," |
| 79 " \"google_transaction_id\":\"google_transaction_id\"," |
| 80 " \"instrument\":" |
| 81 " [" |
| 82 " {" |
| 83 " \"descriptive_name\":\"descriptive_name\"," |
| 84 " \"type\":\"VISA\"," |
| 85 " \"supported_currency\":\"currency_code\"," |
| 86 " \"last_four_digits\":\"last_four_digits\"," |
| 87 " \"expiration_month\":12," |
| 88 " \"expiration_year\":2012," |
| 89 " \"brand\":\"monkeys\"," |
| 90 " \"billing_address\":" |
| 91 " {" |
| 92 " \"name\":\"name\"," |
| 93 " \"address1\":\"address1\"," |
| 94 " \"address2\":\"address2\"," |
| 95 " \"city\":\"city\"," |
| 96 " \"state\":\"state\"," |
| 97 " \"postal_code\":\"postal_code\"," |
| 98 " \"phone_number\":\"phone_number\"," |
| 99 " \"country_code\":\"country_code\"" |
| 100 " }," |
| 101 " \"status\":\"VALID\"," |
| 102 " \"object_id\":\"default_instrument_id\"" |
| 103 " }" |
| 104 " ]," |
| 105 " \"default_instrument_id\":\"default_instrument_id\"," |
| 106 " \"address\":" |
| 107 " [" |
| 108 " ]," |
| 109 " \"default_address_id\":\"default_address_id\"," |
| 110 " \"required_legal_document\":" |
| 111 " [" |
| 112 " ]" |
| 113 "}"; |
| 114 |
| 115 static const char kAcceptLegalDocumentsValidRequest[] = |
| 116 "{" |
| 117 "\"accepted_legal_document\":" |
| 118 "[" |
| 119 "\"doc_1\"," |
| 120 "\"doc_2\"" |
| 121 "]," |
| 122 "\"api_key\":\"abcdefg\"," |
| 123 "\"google_transaction_id\":\"google-transaction-id\"" |
| 124 "}"; |
| 125 |
| 126 static const char kGetFullWalletValidRequest[] = |
| 127 "{" |
| 128 "\"api_key\":\"abcdefg\"," |
| 129 "\"cart\":" |
| 130 "{" |
| 131 "\"currency_code\":\"currency_code\"," |
| 132 "\"total_price\":\"currency_code\"" |
| 133 "}," |
| 134 "\"encrypted_otp\":\"encrypted_otp\"," |
| 135 "\"google_transaction_id\":\"google_transaction_id\"," |
| 136 "\"merchant_domain\":\"merchant_domain\"," |
| 137 "\"risk_params\":\"\"," |
| 138 "\"selected_address_id\":\"shipping_address_id\"," |
| 139 "\"selected_instrument_id\":\"instrument_id\"," |
| 140 "\"session_material\":\"session_material\"" |
| 141 "}"; |
| 142 |
| 143 static const char kGetWalletItemsValidRequest[] = |
| 144 "{" |
| 145 "\"api_key\":\"abcdefg\"," |
| 146 "\"risk_params\":\"\"" |
| 147 "}"; |
| 148 |
| 149 static const char kSendExtendedAutofillStatusOfSuccessValidRequest[] = |
| 150 "{" |
| 151 "\"api_key\":\"abcdefg\"," |
| 152 "\"google_transaction_id\":\"google_transaction_id\"," |
| 153 "\"hostname\":\"hostname\"," |
| 154 "\"success\":true" |
| 155 "}"; |
| 156 |
| 157 static const char kSendExtendedAutofillStatusOfFailureValidRequest[] = |
| 158 "{" |
| 159 "\"api_key\":\"abcdefg\"," |
| 160 "\"google_transaction_id\":\"google_transaction_id\"," |
| 161 "\"hostname\":\"hostname\"," |
| 162 "\"reason\":\"CANNOT_PROCEED\"," |
| 163 "\"success\":false" |
| 164 "}"; |
| 165 |
| 166 } // end anonymous namespace |
| 167 |
| 168 |
| 169 namespace wallet { |
| 170 |
| 171 class WalletDataRetrieverTest : public testing::Test { |
| 172 public: |
| 173 WalletDataRetrieverTest() {} |
| 174 |
| 175 protected: |
| 176 MessageLoop message_loop_; |
| 177 TestingProfile profile_; |
| 178 }; |
| 179 |
| 180 class MockWalletDataRetrieverDelegate |
| 181 : public wallet::WalletDataRetriever::Delegate { |
| 182 public: |
| 183 MockWalletDataRetrieverDelegate() {} |
| 184 ~MockWalletDataRetrieverDelegate() {} |
| 185 |
| 186 MOCK_METHOD0(OnAcceptLegalDocuments, void()); |
| 187 MOCK_METHOD2(OnEncryptOtp, void(const std::string& encrypted_otp, |
| 188 const std::string& session_material)); |
| 189 MOCK_METHOD1(OnGetFullWallet, void(FullWallet* full_wallet)); |
| 190 MOCK_METHOD1(OnGetWalletItems, void(WalletItems* wallet_items)); |
| 191 MOCK_METHOD0(OnSendExtendedAutofillStatus, void()); |
| 192 MOCK_METHOD0(OnWalletError, void()); |
| 193 MOCK_METHOD1(OnNetworkError, void(int response_code)); |
| 194 }; |
| 195 |
| 196 |
| 197 TEST_F(WalletDataRetrieverTest, NetworkFailureOnExpectedVoidResponse) { |
| 198 int response_code = net::HTTP_INTERNAL_SERVER_ERROR; |
| 199 MockWalletDataRetrieverDelegate delegate; |
| 200 EXPECT_CALL(delegate, OnNetworkError(response_code)).Times(1); |
| 201 |
| 202 net::TestURLFetcherFactory factory; |
| 203 |
| 204 WalletDataRetriever wallet_data_retriever(profile_.GetRequestContext()); |
| 205 wallet_data_retriever.SendExtendedAutofillStatus(true, "", "", "", &delegate); |
| 206 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 207 DCHECK(fetcher); |
| 208 fetcher->set_response_code(response_code); |
| 209 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 210 } |
| 211 |
| 212 TEST_F(WalletDataRetrieverTest, NetworkFailureOnExpectedResponse) { |
| 213 int response_code = net::HTTP_INTERNAL_SERVER_ERROR; |
| 214 MockWalletDataRetrieverDelegate delegate; |
| 215 EXPECT_CALL(delegate, OnNetworkError(response_code)).Times(1); |
| 216 |
| 217 net::TestURLFetcherFactory factory; |
| 218 |
| 219 WalletDataRetriever wallet_data_retriever(profile_.GetRequestContext()); |
| 220 Cart cart("currency_code", "currency_code"); |
| 221 wallet_data_retriever.GetFullWallet("", "", "", cart, "", "", "", &delegate); |
| 222 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 223 DCHECK(fetcher); |
| 224 fetcher->set_response_code(response_code); |
| 225 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 226 } |
| 227 |
| 228 TEST_F(WalletDataRetrieverTest, RequestError) { |
| 229 int response_code = net::HTTP_BAD_REQUEST; |
| 230 MockWalletDataRetrieverDelegate delegate; |
| 231 EXPECT_CALL(delegate, OnWalletError()).Times(1); |
| 232 |
| 233 net::TestURLFetcherFactory factory; |
| 234 |
| 235 WalletDataRetriever wallet_data_retriever(profile_.GetRequestContext()); |
| 236 wallet_data_retriever.SendExtendedAutofillStatus(true, "", "", "", &delegate); |
| 237 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 238 DCHECK(fetcher); |
| 239 fetcher->set_response_code(response_code); |
| 240 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 241 } |
| 242 |
| 243 TEST_F(WalletDataRetrieverTest, GetFullWallet) { |
| 244 int response_code = net::HTTP_OK; |
| 245 MockWalletDataRetrieverDelegate delegate; |
| 246 // TODO(ahutter): make this more specific |
| 247 EXPECT_CALL(delegate, OnGetFullWallet(testing::_)).Times(1); |
| 248 |
| 249 net::TestURLFetcherFactory factory; |
| 250 |
| 251 WalletDataRetriever wallet_data_retriever(profile_.GetRequestContext()); |
| 252 Cart cart("currency_code", "currency_code"); |
| 253 wallet_data_retriever.GetFullWallet("instrument_id", |
| 254 "shipping_address_id", |
| 255 "merchant_domain", |
| 256 cart, |
| 257 "google_transaction_id", |
| 258 "encrypted_otp", |
| 259 "session_material", |
| 260 &delegate); |
| 261 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 262 DCHECK(fetcher); |
| 263 ASSERT_EQ(kGetFullWalletValidRequest, fetcher->upload_data()); |
| 264 fetcher->set_response_code(response_code); |
| 265 fetcher->SetResponseString(kGetFullWalletValidResponse); |
| 266 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 267 } |
| 268 |
| 269 TEST_F(WalletDataRetrieverTest, AcceptLegalDocuments) { |
| 270 int response_code = net::HTTP_OK; |
| 271 MockWalletDataRetrieverDelegate delegate; |
| 272 EXPECT_CALL(delegate, OnAcceptLegalDocuments()).Times(1); |
| 273 |
| 274 net::TestURLFetcherFactory factory; |
| 275 |
| 276 WalletDataRetriever wallet_data_retriever(profile_.GetRequestContext()); |
| 277 std::vector<std::string> doc_ids; |
| 278 doc_ids.push_back("doc_1"); |
| 279 doc_ids.push_back("doc_2"); |
| 280 wallet_data_retriever.AcceptLegalDocuments(doc_ids, |
| 281 kGoogleTransactionId, |
| 282 &delegate); |
| 283 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 284 DCHECK(fetcher); |
| 285 ASSERT_EQ(kAcceptLegalDocumentsValidRequest, fetcher->upload_data()); |
| 286 fetcher->set_response_code(response_code); |
| 287 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 288 } |
| 289 |
| 290 TEST_F(WalletDataRetrieverTest, GetWalletItems) { |
| 291 int response_code = net::HTTP_OK; |
| 292 MockWalletDataRetrieverDelegate delegate; |
| 293 // TODO(ahutter): make this more specific |
| 294 EXPECT_CALL(delegate, OnGetWalletItems(testing::_)).Times(1); |
| 295 |
| 296 net::TestURLFetcherFactory factory; |
| 297 |
| 298 WalletDataRetriever wallet_data_retriever(profile_.GetRequestContext()); |
| 299 wallet_data_retriever.GetWalletItems(&delegate); |
| 300 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 301 DCHECK(fetcher); |
| 302 ASSERT_EQ(kGetWalletItemsValidRequest, fetcher->upload_data()); |
| 303 fetcher->set_response_code(response_code); |
| 304 fetcher->SetResponseString(kGetWalletItemsValidResponse); |
| 305 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 306 } |
| 307 |
| 308 TEST_F(WalletDataRetrieverTest, SendExtendedAutofillOfStatusSuccess) { |
| 309 int response_code = net::HTTP_OK; |
| 310 MockWalletDataRetrieverDelegate delegate; |
| 311 EXPECT_CALL(delegate, OnSendExtendedAutofillStatus()).Times(1); |
| 312 |
| 313 net::TestURLFetcherFactory factory; |
| 314 |
| 315 WalletDataRetriever wallet_data_retriever(profile_.GetRequestContext()); |
| 316 wallet_data_retriever.SendExtendedAutofillStatus(true, |
| 317 "hostname", |
| 318 "", |
| 319 "google_transaction_id", |
| 320 &delegate); |
| 321 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 322 DCHECK(fetcher); |
| 323 ASSERT_EQ(kSendExtendedAutofillStatusOfSuccessValidRequest, |
| 324 fetcher->upload_data()); |
| 325 fetcher->set_response_code(response_code); |
| 326 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 327 } |
| 328 |
| 329 TEST_F(WalletDataRetrieverTest, SendExtendedAutofillStatusOfFailure) { |
| 330 int response_code = net::HTTP_OK; |
| 331 MockWalletDataRetrieverDelegate delegate; |
| 332 EXPECT_CALL(delegate, OnSendExtendedAutofillStatus()).Times(1); |
| 333 |
| 334 net::TestURLFetcherFactory factory; |
| 335 |
| 336 WalletDataRetriever wallet_data_retriever(profile_.GetRequestContext()); |
| 337 wallet_data_retriever.SendExtendedAutofillStatus(false, |
| 338 "hostname", |
| 339 "CANNOT_PROCEED", |
| 340 "google_transaction_id", |
| 341 &delegate); |
| 342 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 343 DCHECK(fetcher); |
| 344 ASSERT_EQ(kSendExtendedAutofillStatusOfFailureValidRequest, |
| 345 fetcher->upload_data()); |
| 346 fetcher->set_response_code(response_code); |
| 347 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 348 } |
| 349 |
| 350 } // end wallet namespace |
OLD | NEW |