Chromium Code Reviews| 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 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\"," | |
|
Dan Beam
2012/12/05 19:34:59
nit: why did you indent this JSON differently than
ahutter
2012/12/05 23:37:23
ws matters here since I'm testing the request payl
| |
| 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 | |
|
Dan Beam
2012/12/05 19:34:59
nit: s/\n\n/\n/
ahutter
2012/12/05 23:37:23
Done.
| |
| 168 | |
| 169 namespace wallet { | |
| 170 | |
| 171 class WalletClientTest : public testing::Test { | |
| 172 public: | |
| 173 WalletClientTest() : io_thread_(content::BrowserThread::IO) {} | |
| 174 | |
| 175 virtual void SetUp() { | |
| 176 io_thread_.StartIOThread(); | |
| 177 profile_.CreateRequestContext(); | |
| 178 } | |
| 179 | |
| 180 virtual void TearDown() { | |
| 181 profile_.ResetRequestContext(); | |
| 182 io_thread_.Stop(); | |
| 183 } | |
| 184 | |
| 185 protected: | |
| 186 TestingProfile profile_; | |
|
Dan Beam
2012/12/05 19:34:59
nit: \n
ahutter
2012/12/05 23:37:23
Done.
| |
| 187 private: | |
| 188 // The profile's request context must be released on the IO thread. | |
| 189 content::TestBrowserThread io_thread_; | |
| 190 }; | |
| 191 | |
| 192 class MockWalletClientObserver | |
| 193 : public wallet::WalletClient::WalletClientObserver { | |
| 194 public: | |
| 195 MockWalletClientObserver() {} | |
| 196 ~MockWalletClientObserver() {} | |
| 197 | |
| 198 MOCK_METHOD0(OnAcceptLegalDocuments, void()); | |
| 199 MOCK_METHOD2(OnEncryptOtp, void(const std::string& encrypted_otp, | |
| 200 const std::string& session_material)); | |
| 201 MOCK_METHOD1(OnGetFullWallet, void(FullWallet* full_wallet)); | |
| 202 MOCK_METHOD1(OnGetWalletItems, void(WalletItems* wallet_items)); | |
| 203 MOCK_METHOD0(OnSendExtendedAutofillStatus, void()); | |
| 204 MOCK_METHOD0(OnWalletError, void()); | |
| 205 MOCK_METHOD1(OnNetworkError, void(int response_code)); | |
| 206 }; | |
| 207 | |
| 208 // TODO(ahutter): Improve this when the error body is captured. See | |
| 209 // http://crbug.com/164410. | |
| 210 TEST_F(WalletClientTest, WalletErrorOnExpectedVoidResponse) { | |
| 211 MockWalletClientObserver observer; | |
| 212 EXPECT_CALL(observer, OnWalletError()).Times(1); | |
| 213 | |
| 214 net::TestURLFetcherFactory factory; | |
| 215 | |
| 216 WalletClient wallet_client(profile_.GetRequestContext()); | |
| 217 wallet_client.SendExtendedAutofillStatus(true, "", "", "", &observer); | |
| 218 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | |
| 219 DCHECK(fetcher); | |
| 220 fetcher->set_response_code(net::HTTP_INTERNAL_SERVER_ERROR); | |
| 221 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 222 } | |
| 223 | |
| 224 // TODO(ahutter): Improve this when the error body is captured. See | |
| 225 // http://crbug.com/164410. | |
| 226 TEST_F(WalletClientTest, WalletErrorOnExpectedResponse) { | |
| 227 MockWalletClientObserver observer; | |
| 228 EXPECT_CALL(observer, OnWalletError()).Times(1); | |
| 229 | |
| 230 net::TestURLFetcherFactory factory; | |
| 231 | |
| 232 WalletClient wallet_client(profile_.GetRequestContext()); | |
| 233 Cart cart("currency_code", "currency_code"); | |
| 234 wallet_client.GetFullWallet("", "", "", cart, "", "", "", &observer); | |
| 235 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | |
| 236 DCHECK(fetcher); | |
| 237 fetcher->set_response_code(net::HTTP_INTERNAL_SERVER_ERROR); | |
| 238 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 239 } | |
| 240 | |
| 241 TEST_F(WalletClientTest, NetworkFailureOnExpectedVoidResponse) { | |
| 242 MockWalletClientObserver observer; | |
| 243 EXPECT_CALL(observer, OnNetworkError(net::HTTP_UNAUTHORIZED)).Times(1); | |
| 244 | |
| 245 net::TestURLFetcherFactory factory; | |
| 246 | |
| 247 WalletClient wallet_client(profile_.GetRequestContext()); | |
| 248 wallet_client.SendExtendedAutofillStatus(true, "", "", "", &observer); | |
| 249 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | |
| 250 DCHECK(fetcher); | |
| 251 fetcher->set_response_code(net::HTTP_UNAUTHORIZED); | |
| 252 // The number of retries is set to 3. | |
| 253 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 254 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 255 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 256 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 257 } | |
| 258 | |
| 259 TEST_F(WalletClientTest, NetworkFailureOnExpectedResponse) { | |
| 260 MockWalletClientObserver observer; | |
| 261 EXPECT_CALL(observer, OnNetworkError(net::HTTP_UNAUTHORIZED)).Times(1); | |
| 262 | |
| 263 net::TestURLFetcherFactory factory; | |
| 264 | |
| 265 WalletClient wallet_client(profile_.GetRequestContext()); | |
| 266 Cart cart("currency_code", "currency_code"); | |
| 267 wallet_client.GetFullWallet("", "", "", cart, "", "", "", &observer); | |
| 268 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | |
| 269 DCHECK(fetcher); | |
| 270 fetcher->set_response_code(net::HTTP_UNAUTHORIZED); | |
| 271 // The number of retries is set to 3. | |
| 272 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 273 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 274 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 275 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 276 } | |
| 277 | |
| 278 TEST_F(WalletClientTest, RequestError) { | |
| 279 MockWalletClientObserver observer; | |
| 280 EXPECT_CALL(observer, OnWalletError()).Times(1); | |
| 281 | |
| 282 net::TestURLFetcherFactory factory; | |
| 283 | |
| 284 WalletClient wallet_client(profile_.GetRequestContext()); | |
| 285 wallet_client.SendExtendedAutofillStatus(true, "", "", "", &observer); | |
| 286 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | |
| 287 DCHECK(fetcher); | |
| 288 fetcher->set_response_code(net::HTTP_BAD_REQUEST); | |
| 289 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 290 } | |
| 291 | |
| 292 // TODO(ahutter): Add test for EncryptOtp. | |
| 293 // TODO(ahutter): Add retry and failure tests for EncryptOtp, GetWalletItems, | |
| 294 // GetFullWallet for when data is missing or invalid. | |
| 295 | |
| 296 TEST_F(WalletClientTest, GetFullWallet) { | |
| 297 MockWalletClientObserver observer; | |
| 298 EXPECT_CALL(observer, OnGetFullWallet(testing::NotNull())).Times(1); | |
|
Dan Beam
2012/12/05 19:34:59
cool, didn't know about testing::NotNull()
ahutter
2012/12/05 23:37:23
Ack.
| |
| 299 | |
| 300 net::TestURLFetcherFactory factory; | |
| 301 | |
| 302 WalletClient wallet_client(profile_.GetRequestContext()); | |
| 303 Cart cart("currency_code", "currency_code"); | |
| 304 wallet_client.GetFullWallet("instrument_id", | |
| 305 "shipping_address_id", | |
| 306 "merchant_domain", | |
| 307 cart, | |
| 308 "google_transaction_id", | |
| 309 "encrypted_otp", | |
| 310 "session_material", | |
| 311 &observer); | |
| 312 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | |
| 313 DCHECK(fetcher); | |
| 314 ASSERT_EQ(kGetFullWalletValidRequest, fetcher->upload_data()); | |
| 315 fetcher->set_response_code(net::HTTP_OK); | |
| 316 fetcher->SetResponseString(kGetFullWalletValidResponse); | |
| 317 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 318 } | |
| 319 | |
| 320 TEST_F(WalletClientTest, AcceptLegalDocuments) { | |
| 321 MockWalletClientObserver observer; | |
| 322 EXPECT_CALL(observer, OnAcceptLegalDocuments()).Times(1); | |
| 323 | |
| 324 net::TestURLFetcherFactory factory; | |
| 325 | |
| 326 WalletClient wallet_client(profile_.GetRequestContext()); | |
| 327 std::vector<std::string> doc_ids; | |
| 328 doc_ids.push_back("doc_1"); | |
| 329 doc_ids.push_back("doc_2"); | |
| 330 wallet_client.AcceptLegalDocuments(doc_ids, | |
| 331 kGoogleTransactionId, | |
| 332 &observer); | |
| 333 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | |
| 334 DCHECK(fetcher); | |
| 335 ASSERT_EQ(kAcceptLegalDocumentsValidRequest, fetcher->upload_data()); | |
| 336 fetcher->set_response_code(net::HTTP_OK); | |
| 337 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 338 } | |
| 339 | |
| 340 TEST_F(WalletClientTest, GetWalletItems) { | |
| 341 MockWalletClientObserver observer; | |
| 342 EXPECT_CALL(observer, OnGetWalletItems(testing::NotNull())).Times(1); | |
| 343 | |
| 344 net::TestURLFetcherFactory factory; | |
| 345 | |
| 346 WalletClient wallet_client(profile_.GetRequestContext()); | |
| 347 wallet_client.GetWalletItems(&observer); | |
| 348 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | |
| 349 DCHECK(fetcher); | |
| 350 ASSERT_EQ(kGetWalletItemsValidRequest, fetcher->upload_data()); | |
| 351 fetcher->set_response_code(net::HTTP_OK); | |
| 352 fetcher->SetResponseString(kGetWalletItemsValidResponse); | |
| 353 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 354 } | |
| 355 | |
| 356 TEST_F(WalletClientTest, SendExtendedAutofillOfStatusSuccess) { | |
| 357 MockWalletClientObserver observer; | |
| 358 EXPECT_CALL(observer, OnSendExtendedAutofillStatus()).Times(1); | |
| 359 | |
| 360 net::TestURLFetcherFactory factory; | |
| 361 | |
| 362 WalletClient wallet_client(profile_.GetRequestContext()); | |
| 363 wallet_client.SendExtendedAutofillStatus(true, | |
| 364 "hostname", | |
| 365 "", | |
| 366 "google_transaction_id", | |
| 367 &observer); | |
| 368 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | |
| 369 DCHECK(fetcher); | |
| 370 ASSERT_EQ(kSendExtendedAutofillStatusOfSuccessValidRequest, | |
| 371 fetcher->upload_data()); | |
| 372 fetcher->set_response_code(net::HTTP_OK); | |
| 373 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 374 } | |
| 375 | |
| 376 TEST_F(WalletClientTest, SendExtendedAutofillStatusOfFailure) { | |
| 377 MockWalletClientObserver observer; | |
| 378 EXPECT_CALL(observer, OnSendExtendedAutofillStatus()).Times(1); | |
| 379 | |
| 380 net::TestURLFetcherFactory factory; | |
| 381 | |
| 382 WalletClient wallet_client(profile_.GetRequestContext()); | |
| 383 wallet_client.SendExtendedAutofillStatus(false, | |
| 384 "hostname", | |
| 385 "CANNOT_PROCEED", | |
| 386 "google_transaction_id", | |
| 387 &observer); | |
| 388 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | |
| 389 DCHECK(fetcher); | |
| 390 ASSERT_EQ(kSendExtendedAutofillStatusOfFailureValidRequest, | |
| 391 fetcher->upload_data()); | |
| 392 fetcher->set_response_code(net::HTTP_OK); | |
| 393 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 394 } | |
| 395 | |
| 396 } // end wallet namespace | |
| OLD | NEW |