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 } // 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 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 260 } |
| 261 |
| 262 TEST_F(WalletClientTest, NetworkFailureOnExpectedResponse) { |
| 263 MockWalletClientObserver observer; |
| 264 EXPECT_CALL(observer, OnNetworkError(net::HTTP_UNAUTHORIZED)).Times(1); |
| 265 |
| 266 net::TestURLFetcherFactory factory; |
| 267 |
| 268 WalletClient wallet_client(profile_.GetRequestContext()); |
| 269 Cart cart("currency_code", "currency_code"); |
| 270 wallet_client.GetFullWallet("", "", "", cart, "", "", "", &observer); |
| 271 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 272 DCHECK(fetcher); |
| 273 fetcher->set_response_code(net::HTTP_UNAUTHORIZED); |
| 274 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 275 } |
| 276 |
| 277 TEST_F(WalletClientTest, RequestError) { |
| 278 MockWalletClientObserver observer; |
| 279 EXPECT_CALL(observer, OnWalletError()).Times(1); |
| 280 |
| 281 net::TestURLFetcherFactory factory; |
| 282 |
| 283 WalletClient wallet_client(profile_.GetRequestContext()); |
| 284 wallet_client.SendExtendedAutofillStatus(true, "", "", "", &observer); |
| 285 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 286 DCHECK(fetcher); |
| 287 fetcher->set_response_code(net::HTTP_BAD_REQUEST); |
| 288 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 289 } |
| 290 |
| 291 // TODO(ahutter): Add test for EncryptOtp. |
| 292 // TODO(ahutter): Add retry and failure tests for EncryptOtp, GetWalletItems, |
| 293 // GetFullWallet for when data is missing or invalid. |
| 294 |
| 295 TEST_F(WalletClientTest, GetFullWallet) { |
| 296 MockWalletClientObserver observer; |
| 297 EXPECT_CALL(observer, OnGetFullWallet(testing::NotNull())).Times(1); |
| 298 |
| 299 net::TestURLFetcherFactory factory; |
| 300 |
| 301 WalletClient wallet_client(profile_.GetRequestContext()); |
| 302 Cart cart("currency_code", "currency_code"); |
| 303 wallet_client.GetFullWallet("instrument_id", |
| 304 "shipping_address_id", |
| 305 "merchant_domain", |
| 306 cart, |
| 307 "google_transaction_id", |
| 308 "encrypted_otp", |
| 309 "session_material", |
| 310 &observer); |
| 311 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 312 DCHECK(fetcher); |
| 313 ASSERT_EQ(kGetFullWalletValidRequest, fetcher->upload_data()); |
| 314 fetcher->set_response_code(net::HTTP_OK); |
| 315 fetcher->SetResponseString(kGetFullWalletValidResponse); |
| 316 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 317 } |
| 318 |
| 319 TEST_F(WalletClientTest, AcceptLegalDocuments) { |
| 320 MockWalletClientObserver observer; |
| 321 EXPECT_CALL(observer, OnAcceptLegalDocuments()).Times(1); |
| 322 |
| 323 net::TestURLFetcherFactory factory; |
| 324 |
| 325 WalletClient wallet_client(profile_.GetRequestContext()); |
| 326 std::vector<std::string> doc_ids; |
| 327 doc_ids.push_back("doc_1"); |
| 328 doc_ids.push_back("doc_2"); |
| 329 wallet_client.AcceptLegalDocuments(doc_ids, |
| 330 kGoogleTransactionId, |
| 331 &observer); |
| 332 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 333 DCHECK(fetcher); |
| 334 ASSERT_EQ(kAcceptLegalDocumentsValidRequest, fetcher->upload_data()); |
| 335 fetcher->set_response_code(net::HTTP_OK); |
| 336 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 337 } |
| 338 |
| 339 TEST_F(WalletClientTest, GetWalletItems) { |
| 340 MockWalletClientObserver observer; |
| 341 EXPECT_CALL(observer, OnGetWalletItems(testing::NotNull())).Times(1); |
| 342 |
| 343 net::TestURLFetcherFactory factory; |
| 344 |
| 345 WalletClient wallet_client(profile_.GetRequestContext()); |
| 346 wallet_client.GetWalletItems(&observer); |
| 347 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 348 DCHECK(fetcher); |
| 349 ASSERT_EQ(kGetWalletItemsValidRequest, fetcher->upload_data()); |
| 350 fetcher->set_response_code(net::HTTP_OK); |
| 351 fetcher->SetResponseString(kGetWalletItemsValidResponse); |
| 352 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 353 } |
| 354 |
| 355 TEST_F(WalletClientTest, SendExtendedAutofillOfStatusSuccess) { |
| 356 MockWalletClientObserver observer; |
| 357 EXPECT_CALL(observer, OnSendExtendedAutofillStatus()).Times(1); |
| 358 |
| 359 net::TestURLFetcherFactory factory; |
| 360 |
| 361 WalletClient wallet_client(profile_.GetRequestContext()); |
| 362 wallet_client.SendExtendedAutofillStatus(true, |
| 363 "hostname", |
| 364 "", |
| 365 "google_transaction_id", |
| 366 &observer); |
| 367 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 368 DCHECK(fetcher); |
| 369 ASSERT_EQ(kSendExtendedAutofillStatusOfSuccessValidRequest, |
| 370 fetcher->upload_data()); |
| 371 fetcher->set_response_code(net::HTTP_OK); |
| 372 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 373 } |
| 374 |
| 375 TEST_F(WalletClientTest, SendExtendedAutofillStatusOfFailure) { |
| 376 MockWalletClientObserver observer; |
| 377 EXPECT_CALL(observer, OnSendExtendedAutofillStatus()).Times(1); |
| 378 |
| 379 net::TestURLFetcherFactory factory; |
| 380 |
| 381 WalletClient wallet_client(profile_.GetRequestContext()); |
| 382 wallet_client.SendExtendedAutofillStatus(false, |
| 383 "hostname", |
| 384 "CANNOT_PROCEED", |
| 385 "google_transaction_id", |
| 386 &observer); |
| 387 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 388 DCHECK(fetcher); |
| 389 ASSERT_EQ(kSendExtendedAutofillStatusOfFailureValidRequest, |
| 390 fetcher->upload_data()); |
| 391 fetcher->set_response_code(net::HTTP_OK); |
| 392 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 393 } |
| 394 |
| 395 } // namespace wallet |
| 396 |
OLD | NEW |