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