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> |
6 | 7 |
7 #include "base/command_line.h" | 8 #include "base/command_line.h" |
8 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/strings/string_piece.h" |
9 #include "base/threading/thread_task_runner_handle.h" | 11 #include "base/threading/thread_task_runner_handle.h" |
10 #include "base/values.h" | 12 #include "base/values.h" |
11 #include "components/autofill/core/browser/autofill_test_utils.h" | 13 #include "components/autofill/core/browser/autofill_test_utils.h" |
12 #include "components/autofill/core/browser/payments/payments_client.h" | 14 #include "components/autofill/core/browser/payments/payments_client.h" |
13 #include "components/autofill/core/common/autofill_switches.h" | 15 #include "components/autofill/core/common/autofill_switches.h" |
14 #include "content/public/test/test_browser_thread_bundle.h" | 16 #include "content/public/test/test_browser_thread_bundle.h" |
15 #include "google_apis/gaia/fake_identity_provider.h" | 17 #include "google_apis/gaia/fake_identity_provider.h" |
16 #include "google_apis/gaia/fake_oauth2_token_service.h" | 18 #include "google_apis/gaia/fake_oauth2_token_service.h" |
17 #include "net/url_request/test_url_fetcher_factory.h" | 19 #include "net/url_request/test_url_fetcher_factory.h" |
18 #include "net/url_request/url_request_test_util.h" | 20 #include "net/url_request/url_request_test_util.h" |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 PaymentsClient::UnmaskRequestDetails request_details; | 77 PaymentsClient::UnmaskRequestDetails request_details; |
76 request_details.card = test::GetMaskedServerCard(); | 78 request_details.card = test::GetMaskedServerCard(); |
77 request_details.user_response.cvc = base::ASCIIToUTF16("123"); | 79 request_details.user_response.cvc = base::ASCIIToUTF16("123"); |
78 request_details.risk_data = "some risk data"; | 80 request_details.risk_data = "some risk data"; |
79 client_->UnmaskCard(request_details); | 81 client_->UnmaskCard(request_details); |
80 } | 82 } |
81 | 83 |
82 void StartGettingUploadDetails() { | 84 void StartGettingUploadDetails() { |
83 token_service_->AddAccount("example@gmail.com"); | 85 token_service_->AddAccount("example@gmail.com"); |
84 identity_provider_->LogIn("example@gmail.com"); | 86 identity_provider_->LogIn("example@gmail.com"); |
85 client_->GetUploadDetails("language-LOCALE"); | 87 client_->GetUploadDetails(BuildTestProfiles(), "language-LOCALE"); |
86 } | 88 } |
87 | 89 |
88 void StartUploading() { | 90 void StartUploading() { |
89 token_service_->AddAccount("example@gmail.com"); | 91 token_service_->AddAccount("example@gmail.com"); |
90 identity_provider_->LogIn("example@gmail.com"); | 92 identity_provider_->LogIn("example@gmail.com"); |
91 PaymentsClient::UploadRequestDetails request_details; | 93 PaymentsClient::UploadRequestDetails request_details; |
92 request_details.card = test::GetCreditCard(); | 94 request_details.card = test::GetCreditCard(); |
93 request_details.cvc = base::ASCIIToUTF16("123"); | 95 request_details.cvc = base::ASCIIToUTF16("123"); |
94 request_details.context_token = base::ASCIIToUTF16("context token"); | 96 request_details.context_token = base::ASCIIToUTF16("context token"); |
95 request_details.risk_data = "some risk data"; | 97 request_details.risk_data = "some risk data"; |
96 request_details.app_locale = "language-LOCALE"; | 98 request_details.app_locale = "language-LOCALE"; |
| 99 request_details.profiles = BuildTestProfiles(); |
97 client_->UploadCard(request_details); | 100 client_->UploadCard(request_details); |
98 } | 101 } |
99 | 102 |
| 103 const std::string& GetUploadData() { |
| 104 return factory_.GetFetcherByID(0)->upload_data(); |
| 105 } |
| 106 |
100 void IssueOAuthToken() { | 107 void IssueOAuthToken() { |
101 token_service_->IssueAllTokensForAccount( | 108 token_service_->IssueAllTokensForAccount( |
102 "example@gmail.com", "totally_real_token", | 109 "example@gmail.com", "totally_real_token", |
103 base::Time::Now() + base::TimeDelta::FromDays(10)); | 110 base::Time::Now() + base::TimeDelta::FromDays(10)); |
104 | 111 |
105 // Verify the auth header. | 112 // Verify the auth header. |
106 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); | 113 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); |
107 net::HttpRequestHeaders request_headers; | 114 net::HttpRequestHeaders request_headers; |
108 fetcher->GetExtraRequestHeaders(&request_headers); | 115 fetcher->GetExtraRequestHeaders(&request_headers); |
109 std::string auth_header_value; | 116 std::string auth_header_value; |
(...skipping 18 matching lines...) Expand all Loading... |
128 | 135 |
129 content::TestBrowserThreadBundle thread_bundle_; | 136 content::TestBrowserThreadBundle thread_bundle_; |
130 net::TestURLFetcherFactory factory_; | 137 net::TestURLFetcherFactory factory_; |
131 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | 138 scoped_refptr<net::TestURLRequestContextGetter> request_context_; |
132 std::unique_ptr<FakeOAuth2TokenService> token_service_; | 139 std::unique_ptr<FakeOAuth2TokenService> token_service_; |
133 std::unique_ptr<FakeIdentityProvider> identity_provider_; | 140 std::unique_ptr<FakeIdentityProvider> identity_provider_; |
134 std::unique_ptr<PaymentsClient> client_; | 141 std::unique_ptr<PaymentsClient> client_; |
135 | 142 |
136 private: | 143 private: |
137 DISALLOW_COPY_AND_ASSIGN(PaymentsClientTest); | 144 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 } |
138 }; | 179 }; |
139 | 180 |
140 TEST_F(PaymentsClientTest, OAuthError) { | 181 TEST_F(PaymentsClientTest, OAuthError) { |
141 StartUnmasking(); | 182 StartUnmasking(); |
142 token_service_->IssueErrorForAllPendingRequestsForAccount( | 183 token_service_->IssueErrorForAllPendingRequestsForAccount( |
143 "example@gmail.com", | 184 "example@gmail.com", |
144 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE)); | 185 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE)); |
145 EXPECT_EQ(AutofillClient::PERMANENT_FAILURE, result_); | 186 EXPECT_EQ(AutofillClient::PERMANENT_FAILURE, result_); |
146 EXPECT_TRUE(real_pan_.empty()); | 187 EXPECT_TRUE(real_pan_.empty()); |
147 } | 188 } |
148 | 189 |
149 TEST_F(PaymentsClientTest, UnmaskSuccess) { | 190 TEST_F(PaymentsClientTest, UnmaskSuccess) { |
150 StartUnmasking(); | 191 StartUnmasking(); |
151 IssueOAuthToken(); | 192 IssueOAuthToken(); |
152 ReturnResponse(net::HTTP_OK, "{ \"pan\": \"1234\" }"); | 193 ReturnResponse(net::HTTP_OK, "{ \"pan\": \"1234\" }"); |
153 EXPECT_EQ(AutofillClient::SUCCESS, result_); | 194 EXPECT_EQ(AutofillClient::SUCCESS, result_); |
154 EXPECT_EQ("1234", real_pan_); | 195 EXPECT_EQ("1234", real_pan_); |
155 } | 196 } |
156 | 197 |
157 TEST_F(PaymentsClientTest, GetDetailsSuccess) { | 198 TEST_F(PaymentsClientTest, GetDetailsSuccess) { |
158 StartGettingUploadDetails(); | 199 StartGettingUploadDetails(); |
159 ReturnResponse( | 200 ReturnResponse( |
160 net::HTTP_OK, | 201 net::HTTP_OK, |
161 "{ \"context_token\": \"some_token\", \"legal_message\": {} }"); | 202 "{ \"context_token\": \"some_token\", \"legal_message\": {} }"); |
162 EXPECT_EQ(AutofillClient::SUCCESS, result_); | 203 EXPECT_EQ(AutofillClient::SUCCESS, result_); |
163 EXPECT_NE(nullptr, legal_message_.get()); | 204 EXPECT_NE(nullptr, legal_message_.get()); |
164 } | 205 } |
165 | 206 |
| 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 |
166 TEST_F(PaymentsClientTest, UploadSuccess) { | 230 TEST_F(PaymentsClientTest, UploadSuccess) { |
167 StartUploading(); | 231 StartUploading(); |
168 IssueOAuthToken(); | 232 IssueOAuthToken(); |
169 ReturnResponse(net::HTTP_OK, "{}"); | 233 ReturnResponse(net::HTTP_OK, "{}"); |
170 EXPECT_EQ(AutofillClient::SUCCESS, result_); | 234 EXPECT_EQ(AutofillClient::SUCCESS, result_); |
171 } | 235 } |
172 | 236 |
| 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 |
173 TEST_F(PaymentsClientTest, GetDetailsFollowedByUploadSuccess) { | 260 TEST_F(PaymentsClientTest, GetDetailsFollowedByUploadSuccess) { |
174 StartGettingUploadDetails(); | 261 StartGettingUploadDetails(); |
175 ReturnResponse( | 262 ReturnResponse( |
176 net::HTTP_OK, | 263 net::HTTP_OK, |
177 "{ \"context_token\": \"some_token\", \"legal_message\": {} }"); | 264 "{ \"context_token\": \"some_token\", \"legal_message\": {} }"); |
178 EXPECT_EQ(AutofillClient::SUCCESS, result_); | 265 EXPECT_EQ(AutofillClient::SUCCESS, result_); |
179 | 266 |
180 result_ = AutofillClient::NONE; | 267 result_ = AutofillClient::NONE; |
181 | 268 |
182 StartUploading(); | 269 StartUploading(); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 TEST_F(PaymentsClientTest, OtherError) { | 362 TEST_F(PaymentsClientTest, OtherError) { |
276 StartUnmasking(); | 363 StartUnmasking(); |
277 IssueOAuthToken(); | 364 IssueOAuthToken(); |
278 ReturnResponse(net::HTTP_FORBIDDEN, std::string()); | 365 ReturnResponse(net::HTTP_FORBIDDEN, std::string()); |
279 EXPECT_EQ(AutofillClient::PERMANENT_FAILURE, result_); | 366 EXPECT_EQ(AutofillClient::PERMANENT_FAILURE, result_); |
280 EXPECT_EQ("", real_pan_); | 367 EXPECT_EQ("", real_pan_); |
281 } | 368 } |
282 | 369 |
283 } // namespace autofill | 370 } // namespace autofill |
284 } // namespace payments | 371 } // namespace payments |
OLD | NEW |