- add sources.
[platform/framework/web/crosswalk.git] / src / components / autofill / content / browser / wallet / wallet_signin_helper_unittest.cc
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.
4
5 #include "components/autofill/content/browser/wallet/wallet_signin_helper.h"
6
7 #include "base/bind.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"
29
30 using testing::_;
31
32 namespace autofill {
33 namespace wallet {
34
35 namespace {
36
37 const char kGetAccountInfoValidResponseFormat[] =
38     "{\"user_info\":["
39     "  {"
40     "    \"email\": \"%s\""
41     "  }"
42     "]}";
43
44 class MockWalletSigninHelperDelegate : public WalletSigninHelperDelegate {
45  public:
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));
56 };
57
58 class WalletSigninHelperForTesting : public WalletSigninHelper {
59  public:
60   WalletSigninHelperForTesting(WalletSigninHelperDelegate* delegate,
61                                net::URLRequestContextGetter* getter)
62       : WalletSigninHelper(delegate, getter) {
63   }
64
65   // Bring in the test-only getters.
66   using WalletSigninHelper::GetGetAccountInfoUrlForTesting;
67   using WalletSigninHelper::state;
68
69   // Bring in the State enum.
70   using WalletSigninHelper::State;
71   using WalletSigninHelper::IDLE;
72 };
73
74 }  // namespace
75
76 class WalletSigninHelperTest : public testing::Test {
77  protected:
78   virtual void SetUp() OVERRIDE {
79     signin_helper_.reset(new WalletSigninHelperForTesting(
80         &mock_delegate_,
81         browser_context_.GetRequestContext()));
82     EXPECT_EQ(WalletSigninHelperForTesting::IDLE, state());
83   }
84
85   virtual void TearDown() OVERRIDE {
86     signin_helper_.reset();
87   }
88
89   // Sets up a response for the mock URLFetcher and completes the request.
90   void SetUpFetcherResponseAndCompleteRequest(
91       const std::string& url,
92       int response_code,
93       const net::ResponseCookies& cookies,
94       const std::string& response_string) {
95     net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
96     ASSERT_TRUE(fetcher);
97     ASSERT_TRUE(fetcher->delegate());
98
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);
105   }
106
107   void MockSuccessfulGetAccountInfoResponse(const std::string& username) {
108     SetUpFetcherResponseAndCompleteRequest(
109         signin_helper_->GetGetAccountInfoUrlForTesting(), net::HTTP_OK,
110         net::ResponseCookies(),
111         base::StringPrintf(
112             kGetAccountInfoValidResponseFormat,
113             username.c_str()));
114   }
115
116   void MockFailedGetAccountInfoResponse404() {
117     SetUpFetcherResponseAndCompleteRequest(
118         signin_helper_->GetGetAccountInfoUrlForTesting(),
119         net::HTTP_NOT_FOUND,
120         net::ResponseCookies(),
121         std::string());
122   }
123
124   void MockSuccessfulPassiveSignInResponse() {
125     SetUpFetcherResponseAndCompleteRequest(wallet::GetPassiveAuthUrl().spec(),
126                                            net::HTTP_OK,
127                                            net::ResponseCookies(),
128                                            "YES");
129   }
130
131   void MockFailedPassiveSignInResponseNo() {
132     SetUpFetcherResponseAndCompleteRequest(wallet::GetPassiveAuthUrl().spec(),
133                                            net::HTTP_OK,
134                                            net::ResponseCookies(),
135                                            "NOOOOOOOOOOOOOOO");
136   }
137
138   void MockFailedPassiveSignInResponse404() {
139     SetUpFetcherResponseAndCompleteRequest(wallet::GetPassiveAuthUrl().spec(),
140                                            net::HTTP_NOT_FOUND,
141                                            net::ResponseCookies(),
142                                            std::string());
143   }
144
145   WalletSigninHelperForTesting::State state() const {
146     return signin_helper_->state();
147   }
148
149   content::TestBrowserThreadBundle thread_bundle_;
150   scoped_ptr<WalletSigninHelperForTesting> signin_helper_;
151   MockWalletSigninHelperDelegate mock_delegate_;
152   TestingProfile browser_context_;
153
154  private:
155   net::TestURLFetcherFactory factory_;
156 };
157
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");
165 }
166
167 TEST_F(WalletSigninHelperTest, PassiveSigninFailedSignin404) {
168   EXPECT_CALL(mock_delegate_, OnPassiveSigninFailure(_));
169   signin_helper_->StartPassiveSignin();
170   MockFailedPassiveSignInResponse404();
171 }
172
173 TEST_F(WalletSigninHelperTest, PassiveSigninFailedSigninNo) {
174   EXPECT_CALL(mock_delegate_, OnPassiveSigninFailure(_));
175   signin_helper_->StartPassiveSignin();
176   MockFailedPassiveSignInResponseNo();
177 }
178
179 TEST_F(WalletSigninHelperTest, PassiveSigninFailedUserInfo) {
180   EXPECT_CALL(mock_delegate_, OnPassiveSigninFailure(_));
181   signin_helper_->StartPassiveSignin();
182   MockSuccessfulPassiveSignInResponse();
183   MockFailedGetAccountInfoResponse404();
184 }
185
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");
192 }
193
194 TEST_F(WalletSigninHelperTest, PassiveUserInfoFailedUserInfo) {
195   EXPECT_CALL(mock_delegate_, OnUserNameFetchFailure(_));
196   signin_helper_->StartUserNameFetch();
197   MockFailedGetAccountInfoResponse404();
198 }
199
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",
208                                    base::Time::Now(),
209                                    httponly_options));
210
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();
218 }
219
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",
228                                    base::Time::Now(),
229                                    httponly_options));
230
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();
238 }
239
240 // TODO(aruslan): http://crbug.com/188317 Need more tests.
241
242 }  // namespace wallet
243 }  // namespace autofill