Chromium Code Reviews| 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( | |
| 149 BuildProfile("John", "Smith", "1234 Main St.", "Miami", "FL", "32006")); | |
| 150 profiles.push_back( | |
| 151 BuildProfile("Pat", "Jones", "432 Oak Lane", "Lincoln", "OH", "43005")); | |
| 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 AutofillProfile profile; | |
| 162 | |
| 163 profile.SetInfo(AutofillType(NAME_FIRST), | |
| 164 ASCIIToUTF16(first_name), "en-US"); | |
| 165 profile.SetInfo(AutofillType(NAME_LAST), ASCIIToUTF16(last_name), "en-US"); | |
| 166 profile.SetInfo(AutofillType(ADDRESS_HOME_LINE1), | |
| 167 ASCIIToUTF16(address_line), "en-US"); | |
| 168 profile.SetInfo(AutofillType(ADDRESS_HOME_CITY), | |
| 169 ASCIIToUTF16(city), "en-US"); | |
| 170 profile.SetInfo(AutofillType(ADDRESS_HOME_STATE), | |
| 171 ASCIIToUTF16(state), "en-US"); | |
| 172 profile.SetInfo(AutofillType(ADDRESS_HOME_ZIP), ASCIIToUTF16(zip), "en-US"); | |
| 173 | |
| 174 return profile; | |
| 175 } | |
| 138 }; | 176 }; |
| 139 | 177 |
| 140 TEST_F(PaymentsClientTest, OAuthError) { | 178 TEST_F(PaymentsClientTest, OAuthError) { |
| 141 StartUnmasking(); | 179 StartUnmasking(); |
| 142 token_service_->IssueErrorForAllPendingRequestsForAccount( | 180 token_service_->IssueErrorForAllPendingRequestsForAccount( |
| 143 "example@gmail.com", | 181 "example@gmail.com", |
| 144 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE)); | 182 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE)); |
| 145 EXPECT_EQ(AutofillClient::PERMANENT_FAILURE, result_); | 183 EXPECT_EQ(AutofillClient::PERMANENT_FAILURE, result_); |
| 146 EXPECT_TRUE(real_pan_.empty()); | 184 EXPECT_TRUE(real_pan_.empty()); |
| 147 } | 185 } |
| 148 | 186 |
| 149 TEST_F(PaymentsClientTest, UnmaskSuccess) { | 187 TEST_F(PaymentsClientTest, UnmaskSuccess) { |
| 150 StartUnmasking(); | 188 StartUnmasking(); |
| 151 IssueOAuthToken(); | 189 IssueOAuthToken(); |
| 152 ReturnResponse(net::HTTP_OK, "{ \"pan\": \"1234\" }"); | 190 ReturnResponse(net::HTTP_OK, "{ \"pan\": \"1234\" }"); |
| 153 EXPECT_EQ(AutofillClient::SUCCESS, result_); | 191 EXPECT_EQ(AutofillClient::SUCCESS, result_); |
| 154 EXPECT_EQ("1234", real_pan_); | 192 EXPECT_EQ("1234", real_pan_); |
| 155 } | 193 } |
| 156 | 194 |
| 157 TEST_F(PaymentsClientTest, GetDetailsSuccess) { | 195 TEST_F(PaymentsClientTest, GetDetailsSuccess) { |
| 158 StartGettingUploadDetails(); | 196 StartGettingUploadDetails(); |
| 159 ReturnResponse( | 197 ReturnResponse( |
| 160 net::HTTP_OK, | 198 net::HTTP_OK, |
| 161 "{ \"context_token\": \"some_token\", \"legal_message\": {} }"); | 199 "{ \"context_token\": \"some_token\", \"legal_message\": {} }"); |
| 162 EXPECT_EQ(AutofillClient::SUCCESS, result_); | 200 EXPECT_EQ(AutofillClient::SUCCESS, result_); |
| 163 EXPECT_NE(nullptr, legal_message_.get()); | 201 EXPECT_NE(nullptr, legal_message_.get()); |
| 164 } | 202 } |
| 165 | 203 |
| 204 TEST_F(PaymentsClientTest, GetDetailsRemovesRecipientNames) { | |
| 205 StartGettingUploadDetails(); | |
| 206 EXPECT_TRUE(GetUploadData().find(PaymentsClient::kRecipientName) == | |
| 207 std::string::npos); | |
|
Evan Stade
2016/09/22 16:16:13
nit: verify other fields are present? verify "John
| |
| 208 } | |
| 209 | |
| 166 TEST_F(PaymentsClientTest, UploadSuccess) { | 210 TEST_F(PaymentsClientTest, UploadSuccess) { |
| 167 StartUploading(); | 211 StartUploading(); |
| 168 IssueOAuthToken(); | 212 IssueOAuthToken(); |
| 169 ReturnResponse(net::HTTP_OK, "{}"); | 213 ReturnResponse(net::HTTP_OK, "{}"); |
| 170 EXPECT_EQ(AutofillClient::SUCCESS, result_); | 214 EXPECT_EQ(AutofillClient::SUCCESS, result_); |
| 171 } | 215 } |
| 172 | 216 |
| 217 TEST_F(PaymentsClientTest, UploadIncludesRecipientNames) { | |
| 218 StartUploading(); | |
| 219 EXPECT_TRUE(GetUploadData().find(PaymentsClient::kRecipientName) != | |
| 220 std::string::npos); | |
| 221 } | |
| 222 | |
| 173 TEST_F(PaymentsClientTest, GetDetailsFollowedByUploadSuccess) { | 223 TEST_F(PaymentsClientTest, GetDetailsFollowedByUploadSuccess) { |
| 174 StartGettingUploadDetails(); | 224 StartGettingUploadDetails(); |
| 175 ReturnResponse( | 225 ReturnResponse( |
| 176 net::HTTP_OK, | 226 net::HTTP_OK, |
| 177 "{ \"context_token\": \"some_token\", \"legal_message\": {} }"); | 227 "{ \"context_token\": \"some_token\", \"legal_message\": {} }"); |
| 178 EXPECT_EQ(AutofillClient::SUCCESS, result_); | 228 EXPECT_EQ(AutofillClient::SUCCESS, result_); |
| 179 | 229 |
| 180 result_ = AutofillClient::NONE; | 230 result_ = AutofillClient::NONE; |
| 181 | 231 |
| 182 StartUploading(); | 232 StartUploading(); |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 275 TEST_F(PaymentsClientTest, OtherError) { | 325 TEST_F(PaymentsClientTest, OtherError) { |
| 276 StartUnmasking(); | 326 StartUnmasking(); |
| 277 IssueOAuthToken(); | 327 IssueOAuthToken(); |
| 278 ReturnResponse(net::HTTP_FORBIDDEN, std::string()); | 328 ReturnResponse(net::HTTP_FORBIDDEN, std::string()); |
| 279 EXPECT_EQ(AutofillClient::PERMANENT_FAILURE, result_); | 329 EXPECT_EQ(AutofillClient::PERMANENT_FAILURE, result_); |
| 280 EXPECT_EQ("", real_pan_); | 330 EXPECT_EQ("", real_pan_); |
| 281 } | 331 } |
| 282 | 332 |
| 283 } // namespace autofill | 333 } // namespace autofill |
| 284 } // namespace payments | 334 } // namespace payments |
| OLD | NEW |