OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include <utility> | |
6 | |
7 #include "base/command_line.h" | |
8 #include "base/macros.h" | |
9 #include "base/threading/thread_task_runner_handle.h" | |
10 #include "base/values.h" | |
11 #include "components/autofill/core/browser/autofill_test_utils.h" | |
12 #include "components/autofill/core/browser/payments/payments_client.h" | |
13 #include "components/autofill/core/common/autofill_switches.h" | |
14 #include "content/public/test/test_browser_thread_bundle.h" | |
15 #include "google_apis/gaia/fake_identity_provider.h" | |
16 #include "google_apis/gaia/fake_oauth2_token_service.h" | |
17 #include "net/url_request/test_url_fetcher_factory.h" | |
18 #include "net/url_request/url_request_test_util.h" | |
19 #include "testing/gtest/include/gtest/gtest.h" | |
20 | |
21 namespace autofill { | |
22 namespace payments { | |
23 | |
24 class PaymentsClientTest : public testing::Test, public PaymentsClientDelegate { | |
25 public: | |
26 PaymentsClientTest() : result_(AutofillClient::NONE) {} | |
27 ~PaymentsClientTest() override {} | |
28 | |
29 void SetUp() override { | |
30 // Silence the warning for mismatching sync and Payments servers. | |
31 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
32 switches::kWalletServiceUseSandbox, "0"); | |
33 | |
34 result_ = AutofillClient::NONE; | |
35 real_pan_.clear(); | |
36 legal_message_.reset(); | |
37 | |
38 request_context_ = new net::TestURLRequestContextGetter( | |
39 base::ThreadTaskRunnerHandle::Get()); | |
40 token_service_.reset(new FakeOAuth2TokenService()); | |
41 identity_provider_.reset(new FakeIdentityProvider(token_service_.get())); | |
42 client_.reset(new PaymentsClient(request_context_.get(), this)); | |
43 } | |
44 | |
45 void TearDown() override { client_.reset(); } | |
46 | |
47 // PaymentsClientDelegate | |
48 | |
49 IdentityProvider* GetIdentityProvider() override { | |
50 return identity_provider_.get(); | |
51 } | |
52 | |
53 void OnDidGetRealPan(AutofillClient::PaymentsRpcResult result, | |
54 const std::string& real_pan) override { | |
55 result_ = result; | |
56 real_pan_ = real_pan; | |
57 } | |
58 | |
59 void OnDidGetUploadDetails( | |
60 AutofillClient::PaymentsRpcResult result, | |
61 const base::string16& context_token, | |
62 std::unique_ptr<base::DictionaryValue> legal_message) override { | |
63 result_ = result; | |
64 legal_message_ = std::move(legal_message); | |
65 } | |
66 | |
67 void OnDidUploadCard(AutofillClient::PaymentsRpcResult result) override { | |
68 result_ = result; | |
69 } | |
70 | |
71 protected: | |
72 void StartUnmasking() { | |
73 token_service_->AddAccount("example@gmail.com"); | |
74 identity_provider_->LogIn("example@gmail.com"); | |
75 PaymentsClient::UnmaskRequestDetails request_details; | |
76 request_details.card = test::GetMaskedServerCard(); | |
77 request_details.user_response.cvc = base::ASCIIToUTF16("123"); | |
78 request_details.risk_data = "some risk data"; | |
79 client_->UnmaskCard(request_details); | |
80 } | |
81 | |
82 void StartGettingUploadDetails() { | |
83 token_service_->AddAccount("example@gmail.com"); | |
84 identity_provider_->LogIn("example@gmail.com"); | |
85 client_->GetUploadDetails("language-LOCALE"); | |
86 } | |
87 | |
88 void StartUploading() { | |
89 token_service_->AddAccount("example@gmail.com"); | |
90 identity_provider_->LogIn("example@gmail.com"); | |
91 PaymentsClient::UploadRequestDetails request_details; | |
92 request_details.card = test::GetCreditCard(); | |
93 request_details.cvc = base::ASCIIToUTF16("123"); | |
94 request_details.context_token = base::ASCIIToUTF16("context token"); | |
95 request_details.risk_data = "some risk data"; | |
96 request_details.app_locale = "language-LOCALE"; | |
97 client_->UploadCard(request_details); | |
98 } | |
99 | |
100 void IssueOAuthToken() { | |
101 token_service_->IssueAllTokensForAccount( | |
102 "example@gmail.com", "totally_real_token", | |
103 base::Time::Now() + base::TimeDelta::FromDays(10)); | |
104 | |
105 // Verify the auth header. | |
106 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); | |
107 net::HttpRequestHeaders request_headers; | |
108 fetcher->GetExtraRequestHeaders(&request_headers); | |
109 std::string auth_header_value; | |
110 EXPECT_TRUE(request_headers.GetHeader( | |
111 net::HttpRequestHeaders::kAuthorization, &auth_header_value)) | |
112 << request_headers.ToString(); | |
113 EXPECT_EQ("Bearer totally_real_token", auth_header_value); | |
114 } | |
115 | |
116 void ReturnResponse(net::HttpStatusCode response_code, | |
117 const std::string& response_body) { | |
118 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); | |
119 ASSERT_TRUE(fetcher); | |
120 fetcher->set_response_code(response_code); | |
121 fetcher->SetResponseString(response_body); | |
122 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
123 } | |
124 | |
125 AutofillClient::PaymentsRpcResult result_; | |
126 std::string real_pan_; | |
127 std::unique_ptr<base::DictionaryValue> legal_message_; | |
128 | |
129 content::TestBrowserThreadBundle thread_bundle_; | |
130 net::TestURLFetcherFactory factory_; | |
131 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | |
132 std::unique_ptr<FakeOAuth2TokenService> token_service_; | |
133 std::unique_ptr<FakeIdentityProvider> identity_provider_; | |
134 std::unique_ptr<PaymentsClient> client_; | |
135 | |
136 private: | |
137 DISALLOW_COPY_AND_ASSIGN(PaymentsClientTest); | |
138 }; | |
139 | |
140 TEST_F(PaymentsClientTest, OAuthError) { | |
141 StartUnmasking(); | |
142 token_service_->IssueErrorForAllPendingRequestsForAccount( | |
143 "example@gmail.com", | |
144 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE)); | |
145 EXPECT_EQ(AutofillClient::PERMANENT_FAILURE, result_); | |
146 EXPECT_TRUE(real_pan_.empty()); | |
147 } | |
148 | |
149 TEST_F(PaymentsClientTest, UnmaskSuccess) { | |
150 StartUnmasking(); | |
151 IssueOAuthToken(); | |
152 ReturnResponse(net::HTTP_OK, "{ \"pan\": \"1234\" }"); | |
153 EXPECT_EQ(AutofillClient::SUCCESS, result_); | |
154 EXPECT_EQ("1234", real_pan_); | |
155 } | |
156 | |
157 TEST_F(PaymentsClientTest, GetDetailsSuccess) { | |
158 StartGettingUploadDetails(); | |
159 ReturnResponse( | |
160 net::HTTP_OK, | |
161 "{ \"context_token\": \"some_token\", \"legal_message\": {} }"); | |
162 EXPECT_EQ(AutofillClient::SUCCESS, result_); | |
163 EXPECT_NE(nullptr, legal_message_.get()); | |
164 } | |
165 | |
166 TEST_F(PaymentsClientTest, UploadSuccess) { | |
167 StartUploading(); | |
168 IssueOAuthToken(); | |
169 ReturnResponse(net::HTTP_OK, "{}"); | |
170 EXPECT_EQ(AutofillClient::SUCCESS, result_); | |
171 } | |
172 | |
173 TEST_F(PaymentsClientTest, GetDetailsFollowedByUploadSuccess) { | |
174 StartGettingUploadDetails(); | |
175 ReturnResponse( | |
176 net::HTTP_OK, | |
177 "{ \"context_token\": \"some_token\", \"legal_message\": {} }"); | |
178 EXPECT_EQ(AutofillClient::SUCCESS, result_); | |
179 | |
180 result_ = AutofillClient::NONE; | |
181 | |
182 StartUploading(); | |
183 IssueOAuthToken(); | |
184 ReturnResponse(net::HTTP_OK, "{}"); | |
185 EXPECT_EQ(AutofillClient::SUCCESS, result_); | |
186 } | |
187 | |
188 TEST_F(PaymentsClientTest, UnmaskMissingPan) { | |
189 StartUnmasking(); | |
190 ReturnResponse(net::HTTP_OK, "{}"); | |
191 EXPECT_EQ(AutofillClient::PERMANENT_FAILURE, result_); | |
192 } | |
193 | |
194 TEST_F(PaymentsClientTest, GetDetailsMissingContextToken) { | |
195 StartGettingUploadDetails(); | |
196 ReturnResponse(net::HTTP_OK, "{ \"legal_message\": {} }"); | |
197 EXPECT_EQ(AutofillClient::PERMANENT_FAILURE, result_); | |
198 } | |
199 | |
200 TEST_F(PaymentsClientTest, GetDetailsMissingLegalMessage) { | |
201 StartGettingUploadDetails(); | |
202 ReturnResponse(net::HTTP_OK, "{ \"context_token\": \"some_token\" }"); | |
203 EXPECT_EQ(AutofillClient::PERMANENT_FAILURE, result_); | |
204 EXPECT_EQ(nullptr, legal_message_.get()); | |
205 } | |
206 | |
207 TEST_F(PaymentsClientTest, RetryFailure) { | |
208 StartUnmasking(); | |
209 IssueOAuthToken(); | |
210 ReturnResponse(net::HTTP_OK, "{ \"error\": { \"code\": \"INTERNAL\" } }"); | |
211 EXPECT_EQ(AutofillClient::TRY_AGAIN_FAILURE, result_); | |
212 EXPECT_EQ("", real_pan_); | |
213 } | |
214 | |
215 TEST_F(PaymentsClientTest, PermanentFailure) { | |
216 StartUnmasking(); | |
217 IssueOAuthToken(); | |
218 ReturnResponse(net::HTTP_OK, | |
219 "{ \"error\": { \"code\": \"ANYTHING_ELSE\" } }"); | |
220 EXPECT_EQ(AutofillClient::PERMANENT_FAILURE, result_); | |
221 EXPECT_EQ("", real_pan_); | |
222 } | |
223 | |
224 TEST_F(PaymentsClientTest, MalformedResponse) { | |
225 StartUnmasking(); | |
226 IssueOAuthToken(); | |
227 ReturnResponse(net::HTTP_OK, "{ \"error_code\": \"WRONG_JSON_FORMAT\" }"); | |
228 EXPECT_EQ(AutofillClient::PERMANENT_FAILURE, result_); | |
229 EXPECT_EQ("", real_pan_); | |
230 } | |
231 | |
232 TEST_F(PaymentsClientTest, ReauthNeeded) { | |
233 { | |
234 StartUnmasking(); | |
235 IssueOAuthToken(); | |
236 ReturnResponse(net::HTTP_UNAUTHORIZED, ""); | |
237 // No response yet. | |
238 EXPECT_EQ(AutofillClient::NONE, result_); | |
239 EXPECT_EQ("", real_pan_); | |
240 | |
241 // Second HTTP_UNAUTHORIZED causes permanent failure. | |
242 IssueOAuthToken(); | |
243 ReturnResponse(net::HTTP_UNAUTHORIZED, ""); | |
244 EXPECT_EQ(AutofillClient::PERMANENT_FAILURE, result_); | |
245 EXPECT_EQ("", real_pan_); | |
246 } | |
247 | |
248 result_ = AutofillClient::NONE; | |
249 real_pan_.clear(); | |
250 | |
251 { | |
252 StartUnmasking(); | |
253 IssueOAuthToken(); | |
254 ReturnResponse(net::HTTP_UNAUTHORIZED, ""); | |
255 // No response yet. | |
256 EXPECT_EQ(AutofillClient::NONE, result_); | |
257 EXPECT_EQ("", real_pan_); | |
258 | |
259 // HTTP_OK after first HTTP_UNAUTHORIZED results in success. | |
260 IssueOAuthToken(); | |
261 ReturnResponse(net::HTTP_OK, "{ \"pan\": \"1234\" }"); | |
262 EXPECT_EQ(AutofillClient::SUCCESS, result_); | |
263 EXPECT_EQ("1234", real_pan_); | |
264 } | |
265 } | |
266 | |
267 TEST_F(PaymentsClientTest, NetworkError) { | |
268 StartUnmasking(); | |
269 IssueOAuthToken(); | |
270 ReturnResponse(net::HTTP_REQUEST_TIMEOUT, std::string()); | |
271 EXPECT_EQ(AutofillClient::NETWORK_ERROR, result_); | |
272 EXPECT_EQ("", real_pan_); | |
273 } | |
274 | |
275 TEST_F(PaymentsClientTest, OtherError) { | |
276 StartUnmasking(); | |
277 IssueOAuthToken(); | |
278 ReturnResponse(net::HTTP_FORBIDDEN, std::string()); | |
279 EXPECT_EQ(AutofillClient::PERMANENT_FAILURE, result_); | |
280 EXPECT_EQ("", real_pan_); | |
281 } | |
282 | |
283 } // namespace autofill | |
284 } // namespace payments | |
OLD | NEW |