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 "content/public/test/test_browser_thread.h"
9 #include "google_apis/gaia/fake_oauth2_token_service.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 MockableConfirmCallback {
41 MOCK_METHOD1(ConfirmCallback, void(CloudPrintBaseApiFlow::Status));
43 PrivetConfirmApiCallFlow::ResponseCallback callback() {
44 return base::Bind(&MockableConfirmCallback::ConfirmCallback,
45 base::Unretained(this));
49 class PrivetConfirmApiFlowTest : public testing::Test {
51 PrivetConfirmApiFlowTest()
52 : ui_thread_(content::BrowserThread::UI,
54 request_context_(new net::TestURLRequestContextGetter(
55 base::MessageLoopProxy::current())),
56 account_id_(kAccountId) {
57 token_service_.set_request_context(request_context_.get());
58 token_service_.AddAccount(account_id_);
59 ui_thread_.Stop(); // HACK: Fake being on the UI thread
62 virtual ~PrivetConfirmApiFlowTest() {
66 base::MessageLoopForUI loop_;
67 content::TestBrowserThread ui_thread_;
68 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
69 net::TestURLFetcherFactory fetcher_factory_;
70 FakeOAuth2TokenService token_service_;
71 MockableConfirmCallback callback_;
72 std::string account_id_;
75 TEST_F(PrivetConfirmApiFlowTest, SuccessOAuth2) {
76 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(),
79 GURL("http://SoMeUrL.com"),
80 callback_.callback());
81 CloudPrintBaseApiFlow* cloudprint_flow =
82 confirm_flow.GetBaseApiFlowForTests();
86 cloudprint_flow->OnGetTokenSuccess(NULL, "SomeToken", base::Time());
87 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
89 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL());
91 net::HttpRequestHeaders headers;
92 fetcher->GetExtraRequestHeaders(&headers);
93 std::string oauth_header;
95 EXPECT_TRUE(headers.GetHeader("Authorization", &oauth_header));
96 EXPECT_EQ("Bearer SomeToken", oauth_header);
97 EXPECT_TRUE(headers.GetHeader("X-Cloudprint-Proxy", &proxy));
98 EXPECT_EQ("Chrome", proxy);
100 fetcher->SetResponseString(kSampleConfirmResponse);
101 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
103 fetcher->set_response_code(200);
105 EXPECT_CALL(callback_, ConfirmCallback(CloudPrintBaseApiFlow::SUCCESS));
107 fetcher->delegate()->OnURLFetchComplete(fetcher);
110 TEST_F(PrivetConfirmApiFlowTest, SuccessCookies) {
111 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(),
114 GURL("http://SoMeUrL.com?token=tkn"),
115 callback_.callback());
117 confirm_flow.Start();
119 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
121 EXPECT_EQ(GURL("http://SoMeUrL.com?token=tkn&xsrf=SomeToken&user=1"),
122 fetcher->GetOriginalURL());
124 net::HttpRequestHeaders headers;
125 fetcher->GetExtraRequestHeaders(&headers);
127 EXPECT_TRUE(headers.GetHeader("X-Cloudprint-Proxy", &proxy));
128 EXPECT_EQ("Chrome", proxy);
130 fetcher->SetResponseString(kSampleConfirmResponse);
131 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
133 fetcher->set_response_code(200);
135 EXPECT_CALL(callback_, ConfirmCallback(CloudPrintBaseApiFlow::SUCCESS));
137 fetcher->delegate()->OnURLFetchComplete(fetcher);
140 TEST_F(PrivetConfirmApiFlowTest, BadToken) {
141 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(),
144 GURL("http://SoMeUrL.com"),
145 callback_.callback());
147 confirm_flow.Start();
149 CloudPrintBaseApiFlow* cloudprint_flow =
150 confirm_flow.GetBaseApiFlowForTests();
152 EXPECT_CALL(callback_,
153 ConfirmCallback(CloudPrintBaseApiFlow::ERROR_TOKEN));
154 cloudprint_flow->OnGetTokenFailure(NULL, GoogleServiceAuthError(
155 GoogleServiceAuthError::USER_NOT_SIGNED_UP));
158 TEST_F(PrivetConfirmApiFlowTest, ServerFailure) {
159 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(),
162 GURL("http://SoMeUrL.com"),
163 callback_.callback());
165 confirm_flow.Start();
167 CloudPrintBaseApiFlow* cloudprint_flow =
168 confirm_flow.GetBaseApiFlowForTests();
170 cloudprint_flow->OnGetTokenSuccess(NULL, "SomeToken", base::Time());
171 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
173 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL());
175 fetcher->SetResponseString(kFailedConfirmResponse);
176 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
178 fetcher->set_response_code(200);
180 EXPECT_CALL(callback_,
181 ConfirmCallback(CloudPrintBaseApiFlow::ERROR_FROM_SERVER));
183 fetcher->delegate()->OnURLFetchComplete(fetcher);
186 TEST_F(PrivetConfirmApiFlowTest, BadJson) {
187 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(),
190 GURL("http://SoMeUrL.com"),
191 callback_.callback());
193 confirm_flow.Start();
195 CloudPrintBaseApiFlow* cloudprint_flow =
196 confirm_flow.GetBaseApiFlowForTests();
198 cloudprint_flow->OnGetTokenSuccess(NULL, "SomeToken", base::Time());
199 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
201 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL());
203 fetcher->SetResponseString(kFailedConfirmResponseBadJson);
204 fetcher->set_status(net::URLRequestStatus(
205 net::URLRequestStatus::SUCCESS,
207 fetcher->set_response_code(200);
209 EXPECT_CALL(callback_, ConfirmCallback
210 (CloudPrintBaseApiFlow::ERROR_MALFORMED_RESPONSE));
212 fetcher->delegate()->OnURLFetchComplete(fetcher);
217 } // namespace local_discovery