| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <utility> | 5 #include <utility> |
| 6 #include <vector> | |
| 7 | 6 |
| 8 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 9 #include "base/macros.h" | 8 #include "base/macros.h" |
| 10 #include "base/strings/string_piece.h" | |
| 11 #include "base/threading/thread_task_runner_handle.h" | 9 #include "base/threading/thread_task_runner_handle.h" |
| 12 #include "base/values.h" | 10 #include "base/values.h" |
| 13 #include "components/autofill/core/browser/autofill_test_utils.h" | 11 #include "components/autofill/core/browser/autofill_test_utils.h" |
| 14 #include "components/autofill/core/browser/payments/payments_client.h" | 12 #include "components/autofill/core/browser/payments/payments_client.h" |
| 15 #include "components/autofill/core/common/autofill_switches.h" | 13 #include "components/autofill/core/common/autofill_switches.h" |
| 16 #include "content/public/test/test_browser_thread_bundle.h" | 14 #include "content/public/test/test_browser_thread_bundle.h" |
| 17 #include "google_apis/gaia/fake_identity_provider.h" | 15 #include "google_apis/gaia/fake_identity_provider.h" |
| 18 #include "google_apis/gaia/fake_oauth2_token_service.h" | 16 #include "google_apis/gaia/fake_oauth2_token_service.h" |
| 19 #include "net/url_request/test_url_fetcher_factory.h" | 17 #include "net/url_request/test_url_fetcher_factory.h" |
| 20 #include "net/url_request/url_request_test_util.h" | 18 #include "net/url_request/url_request_test_util.h" |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 PaymentsClient::UnmaskRequestDetails request_details; | 75 PaymentsClient::UnmaskRequestDetails request_details; |
| 78 request_details.card = test::GetMaskedServerCard(); | 76 request_details.card = test::GetMaskedServerCard(); |
| 79 request_details.user_response.cvc = base::ASCIIToUTF16("123"); | 77 request_details.user_response.cvc = base::ASCIIToUTF16("123"); |
| 80 request_details.risk_data = "some risk data"; | 78 request_details.risk_data = "some risk data"; |
| 81 client_->UnmaskCard(request_details); | 79 client_->UnmaskCard(request_details); |
| 82 } | 80 } |
| 83 | 81 |
| 84 void StartGettingUploadDetails() { | 82 void StartGettingUploadDetails() { |
| 85 token_service_->AddAccount("example@gmail.com"); | 83 token_service_->AddAccount("example@gmail.com"); |
| 86 identity_provider_->LogIn("example@gmail.com"); | 84 identity_provider_->LogIn("example@gmail.com"); |
| 87 client_->GetUploadDetails(BuildTestProfiles(), "language-LOCALE"); | 85 client_->GetUploadDetails("language-LOCALE"); |
| 88 } | 86 } |
| 89 | 87 |
| 90 void StartUploading() { | 88 void StartUploading() { |
| 91 token_service_->AddAccount("example@gmail.com"); | 89 token_service_->AddAccount("example@gmail.com"); |
| 92 identity_provider_->LogIn("example@gmail.com"); | 90 identity_provider_->LogIn("example@gmail.com"); |
| 93 PaymentsClient::UploadRequestDetails request_details; | 91 PaymentsClient::UploadRequestDetails request_details; |
| 94 request_details.card = test::GetCreditCard(); | 92 request_details.card = test::GetCreditCard(); |
| 95 request_details.cvc = base::ASCIIToUTF16("123"); | 93 request_details.cvc = base::ASCIIToUTF16("123"); |
| 96 request_details.context_token = base::ASCIIToUTF16("context token"); | 94 request_details.context_token = base::ASCIIToUTF16("context token"); |
| 97 request_details.risk_data = "some risk data"; | 95 request_details.risk_data = "some risk data"; |
| 98 request_details.app_locale = "language-LOCALE"; | 96 request_details.app_locale = "language-LOCALE"; |
| 99 request_details.profiles = BuildTestProfiles(); | |
| 100 client_->UploadCard(request_details); | 97 client_->UploadCard(request_details); |
| 101 } | 98 } |
| 102 | 99 |
| 103 const std::string& GetUploadData() { | |
| 104 return factory_.GetFetcherByID(0)->upload_data(); | |
| 105 } | |
| 106 | |
| 107 void IssueOAuthToken() { | 100 void IssueOAuthToken() { |
| 108 token_service_->IssueAllTokensForAccount( | 101 token_service_->IssueAllTokensForAccount( |
| 109 "example@gmail.com", "totally_real_token", | 102 "example@gmail.com", "totally_real_token", |
| 110 base::Time::Now() + base::TimeDelta::FromDays(10)); | 103 base::Time::Now() + base::TimeDelta::FromDays(10)); |
| 111 | 104 |
| 112 // Verify the auth header. | 105 // Verify the auth header. |
| 113 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); | 106 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); |
| 114 net::HttpRequestHeaders request_headers; | 107 net::HttpRequestHeaders request_headers; |
| 115 fetcher->GetExtraRequestHeaders(&request_headers); | 108 fetcher->GetExtraRequestHeaders(&request_headers); |
| 116 std::string auth_header_value; | 109 std::string auth_header_value; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 135 | 128 |
| 136 content::TestBrowserThreadBundle thread_bundle_; | 129 content::TestBrowserThreadBundle thread_bundle_; |
| 137 net::TestURLFetcherFactory factory_; | 130 net::TestURLFetcherFactory factory_; |
| 138 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | 131 scoped_refptr<net::TestURLRequestContextGetter> request_context_; |
| 139 std::unique_ptr<FakeOAuth2TokenService> token_service_; | 132 std::unique_ptr<FakeOAuth2TokenService> token_service_; |
| 140 std::unique_ptr<FakeIdentityProvider> identity_provider_; | 133 std::unique_ptr<FakeIdentityProvider> identity_provider_; |
| 141 std::unique_ptr<PaymentsClient> client_; | 134 std::unique_ptr<PaymentsClient> client_; |
| 142 | 135 |
| 143 private: | 136 private: |
| 144 DISALLOW_COPY_AND_ASSIGN(PaymentsClientTest); | 137 DISALLOW_COPY_AND_ASSIGN(PaymentsClientTest); |
| 145 | |
| 146 std::vector<AutofillProfile> BuildTestProfiles() { | |
| 147 std::vector<AutofillProfile> profiles; | |
| 148 profiles.push_back(BuildProfile("John", "Smith", "1234 Main St.", "Miami", | |
| 149 "FL", "32006", "212-555-0162")); | |
| 150 profiles.push_back(BuildProfile("Pat", "Jones", "432 Oak Lane", "Lincoln", | |
| 151 "OH", "43005", "(834)555-0090")); | |
| 152 return profiles; | |
| 153 } | |
| 154 | |
| 155 AutofillProfile BuildProfile(base::StringPiece first_name, | |
| 156 base::StringPiece last_name, | |
| 157 base::StringPiece address_line, | |
| 158 base::StringPiece city, | |
| 159 base::StringPiece state, | |
| 160 base::StringPiece zip, | |
| 161 base::StringPiece phone_number) { | |
| 162 AutofillProfile profile; | |
| 163 | |
| 164 profile.SetInfo(AutofillType(NAME_FIRST), ASCIIToUTF16(first_name), | |
| 165 "en-US"); | |
| 166 profile.SetInfo(AutofillType(NAME_LAST), ASCIIToUTF16(last_name), "en-US"); | |
| 167 profile.SetInfo(AutofillType(ADDRESS_HOME_LINE1), | |
| 168 ASCIIToUTF16(address_line), "en-US"); | |
| 169 profile.SetInfo(AutofillType(ADDRESS_HOME_CITY), ASCIIToUTF16(city), | |
| 170 "en-US"); | |
| 171 profile.SetInfo(AutofillType(ADDRESS_HOME_STATE), ASCIIToUTF16(state), | |
| 172 "en-US"); | |
| 173 profile.SetInfo(AutofillType(ADDRESS_HOME_ZIP), ASCIIToUTF16(zip), "en-US"); | |
| 174 profile.SetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER), | |
| 175 ASCIIToUTF16(phone_number), "en-US"); | |
| 176 | |
| 177 return profile; | |
| 178 } | |
| 179 }; | 138 }; |
| 180 | 139 |
| 181 TEST_F(PaymentsClientTest, OAuthError) { | 140 TEST_F(PaymentsClientTest, OAuthError) { |
| 182 StartUnmasking(); | 141 StartUnmasking(); |
| 183 token_service_->IssueErrorForAllPendingRequestsForAccount( | 142 token_service_->IssueErrorForAllPendingRequestsForAccount( |
| 184 "example@gmail.com", | 143 "example@gmail.com", |
| 185 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE)); | 144 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE)); |
| 186 EXPECT_EQ(AutofillClient::PERMANENT_FAILURE, result_); | 145 EXPECT_EQ(AutofillClient::PERMANENT_FAILURE, result_); |
| 187 EXPECT_TRUE(real_pan_.empty()); | 146 EXPECT_TRUE(real_pan_.empty()); |
| 188 } | 147 } |
| 189 | 148 |
| 190 TEST_F(PaymentsClientTest, UnmaskSuccess) { | 149 TEST_F(PaymentsClientTest, UnmaskSuccess) { |
| 191 StartUnmasking(); | 150 StartUnmasking(); |
| 192 IssueOAuthToken(); | 151 IssueOAuthToken(); |
| 193 ReturnResponse(net::HTTP_OK, "{ \"pan\": \"1234\" }"); | 152 ReturnResponse(net::HTTP_OK, "{ \"pan\": \"1234\" }"); |
| 194 EXPECT_EQ(AutofillClient::SUCCESS, result_); | 153 EXPECT_EQ(AutofillClient::SUCCESS, result_); |
| 195 EXPECT_EQ("1234", real_pan_); | 154 EXPECT_EQ("1234", real_pan_); |
| 196 } | 155 } |
| 197 | 156 |
| 198 TEST_F(PaymentsClientTest, GetDetailsSuccess) { | 157 TEST_F(PaymentsClientTest, GetDetailsSuccess) { |
| 199 StartGettingUploadDetails(); | 158 StartGettingUploadDetails(); |
| 200 ReturnResponse( | 159 ReturnResponse( |
| 201 net::HTTP_OK, | 160 net::HTTP_OK, |
| 202 "{ \"context_token\": \"some_token\", \"legal_message\": {} }"); | 161 "{ \"context_token\": \"some_token\", \"legal_message\": {} }"); |
| 203 EXPECT_EQ(AutofillClient::SUCCESS, result_); | 162 EXPECT_EQ(AutofillClient::SUCCESS, result_); |
| 204 EXPECT_NE(nullptr, legal_message_.get()); | 163 EXPECT_NE(nullptr, legal_message_.get()); |
| 205 } | 164 } |
| 206 | 165 |
| 207 TEST_F(PaymentsClientTest, GetDetailsRemovesNonLocationData) { | |
| 208 StartGettingUploadDetails(); | |
| 209 | |
| 210 // Verify that the recipient name field and test names appear nowhere in the | |
| 211 // upload data. | |
| 212 EXPECT_TRUE(GetUploadData().find(PaymentsClient::kRecipientName) == | |
| 213 std::string::npos); | |
| 214 EXPECT_TRUE(GetUploadData().find("John") == std::string::npos); | |
| 215 EXPECT_TRUE(GetUploadData().find("Smith") == std::string::npos); | |
| 216 EXPECT_TRUE(GetUploadData().find("Pat") == std::string::npos); | |
| 217 EXPECT_TRUE(GetUploadData().find("Jones") == std::string::npos); | |
| 218 | |
| 219 // Verify that the phone number field and test numbers appear nowhere in the | |
| 220 // upload data. | |
| 221 EXPECT_TRUE(GetUploadData().find(PaymentsClient::kPhoneNumber) == | |
| 222 std::string::npos); | |
| 223 EXPECT_TRUE(GetUploadData().find("212") == std::string::npos); | |
| 224 EXPECT_TRUE(GetUploadData().find("555") == std::string::npos); | |
| 225 EXPECT_TRUE(GetUploadData().find("0162") == std::string::npos); | |
| 226 EXPECT_TRUE(GetUploadData().find("834") == std::string::npos); | |
| 227 EXPECT_TRUE(GetUploadData().find("0090") == std::string::npos); | |
| 228 } | |
| 229 | |
| 230 TEST_F(PaymentsClientTest, UploadSuccess) { | 166 TEST_F(PaymentsClientTest, UploadSuccess) { |
| 231 StartUploading(); | 167 StartUploading(); |
| 232 IssueOAuthToken(); | 168 IssueOAuthToken(); |
| 233 ReturnResponse(net::HTTP_OK, "{}"); | 169 ReturnResponse(net::HTTP_OK, "{}"); |
| 234 EXPECT_EQ(AutofillClient::SUCCESS, result_); | 170 EXPECT_EQ(AutofillClient::SUCCESS, result_); |
| 235 } | 171 } |
| 236 | 172 |
| 237 TEST_F(PaymentsClientTest, UploadIncludesNonLocationData) { | |
| 238 StartUploading(); | |
| 239 | |
| 240 // Verify that the recipient name field and test names do appear in the upload | |
| 241 // data. | |
| 242 EXPECT_TRUE(GetUploadData().find(PaymentsClient::kRecipientName) != | |
| 243 std::string::npos); | |
| 244 EXPECT_TRUE(GetUploadData().find("John") != std::string::npos); | |
| 245 EXPECT_TRUE(GetUploadData().find("Smith") != std::string::npos); | |
| 246 EXPECT_TRUE(GetUploadData().find("Pat") != std::string::npos); | |
| 247 EXPECT_TRUE(GetUploadData().find("Jones") != std::string::npos); | |
| 248 | |
| 249 // Verify that the phone number field and test numbers do appear in the upload | |
| 250 // data. | |
| 251 EXPECT_TRUE(GetUploadData().find(PaymentsClient::kPhoneNumber) != | |
| 252 std::string::npos); | |
| 253 EXPECT_TRUE(GetUploadData().find("212") != std::string::npos); | |
| 254 EXPECT_TRUE(GetUploadData().find("555") != std::string::npos); | |
| 255 EXPECT_TRUE(GetUploadData().find("0162") != std::string::npos); | |
| 256 EXPECT_TRUE(GetUploadData().find("834") != std::string::npos); | |
| 257 EXPECT_TRUE(GetUploadData().find("0090") != std::string::npos); | |
| 258 } | |
| 259 | |
| 260 TEST_F(PaymentsClientTest, GetDetailsFollowedByUploadSuccess) { | 173 TEST_F(PaymentsClientTest, GetDetailsFollowedByUploadSuccess) { |
| 261 StartGettingUploadDetails(); | 174 StartGettingUploadDetails(); |
| 262 ReturnResponse( | 175 ReturnResponse( |
| 263 net::HTTP_OK, | 176 net::HTTP_OK, |
| 264 "{ \"context_token\": \"some_token\", \"legal_message\": {} }"); | 177 "{ \"context_token\": \"some_token\", \"legal_message\": {} }"); |
| 265 EXPECT_EQ(AutofillClient::SUCCESS, result_); | 178 EXPECT_EQ(AutofillClient::SUCCESS, result_); |
| 266 | 179 |
| 267 result_ = AutofillClient::NONE; | 180 result_ = AutofillClient::NONE; |
| 268 | 181 |
| 269 StartUploading(); | 182 StartUploading(); |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 TEST_F(PaymentsClientTest, OtherError) { | 275 TEST_F(PaymentsClientTest, OtherError) { |
| 363 StartUnmasking(); | 276 StartUnmasking(); |
| 364 IssueOAuthToken(); | 277 IssueOAuthToken(); |
| 365 ReturnResponse(net::HTTP_FORBIDDEN, std::string()); | 278 ReturnResponse(net::HTTP_FORBIDDEN, std::string()); |
| 366 EXPECT_EQ(AutofillClient::PERMANENT_FAILURE, result_); | 279 EXPECT_EQ(AutofillClient::PERMANENT_FAILURE, result_); |
| 367 EXPECT_EQ("", real_pan_); | 280 EXPECT_EQ("", real_pan_); |
| 368 } | 281 } |
| 369 | 282 |
| 370 } // namespace autofill | 283 } // namespace autofill |
| 371 } // namespace payments | 284 } // namespace payments |
| OLD | NEW |