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.
6 #include "base/message_loop/message_loop.h"
7 #include "chrome/browser/local_discovery/privet_confirm_api_flow.h"
8 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
9 #include "content/public/test/test_browser_thread.h"
10 #include "google_apis/gaia/google_service_auth_error.h"
11 #include "net/base/host_port_pair.h"
12 #include "net/base/net_errors.h"
13 #include "net/http/http_request_headers.h"
14 #include "net/url_request/test_url_fetcher_factory.h"
15 #include "net/url_request/url_request_test_util.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 using testing::NiceMock
;
21 namespace local_discovery
{
25 const char kSampleConfirmResponse
[] = "{"
29 const char kFailedConfirmResponse
[] = "{"
33 const char kFailedConfirmResponseBadJson
[] = "["
37 const char kAccountId
[] = "account_id";
39 class TestOAuth2TokenService
: public OAuth2TokenService
{
41 explicit TestOAuth2TokenService(net::URLRequestContextGetter
* request_context
)
42 : request_context_(request_context
) {
45 virtual std::string
GetRefreshToken(const std::string
& account_id
) OVERRIDE
{
49 virtual net::URLRequestContextGetter
* GetRequestContext() OVERRIDE
{
50 return request_context_
.get();
54 scoped_refptr
<net::URLRequestContextGetter
> request_context_
;
57 class MockableConfirmCallback
{
59 MOCK_METHOD1(ConfirmCallback
, void(CloudPrintBaseApiFlow::Status
));
61 PrivetConfirmApiCallFlow::ResponseCallback
callback() {
62 return base::Bind(&MockableConfirmCallback::ConfirmCallback
,
63 base::Unretained(this));
67 class PrivetConfirmApiFlowTest
: public testing::Test
{
69 PrivetConfirmApiFlowTest()
70 : ui_thread_(content::BrowserThread::UI
,
72 request_context_(new net::TestURLRequestContextGetter(
73 base::MessageLoopProxy::current())),
74 token_service_(request_context_
.get()),
75 account_id_(kAccountId
) {
76 ui_thread_
.Stop(); // HACK: Fake being on the UI thread
79 virtual ~PrivetConfirmApiFlowTest() {
83 base::MessageLoopForUI loop_
;
84 content::TestBrowserThread ui_thread_
;
85 scoped_refptr
<net::TestURLRequestContextGetter
> request_context_
;
86 net::TestURLFetcherFactory fetcher_factory_
;
87 TestOAuth2TokenService token_service_
;
88 MockableConfirmCallback callback_
;
89 std::string account_id_
;
92 TEST_F(PrivetConfirmApiFlowTest
, SuccessOAuth2
) {
93 PrivetConfirmApiCallFlow
confirm_flow(request_context_
.get(),
96 GURL("http://SoMeUrL.com"),
97 callback_
.callback());
98 CloudPrintBaseApiFlow
* cloudprint_flow
=
99 confirm_flow
.GetBaseApiFlowForTests();
101 confirm_flow
.Start();
103 cloudprint_flow
->OnGetTokenSuccess(NULL
, "SomeToken", base::Time());
104 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
106 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher
->GetOriginalURL());
108 net::HttpRequestHeaders headers
;
109 fetcher
->GetExtraRequestHeaders(&headers
);
110 std::string oauth_header
;
112 EXPECT_TRUE(headers
.GetHeader("Authorization", &oauth_header
));
113 EXPECT_EQ("Bearer SomeToken", oauth_header
);
114 EXPECT_TRUE(headers
.GetHeader("X-Cloudprint-Proxy", &proxy
));
115 EXPECT_EQ("Chrome", proxy
);
117 fetcher
->SetResponseString(kSampleConfirmResponse
);
118 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
120 fetcher
->set_response_code(200);
122 EXPECT_CALL(callback_
, ConfirmCallback(CloudPrintBaseApiFlow::SUCCESS
));
124 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
127 TEST_F(PrivetConfirmApiFlowTest
, SuccessCookies
) {
128 PrivetConfirmApiCallFlow
confirm_flow(request_context_
.get(),
131 GURL("http://SoMeUrL.com?token=tkn"),
132 callback_
.callback());
134 confirm_flow
.Start();
136 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
138 EXPECT_EQ(GURL("http://SoMeUrL.com?token=tkn&xsrf=SomeToken&user=1"),
139 fetcher
->GetOriginalURL());
141 net::HttpRequestHeaders headers
;
142 fetcher
->GetExtraRequestHeaders(&headers
);
144 EXPECT_TRUE(headers
.GetHeader("X-Cloudprint-Proxy", &proxy
));
145 EXPECT_EQ("Chrome", proxy
);
147 fetcher
->SetResponseString(kSampleConfirmResponse
);
148 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
150 fetcher
->set_response_code(200);
152 EXPECT_CALL(callback_
, ConfirmCallback(CloudPrintBaseApiFlow::SUCCESS
));
154 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
157 TEST_F(PrivetConfirmApiFlowTest
, BadToken
) {
158 PrivetConfirmApiCallFlow
confirm_flow(request_context_
.get(),
161 GURL("http://SoMeUrL.com"),
162 callback_
.callback());
164 confirm_flow
.Start();
166 CloudPrintBaseApiFlow
* cloudprint_flow
=
167 confirm_flow
.GetBaseApiFlowForTests();
169 EXPECT_CALL(callback_
,
170 ConfirmCallback(CloudPrintBaseApiFlow::ERROR_TOKEN
));
171 cloudprint_flow
->OnGetTokenFailure(NULL
, GoogleServiceAuthError(
172 GoogleServiceAuthError::USER_NOT_SIGNED_UP
));
175 TEST_F(PrivetConfirmApiFlowTest
, ServerFailure
) {
176 PrivetConfirmApiCallFlow
confirm_flow(request_context_
.get(),
179 GURL("http://SoMeUrL.com"),
180 callback_
.callback());
182 confirm_flow
.Start();
184 CloudPrintBaseApiFlow
* cloudprint_flow
=
185 confirm_flow
.GetBaseApiFlowForTests();
187 cloudprint_flow
->OnGetTokenSuccess(NULL
, "SomeToken", base::Time());
188 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
190 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher
->GetOriginalURL());
192 fetcher
->SetResponseString(kFailedConfirmResponse
);
193 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
195 fetcher
->set_response_code(200);
197 EXPECT_CALL(callback_
,
198 ConfirmCallback(CloudPrintBaseApiFlow::ERROR_FROM_SERVER
));
200 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
203 TEST_F(PrivetConfirmApiFlowTest
, BadJson
) {
204 PrivetConfirmApiCallFlow
confirm_flow(request_context_
.get(),
207 GURL("http://SoMeUrL.com"),
208 callback_
.callback());
210 confirm_flow
.Start();
212 CloudPrintBaseApiFlow
* cloudprint_flow
=
213 confirm_flow
.GetBaseApiFlowForTests();
215 cloudprint_flow
->OnGetTokenSuccess(NULL
, "SomeToken", base::Time());
216 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
218 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher
->GetOriginalURL());
220 fetcher
->SetResponseString(kFailedConfirmResponseBadJson
);
221 fetcher
->set_status(net::URLRequestStatus(
222 net::URLRequestStatus::SUCCESS
,
224 fetcher
->set_response_code(200);
226 EXPECT_CALL(callback_
, ConfirmCallback
227 (CloudPrintBaseApiFlow::ERROR_MALFORMED_RESPONSE
));
229 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
234 } // namespace local_discovery