Roll src/third_party/skia de7665a:76033be
[chromium-blink-merge.git] / components / autofill / content / browser / wallet / wallet_signin_helper_unittest.cc
blobdf422a3e55d4c85bbe984dfdbee194b3aac024fc
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"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "base/strings/stringprintf.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/browser/cookie_store_factory.h"
15 #include "content/public/test/test_browser_thread_bundle.h"
16 #include "google_apis/gaia/gaia_constants.h"
17 #include "google_apis/gaia/gaia_urls.h"
18 #include "google_apis/gaia/google_service_auth_error.h"
19 #include "net/cookies/canonical_cookie.h"
20 #include "net/cookies/cookie_monster.h"
21 #include "net/cookies/cookie_options.h"
22 #include "net/http/http_status_code.h"
23 #include "net/url_request/test_url_fetcher_factory.h"
24 #include "net/url_request/url_request.h"
25 #include "net/url_request/url_request_context.h"
26 #include "net/url_request/url_request_context_getter.h"
27 #include "net/url_request/url_request_status.h"
28 #include "net/url_request/url_request_test_util.h"
29 #include "testing/gmock/include/gmock/gmock.h"
30 #include "testing/gtest/include/gtest/gtest.h"
32 using testing::_;
34 namespace autofill {
35 namespace wallet {
37 namespace {
39 class MockWalletSigninHelperDelegate : public WalletSigninHelperDelegate {
40 public:
41 MOCK_METHOD0(OnPassiveSigninSuccess, void());
42 MOCK_METHOD1(OnPassiveSigninFailure,
43 void(const GoogleServiceAuthError& error));
44 MOCK_METHOD1(OnDidFetchWalletCookieValue,
45 void(const std::string& cookie_value));
48 } // namespace
50 class WalletSigninHelperTest : public testing::Test {
51 protected:
52 WalletSigninHelperTest()
53 : request_context_(new net::TestURLRequestContextGetter(
54 base::MessageLoopProxy::current())) {}
55 ~WalletSigninHelperTest() override {}
57 void SetUp() override {
58 signin_helper_.reset(
59 new WalletSigninHelper(&mock_delegate_, request_context_.get()));
62 void TearDown() override { signin_helper_.reset(); }
64 // Sets up a response for the mock URLFetcher and completes the request.
65 void SetUpFetcherResponseAndCompleteRequest(
66 const std::string& url,
67 int response_code,
68 const net::ResponseCookies& cookies,
69 const std::string& response_string) {
70 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
71 ASSERT_TRUE(fetcher);
72 ASSERT_TRUE(fetcher->delegate());
74 fetcher->set_url(GURL(url));
75 fetcher->set_status(net::URLRequestStatus());
76 fetcher->set_response_code(response_code);
77 fetcher->SetResponseString(response_string);
78 fetcher->set_cookies(cookies);
79 fetcher->delegate()->OnURLFetchComplete(fetcher);
82 void MockSuccessfulPassiveSignInResponse() {
83 SetUpFetcherResponseAndCompleteRequest(wallet::GetPassiveAuthUrl(0).spec(),
84 net::HTTP_OK,
85 net::ResponseCookies(),
86 "YES");
89 void MockFailedPassiveSignInResponseNo() {
90 SetUpFetcherResponseAndCompleteRequest(wallet::GetPassiveAuthUrl(0).spec(),
91 net::HTTP_OK,
92 net::ResponseCookies(),
93 "NOOOOOOOOOOOOOOO");
96 void MockFailedPassiveSignInResponse404() {
97 SetUpFetcherResponseAndCompleteRequest(wallet::GetPassiveAuthUrl(0).spec(),
98 net::HTTP_NOT_FOUND,
99 net::ResponseCookies(),
100 std::string());
103 content::TestBrowserThreadBundle thread_bundle_;
104 scoped_ptr<WalletSigninHelper> signin_helper_;
105 MockWalletSigninHelperDelegate mock_delegate_;
106 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
108 private:
109 net::TestURLFetcherFactory factory_;
112 TEST_F(WalletSigninHelperTest, PassiveSigninSuccessful) {
113 EXPECT_CALL(mock_delegate_, OnPassiveSigninSuccess());
114 signin_helper_->StartPassiveSignin(0);
115 MockSuccessfulPassiveSignInResponse();
118 TEST_F(WalletSigninHelperTest, PassiveSigninFailedSignin404) {
119 EXPECT_CALL(mock_delegate_, OnPassiveSigninFailure(_));
120 signin_helper_->StartPassiveSignin(0);
121 MockFailedPassiveSignInResponse404();
124 TEST_F(WalletSigninHelperTest, PassiveSigninFailedSigninNo) {
125 EXPECT_CALL(mock_delegate_, OnPassiveSigninFailure(_));
126 signin_helper_->StartPassiveSignin(0);
127 MockFailedPassiveSignInResponseNo();
130 TEST_F(WalletSigninHelperTest, GetWalletCookieValueWhenPresent) {
131 EXPECT_CALL(mock_delegate_, OnDidFetchWalletCookieValue("gdToken"));
132 net::CookieMonster* cookie_monster =
133 content::CreateCookieStore(content::CookieStoreConfig())->
134 GetCookieMonster();
135 net::CookieOptions httponly_options;
136 httponly_options.set_include_httponly();
137 scoped_ptr<net::CanonicalCookie> cookie(
138 net::CanonicalCookie::Create(GetPassiveAuthUrl(0).GetWithEmptyPath(),
139 "gdToken=gdToken; HttpOnly",
140 base::Time::Now(),
141 httponly_options));
143 net::CookieList cookie_list;
144 cookie_list.push_back(*cookie);
145 cookie_monster->ImportCookies(cookie_list);
146 request_context_->GetURLRequestContext()
147 ->set_cookie_store(cookie_monster);
148 signin_helper_->StartWalletCookieValueFetch();
149 base::RunLoop().RunUntilIdle();
152 TEST_F(WalletSigninHelperTest, GetWalletCookieValueWhenMissing) {
153 EXPECT_CALL(mock_delegate_, OnDidFetchWalletCookieValue(std::string()));
154 net::CookieMonster* cookie_monster =
155 content::CreateCookieStore(content::CookieStoreConfig())->
156 GetCookieMonster();
157 net::CookieOptions httponly_options;
158 httponly_options.set_include_httponly();
159 scoped_ptr<net::CanonicalCookie> cookie(
160 net::CanonicalCookie::Create(GetPassiveAuthUrl(0).GetWithEmptyPath(),
161 "fake_cookie=monkeys; HttpOnly",
162 base::Time::Now(),
163 httponly_options));
165 net::CookieList cookie_list;
166 cookie_list.push_back(*cookie);
167 cookie_monster->ImportCookies(cookie_list);
168 request_context_->GetURLRequestContext()
169 ->set_cookie_store(cookie_monster);
170 signin_helper_->StartWalletCookieValueFetch();
171 base::RunLoop().RunUntilIdle();
174 // TODO(aruslan): http://crbug.com/188317 Need more tests.
176 } // namespace wallet
177 } // namespace autofill