OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "components/autofill/core/browser/payments/full_card_request.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/macros.h" |
| 9 #include "base/memory/weak_ptr.h" |
| 10 #include "components/autofill/core/browser/credit_card.h" |
| 11 #include "components/autofill/core/browser/payments/payments_client.h" |
| 12 #include "components/autofill/core/browser/personal_data_manager.h" |
| 13 #include "components/autofill/core/browser/test_autofill_client.h" |
| 14 #include "components/autofill/core/browser/test_autofill_driver.h" |
| 15 #include "net/url_request/url_request_test_util.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 namespace autofill { |
| 20 namespace payments { |
| 21 |
| 22 using testing::_; |
| 23 |
| 24 // The consumer of the full card request API. |
| 25 class MockDelegate : public FullCardRequest::Delegate, |
| 26 public base::SupportsWeakPtr<MockDelegate> { |
| 27 public: |
| 28 MOCK_METHOD2(OnFullCardDetails, |
| 29 void(const CreditCard&, const base::string16&)); |
| 30 MOCK_METHOD0(OnFullCardError, void()); |
| 31 }; |
| 32 |
| 33 // The autofill client. |
| 34 class MockAutofillClient : public TestAutofillClient { |
| 35 public: |
| 36 MOCK_METHOD3(ShowUnmaskPrompt, |
| 37 void(const CreditCard&, |
| 38 UnmaskCardReason, |
| 39 base::WeakPtr<CardUnmaskDelegate>)); |
| 40 MOCK_METHOD1(OnUnmaskVerificationResult, |
| 41 void(AutofillClient::PaymentsRpcResult)); |
| 42 }; |
| 43 |
| 44 // The test fixture for full card request. |
| 45 class FullCardRequestTest : public testing::Test, |
| 46 public PaymentsClientDelegate { |
| 47 public: |
| 48 FullCardRequestTest() |
| 49 : personal_data_("en_US"), |
| 50 request_context_(new net::TestURLRequestContextGetter( |
| 51 base::ThreadTaskRunnerHandle::Get())), |
| 52 payments_client_(request_context_.get(), this), |
| 53 request_(&autofill_client_, &payments_client_, &personal_data_) { |
| 54 // Silence the warning from PaymentsClient about matching sync and Payments |
| 55 // server types. |
| 56 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 57 "sync-url", "https://google.com"); |
| 58 } |
| 59 |
| 60 ~FullCardRequestTest() override {} |
| 61 |
| 62 MockAutofillClient* client() { return &autofill_client_; } |
| 63 |
| 64 FullCardRequest* request() { return &request_; } |
| 65 |
| 66 CardUnmaskDelegate* ui_delegate() { return (CardUnmaskDelegate*)&request_; } |
| 67 |
| 68 MockDelegate* delegate() { return &delegate_; } |
| 69 |
| 70 // PaymentsClientDelegate: |
| 71 void OnDidGetRealPan(AutofillClient::PaymentsRpcResult result, |
| 72 const std::string& real_pan) override { |
| 73 request_.OnDidGetRealPan(result, real_pan); |
| 74 } |
| 75 |
| 76 private: |
| 77 // PaymentsClientDelegate: |
| 78 IdentityProvider* GetIdentityProvider() override { |
| 79 return autofill_client_.GetIdentityProvider(); |
| 80 } |
| 81 void OnDidGetUploadDetails( |
| 82 AutofillClient::PaymentsRpcResult result, |
| 83 const base::string16& context_token, |
| 84 std::unique_ptr<base::DictionaryValue> legal_message) override { |
| 85 ASSERT_TRUE(false) << "No upload details in this test"; |
| 86 } |
| 87 void OnDidUploadCard(AutofillClient::PaymentsRpcResult result) override { |
| 88 ASSERT_TRUE(false) << "No card uploads in this test."; |
| 89 } |
| 90 |
| 91 base::MessageLoop message_loop_; |
| 92 PersonalDataManager personal_data_; |
| 93 MockAutofillClient autofill_client_; |
| 94 scoped_refptr<net::TestURLRequestContextGetter> request_context_; |
| 95 PaymentsClient payments_client_; |
| 96 MockDelegate delegate_; |
| 97 FullCardRequest request_; |
| 98 |
| 99 DISALLOW_COPY_AND_ASSIGN(FullCardRequestTest); |
| 100 }; |
| 101 |
| 102 // Matches the |arg| credit card to the given |record_type| and |card_number|. |
| 103 MATCHER_P2(CardMatches, record_type, card_number, "") { |
| 104 return arg.record_type() == record_type && |
| 105 arg.GetRawInfo(CREDIT_CARD_NUMBER) == base::ASCIIToUTF16(card_number); |
| 106 } |
| 107 |
| 108 // Matches the |arg| credit card to the given |record_type|, card |number|, |
| 109 // expiration |month|, and expiration |year|. |
| 110 MATCHER_P4(CardMatches, record_type, number, month, year, "") { |
| 111 return arg.record_type() == record_type && |
| 112 arg.GetRawInfo(CREDIT_CARD_NUMBER) == base::ASCIIToUTF16(number) && |
| 113 arg.GetRawInfo(CREDIT_CARD_EXP_MONTH) == base::ASCIIToUTF16(month) && |
| 114 arg.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR) == |
| 115 base::ASCIIToUTF16(year); |
| 116 } |
| 117 |
| 118 // Verify getting the full PAN and the CVC for a masked server card. |
| 119 TEST_F(FullCardRequestTest, GetFullCardPanAndCvcForMaskedServerCard) { |
| 120 EXPECT_CALL( |
| 121 *delegate(), |
| 122 OnFullCardDetails(CardMatches(CreditCard::FULL_SERVER_CARD, "4111"), |
| 123 base::ASCIIToUTF16("123"))); |
| 124 EXPECT_CALL(*client(), ShowUnmaskPrompt(_, _, _)); |
| 125 EXPECT_CALL(*client(), OnUnmaskVerificationResult(AutofillClient::SUCCESS)); |
| 126 |
| 127 request()->GetFullCard( |
| 128 CreditCard(CreditCard::MASKED_SERVER_CARD, "server_id"), |
| 129 AutofillClient::UNMASK_FOR_AUTOFILL, delegate()->AsWeakPtr()); |
| 130 CardUnmaskDelegate::UnmaskResponse response; |
| 131 response.cvc = base::ASCIIToUTF16("123"); |
| 132 ui_delegate()->OnUnmaskResponse(response); |
| 133 OnDidGetRealPan(AutofillClient::SUCCESS, "4111"); |
| 134 ui_delegate()->OnUnmaskPromptClosed(); |
| 135 } |
| 136 |
| 137 // Verify getting the CVC for a local card. |
| 138 TEST_F(FullCardRequestTest, GetFullCardPanAndCvcForLocalCard) { |
| 139 EXPECT_CALL(*delegate(), |
| 140 OnFullCardDetails(CardMatches(CreditCard::LOCAL_CARD, "4111"), |
| 141 base::ASCIIToUTF16("123"))); |
| 142 EXPECT_CALL(*client(), ShowUnmaskPrompt(_, _, _)); |
| 143 EXPECT_CALL(*client(), OnUnmaskVerificationResult(AutofillClient::SUCCESS)); |
| 144 |
| 145 request()->GetFullCard(CreditCard(base::ASCIIToUTF16("4111"), 12, 2050), |
| 146 AutofillClient::UNMASK_FOR_AUTOFILL, |
| 147 delegate()->AsWeakPtr()); |
| 148 CardUnmaskDelegate::UnmaskResponse response; |
| 149 response.cvc = base::ASCIIToUTF16("123"); |
| 150 ui_delegate()->OnUnmaskResponse(response); |
| 151 ui_delegate()->OnUnmaskPromptClosed(); |
| 152 } |
| 153 |
| 154 // Verify getting the CVC for an unmasked server card. |
| 155 TEST_F(FullCardRequestTest, GetFullCardPanAndCvcForFullServerCard) { |
| 156 EXPECT_CALL( |
| 157 *delegate(), |
| 158 OnFullCardDetails(CardMatches(CreditCard::FULL_SERVER_CARD, "4111"), |
| 159 base::ASCIIToUTF16("123"))); |
| 160 EXPECT_CALL(*client(), ShowUnmaskPrompt(_, _, _)); |
| 161 EXPECT_CALL(*client(), OnUnmaskVerificationResult(AutofillClient::SUCCESS)); |
| 162 |
| 163 CreditCard full_server_card(base::ASCIIToUTF16("4111"), 12, 2050); |
| 164 full_server_card.set_record_type(CreditCard::FULL_SERVER_CARD); |
| 165 request()->GetFullCard(full_server_card, AutofillClient::UNMASK_FOR_AUTOFILL, |
| 166 delegate()->AsWeakPtr()); |
| 167 CardUnmaskDelegate::UnmaskResponse response; |
| 168 response.cvc = base::ASCIIToUTF16("123"); |
| 169 ui_delegate()->OnUnmaskResponse(response); |
| 170 ui_delegate()->OnUnmaskPromptClosed(); |
| 171 } |
| 172 |
| 173 // Only one request at a time should be allowed. |
| 174 TEST_F(FullCardRequestTest, OneRequestAtATime) { |
| 175 EXPECT_CALL(*delegate(), OnFullCardError()); |
| 176 EXPECT_CALL(*client(), ShowUnmaskPrompt(_, _, _)); |
| 177 EXPECT_CALL(*client(), OnUnmaskVerificationResult(_)).Times(0); |
| 178 |
| 179 request()->GetFullCard( |
| 180 CreditCard(CreditCard::MASKED_SERVER_CARD, "server_id_1"), |
| 181 AutofillClient::UNMASK_FOR_AUTOFILL, delegate()->AsWeakPtr()); |
| 182 request()->GetFullCard( |
| 183 CreditCard(CreditCard::MASKED_SERVER_CARD, "server_id_2"), |
| 184 AutofillClient::UNMASK_FOR_PAYMENT_REQUEST, delegate()->AsWeakPtr()); |
| 185 } |
| 186 |
| 187 // After the first request completes, it's OK to start the second request. |
| 188 TEST_F(FullCardRequestTest, SecondRequestOkAfterFirstFinished) { |
| 189 EXPECT_CALL(*delegate(), OnFullCardError()).Times(0); |
| 190 EXPECT_CALL(*delegate(), |
| 191 OnFullCardDetails(CardMatches(CreditCard::LOCAL_CARD, "4111"), |
| 192 base::ASCIIToUTF16("123"))) |
| 193 .Times(2); |
| 194 EXPECT_CALL(*client(), ShowUnmaskPrompt(_, _, _)).Times(2); |
| 195 EXPECT_CALL(*client(), OnUnmaskVerificationResult(AutofillClient::SUCCESS)) |
| 196 .Times(2); |
| 197 |
| 198 request()->GetFullCard(CreditCard(base::ASCIIToUTF16("4111"), 12, 2050), |
| 199 AutofillClient::UNMASK_FOR_AUTOFILL, |
| 200 delegate()->AsWeakPtr()); |
| 201 CardUnmaskDelegate::UnmaskResponse response; |
| 202 response.cvc = base::ASCIIToUTF16("123"); |
| 203 ui_delegate()->OnUnmaskResponse(response); |
| 204 ui_delegate()->OnUnmaskPromptClosed(); |
| 205 |
| 206 request()->GetFullCard(CreditCard(base::ASCIIToUTF16("4111"), 12, 2050), |
| 207 AutofillClient::UNMASK_FOR_AUTOFILL, |
| 208 delegate()->AsWeakPtr()); |
| 209 ui_delegate()->OnUnmaskResponse(response); |
| 210 ui_delegate()->OnUnmaskPromptClosed(); |
| 211 } |
| 212 |
| 213 // If the user cancels the CVC prompt, |
| 214 // FullCardRequest::Delegate::OnFullCardError() should be invoked. |
| 215 TEST_F(FullCardRequestTest, ClosePromptWithoutUserInput) { |
| 216 EXPECT_CALL(*delegate(), OnFullCardError()); |
| 217 EXPECT_CALL(*client(), ShowUnmaskPrompt(_, _, _)); |
| 218 EXPECT_CALL(*client(), OnUnmaskVerificationResult(_)).Times(0); |
| 219 |
| 220 request()->GetFullCard( |
| 221 CreditCard(CreditCard::MASKED_SERVER_CARD, "server_id"), |
| 222 AutofillClient::UNMASK_FOR_AUTOFILL, delegate()->AsWeakPtr()); |
| 223 ui_delegate()->OnUnmaskPromptClosed(); |
| 224 } |
| 225 |
| 226 // If the server provides an empty PAN, |
| 227 // FullCardRequest::Delegate::OnFullCardError() should be invoked. |
| 228 TEST_F(FullCardRequestTest, EmptyFullPan) { |
| 229 EXPECT_CALL(*delegate(), OnFullCardError()); |
| 230 EXPECT_CALL(*client(), ShowUnmaskPrompt(_, _, _)); |
| 231 EXPECT_CALL(*client(), |
| 232 OnUnmaskVerificationResult(AutofillClient::PERMANENT_FAILURE)); |
| 233 |
| 234 request()->GetFullCard( |
| 235 CreditCard(CreditCard::MASKED_SERVER_CARD, "server_id"), |
| 236 AutofillClient::UNMASK_FOR_AUTOFILL, delegate()->AsWeakPtr()); |
| 237 CardUnmaskDelegate::UnmaskResponse response; |
| 238 response.cvc = base::ASCIIToUTF16("123"); |
| 239 ui_delegate()->OnUnmaskResponse(response); |
| 240 OnDidGetRealPan(AutofillClient::PERMANENT_FAILURE, ""); |
| 241 ui_delegate()->OnUnmaskPromptClosed(); |
| 242 } |
| 243 |
| 244 // Verify updating expiration date for a masked server card. |
| 245 TEST_F(FullCardRequestTest, UpdateExpDateForMaskedServerCard) { |
| 246 EXPECT_CALL(*delegate(), |
| 247 OnFullCardDetails(CardMatches(CreditCard::FULL_SERVER_CARD, |
| 248 "4111", "12", "2050"), |
| 249 base::ASCIIToUTF16("123"))); |
| 250 EXPECT_CALL(*client(), ShowUnmaskPrompt(_, _, _)); |
| 251 EXPECT_CALL(*client(), OnUnmaskVerificationResult(AutofillClient::SUCCESS)); |
| 252 |
| 253 request()->GetFullCard( |
| 254 CreditCard(CreditCard::MASKED_SERVER_CARD, "server_id"), |
| 255 AutofillClient::UNMASK_FOR_AUTOFILL, delegate()->AsWeakPtr()); |
| 256 CardUnmaskDelegate::UnmaskResponse response; |
| 257 response.cvc = base::ASCIIToUTF16("123"); |
| 258 response.exp_month = base::ASCIIToUTF16("12"); |
| 259 response.exp_year = base::ASCIIToUTF16("2050"); |
| 260 ui_delegate()->OnUnmaskResponse(response); |
| 261 OnDidGetRealPan(AutofillClient::SUCCESS, "4111"); |
| 262 ui_delegate()->OnUnmaskPromptClosed(); |
| 263 } |
| 264 |
| 265 // Verify updating expiration date for an unmasked server card. |
| 266 TEST_F(FullCardRequestTest, UpdateExpDateForFullServerCard) { |
| 267 EXPECT_CALL(*delegate(), |
| 268 OnFullCardDetails(CardMatches(CreditCard::FULL_SERVER_CARD, |
| 269 "4111", "12", "2050"), |
| 270 base::ASCIIToUTF16("123"))); |
| 271 EXPECT_CALL(*client(), ShowUnmaskPrompt(_, _, _)); |
| 272 EXPECT_CALL(*client(), OnUnmaskVerificationResult(AutofillClient::SUCCESS)); |
| 273 |
| 274 CreditCard full_server_card(base::ASCIIToUTF16("4111"), 10, 2000); |
| 275 full_server_card.set_record_type(CreditCard::FULL_SERVER_CARD); |
| 276 request()->GetFullCard(full_server_card, AutofillClient::UNMASK_FOR_AUTOFILL, |
| 277 delegate()->AsWeakPtr()); |
| 278 CardUnmaskDelegate::UnmaskResponse response; |
| 279 response.cvc = base::ASCIIToUTF16("123"); |
| 280 response.exp_month = base::ASCIIToUTF16("12"); |
| 281 response.exp_year = base::ASCIIToUTF16("2050"); |
| 282 ui_delegate()->OnUnmaskResponse(response); |
| 283 ui_delegate()->OnUnmaskPromptClosed(); |
| 284 } |
| 285 |
| 286 // Verify updating expiration date for a local card. |
| 287 TEST_F(FullCardRequestTest, UpdateExpDateForLocalCard) { |
| 288 EXPECT_CALL(*delegate(), OnFullCardDetails(CardMatches(CreditCard::LOCAL_CARD, |
| 289 "4111", "12", "2050"), |
| 290 base::ASCIIToUTF16("123"))); |
| 291 EXPECT_CALL(*client(), ShowUnmaskPrompt(_, _, _)); |
| 292 EXPECT_CALL(*client(), OnUnmaskVerificationResult(AutofillClient::SUCCESS)); |
| 293 |
| 294 request()->GetFullCard(CreditCard(base::ASCIIToUTF16("4111"), 10, 2000), |
| 295 AutofillClient::UNMASK_FOR_AUTOFILL, |
| 296 delegate()->AsWeakPtr()); |
| 297 CardUnmaskDelegate::UnmaskResponse response; |
| 298 response.cvc = base::ASCIIToUTF16("123"); |
| 299 response.exp_month = base::ASCIIToUTF16("12"); |
| 300 response.exp_year = base::ASCIIToUTF16("2050"); |
| 301 ui_delegate()->OnUnmaskResponse(response); |
| 302 ui_delegate()->OnUnmaskPromptClosed(); |
| 303 } |
| 304 |
| 305 // Verify saving full PAN on disk. |
| 306 TEST_F(FullCardRequestTest, SaveRealPan) { |
| 307 EXPECT_CALL(*delegate(), |
| 308 OnFullCardDetails(CardMatches(CreditCard::FULL_SERVER_CARD, |
| 309 "4111", "12", "2050"), |
| 310 base::ASCIIToUTF16("123"))); |
| 311 EXPECT_CALL(*client(), ShowUnmaskPrompt(_, _, _)); |
| 312 EXPECT_CALL(*client(), OnUnmaskVerificationResult(AutofillClient::SUCCESS)); |
| 313 |
| 314 request()->GetFullCard( |
| 315 CreditCard(CreditCard::MASKED_SERVER_CARD, "server_id"), |
| 316 AutofillClient::UNMASK_FOR_AUTOFILL, delegate()->AsWeakPtr()); |
| 317 CardUnmaskDelegate::UnmaskResponse response; |
| 318 response.cvc = base::ASCIIToUTF16("123"); |
| 319 response.exp_month = base::ASCIIToUTF16("12"); |
| 320 response.exp_year = base::ASCIIToUTF16("2050"); |
| 321 response.should_store_pan = true; |
| 322 ui_delegate()->OnUnmaskResponse(response); |
| 323 OnDidGetRealPan(AutofillClient::SUCCESS, "4111"); |
| 324 ui_delegate()->OnUnmaskPromptClosed(); |
| 325 } |
| 326 |
| 327 // Verify getting full PAN and CVC for PaymentRequest. |
| 328 TEST_F(FullCardRequestTest, UnmaskForPaymentRequest) { |
| 329 EXPECT_CALL( |
| 330 *delegate(), |
| 331 OnFullCardDetails(CardMatches(CreditCard::FULL_SERVER_CARD, "4111"), |
| 332 base::ASCIIToUTF16("123"))); |
| 333 EXPECT_CALL(*client(), ShowUnmaskPrompt(_, _, _)); |
| 334 EXPECT_CALL(*client(), OnUnmaskVerificationResult(AutofillClient::SUCCESS)); |
| 335 |
| 336 request()->GetFullCard( |
| 337 CreditCard(CreditCard::MASKED_SERVER_CARD, "server_id"), |
| 338 AutofillClient::UNMASK_FOR_PAYMENT_REQUEST, delegate()->AsWeakPtr()); |
| 339 CardUnmaskDelegate::UnmaskResponse response; |
| 340 response.cvc = base::ASCIIToUTF16("123"); |
| 341 ui_delegate()->OnUnmaskResponse(response); |
| 342 OnDidGetRealPan(AutofillClient::SUCCESS, "4111"); |
| 343 ui_delegate()->OnUnmaskPromptClosed(); |
| 344 } |
| 345 |
| 346 // Verify that FullCardRequest::IsGettingFullCard() is true until the server |
| 347 // returns the full PAN for a masked card. |
| 348 TEST_F(FullCardRequestTest, IsGettingFullCardForMaskedServerCard) { |
| 349 EXPECT_CALL( |
| 350 *delegate(), |
| 351 OnFullCardDetails(CardMatches(CreditCard::FULL_SERVER_CARD, "4111"), |
| 352 base::ASCIIToUTF16("123"))); |
| 353 EXPECT_CALL(*client(), ShowUnmaskPrompt(_, _, _)); |
| 354 EXPECT_CALL(*client(), OnUnmaskVerificationResult(AutofillClient::SUCCESS)); |
| 355 |
| 356 EXPECT_FALSE(request()->IsGettingFullCard()); |
| 357 |
| 358 request()->GetFullCard( |
| 359 CreditCard(CreditCard::MASKED_SERVER_CARD, "server_id"), |
| 360 AutofillClient::UNMASK_FOR_AUTOFILL, delegate()->AsWeakPtr()); |
| 361 |
| 362 EXPECT_TRUE(request()->IsGettingFullCard()); |
| 363 |
| 364 CardUnmaskDelegate::UnmaskResponse response; |
| 365 response.cvc = base::ASCIIToUTF16("123"); |
| 366 ui_delegate()->OnUnmaskResponse(response); |
| 367 |
| 368 EXPECT_TRUE(request()->IsGettingFullCard()); |
| 369 |
| 370 OnDidGetRealPan(AutofillClient::SUCCESS, "4111"); |
| 371 |
| 372 EXPECT_FALSE(request()->IsGettingFullCard()); |
| 373 |
| 374 ui_delegate()->OnUnmaskPromptClosed(); |
| 375 |
| 376 EXPECT_FALSE(request()->IsGettingFullCard()); |
| 377 } |
| 378 |
| 379 // Verify that FullCardRequest::IsGettingFullCard() is true until the user types |
| 380 // in the CVC for a card that is not masked. |
| 381 TEST_F(FullCardRequestTest, IsGettingFullCardForLocalCard) { |
| 382 EXPECT_CALL(*delegate(), |
| 383 OnFullCardDetails(CardMatches(CreditCard::LOCAL_CARD, "4111"), |
| 384 base::ASCIIToUTF16("123"))); |
| 385 EXPECT_CALL(*client(), ShowUnmaskPrompt(_, _, _)); |
| 386 EXPECT_CALL(*client(), OnUnmaskVerificationResult(AutofillClient::SUCCESS)); |
| 387 |
| 388 EXPECT_FALSE(request()->IsGettingFullCard()); |
| 389 |
| 390 request()->GetFullCard(CreditCard(base::ASCIIToUTF16("4111"), 12, 2050), |
| 391 AutofillClient::UNMASK_FOR_AUTOFILL, |
| 392 delegate()->AsWeakPtr()); |
| 393 |
| 394 EXPECT_TRUE(request()->IsGettingFullCard()); |
| 395 |
| 396 CardUnmaskDelegate::UnmaskResponse response; |
| 397 response.cvc = base::ASCIIToUTF16("123"); |
| 398 ui_delegate()->OnUnmaskResponse(response); |
| 399 |
| 400 EXPECT_FALSE(request()->IsGettingFullCard()); |
| 401 |
| 402 ui_delegate()->OnUnmaskPromptClosed(); |
| 403 |
| 404 EXPECT_FALSE(request()->IsGettingFullCard()); |
| 405 } |
| 406 |
| 407 } // namespace payments |
| 408 } // namespace autofill |
OLD | NEW |