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/string_number_conversions.h" |
| 7 #include "base/string_util.h" |
| 8 #include "chrome/browser/autofill/wallet/cart.h" |
| 9 #include "chrome/browser/autofill/wallet/full_wallet.h" |
| 10 #include "chrome/browser/autofill/wallet/wallet_client.h" |
| 11 #include "chrome/browser/autofill/wallet/wallet_items.h" |
| 12 #include "chrome/test/base/testing_profile.h" |
| 13 #include "content/public/test/test_browser_thread.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 const char kGoogleTransactionId[] = "google-transaction-id"; |
| 26 |
| 27 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 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 // The JSON below is used to test against the request payload being sent to |
| 116 // Online Wallet. It's indented differently since JSONWriter creates compact |
| 117 // JSON from DictionaryValues. |
| 118 |
| 119 const char kAcceptLegalDocumentsValidRequest[] = |
| 120 "{" |
| 121 "\"accepted_legal_document\":" |
| 122 "[" |
| 123 "\"doc_1\"," |
| 124 "\"doc_2\"" |
| 125 "]," |
| 126 "\"api_key\":\"abcdefg\"," |
| 127 "\"google_transaction_id\":\"google-transaction-id\"" |
| 128 "}"; |
| 129 |
| 130 const char kGetFullWalletValidRequest[] = |
| 131 "{" |
| 132 "\"api_key\":\"abcdefg\"," |
| 133 "\"cart\":" |
| 134 "{" |
| 135 "\"currency_code\":\"currency_code\"," |
| 136 "\"total_price\":\"currency_code\"" |
| 137 "}," |
| 138 "\"encrypted_otp\":\"encrypted_otp\"," |
| 139 "\"google_transaction_id\":\"google_transaction_id\"," |
| 140 "\"merchant_domain\":\"merchant_domain\"," |
| 141 "\"risk_params\":\"\"," |
| 142 "\"selected_address_id\":\"shipping_address_id\"," |
| 143 "\"selected_instrument_id\":\"instrument_id\"," |
| 144 "\"session_material\":\"session_material\"" |
| 145 "}"; |
| 146 |
| 147 const char kGetWalletItemsValidRequest[] = |
| 148 "{" |
| 149 "\"api_key\":\"abcdefg\"," |
| 150 "\"risk_params\":\"\"" |
| 151 "}"; |
| 152 |
| 153 const char kSendExtendedAutofillStatusOfSuccessValidRequest[] = |
| 154 "{" |
| 155 "\"api_key\":\"abcdefg\"," |
| 156 "\"google_transaction_id\":\"google_transaction_id\"," |
| 157 "\"hostname\":\"hostname\"," |
| 158 "\"success\":true" |
| 159 "}"; |
| 160 |
| 161 const char kSendExtendedAutofillStatusOfFailureValidRequest[] = |
| 162 "{" |
| 163 "\"api_key\":\"abcdefg\"," |
| 164 "\"google_transaction_id\":\"google_transaction_id\"," |
| 165 "\"hostname\":\"hostname\"," |
| 166 "\"reason\":\"CANNOT_PROCEED\"," |
| 167 "\"success\":false" |
| 168 "}"; |
| 169 |
| 170 } // end anonymous namespace |
| 171 |
| 172 namespace wallet { |
| 173 |
| 174 class WalletClientTest : public testing::Test { |
| 175 public: |
| 176 WalletClientTest() : io_thread_(content::BrowserThread::IO) {} |
| 177 |
| 178 virtual void SetUp() { |
| 179 io_thread_.StartIOThread(); |
| 180 profile_.CreateRequestContext(); |
| 181 } |
| 182 |
| 183 virtual void TearDown() { |
| 184 profile_.ResetRequestContext(); |
| 185 io_thread_.Stop(); |
| 186 } |
| 187 |
| 188 protected: |
| 189 TestingProfile profile_; |
| 190 |
| 191 private: |
| 192 // The profile's request context must be released on the IO thread. |
| 193 content::TestBrowserThread io_thread_; |
| 194 }; |
| 195 |
| 196 class MockWalletClientObserver |
| 197 : public wallet::WalletClient::WalletClientObserver { |
| 198 public: |
| 199 MockWalletClientObserver() {} |
| 200 ~MockWalletClientObserver() {} |
| 201 |
| 202 MOCK_METHOD0(OnAcceptLegalDocuments, void()); |
| 203 MOCK_METHOD2(OnEncryptOtp, void(const std::string& encrypted_otp, |
| 204 const std::string& session_material)); |
| 205 MOCK_METHOD1(OnGetFullWallet, void(FullWallet* full_wallet)); |
| 206 MOCK_METHOD1(OnGetWalletItems, void(WalletItems* wallet_items)); |
| 207 MOCK_METHOD0(OnSendExtendedAutofillStatus, void()); |
| 208 MOCK_METHOD0(OnWalletError, void()); |
| 209 MOCK_METHOD1(OnNetworkError, void(int response_code)); |
| 210 }; |
| 211 |
| 212 // TODO(ahutter): Implement API compatibility tests. See |
| 213 // http://crbug.com/164465. |
| 214 |
| 215 // TODO(ahutter): Improve this when the error body is captured. See |
| 216 // http://crbug.com/164410. |
| 217 TEST_F(WalletClientTest, WalletErrorOnExpectedVoidResponse) { |
| 218 MockWalletClientObserver observer; |
| 219 EXPECT_CALL(observer, OnWalletError()).Times(1); |
| 220 |
| 221 net::TestURLFetcherFactory factory; |
| 222 |
| 223 WalletClient wallet_client(profile_.GetRequestContext()); |
| 224 wallet_client.SendExtendedAutofillStatus(true, "", "", "", &observer); |
| 225 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 226 DCHECK(fetcher); |
| 227 fetcher->set_response_code(net::HTTP_INTERNAL_SERVER_ERROR); |
| 228 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 229 } |
| 230 |
| 231 // TODO(ahutter): Improve this when the error body is captured. See |
| 232 // http://crbug.com/164410. |
| 233 TEST_F(WalletClientTest, WalletErrorOnExpectedResponse) { |
| 234 MockWalletClientObserver observer; |
| 235 EXPECT_CALL(observer, OnWalletError()).Times(1); |
| 236 |
| 237 net::TestURLFetcherFactory factory; |
| 238 |
| 239 WalletClient wallet_client(profile_.GetRequestContext()); |
| 240 Cart cart("currency_code", "currency_code"); |
| 241 wallet_client.GetFullWallet("", "", "", cart, "", "", "", &observer); |
| 242 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 243 DCHECK(fetcher); |
| 244 fetcher->set_response_code(net::HTTP_INTERNAL_SERVER_ERROR); |
| 245 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 246 } |
| 247 |
| 248 TEST_F(WalletClientTest, NetworkFailureOnExpectedVoidResponse) { |
| 249 MockWalletClientObserver observer; |
| 250 EXPECT_CALL(observer, OnNetworkError(net::HTTP_UNAUTHORIZED)).Times(1); |
| 251 |
| 252 net::TestURLFetcherFactory factory; |
| 253 |
| 254 WalletClient wallet_client(profile_.GetRequestContext()); |
| 255 wallet_client.SendExtendedAutofillStatus(true, "", "", "", &observer); |
| 256 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 257 DCHECK(fetcher); |
| 258 fetcher->set_response_code(net::HTTP_UNAUTHORIZED); |
| 259 // The number of retries is set to 3. |
| 260 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 261 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 262 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 263 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 264 } |
| 265 |
| 266 TEST_F(WalletClientTest, NetworkFailureOnExpectedResponse) { |
| 267 MockWalletClientObserver observer; |
| 268 EXPECT_CALL(observer, OnNetworkError(net::HTTP_UNAUTHORIZED)).Times(1); |
| 269 |
| 270 net::TestURLFetcherFactory factory; |
| 271 |
| 272 WalletClient wallet_client(profile_.GetRequestContext()); |
| 273 Cart cart("currency_code", "currency_code"); |
| 274 wallet_client.GetFullWallet("", "", "", cart, "", "", "", &observer); |
| 275 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 276 DCHECK(fetcher); |
| 277 fetcher->set_response_code(net::HTTP_UNAUTHORIZED); |
| 278 // The number of retries is set to 3. |
| 279 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 280 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 281 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 282 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 283 } |
| 284 |
| 285 TEST_F(WalletClientTest, RequestError) { |
| 286 MockWalletClientObserver observer; |
| 287 EXPECT_CALL(observer, OnWalletError()).Times(1); |
| 288 |
| 289 net::TestURLFetcherFactory factory; |
| 290 |
| 291 WalletClient wallet_client(profile_.GetRequestContext()); |
| 292 wallet_client.SendExtendedAutofillStatus(true, "", "", "", &observer); |
| 293 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 294 DCHECK(fetcher); |
| 295 fetcher->set_response_code(net::HTTP_BAD_REQUEST); |
| 296 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 297 } |
| 298 |
| 299 // TODO(ahutter): Add test for EncryptOtp. |
| 300 // TODO(ahutter): Add retry and failure tests for EncryptOtp, GetWalletItems, |
| 301 // GetFullWallet for when data is missing or invalid. |
| 302 |
| 303 TEST_F(WalletClientTest, GetFullWallet) { |
| 304 MockWalletClientObserver observer; |
| 305 EXPECT_CALL(observer, OnGetFullWallet(testing::NotNull())).Times(1); |
| 306 |
| 307 net::TestURLFetcherFactory factory; |
| 308 |
| 309 WalletClient wallet_client(profile_.GetRequestContext()); |
| 310 Cart cart("currency_code", "currency_code"); |
| 311 wallet_client.GetFullWallet("instrument_id", |
| 312 "shipping_address_id", |
| 313 "merchant_domain", |
| 314 cart, |
| 315 "google_transaction_id", |
| 316 "encrypted_otp", |
| 317 "session_material", |
| 318 &observer); |
| 319 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 320 DCHECK(fetcher); |
| 321 ASSERT_EQ(kGetFullWalletValidRequest, fetcher->upload_data()); |
| 322 fetcher->set_response_code(net::HTTP_OK); |
| 323 fetcher->SetResponseString(kGetFullWalletValidResponse); |
| 324 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 325 } |
| 326 |
| 327 TEST_F(WalletClientTest, AcceptLegalDocuments) { |
| 328 MockWalletClientObserver observer; |
| 329 EXPECT_CALL(observer, OnAcceptLegalDocuments()).Times(1); |
| 330 |
| 331 net::TestURLFetcherFactory factory; |
| 332 |
| 333 WalletClient wallet_client(profile_.GetRequestContext()); |
| 334 std::vector<std::string> doc_ids; |
| 335 doc_ids.push_back("doc_1"); |
| 336 doc_ids.push_back("doc_2"); |
| 337 wallet_client.AcceptLegalDocuments(doc_ids, |
| 338 kGoogleTransactionId, |
| 339 &observer); |
| 340 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 341 DCHECK(fetcher); |
| 342 ASSERT_EQ(kAcceptLegalDocumentsValidRequest, fetcher->upload_data()); |
| 343 fetcher->set_response_code(net::HTTP_OK); |
| 344 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 345 } |
| 346 |
| 347 TEST_F(WalletClientTest, GetWalletItems) { |
| 348 MockWalletClientObserver observer; |
| 349 EXPECT_CALL(observer, OnGetWalletItems(testing::NotNull())).Times(1); |
| 350 |
| 351 net::TestURLFetcherFactory factory; |
| 352 |
| 353 WalletClient wallet_client(profile_.GetRequestContext()); |
| 354 wallet_client.GetWalletItems(&observer); |
| 355 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 356 DCHECK(fetcher); |
| 357 ASSERT_EQ(kGetWalletItemsValidRequest, fetcher->upload_data()); |
| 358 fetcher->set_response_code(net::HTTP_OK); |
| 359 fetcher->SetResponseString(kGetWalletItemsValidResponse); |
| 360 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 361 } |
| 362 |
| 363 TEST_F(WalletClientTest, SendExtendedAutofillOfStatusSuccess) { |
| 364 MockWalletClientObserver observer; |
| 365 EXPECT_CALL(observer, OnSendExtendedAutofillStatus()).Times(1); |
| 366 |
| 367 net::TestURLFetcherFactory factory; |
| 368 |
| 369 WalletClient wallet_client(profile_.GetRequestContext()); |
| 370 wallet_client.SendExtendedAutofillStatus(true, |
| 371 "hostname", |
| 372 "", |
| 373 "google_transaction_id", |
| 374 &observer); |
| 375 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 376 DCHECK(fetcher); |
| 377 ASSERT_EQ(kSendExtendedAutofillStatusOfSuccessValidRequest, |
| 378 fetcher->upload_data()); |
| 379 fetcher->set_response_code(net::HTTP_OK); |
| 380 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 381 } |
| 382 |
| 383 TEST_F(WalletClientTest, SendExtendedAutofillStatusOfFailure) { |
| 384 MockWalletClientObserver observer; |
| 385 EXPECT_CALL(observer, OnSendExtendedAutofillStatus()).Times(1); |
| 386 |
| 387 net::TestURLFetcherFactory factory; |
| 388 |
| 389 WalletClient wallet_client(profile_.GetRequestContext()); |
| 390 wallet_client.SendExtendedAutofillStatus(false, |
| 391 "hostname", |
| 392 "CANNOT_PROCEED", |
| 393 "google_transaction_id", |
| 394 &observer); |
| 395 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 396 DCHECK(fetcher); |
| 397 ASSERT_EQ(kSendExtendedAutofillStatusOfFailureValidRequest, |
| 398 fetcher->upload_data()); |
| 399 fetcher->set_response_code(net::HTTP_OK); |
| 400 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 401 } |
| 402 |
| 403 } // end wallet namespace |
OLD | NEW |