OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/autofill/content/browser/wallet/wallet_signin_helper.h" | 5 #include "components/autofill/content/browser/wallet/wallet_signin_helper.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "chrome/test/base/testing_profile.h" | 10 #include "chrome/test/base/testing_profile.h" |
11 #include "components/autofill/content/browser/wallet/wallet_service_url.h" | 11 #include "components/autofill/content/browser/wallet/wallet_service_url.h" |
12 #include "components/autofill/content/browser/wallet/wallet_signin_helper_delega
te.h" | 12 #include "components/autofill/content/browser/wallet/wallet_signin_helper_delega
te.h" |
13 #include "content/public/test/test_browser_thread.h" | 13 #include "content/public/test/test_browser_thread.h" |
14 #include "google_apis/gaia/gaia_constants.h" | 14 #include "google_apis/gaia/gaia_constants.h" |
15 #include "google_apis/gaia/gaia_urls.h" | 15 #include "google_apis/gaia/gaia_urls.h" |
16 #include "google_apis/gaia/google_service_auth_error.h" | 16 #include "google_apis/gaia/google_service_auth_error.h" |
| 17 #include "net/http/http_status_code.h" |
17 #include "net/url_request/test_url_fetcher_factory.h" | 18 #include "net/url_request/test_url_fetcher_factory.h" |
18 #include "net/url_request/url_request.h" | 19 #include "net/url_request/url_request.h" |
19 #include "net/url_request/url_request_context_getter.h" | 20 #include "net/url_request/url_request_context_getter.h" |
20 #include "net/url_request/url_request_status.h" | 21 #include "net/url_request/url_request_status.h" |
21 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
23 | 24 |
24 using content::BrowserThread; | 25 using content::BrowserThread; |
25 using testing::_; | 26 using testing::_; |
26 | 27 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 fetcher->set_url(GURL(url)); | 107 fetcher->set_url(GURL(url)); |
107 fetcher->set_status(net::URLRequestStatus()); | 108 fetcher->set_status(net::URLRequestStatus()); |
108 fetcher->set_response_code(response_code); | 109 fetcher->set_response_code(response_code); |
109 fetcher->SetResponseString(response_string); | 110 fetcher->SetResponseString(response_string); |
110 fetcher->set_cookies(cookies); | 111 fetcher->set_cookies(cookies); |
111 fetcher->delegate()->OnURLFetchComplete(fetcher); | 112 fetcher->delegate()->OnURLFetchComplete(fetcher); |
112 } | 113 } |
113 | 114 |
114 void MockSuccessfulOAuthLoginResponse() { | 115 void MockSuccessfulOAuthLoginResponse() { |
115 SetUpFetcherResponseAndCompleteRequest( | 116 SetUpFetcherResponseAndCompleteRequest( |
116 GaiaUrls::GetInstance()->client_login_url(), 200, | 117 GaiaUrls::GetInstance()->client_login_url(), net::HTTP_OK, |
117 net::ResponseCookies(), | 118 net::ResponseCookies(), |
118 "SID=sid\nLSID=lsid\nAuth=auth"); | 119 "SID=sid\nLSID=lsid\nAuth=auth"); |
119 } | 120 } |
120 | 121 |
121 void MockFailedOAuthLoginResponse404() { | 122 void MockFailedOAuthLoginResponse404() { |
122 SetUpFetcherResponseAndCompleteRequest( | 123 SetUpFetcherResponseAndCompleteRequest( |
123 GaiaUrls::GetInstance()->client_login_url(), | 124 GaiaUrls::GetInstance()->client_login_url(), |
124 404, | 125 net::HTTP_NOT_FOUND, |
125 net::ResponseCookies(), | 126 net::ResponseCookies(), |
126 std::string()); | 127 std::string()); |
127 } | 128 } |
128 | 129 |
129 void MockSuccessfulGaiaUserInfoResponse(const std::string& username) { | 130 void MockSuccessfulGaiaUserInfoResponse(const std::string& username) { |
130 SetUpFetcherResponseAndCompleteRequest( | 131 SetUpFetcherResponseAndCompleteRequest( |
131 GaiaUrls::GetInstance()->get_user_info_url(), 200, | 132 GaiaUrls::GetInstance()->get_user_info_url(), net::HTTP_OK, |
132 net::ResponseCookies(), | 133 net::ResponseCookies(), |
133 "email=" + username); | 134 "email=" + username); |
134 } | 135 } |
135 | 136 |
136 void MockFailedGaiaUserInfoResponse404() { | 137 void MockFailedGaiaUserInfoResponse404() { |
137 SetUpFetcherResponseAndCompleteRequest( | 138 SetUpFetcherResponseAndCompleteRequest( |
138 GaiaUrls::GetInstance()->get_user_info_url(), | 139 GaiaUrls::GetInstance()->get_user_info_url(), |
139 404, | 140 net::HTTP_NOT_FOUND, |
140 net::ResponseCookies(), | 141 net::ResponseCookies(), |
141 std::string()); | 142 std::string()); |
142 } | 143 } |
143 | 144 |
144 void MockSuccessfulGetAccountInfoResponse(const std::string& username) { | 145 void MockSuccessfulGetAccountInfoResponse(const std::string& username) { |
145 SetUpFetcherResponseAndCompleteRequest( | 146 SetUpFetcherResponseAndCompleteRequest( |
146 signin_helper_->GetGetAccountInfoUrlForTesting(), 200, | 147 signin_helper_->GetGetAccountInfoUrlForTesting(), net::HTTP_OK, |
147 net::ResponseCookies(), | 148 net::ResponseCookies(), |
148 base::StringPrintf( | 149 base::StringPrintf( |
149 kGetAccountInfoValidResponseFormat, | 150 kGetAccountInfoValidResponseFormat, |
150 username.c_str())); | 151 username.c_str())); |
151 } | 152 } |
152 | 153 |
153 void MockFailedGetAccountInfoResponse404() { | 154 void MockFailedGetAccountInfoResponse404() { |
154 SetUpFetcherResponseAndCompleteRequest( | 155 SetUpFetcherResponseAndCompleteRequest( |
155 signin_helper_->GetGetAccountInfoUrlForTesting(), | 156 signin_helper_->GetGetAccountInfoUrlForTesting(), |
156 404, | 157 net::HTTP_NOT_FOUND, |
157 net::ResponseCookies(), | 158 net::ResponseCookies(), |
158 std::string()); | 159 std::string()); |
159 } | 160 } |
160 | 161 |
161 void MockSuccessfulPassiveAuthUrlMergeAndRedirectResponse() { | 162 void MockSuccessfulPassiveSignInResponse() { |
162 SetUpFetcherResponseAndCompleteRequest(wallet::GetPassiveAuthUrl().spec(), | 163 SetUpFetcherResponseAndCompleteRequest(wallet::GetPassiveAuthUrl().spec(), |
163 200, | 164 net::HTTP_OK, |
| 165 net::ResponseCookies(), |
| 166 "YES"); |
| 167 } |
| 168 |
| 169 void MockFailedPassiveSignInResponseNo() { |
| 170 SetUpFetcherResponseAndCompleteRequest(wallet::GetPassiveAuthUrl().spec(), |
| 171 net::HTTP_OK, |
| 172 net::ResponseCookies(), |
| 173 "NOOOOOOOOOOOOOOO"); |
| 174 } |
| 175 |
| 176 void MockFailedPassiveSignInResponse404() { |
| 177 SetUpFetcherResponseAndCompleteRequest(wallet::GetPassiveAuthUrl().spec(), |
| 178 net::HTTP_NOT_FOUND, |
164 net::ResponseCookies(), | 179 net::ResponseCookies(), |
165 std::string()); | 180 std::string()); |
166 } | 181 } |
167 | |
168 void MockFailedPassiveAuthUrlMergeAndRedirectResponse404() { | |
169 SetUpFetcherResponseAndCompleteRequest(wallet::GetPassiveAuthUrl().spec(), | |
170 404, | |
171 net::ResponseCookies(), | |
172 std::string()); | |
173 } | |
174 | 182 |
175 WalletSigninHelperForTesting::State state() const { | 183 WalletSigninHelperForTesting::State state() const { |
176 return signin_helper_->state(); | 184 return signin_helper_->state(); |
177 } | 185 } |
178 | 186 |
179 scoped_ptr<WalletSigninHelperForTesting> signin_helper_; | 187 scoped_ptr<WalletSigninHelperForTesting> signin_helper_; |
180 MockWalletSigninHelperDelegate mock_delegate_; | 188 MockWalletSigninHelperDelegate mock_delegate_; |
181 | 189 |
182 private: | 190 private: |
183 // The profile's request context must be released on the IO thread. | 191 // The profile's request context must be released on the IO thread. |
184 content::TestBrowserThread io_thread_; | 192 content::TestBrowserThread io_thread_; |
185 net::TestURLFetcherFactory factory_; | 193 net::TestURLFetcherFactory factory_; |
186 TestingProfile profile_; | 194 TestingProfile profile_; |
187 }; | 195 }; |
188 | 196 |
189 TEST_F(WalletSigninHelperTest, PassiveSigninSuccessful) { | 197 TEST_F(WalletSigninHelperTest, PassiveSigninSuccessful) { |
190 EXPECT_CALL(mock_delegate_, OnPassiveSigninSuccess("user@gmail.com")); | 198 EXPECT_CALL(mock_delegate_, OnPassiveSigninSuccess("user@gmail.com")); |
191 signin_helper_->StartPassiveSignin(); | 199 signin_helper_->StartPassiveSignin(); |
192 MockSuccessfulPassiveAuthUrlMergeAndRedirectResponse(); | 200 MockSuccessfulPassiveSignInResponse(); |
193 MockSuccessfulGetAccountInfoResponse("user@gmail.com"); | 201 MockSuccessfulGetAccountInfoResponse("user@gmail.com"); |
194 } | 202 } |
195 | 203 |
196 TEST_F(WalletSigninHelperTest, PassiveSigninFailedSignin) { | 204 TEST_F(WalletSigninHelperTest, PassiveSigninFailedSignin404) { |
197 EXPECT_CALL(mock_delegate_, OnPassiveSigninFailure(_)); | 205 EXPECT_CALL(mock_delegate_, OnPassiveSigninFailure(_)); |
198 signin_helper_->StartPassiveSignin(); | 206 signin_helper_->StartPassiveSignin(); |
199 MockFailedPassiveAuthUrlMergeAndRedirectResponse404(); | 207 MockFailedPassiveSignInResponse404(); |
| 208 } |
| 209 |
| 210 TEST_F(WalletSigninHelperTest, PassiveSigninFailedSigninNo) { |
| 211 EXPECT_CALL(mock_delegate_, OnPassiveSigninFailure(_)); |
| 212 signin_helper_->StartPassiveSignin(); |
| 213 MockFailedPassiveSignInResponseNo(); |
200 } | 214 } |
201 | 215 |
202 TEST_F(WalletSigninHelperTest, PassiveSigninFailedUserInfo) { | 216 TEST_F(WalletSigninHelperTest, PassiveSigninFailedUserInfo) { |
203 EXPECT_CALL(mock_delegate_, OnPassiveSigninFailure(_)); | 217 EXPECT_CALL(mock_delegate_, OnPassiveSigninFailure(_)); |
204 signin_helper_->StartPassiveSignin(); | 218 signin_helper_->StartPassiveSignin(); |
205 MockSuccessfulPassiveAuthUrlMergeAndRedirectResponse(); | 219 MockSuccessfulPassiveSignInResponse(); |
206 MockFailedGetAccountInfoResponse404(); | 220 MockFailedGetAccountInfoResponse404(); |
207 } | 221 } |
208 | 222 |
209 TEST_F(WalletSigninHelperTest, PassiveUserInfoSuccessful) { | 223 TEST_F(WalletSigninHelperTest, PassiveUserInfoSuccessful) { |
210 EXPECT_CALL(mock_delegate_, OnUserNameFetchSuccess("user@gmail.com")); | 224 EXPECT_CALL(mock_delegate_, OnUserNameFetchSuccess("user@gmail.com")); |
211 signin_helper_->StartUserNameFetch(); | 225 signin_helper_->StartUserNameFetch(); |
212 MockSuccessfulGetAccountInfoResponse("user@gmail.com"); | 226 MockSuccessfulGetAccountInfoResponse("user@gmail.com"); |
213 } | 227 } |
214 | 228 |
215 TEST_F(WalletSigninHelperTest, PassiveUserInfoFailedUserInfo) { | 229 TEST_F(WalletSigninHelperTest, PassiveUserInfoFailedUserInfo) { |
216 EXPECT_CALL(mock_delegate_, OnUserNameFetchFailure(_)); | 230 EXPECT_CALL(mock_delegate_, OnUserNameFetchFailure(_)); |
217 signin_helper_->StartUserNameFetch(); | 231 signin_helper_->StartUserNameFetch(); |
218 MockFailedGetAccountInfoResponse404(); | 232 MockFailedGetAccountInfoResponse404(); |
219 } | 233 } |
220 | 234 |
221 TEST_F(WalletSigninHelperTest, AutomaticSigninSuccessful) { | 235 TEST_F(WalletSigninHelperTest, AutomaticSigninSuccessful) { |
222 EXPECT_CALL(mock_delegate_, OnAutomaticSigninSuccess("user@gmail.com")); | 236 EXPECT_CALL(mock_delegate_, OnAutomaticSigninSuccess("user@gmail.com")); |
223 signin_helper_->StartAutomaticSignin("123SID", "123LSID"); | 237 signin_helper_->StartAutomaticSignin("123SID", "123LSID"); |
224 MockSuccessfulGaiaUserInfoResponse("user@gmail.com"); | 238 MockSuccessfulGaiaUserInfoResponse("user@gmail.com"); |
225 MockSuccessfulOAuthLoginResponse(); | 239 MockSuccessfulOAuthLoginResponse(); |
226 MockSuccessfulPassiveAuthUrlMergeAndRedirectResponse(); | 240 MockSuccessfulPassiveSignInResponse(); |
227 } | 241 } |
228 | 242 |
229 TEST_F(WalletSigninHelperTest, AutomaticSigninFailedGetUserInfo) { | 243 TEST_F(WalletSigninHelperTest, AutomaticSigninFailedGetUserInfo) { |
230 EXPECT_CALL(mock_delegate_, OnAutomaticSigninFailure(_)); | 244 EXPECT_CALL(mock_delegate_, OnAutomaticSigninFailure(_)); |
231 signin_helper_->StartAutomaticSignin("123SID", "123LSID"); | 245 signin_helper_->StartAutomaticSignin("123SID", "123LSID"); |
232 MockFailedGaiaUserInfoResponse404(); | 246 MockFailedGaiaUserInfoResponse404(); |
233 } | 247 } |
234 | 248 |
235 TEST_F(WalletSigninHelperTest, AutomaticSigninFailedOAuthLogin) { | 249 TEST_F(WalletSigninHelperTest, AutomaticSigninFailedOAuthLogin) { |
236 EXPECT_CALL(mock_delegate_, OnAutomaticSigninFailure(_)); | 250 EXPECT_CALL(mock_delegate_, OnAutomaticSigninFailure(_)); |
237 signin_helper_->StartAutomaticSignin("123SID", "123LSID"); | 251 signin_helper_->StartAutomaticSignin("123SID", "123LSID"); |
238 MockSuccessfulGaiaUserInfoResponse("user@gmail.com"); | 252 MockSuccessfulGaiaUserInfoResponse("user@gmail.com"); |
239 MockFailedOAuthLoginResponse404(); | 253 MockFailedOAuthLoginResponse404(); |
240 } | 254 } |
241 | 255 |
242 TEST_F(WalletSigninHelperTest, AutomaticSigninFailedSignin) { | 256 TEST_F(WalletSigninHelperTest, AutomaticSigninFailedSignin404) { |
243 EXPECT_CALL(mock_delegate_, OnAutomaticSigninFailure(_)); | 257 EXPECT_CALL(mock_delegate_, OnAutomaticSigninFailure(_)); |
244 signin_helper_->StartAutomaticSignin("123SID", "123LSID"); | 258 signin_helper_->StartAutomaticSignin("123SID", "123LSID"); |
245 MockSuccessfulGaiaUserInfoResponse("user@gmail.com"); | 259 MockSuccessfulGaiaUserInfoResponse("user@gmail.com"); |
246 MockSuccessfulOAuthLoginResponse(); | 260 MockSuccessfulOAuthLoginResponse(); |
247 MockFailedPassiveAuthUrlMergeAndRedirectResponse404(); | 261 MockFailedPassiveSignInResponse404(); |
| 262 } |
| 263 |
| 264 TEST_F(WalletSigninHelperTest, AutomaticSigninFailedSigninNo) { |
| 265 EXPECT_CALL(mock_delegate_, OnAutomaticSigninFailure(_)); |
| 266 signin_helper_->StartAutomaticSignin("123SID", "123LSID"); |
| 267 MockSuccessfulGaiaUserInfoResponse("user@gmail.com"); |
| 268 MockSuccessfulOAuthLoginResponse(); |
| 269 MockFailedPassiveSignInResponseNo(); |
248 } | 270 } |
249 | 271 |
250 // TODO(aruslan): http://crbug.com/188317 Need more tests. | 272 // TODO(aruslan): http://crbug.com/188317 Need more tests. |
251 | 273 |
252 } // namespace wallet | 274 } // namespace wallet |
253 } // namespace autofill | 275 } // namespace autofill |
OLD | NEW |