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
3 // found in the LICENSE file.
5 #include "components/autofill/content/browser/wallet/wallet_signin_helper.h"
8 #include "base/bind_helpers.h"
9 #include "base/run_loop.h"
10 #include "base/strings/stringprintf.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "components/autofill/content/browser/wallet/wallet_service_url.h"
13 #include "components/autofill/content/browser/wallet/wallet_signin_helper_delegate.h"
14 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "google_apis/gaia/gaia_constants.h"
16 #include "google_apis/gaia/gaia_urls.h"
17 #include "google_apis/gaia/google_service_auth_error.h"
18 #include "net/cookies/canonical_cookie.h"
19 #include "net/cookies/cookie_monster.h"
20 #include "net/cookies/cookie_options.h"
21 #include "net/http/http_status_code.h"
22 #include "net/url_request/test_url_fetcher_factory.h"
23 #include "net/url_request/url_request.h"
24 #include "net/url_request/url_request_context.h"
25 #include "net/url_request/url_request_context_getter.h"
26 #include "net/url_request/url_request_status.h"
27 #include "testing/gmock/include/gmock/gmock.h"
28 #include "testing/gtest/include/gtest/gtest.h"
37 const char kGetAccountInfoValidResponseFormat[] =
44 class MockWalletSigninHelperDelegate : public WalletSigninHelperDelegate {
46 MOCK_METHOD1(OnPassiveSigninSuccess,
47 void(const std::vector<std::string>& usernames));
48 MOCK_METHOD1(OnUserNameFetchSuccess,
49 void(const std::vector<std::string>& usernames));
50 MOCK_METHOD1(OnPassiveSigninFailure,
51 void(const GoogleServiceAuthError& error));
52 MOCK_METHOD1(OnUserNameFetchFailure,
53 void(const GoogleServiceAuthError& error));
54 MOCK_METHOD1(OnDidFetchWalletCookieValue,
55 void(const std::string& cookie_value));
58 class WalletSigninHelperForTesting : public WalletSigninHelper {
60 WalletSigninHelperForTesting(WalletSigninHelperDelegate* delegate,
61 net::URLRequestContextGetter* getter)
62 : WalletSigninHelper(delegate, getter) {
65 // Bring in the test-only getters.
66 using WalletSigninHelper::GetGetAccountInfoUrlForTesting;
67 using WalletSigninHelper::state;
69 // Bring in the State enum.
70 using WalletSigninHelper::State;
71 using WalletSigninHelper::IDLE;
76 class WalletSigninHelperTest : public testing::Test {
78 virtual void SetUp() OVERRIDE {
79 signin_helper_.reset(new WalletSigninHelperForTesting(
81 browser_context_.GetRequestContext()));
82 EXPECT_EQ(WalletSigninHelperForTesting::IDLE, state());
85 virtual void TearDown() OVERRIDE {
86 signin_helper_.reset();
89 // Sets up a response for the mock URLFetcher and completes the request.
90 void SetUpFetcherResponseAndCompleteRequest(
91 const std::string& url,
93 const net::ResponseCookies& cookies,
94 const std::string& response_string) {
95 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
97 ASSERT_TRUE(fetcher->delegate());
99 fetcher->set_url(GURL(url));
100 fetcher->set_status(net::URLRequestStatus());
101 fetcher->set_response_code(response_code);
102 fetcher->SetResponseString(response_string);
103 fetcher->set_cookies(cookies);
104 fetcher->delegate()->OnURLFetchComplete(fetcher);
107 void MockSuccessfulGetAccountInfoResponse(const std::string& username) {
108 SetUpFetcherResponseAndCompleteRequest(
109 signin_helper_->GetGetAccountInfoUrlForTesting(), net::HTTP_OK,
110 net::ResponseCookies(),
112 kGetAccountInfoValidResponseFormat,
116 void MockFailedGetAccountInfoResponse404() {
117 SetUpFetcherResponseAndCompleteRequest(
118 signin_helper_->GetGetAccountInfoUrlForTesting(),
120 net::ResponseCookies(),
124 void MockSuccessfulPassiveSignInResponse() {
125 SetUpFetcherResponseAndCompleteRequest(wallet::GetPassiveAuthUrl().spec(),
127 net::ResponseCookies(),
131 void MockFailedPassiveSignInResponseNo() {
132 SetUpFetcherResponseAndCompleteRequest(wallet::GetPassiveAuthUrl().spec(),
134 net::ResponseCookies(),
138 void MockFailedPassiveSignInResponse404() {
139 SetUpFetcherResponseAndCompleteRequest(wallet::GetPassiveAuthUrl().spec(),
141 net::ResponseCookies(),
145 WalletSigninHelperForTesting::State state() const {
146 return signin_helper_->state();
149 content::TestBrowserThreadBundle thread_bundle_;
150 scoped_ptr<WalletSigninHelperForTesting> signin_helper_;
151 MockWalletSigninHelperDelegate mock_delegate_;
152 TestingProfile browser_context_;
155 net::TestURLFetcherFactory factory_;
158 TEST_F(WalletSigninHelperTest, PassiveSigninSuccessful) {
159 std::vector<std::string> usernames;
160 usernames.push_back("user@gmail.com");
161 EXPECT_CALL(mock_delegate_, OnPassiveSigninSuccess(usernames));
162 signin_helper_->StartPassiveSignin();
163 MockSuccessfulPassiveSignInResponse();
164 MockSuccessfulGetAccountInfoResponse("user@gmail.com");
167 TEST_F(WalletSigninHelperTest, PassiveSigninFailedSignin404) {
168 EXPECT_CALL(mock_delegate_, OnPassiveSigninFailure(_));
169 signin_helper_->StartPassiveSignin();
170 MockFailedPassiveSignInResponse404();
173 TEST_F(WalletSigninHelperTest, PassiveSigninFailedSigninNo) {
174 EXPECT_CALL(mock_delegate_, OnPassiveSigninFailure(_));
175 signin_helper_->StartPassiveSignin();
176 MockFailedPassiveSignInResponseNo();
179 TEST_F(WalletSigninHelperTest, PassiveSigninFailedUserInfo) {
180 EXPECT_CALL(mock_delegate_, OnPassiveSigninFailure(_));
181 signin_helper_->StartPassiveSignin();
182 MockSuccessfulPassiveSignInResponse();
183 MockFailedGetAccountInfoResponse404();
186 TEST_F(WalletSigninHelperTest, PassiveUserInfoSuccessful) {
187 std::vector<std::string> usernames;
188 usernames.push_back("user@gmail.com");
189 EXPECT_CALL(mock_delegate_, OnUserNameFetchSuccess(usernames));
190 signin_helper_->StartUserNameFetch();
191 MockSuccessfulGetAccountInfoResponse("user@gmail.com");
194 TEST_F(WalletSigninHelperTest, PassiveUserInfoFailedUserInfo) {
195 EXPECT_CALL(mock_delegate_, OnUserNameFetchFailure(_));
196 signin_helper_->StartUserNameFetch();
197 MockFailedGetAccountInfoResponse404();
200 TEST_F(WalletSigninHelperTest, GetWalletCookieValueWhenPresent) {
201 EXPECT_CALL(mock_delegate_, OnDidFetchWalletCookieValue("gdToken"));
202 net::CookieMonster* cookie_monster = new net::CookieMonster(NULL, NULL);
203 net::CookieOptions httponly_options;
204 httponly_options.set_include_httponly();
205 scoped_ptr<net::CanonicalCookie> cookie(
206 net::CanonicalCookie::Create(GetPassiveAuthUrl().GetWithEmptyPath(),
207 "gdToken=gdToken; HttpOnly",
211 net::CookieList cookie_list;
212 cookie_list.push_back(*cookie);
213 cookie_monster->InitializeFrom(cookie_list);
214 browser_context_.GetRequestContext()->GetURLRequestContext()
215 ->set_cookie_store(cookie_monster);
216 signin_helper_->StartWalletCookieValueFetch();
217 base::RunLoop().RunUntilIdle();
220 TEST_F(WalletSigninHelperTest, GetWalletCookieValueWhenMissing) {
221 EXPECT_CALL(mock_delegate_, OnDidFetchWalletCookieValue(std::string()));
222 net::CookieMonster* cookie_monster = new net::CookieMonster(NULL, NULL);
223 net::CookieOptions httponly_options;
224 httponly_options.set_include_httponly();
225 scoped_ptr<net::CanonicalCookie> cookie(
226 net::CanonicalCookie::Create(GetPassiveAuthUrl().GetWithEmptyPath(),
227 "fake_cookie=monkeys; HttpOnly",
231 net::CookieList cookie_list;
232 cookie_list.push_back(*cookie);
233 cookie_monster->InitializeFrom(cookie_list);
234 browser_context_.GetRequestContext()->GetURLRequestContext()
235 ->set_cookie_store(cookie_monster);
236 signin_helper_->StartWalletCookieValueFetch();
237 base::RunLoop().RunUntilIdle();
240 // TODO(aruslan): http://crbug.com/188317 Need more tests.
242 } // namespace wallet
243 } // namespace autofill