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/full_card_request.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/memory/weak_ptr.h" |
| 9 #include "components/autofill/core/browser/credit_card.h" |
| 10 #include "components/autofill/core/browser/personal_data_manager.h" |
| 11 #include "components/autofill/core/browser/test_autofill_client.h" |
| 12 #include "components/autofill/core/browser/test_autofill_driver.h" |
| 13 #include "net/url_request/url_request_test_util.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace autofill { |
| 18 |
| 19 using testing::_; |
| 20 |
| 21 // The consumer of the full card request API. |
| 22 class MockDelegate : public FullCardRequest::Delegate, |
| 23 public base::SupportsWeakPtr<MockDelegate> { |
| 24 public: |
| 25 MOCK_METHOD2(OnFullCardDetails, |
| 26 void(const CreditCard&, const base::string16&)); |
| 27 MOCK_METHOD0(OnFullCardError, void()); |
| 28 }; |
| 29 |
| 30 // The autofill client. |
| 31 class MockAutofillClient : public TestAutofillClient { |
| 32 public: |
| 33 MOCK_METHOD3(ShowUnmaskPrompt, |
| 34 void(const CreditCard&, |
| 35 UnmaskCardReason, |
| 36 base::WeakPtr<CardUnmaskDelegate>)); |
| 37 MOCK_METHOD1(OnUnmaskVerificationResult, |
| 38 void(AutofillClient::PaymentsRpcResult)); |
| 39 }; |
| 40 |
| 41 // Test test fixture for full card request. |
| 42 class FullCardRequestTest : public testing::Test { |
| 43 public: |
| 44 FullCardRequestTest() |
| 45 : request_context_(new net::TestURLRequestContextGetter( |
| 46 base::ThreadTaskRunnerHandle::Get())), |
| 47 personal_data_("en_US"), |
| 48 request_(request_context_.get(), &autofill_client_, &personal_data_) { |
| 49 // Silence the warning from PaymentsClient about matching sync and Payments |
| 50 // server types. |
| 51 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 52 "sync-url", "https://google.com"); |
| 53 } |
| 54 |
| 55 ~FullCardRequestTest() override {} |
| 56 |
| 57 MockAutofillClient* client() { return &autofill_client_; } |
| 58 |
| 59 FullCardRequest* request() { return &request_; } |
| 60 |
| 61 CardUnmaskDelegate* ui_delegate() { return (CardUnmaskDelegate*)&request_; } |
| 62 |
| 63 payments::PaymentsClientDelegate* server_delegate() { |
| 64 return (payments::PaymentsClientDelegate*)&request_; |
| 65 } |
| 66 |
| 67 MockDelegate* delegate() { return &delegate_; } |
| 68 |
| 69 private: |
| 70 base::MessageLoop message_loop_; |
| 71 MockAutofillClient autofill_client_; |
| 72 scoped_refptr<net::TestURLRequestContextGetter> request_context_; |
| 73 PersonalDataManager personal_data_; |
| 74 MockDelegate delegate_; |
| 75 FullCardRequest request_; |
| 76 }; |
| 77 |
| 78 // Matches the |arg| credit card to the given |record_type| and |card_number|. |
| 79 MATCHER_P2(CardMatches, record_type, card_number, "") { |
| 80 return arg.record_type() == record_type && |
| 81 arg.GetRawInfo(CREDIT_CARD_NUMBER) == base::ASCIIToUTF16(card_number); |
| 82 } |
| 83 |
| 84 // Matches the |arg| credit card to the given |record_type|, card |number|, |
| 85 // expiration |month|, and expiration |year|. |
| 86 MATCHER_P4(CardMatches, record_type, number, month, year, "") { |
| 87 return arg.record_type() == record_type && |
| 88 arg.GetRawInfo(CREDIT_CARD_NUMBER) == base::ASCIIToUTF16(number) && |
| 89 arg.GetRawInfo(CREDIT_CARD_EXP_MONTH) == base::ASCIIToUTF16(month) && |
| 90 arg.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR) == |
| 91 base::ASCIIToUTF16(year); |
| 92 } |
| 93 |
| 94 TEST_F(FullCardRequestTest, GetFullCardPanAndCvcForMaskedServerCard) { |
| 95 EXPECT_CALL( |
| 96 *delegate(), |
| 97 OnFullCardDetails(CardMatches(CreditCard::FULL_SERVER_CARD, "4111"), |
| 98 base::ASCIIToUTF16("123"))); |
| 99 EXPECT_CALL(*client(), ShowUnmaskPrompt(_, _, _)); |
| 100 EXPECT_CALL(*client(), OnUnmaskVerificationResult(AutofillClient::SUCCESS)); |
| 101 |
| 102 request()->GetFullCard( |
| 103 CreditCard(CreditCard::MASKED_SERVER_CARD, "server_id"), |
| 104 AutofillClient::UNMASK_FOR_AUTOFILL, delegate()->AsWeakPtr()); |
| 105 CardUnmaskDelegate::UnmaskResponse response; |
| 106 response.cvc = base::ASCIIToUTF16("123"); |
| 107 ui_delegate()->OnUnmaskResponse(response); |
| 108 server_delegate()->OnDidGetRealPan(AutofillClient::SUCCESS, "4111"); |
| 109 ui_delegate()->OnUnmaskPromptClosed(); |
| 110 } |
| 111 |
| 112 TEST_F(FullCardRequestTest, GetFullCardPanAndCvcForLocalCard) { |
| 113 EXPECT_CALL(*delegate(), |
| 114 OnFullCardDetails(CardMatches(CreditCard::LOCAL_CARD, "4111"), |
| 115 base::ASCIIToUTF16("123"))); |
| 116 EXPECT_CALL(*client(), ShowUnmaskPrompt(_, _, _)); |
| 117 EXPECT_CALL(*client(), OnUnmaskVerificationResult(AutofillClient::SUCCESS)); |
| 118 |
| 119 request()->GetFullCard(CreditCard(base::ASCIIToUTF16("4111"), 12, 2050), |
| 120 AutofillClient::UNMASK_FOR_AUTOFILL, |
| 121 delegate()->AsWeakPtr()); |
| 122 CardUnmaskDelegate::UnmaskResponse response; |
| 123 response.cvc = base::ASCIIToUTF16("123"); |
| 124 ui_delegate()->OnUnmaskResponse(response); |
| 125 ui_delegate()->OnUnmaskPromptClosed(); |
| 126 } |
| 127 |
| 128 TEST_F(FullCardRequestTest, GetFullCardPanAndCvcForFullServerCard) { |
| 129 EXPECT_CALL( |
| 130 *delegate(), |
| 131 OnFullCardDetails(CardMatches(CreditCard::FULL_SERVER_CARD, "4111"), |
| 132 base::ASCIIToUTF16("123"))); |
| 133 EXPECT_CALL(*client(), ShowUnmaskPrompt(_, _, _)); |
| 134 EXPECT_CALL(*client(), OnUnmaskVerificationResult(AutofillClient::SUCCESS)); |
| 135 |
| 136 CreditCard full_server_card(base::ASCIIToUTF16("4111"), 12, 2050); |
| 137 full_server_card.set_record_type(CreditCard::FULL_SERVER_CARD); |
| 138 request()->GetFullCard(full_server_card, AutofillClient::UNMASK_FOR_AUTOFILL, |
| 139 delegate()->AsWeakPtr()); |
| 140 CardUnmaskDelegate::UnmaskResponse response; |
| 141 response.cvc = base::ASCIIToUTF16("123"); |
| 142 ui_delegate()->OnUnmaskResponse(response); |
| 143 ui_delegate()->OnUnmaskPromptClosed(); |
| 144 } |
| 145 |
| 146 TEST_F(FullCardRequestTest, OneRequestAtATime) { |
| 147 EXPECT_CALL(*delegate(), OnFullCardError()); |
| 148 EXPECT_CALL(*client(), ShowUnmaskPrompt(_, _, _)); |
| 149 EXPECT_CALL(*client(), OnUnmaskVerificationResult(_)).Times(0); |
| 150 |
| 151 request()->GetFullCard( |
| 152 CreditCard(CreditCard::MASKED_SERVER_CARD, "server_id_1"), |
| 153 AutofillClient::UNMASK_FOR_AUTOFILL, delegate()->AsWeakPtr()); |
| 154 request()->GetFullCard( |
| 155 CreditCard(CreditCard::MASKED_SERVER_CARD, "server_id_2"), |
| 156 AutofillClient::UNMASK_FOR_PAYMENT_REQUEST, delegate()->AsWeakPtr()); |
| 157 } |
| 158 |
| 159 TEST_F(FullCardRequestTest, ClosePromptWithoutUserInput) { |
| 160 EXPECT_CALL(*delegate(), OnFullCardError()); |
| 161 EXPECT_CALL(*client(), ShowUnmaskPrompt(_, _, _)); |
| 162 EXPECT_CALL(*client(), OnUnmaskVerificationResult(_)).Times(0); |
| 163 |
| 164 request()->GetFullCard( |
| 165 CreditCard(CreditCard::MASKED_SERVER_CARD, "server_id"), |
| 166 AutofillClient::UNMASK_FOR_AUTOFILL, delegate()->AsWeakPtr()); |
| 167 ui_delegate()->OnUnmaskPromptClosed(); |
| 168 } |
| 169 |
| 170 TEST_F(FullCardRequestTest, EmptyFullPan) { |
| 171 EXPECT_CALL(*delegate(), OnFullCardError()); |
| 172 EXPECT_CALL(*client(), ShowUnmaskPrompt(_, _, _)); |
| 173 EXPECT_CALL(*client(), |
| 174 OnUnmaskVerificationResult(AutofillClient::PERMANENT_FAILURE)); |
| 175 |
| 176 request()->GetFullCard( |
| 177 CreditCard(CreditCard::MASKED_SERVER_CARD, "server_id"), |
| 178 AutofillClient::UNMASK_FOR_AUTOFILL, delegate()->AsWeakPtr()); |
| 179 CardUnmaskDelegate::UnmaskResponse response; |
| 180 response.cvc = base::ASCIIToUTF16("123"); |
| 181 ui_delegate()->OnUnmaskResponse(response); |
| 182 server_delegate()->OnDidGetRealPan(AutofillClient::PERMANENT_FAILURE, ""); |
| 183 ui_delegate()->OnUnmaskPromptClosed(); |
| 184 } |
| 185 |
| 186 TEST_F(FullCardRequestTest, UpdateExpDateForMaskedServerCard) { |
| 187 EXPECT_CALL(*delegate(), |
| 188 OnFullCardDetails(CardMatches(CreditCard::FULL_SERVER_CARD, |
| 189 "4111", "12", "2050"), |
| 190 base::ASCIIToUTF16("123"))); |
| 191 EXPECT_CALL(*client(), ShowUnmaskPrompt(_, _, _)); |
| 192 EXPECT_CALL(*client(), OnUnmaskVerificationResult(AutofillClient::SUCCESS)); |
| 193 |
| 194 CreditCard masked_server_card(base::ASCIIToUTF16(""), 10, 2000); |
| 195 masked_server_card.set_record_type(CreditCard::MASKED_SERVER_CARD); |
| 196 request()->GetFullCard(masked_server_card, |
| 197 AutofillClient::UNMASK_FOR_AUTOFILL, |
| 198 delegate()->AsWeakPtr()); |
| 199 CardUnmaskDelegate::UnmaskResponse response; |
| 200 response.cvc = base::ASCIIToUTF16("123"); |
| 201 response.exp_month = base::ASCIIToUTF16("12"); |
| 202 response.exp_year = base::ASCIIToUTF16("2050"); |
| 203 ui_delegate()->OnUnmaskResponse(response); |
| 204 server_delegate()->OnDidGetRealPan(AutofillClient::SUCCESS, "4111"); |
| 205 ui_delegate()->OnUnmaskPromptClosed(); |
| 206 } |
| 207 |
| 208 TEST_F(FullCardRequestTest, UpdateExpDateForFullServerCard) { |
| 209 EXPECT_CALL(*delegate(), |
| 210 OnFullCardDetails(CardMatches(CreditCard::FULL_SERVER_CARD, |
| 211 "4111", "12", "2050"), |
| 212 base::ASCIIToUTF16("123"))); |
| 213 EXPECT_CALL(*client(), ShowUnmaskPrompt(_, _, _)); |
| 214 EXPECT_CALL(*client(), OnUnmaskVerificationResult(AutofillClient::SUCCESS)); |
| 215 |
| 216 CreditCard full_server_card(base::ASCIIToUTF16("4111"), 10, 2000); |
| 217 full_server_card.set_record_type(CreditCard::FULL_SERVER_CARD); |
| 218 request()->GetFullCard(full_server_card, AutofillClient::UNMASK_FOR_AUTOFILL, |
| 219 delegate()->AsWeakPtr()); |
| 220 CardUnmaskDelegate::UnmaskResponse response; |
| 221 response.cvc = base::ASCIIToUTF16("123"); |
| 222 response.exp_month = base::ASCIIToUTF16("12"); |
| 223 response.exp_year = base::ASCIIToUTF16("2050"); |
| 224 ui_delegate()->OnUnmaskResponse(response); |
| 225 ui_delegate()->OnUnmaskPromptClosed(); |
| 226 } |
| 227 |
| 228 TEST_F(FullCardRequestTest, UpdateExpDateForLocalCard) { |
| 229 EXPECT_CALL(*delegate(), OnFullCardDetails(CardMatches(CreditCard::LOCAL_CARD, |
| 230 "4111", "12", "2050"), |
| 231 base::ASCIIToUTF16("123"))); |
| 232 EXPECT_CALL(*client(), ShowUnmaskPrompt(_, _, _)); |
| 233 EXPECT_CALL(*client(), OnUnmaskVerificationResult(AutofillClient::SUCCESS)); |
| 234 |
| 235 request()->GetFullCard(CreditCard(base::ASCIIToUTF16("4111"), 10, 2000), |
| 236 AutofillClient::UNMASK_FOR_AUTOFILL, |
| 237 delegate()->AsWeakPtr()); |
| 238 CardUnmaskDelegate::UnmaskResponse response; |
| 239 response.cvc = base::ASCIIToUTF16("123"); |
| 240 response.exp_month = base::ASCIIToUTF16("12"); |
| 241 response.exp_year = base::ASCIIToUTF16("2050"); |
| 242 ui_delegate()->OnUnmaskResponse(response); |
| 243 ui_delegate()->OnUnmaskPromptClosed(); |
| 244 } |
| 245 |
| 246 TEST_F(FullCardRequestTest, SaveRealPan) { |
| 247 EXPECT_CALL(*delegate(), |
| 248 OnFullCardDetails(CardMatches(CreditCard::FULL_SERVER_CARD, |
| 249 "4111", "12", "2050"), |
| 250 base::ASCIIToUTF16("123"))); |
| 251 EXPECT_CALL(*client(), ShowUnmaskPrompt(_, _, _)); |
| 252 EXPECT_CALL(*client(), OnUnmaskVerificationResult(AutofillClient::SUCCESS)); |
| 253 |
| 254 request()->GetFullCard( |
| 255 CreditCard(CreditCard::MASKED_SERVER_CARD, "server_id"), |
| 256 AutofillClient::UNMASK_FOR_AUTOFILL, delegate()->AsWeakPtr()); |
| 257 CardUnmaskDelegate::UnmaskResponse response; |
| 258 response.cvc = base::ASCIIToUTF16("123"); |
| 259 response.exp_month = base::ASCIIToUTF16("12"); |
| 260 response.exp_year = base::ASCIIToUTF16("2050"); |
| 261 response.should_store_pan = true; |
| 262 ui_delegate()->OnUnmaskResponse(response); |
| 263 server_delegate()->OnDidGetRealPan(AutofillClient::SUCCESS, "4111"); |
| 264 ui_delegate()->OnUnmaskPromptClosed(); |
| 265 } |
| 266 |
| 267 TEST_F(FullCardRequestTest, UnmaskForPaymentRequest) { |
| 268 EXPECT_CALL( |
| 269 *delegate(), |
| 270 OnFullCardDetails(CardMatches(CreditCard::FULL_SERVER_CARD, "4111"), |
| 271 base::ASCIIToUTF16("123"))); |
| 272 EXPECT_CALL(*client(), ShowUnmaskPrompt(_, _, _)); |
| 273 EXPECT_CALL(*client(), OnUnmaskVerificationResult(AutofillClient::SUCCESS)); |
| 274 |
| 275 request()->GetFullCard( |
| 276 CreditCard(CreditCard::MASKED_SERVER_CARD, "server_id"), |
| 277 AutofillClient::UNMASK_FOR_AUTOFILL, delegate()->AsWeakPtr()); |
| 278 CardUnmaskDelegate::UnmaskResponse response; |
| 279 response.cvc = base::ASCIIToUTF16("123"); |
| 280 ui_delegate()->OnUnmaskResponse(response); |
| 281 server_delegate()->OnDidGetRealPan(AutofillClient::SUCCESS, "4111"); |
| 282 ui_delegate()->OnUnmaskPromptClosed(); |
| 283 } |
| 284 |
| 285 TEST_F(FullCardRequestTest, IsGettingFullCardForMaskedServerCard) { |
| 286 EXPECT_CALL( |
| 287 *delegate(), |
| 288 OnFullCardDetails(CardMatches(CreditCard::FULL_SERVER_CARD, "4111"), |
| 289 base::ASCIIToUTF16("123"))); |
| 290 EXPECT_CALL(*client(), ShowUnmaskPrompt(_, _, _)); |
| 291 EXPECT_CALL(*client(), OnUnmaskVerificationResult(AutofillClient::SUCCESS)); |
| 292 |
| 293 EXPECT_FALSE(request()->IsGettingFullCard()); |
| 294 |
| 295 request()->GetFullCard( |
| 296 CreditCard(CreditCard::MASKED_SERVER_CARD, "server_id"), |
| 297 AutofillClient::UNMASK_FOR_AUTOFILL, delegate()->AsWeakPtr()); |
| 298 |
| 299 EXPECT_TRUE(request()->IsGettingFullCard()); |
| 300 |
| 301 CardUnmaskDelegate::UnmaskResponse response; |
| 302 response.cvc = base::ASCIIToUTF16("123"); |
| 303 ui_delegate()->OnUnmaskResponse(response); |
| 304 |
| 305 EXPECT_TRUE(request()->IsGettingFullCard()); |
| 306 |
| 307 server_delegate()->OnDidGetRealPan(AutofillClient::SUCCESS, "4111"); |
| 308 |
| 309 EXPECT_FALSE(request()->IsGettingFullCard()); |
| 310 |
| 311 ui_delegate()->OnUnmaskPromptClosed(); |
| 312 |
| 313 EXPECT_FALSE(request()->IsGettingFullCard()); |
| 314 } |
| 315 |
| 316 TEST_F(FullCardRequestTest, IsGettingFullCardForLocalCard) { |
| 317 EXPECT_CALL(*delegate(), |
| 318 OnFullCardDetails(CardMatches(CreditCard::LOCAL_CARD, "4111"), |
| 319 base::ASCIIToUTF16("123"))); |
| 320 EXPECT_CALL(*client(), ShowUnmaskPrompt(_, _, _)); |
| 321 EXPECT_CALL(*client(), OnUnmaskVerificationResult(AutofillClient::SUCCESS)); |
| 322 |
| 323 EXPECT_FALSE(request()->IsGettingFullCard()); |
| 324 |
| 325 request()->GetFullCard(CreditCard(base::ASCIIToUTF16("4111"), 12, 2050), |
| 326 AutofillClient::UNMASK_FOR_AUTOFILL, |
| 327 delegate()->AsWeakPtr()); |
| 328 |
| 329 EXPECT_TRUE(request()->IsGettingFullCard()); |
| 330 |
| 331 CardUnmaskDelegate::UnmaskResponse response; |
| 332 response.cvc = base::ASCIIToUTF16("123"); |
| 333 ui_delegate()->OnUnmaskResponse(response); |
| 334 |
| 335 EXPECT_FALSE(request()->IsGettingFullCard()); |
| 336 |
| 337 ui_delegate()->OnUnmaskPromptClosed(); |
| 338 |
| 339 EXPECT_FALSE(request()->IsGettingFullCard()); |
| 340 } |
| 341 |
| 342 } // namespace autofill |
OLD | NEW |