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 "base/command_line.h" | |
6 #include "components/autofill/core/browser/autofill_test_utils.h" | |
7 #include "components/autofill/core/browser/wallet/real_pan_wallet_client.h" | |
8 #include "components/autofill/core/common/autofill_switches.h" | |
9 #include "content/public/test/test_browser_thread_bundle.h" | |
10 #include "google_apis/gaia/fake_identity_provider.h" | |
11 #include "google_apis/gaia/fake_oauth2_token_service.h" | |
12 #include "net/url_request/test_url_fetcher_factory.h" | |
13 #include "net/url_request/url_request_test_util.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 | |
16 namespace autofill { | |
17 namespace wallet { | |
18 | |
19 class RealPanWalletClientTest : public testing::Test, | |
20 public RealPanWalletClient::Delegate { | |
21 public: | |
22 RealPanWalletClientTest() : result_(AutofillClient::SUCCESS) {} | |
23 ~RealPanWalletClientTest() override {} | |
24 | |
25 void SetUp() override { | |
26 // Silence the warning for mismatching sync and wallet servers. | |
27 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
28 switches::kWalletServiceUseSandbox, "0"); | |
29 | |
30 request_context_ = new net::TestURLRequestContextGetter( | |
31 base::MessageLoopProxy::current()); | |
32 token_service_.reset(new FakeOAuth2TokenService()); | |
33 identity_provider_.reset(new FakeIdentityProvider(token_service_.get())); | |
34 client_.reset(new RealPanWalletClient(request_context_.get(), this)); | |
35 } | |
36 | |
37 void TearDown() override { client_.reset(); } | |
38 | |
39 // RealPanWalletClient::Delegate | |
40 | |
41 IdentityProvider* GetIdentityProvider() override { | |
42 return identity_provider_.get(); | |
43 } | |
44 | |
45 void OnDidGetRealPan(AutofillClient::GetRealPanResult result, | |
46 const std::string& real_pan) override { | |
47 result_ = result; | |
48 real_pan_ = real_pan; | |
49 } | |
50 | |
51 protected: | |
52 void StartUnmasking() { | |
53 token_service_->AddAccount("example@gmail.com"); | |
54 identity_provider_->LogIn("example@gmail.com"); | |
55 CreditCard card = test::GetMaskedServerCard(); | |
56 CardUnmaskDelegate::UnmaskResponse response; | |
57 response.cvc = base::ASCIIToUTF16("123"); | |
58 client_->UnmaskCard(card, response); | |
59 } | |
60 | |
61 void IssueOAuthToken() { | |
62 token_service_->IssueAllTokensForAccount( | |
63 "example@gmail.com", | |
64 "totally_real_token", | |
65 base::Time::Now() + base::TimeDelta::FromDays(10)); | |
66 | |
67 // Verify the auth header. | |
68 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); | |
69 net::HttpRequestHeaders request_headers; | |
70 fetcher->GetExtraRequestHeaders(&request_headers); | |
71 std::string auth_header_value; | |
72 EXPECT_TRUE(request_headers.GetHeader( | |
73 net::HttpRequestHeaders::kAuthorization, | |
74 &auth_header_value)) << request_headers.ToString(); | |
75 EXPECT_EQ("Bearer totally_real_token", auth_header_value); | |
76 } | |
77 | |
78 void ReturnResponse(net::HttpStatusCode response_code, | |
79 const std::string& response_body) { | |
80 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); | |
81 ASSERT_TRUE(fetcher); | |
82 fetcher->set_response_code(response_code); | |
83 fetcher->SetResponseString(response_body); | |
84 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
85 } | |
86 | |
87 AutofillClient::GetRealPanResult result_; | |
88 std::string real_pan_; | |
89 | |
90 content::TestBrowserThreadBundle thread_bundle_; | |
91 net::TestURLFetcherFactory factory_; | |
92 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | |
93 scoped_ptr<FakeOAuth2TokenService> token_service_; | |
94 scoped_ptr<FakeIdentityProvider> identity_provider_; | |
95 scoped_ptr<RealPanWalletClient> client_; | |
96 | |
97 private: | |
98 DISALLOW_COPY_AND_ASSIGN(RealPanWalletClientTest); | |
99 }; | |
100 | |
101 TEST_F(RealPanWalletClientTest, OAuthError) { | |
102 StartUnmasking(); | |
103 token_service_->IssueErrorForAllPendingRequestsForAccount( | |
104 "example@gmail.com", | |
105 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE)); | |
106 EXPECT_EQ(AutofillClient::PERMANENT_FAILURE, result_); | |
107 EXPECT_TRUE(real_pan_.empty()); | |
108 } | |
109 | |
110 TEST_F(RealPanWalletClientTest, Success) { | |
111 StartUnmasking(); | |
112 IssueOAuthToken(); | |
113 ReturnResponse(net::HTTP_OK, "{ \"pan\": \"1234\" }"); | |
114 EXPECT_EQ(AutofillClient::SUCCESS, result_); | |
115 EXPECT_EQ("1234", real_pan_); | |
116 } | |
117 | |
118 TEST_F(RealPanWalletClientTest, RetryFailure) { | |
119 StartUnmasking(); | |
120 IssueOAuthToken(); | |
121 ReturnResponse(net::HTTP_OK, "{ \"error\": { \"code\": \"INTERNAL\" } }"); | |
122 EXPECT_EQ(AutofillClient::TRY_AGAIN_FAILURE, result_); | |
123 EXPECT_EQ("", real_pan_); | |
please use gerrit instead
2015/03/25 22:03:12
EXPECT_TRUE(real_pan_.empty());
Evan Stade
2015/03/25 22:03:58
that gives less useful output when it fails
ditto
| |
124 } | |
125 | |
126 // TODO(estade): enable when https://codereview.chromium.org/1028313006/ | |
127 // lands. | |
128 TEST_F(RealPanWalletClientTest, DISABLED_PermanentFailure) { | |
129 StartUnmasking(); | |
130 IssueOAuthToken(); | |
131 ReturnResponse(net::HTTP_OK, | |
132 "{ \"error\": { \"code\": \"ANYTHING_ELSE\" } }"); | |
133 EXPECT_EQ(AutofillClient::PERMANENT_FAILURE, result_); | |
134 EXPECT_EQ("", real_pan_); | |
please use gerrit instead
2015/03/25 22:03:12
EXPECT_TRUE(real_pan_.empty());
| |
135 } | |
136 | |
137 TEST_F(RealPanWalletClientTest, NetworkError) { | |
138 StartUnmasking(); | |
139 IssueOAuthToken(); | |
140 ReturnResponse(net::HTTP_REQUEST_TIMEOUT, std::string()); | |
141 EXPECT_EQ(AutofillClient::NETWORK_ERROR, result_); | |
142 EXPECT_EQ("", real_pan_); | |
please use gerrit instead
2015/03/25 22:03:12
EXPECT_TRUE(real_pan_.empty());
| |
143 } | |
144 | |
145 TEST_F(RealPanWalletClientTest, OtherError) { | |
146 StartUnmasking(); | |
147 IssueOAuthToken(); | |
148 ReturnResponse(net::HTTP_FORBIDDEN, std::string()); | |
149 EXPECT_EQ(AutofillClient::PERMANENT_FAILURE, result_); | |
150 EXPECT_EQ("", real_pan_); | |
please use gerrit instead
2015/03/25 22:03:12
EXPECT_TRUE(real_pan_.empty());
| |
151 } | |
152 | |
153 } // namespace autofill | |
154 } // namespace wallet | |
OLD | NEW |